first commit

This commit is contained in:
pvvx 2016-09-23 07:21:45 +03:00
commit c399bf5be0
806 changed files with 421674 additions and 0 deletions

View file

@ -0,0 +1,10 @@
Example Description
This example describes how to use i2s by using mbed extend api
1.Plug ALC5651 shield to Ameba HDK
2.Run the main function.
3.Plug earphone to Green phone jack

View file

@ -0,0 +1,183 @@
#include <stdio.h>
#include "PinNames.h"
#include "basic_types.h"
#include "diag.h"
#include <osdep_api.h>
#include "i2c_api.h"
#include "pinmap.h"
//#define I2C_MTR_SDA PC_4//PB_3
//#define I2C_MTR_SCL PC_5//PB_2
#define I2C_MTR_SDA PB_3
#define I2C_MTR_SCL PB_2
#define I2C_BUS_CLK 100000 //hz
#define I2C_ALC5651_ADDR (0x34/2)
#define RT5651_PRIV_INDEX 0x6a
#define RT5651_PRIV_DATA 0x6c
#if defined (__ICCARM__)
i2c_t alc5651_i2c;
#else
volatile i2c_t alc5651_i2c;
#define printf DBG_8195A
#endif
static void alc5651_delay(void)
{
int i;
i=10000;
while (i) {
i--;
asm volatile ("nop\n\t");
}
}
void alc5651_reg_write(unsigned int reg, unsigned int value)
{
char buf[4];
buf[0] = (char)reg;
buf[1] = (char)(value>>8);
buf[2] = (char)(value&0xff);
i2c_write(&alc5651_i2c, I2C_ALC5651_ADDR, &buf[0], 3, 1);
alc5651_delay();
}
void alc5651_reg_read(unsigned int reg, unsigned int *value)
{
int tmp;
char *buf = (char*)&tmp;
buf[0] = (char)reg;
i2c_write(&alc5651_i2c, I2C_ALC5651_ADDR, &buf[0], 1, 1);
alc5651_delay();
buf[0] = 0xaa;
buf[1] = 0xaa;
i2c_read(&alc5651_i2c, I2C_ALC5651_ADDR, &buf[0], 2, 1);
alc5651_delay();
*value= ((buf[0]&0xFF)<<8)|(buf[1]&0xFF);
}
void alc5651_index_write(unsigned int reg, unsigned int value)
{
alc5651_reg_write(RT5651_PRIV_INDEX, reg);
alc5651_reg_write(RT5651_PRIV_DATA, value);
}
void alc5651_index_read(unsigned int reg, unsigned int *value)
{
alc5651_reg_write(RT5651_PRIV_INDEX, reg);
alc5651_reg_read(RT5651_PRIV_DATA, value);
}
void alc5651_reg_dump(void)
{
int i;
unsigned int value;
printf("alc5651 codec reg dump\n\r");
printf("------------------------\n\r");
for(i=0;i<=0xff;i++){
alc5651_reg_read(i, &value);
printf("%02x : %04x\n\r", i, (unsigned short)value);
}
printf("------------------------\n\r");
}
void alc5651_index_dump(void)
{
int i;
unsigned int value;
printf("alc5651 codec index dump\n\r");
printf("------------------------\n\r");
for(i=0;i<=0xff;i++){
alc5651_index_read(i, &value);
printf("%02x : %04x\n\r", i, (unsigned short)value);
}
printf("------------------------\n\r");
}
void alc5651_init(void)
{
i2c_init(&alc5651_i2c, I2C_MTR_SDA, I2C_MTR_SCL);
i2c_frequency(&alc5651_i2c, I2C_BUS_CLK);
}
void alc5651_set_word_len(int len_idx) // interface2
{
// 0: 16 1: 20 2: 24 3: 8
unsigned int val;
alc5651_reg_read(0x71,&val);
val &= (~(0x3<<2));
val |= (len_idx<<2);
alc5651_reg_write(0x71,val);
alc5651_reg_read(0x70,&val);
val &= (~(0x3<<2));
val |= (len_idx<<2);
alc5651_reg_write(0x70,val);
}
void alc5651_init_interface1(void)
{
alc5651_reg_write(0x00,0x0021);
alc5651_reg_write(0x63,0xE8FE);
alc5651_reg_write(0x61,0x5800);
alc5651_reg_write(0x62,0x0C00);
alc5651_reg_write(0x73,0x0000);
alc5651_reg_write(0x2A,0x4242);
alc5651_reg_write(0x45,0x2000);
alc5651_reg_write(0x02,0x4848);
alc5651_reg_write(0x8E,0x0019);
alc5651_reg_write(0x8F,0x3100);
alc5651_reg_write(0x91,0x0E00);
alc5651_index_write(0x3D,0x3E00);
alc5651_reg_write(0xFA,0x0011);
alc5651_reg_write(0x83,0x0800);
alc5651_reg_write(0x84,0xA000);
alc5651_reg_write(0xFA,0x0C11);
alc5651_reg_write(0x64,0x4010);
alc5651_reg_write(0x65,0x0C00);
alc5651_reg_write(0x61,0x5806);
alc5651_reg_write(0x62,0xCC00);
alc5651_reg_write(0x3C,0x004F);
alc5651_reg_write(0x3E,0x004F);
alc5651_reg_write(0x27,0x3820);
alc5651_reg_write(0x77,0x0000);
}
void alc5651_init_interface2(void)
{
alc5651_reg_write(0x00,0x0021);
alc5651_reg_write(0x63,0xE8FE);
alc5651_reg_write(0x61,0x5800);
alc5651_reg_write(0x62,0x0C00);
alc5651_reg_write(0x73,0x0000);
alc5651_reg_write(0x2A,0x4242);
alc5651_reg_write(0x45,0x2000);
alc5651_reg_write(0x02,0x4848);
alc5651_reg_write(0x8E,0x0019);
alc5651_reg_write(0x8F,0x3100);
alc5651_reg_write(0x91,0x0E00);
alc5651_index_write(0x3D,0x3E00);
alc5651_reg_write(0xFA,0x0011);
alc5651_reg_write(0x83,0x0800);
alc5651_reg_write(0x84,0xA000);
alc5651_reg_write(0xFA,0x0C11);
alc5651_reg_write(0x64,0x4010);
alc5651_reg_write(0x65,0x0C00);
alc5651_reg_write(0x61,0x5806);
alc5651_reg_write(0x62,0xCC00);
alc5651_reg_write(0x3C,0x004F);
alc5651_reg_write(0x3E,0x004F);
alc5651_reg_write(0x28,0x3030);
alc5651_reg_write(0x2F,0x0080);
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,326 @@
#include "FreeRTOS.h"
#include "task.h"
#include "diag.h"
#include "main.h"
#include "i2s_api.h"
/**
* @brief Main program.
* @param None
* @retval None
*/
#include "alc5651.c"
/*
extern void alc5651_init(void);
extern void alc5651_init_interface2(void);
extern void alc5651_reg_dump(void);
extern void alc5651_index_dump(void);
extern void alc5651_set_word_len(int len_idx);
*/
i2s_t i2s_obj;
#define I2S_DMA_PAGE_SIZE 768 // 2 ~ 4096
#define I2S_DMA_PAGE_NUM 4 // Vaild number is 2~4
u8 i2s_tx_buf[I2S_DMA_PAGE_SIZE*I2S_DMA_PAGE_NUM];
u8 i2s_rx_buf[I2S_DMA_PAGE_SIZE*I2S_DMA_PAGE_NUM];
#define SAMPLE_FILE
#define SAMPLE_FILE_RATE 44100
#define SAMPLE_FILE_CHNUM 2
#define I2S_SCLK_PIN PC_1
#define I2S_WS_PIN PC_0
#define I2S_SD_PIN PC_2
#if defined(SAMPLE_FILE)
// no sample
// SR_96KHZ,
// SR_7p35KHZ,
// SR_29p4KHZ,
// SR_88p2KHZ
#if SAMPLE_FILE_RATE==8000
#if SAMPLE_FILE_CHNUM==2
#include "birds_8000_2ch_16b.c"
#undef SAMPLE_FILE_RATE
#define SAMPLE_FILE_RATE SR_8KHZ
#endif
#elif SAMPLE_FILE_RATE==11025
#if SAMPLE_FILE_CHNUM==2
#include "birds_11025_2ch_16b.c"
#undef SAMPLE_FILE_RATE
#define SAMPLE_FILE_RATE SR_11p02KHZ
#endif
#elif SAMPLE_FILE_RATE==16000
#if SAMPLE_FILE_CHNUM==2
#include "birds_16000_2ch_16b.c"
#undef SAMPLE_FILE_RATE
#define SAMPLE_FILE_RATE SR_16KHZ
#endif
#elif SAMPLE_FILE_RATE==22050
#if SAMPLE_FILE_CHNUM==2
#include "birds_22050_2ch_16b.c"
#undef SAMPLE_FILE_RATE
#define SAMPLE_FILE_RATE SR_22p05KHZ
#endif
#elif SAMPLE_FILE_RATE==24000
#if SAMPLE_FILE_CHNUM==2
#include "birds_24000_2ch_16b.c"
#undef SAMPLE_FILE_RATE
#define SAMPLE_FILE_RATE SR_24KHZ
#endif
#elif SAMPLE_FILE_RATE==32000
#if SAMPLE_FILE_CHNUM==2
#include "birds_32000_2ch_16b.c"
#undef SAMPLE_FILE_RATE
#define SAMPLE_FILE_RATE SR_32KHZ
#endif
#elif SAMPLE_FILE_RATE==44100
#if SAMPLE_FILE_CHNUM==2
#include "birds_44100_2ch_16b.c"
#undef SAMPLE_FILE_RATE
#define SAMPLE_FILE_RATE SR_44p1KHZ
#endif
#elif SAMPLE_FILE_RATE==48000
#if SAMPLE_FILE_CHNUM==2
#include "birds_48000_2ch_16b.c"
#undef SAMPLE_FILE_RATE
#define SAMPLE_FILE_RATE SR_48KHZ
#endif
#endif
#if SAMPLE_FILE_CHNUM==2
#undef SAMPLE_FILE_CHNUM
#define SAMPLE_FILE_CHNUM CH_STEREO
#endif
int curr_cnt=0;
#else
short test_sine16[16]={0, 12539/4, 23170/4, 30273/4, 32767/4, 30273/4, 23170/4, 12539/4,
0, -12539/4, -23170/4, -30273/4, -32767/4, -30273/4, -23170/4, -12539/4};
int test_sine24[16]={0, 12539*256/4, 23170*256/4, 30273*256/4, 32767*256/4, 30273*256/4, 23170*256/4, 12539*256/4,
0, -12539*256/4, -23170*256/4, -30273*256/4, -32767*256/4, -30273*256/4, -23170*256/4, -12539*256/4};
extern void wait_ms(u32);
#include <math.h>
short remap_level_to_signed_16_bit(float val)
{
val*=32767;
if(val>32767) val=32767;
if(val<-32768) val=-32768;
return val;
}
void generate_freq_16bit(short *buffer, int count, float freq, float sampling_rate)
{
int pos; // sample number we're on
for (pos = 0; pos < count; pos++) {
float a = 2 * 3.14159f * freq * pos / sampling_rate;
// convert from [-1.0,1.0] to [-32767,32767]:
buffer[pos] = remap_level_to_signed_16_bit(a);
}
}
void gen_sound_sample16(short *buf, int buf_size, int channel_num)
{
int i;
for (i = 0 ; i < buf_size ; i+=channel_num){
buf[i] = test_sine16[(i/channel_num)%16];
if(channel_num>=2)
buf[i+1] = test_sine16[(i/channel_num)%16];
}
}
void gen_sound_sample24(int *buf, int buf_size, int channel_num)
{
int i;
for (i = 0 ; i < buf_size ; i+=channel_num){
buf[i] = test_sine24[(i/channel_num)%16]&0xFFFFFF;
if(channel_num>=2)
//buf[i+1] = test_sine24[(i/channel_num)%16]&0xFFFFFF;
buf[i+1] = test_sine24[(i/channel_num)%16]&0xFFFFFF;
}
}
#if 0
void test_delay(int sec)
{
for(int i=0;i<166*1000*100*sec;i++)
asm(" nop");
}
#endif
int test_rate_list[12] = {
SR_8KHZ,
SR_16KHZ,
SR_24KHZ,
SR_32KHZ,
SR_48KHZ,
SR_96KHZ,
SR_7p35KHZ,
SR_11p02KHZ,
SR_22p05KHZ,
SR_29p4KHZ,
SR_44p1KHZ,
SR_88p2KHZ
};
#endif
void test_tx_complete(void *data, char *pbuf)
{
int *ptx_buf;
i2s_t *obj = (i2s_t *)data;
static u32 count=0;
//DBG_8195A_I2S_LVL(VERI_I2S_LVL, "I2S%d %s\n",pI2SDemoHnd->DevNum,__func__);
count++;
if ((count&1023) == 1023)
{
DBG_8195A_I2S_LVL(VERI_I2S_LVL, ",\n");
}
ptx_buf = i2s_get_tx_page(obj);
//ptx_buf = (int*)pbuf;
#if defined(SAMPLE_FILE)
_memcpy((void*)ptx_buf, (void*)&sample[curr_cnt], I2S_DMA_PAGE_SIZE);
curr_cnt+=(I2S_DMA_PAGE_SIZE/sizeof(short));
if(curr_cnt >= sample_size*(obj->channel_num==CH_MONO?1:2)) {
curr_cnt = 0;
}
#else
if(obj->word_length == WL_16b){
gen_sound_sample16((short*)ptx_buf, I2S_DMA_PAGE_SIZE/sizeof(short), obj->channel_num==CH_MONO?1:2);
}else{
gen_sound_sample24((int*)ptx_buf, I2S_DMA_PAGE_SIZE/sizeof(int), obj->channel_num==CH_MONO?1:2);
}
#endif
i2s_send_page(obj, (uint32_t*)ptx_buf);
}
void test_rx_complete(void *data, char* pbuf)
{
i2s_t *obj = (i2s_t *)data;
int *ptx_buf;
static u32 count=0;
count++;
if ((count&1023) == 1023)
{
DBG_8195A_I2S_LVL(VERI_I2S_LVL, ".\n");
}
ptx_buf = i2s_get_tx_page(obj);
_memcpy((void*)ptx_buf, (void*)pbuf, I2S_DMA_PAGE_SIZE);
i2s_recv_page(obj); // submit a new page for receive
i2s_send_page(obj, (uint32_t*)ptx_buf); // loopback
}
void main(void)
{
int *ptx_buf;
int i,j;
alc5651_init();
alc5651_init_interface2(); // connect to ALC interface 2
// dump register
//alc5651_reg_dump();
//alc5651_index_dump();
// I2S init
i2s_obj.channel_num = CH_MONO;//CH_STEREO;
i2s_obj.sampling_rate = SR_44p1KHZ;
i2s_obj.word_length = WL_16b;
i2s_obj.direction = I2S_DIR_TXRX;
i2s_init(&i2s_obj, I2S_SCLK_PIN, I2S_WS_PIN, I2S_SD_PIN);
i2s_set_dma_buffer(&i2s_obj, (char*)i2s_tx_buf, (char*)i2s_rx_buf, \
I2S_DMA_PAGE_NUM, I2S_DMA_PAGE_SIZE);
i2s_tx_irq_handler(&i2s_obj, (i2s_irq_handler)test_tx_complete, (uint32_t)&i2s_obj);
i2s_rx_irq_handler(&i2s_obj, (i2s_irq_handler)test_rx_complete, (uint32_t)&i2s_obj);
#if defined(SAMPLE_FILE)
i2s_set_param(&i2s_obj,SAMPLE_FILE_CHNUM,SAMPLE_FILE_RATE,WL_16b);
for (i=0;i<I2S_DMA_PAGE_NUM;i++) {
ptx_buf = i2s_get_tx_page(&i2s_obj);
if (ptx_buf) {
_memcpy((void*)ptx_buf, (void*)&sample[curr_cnt], I2S_DMA_PAGE_SIZE);
i2s_send_page(&i2s_obj, (uint32_t*)ptx_buf);
curr_cnt+=(I2S_DMA_PAGE_SIZE/sizeof(short));
if(curr_cnt >= sample_size*(i2s_obj.channel_num==CH_MONO?1:2)) {
curr_cnt = 0;
}
}
}
#else
// output freq, @ sampling rate
// 6kHz @ 96kHz
// 3kHz @ 48kHz
// 2kHz @ 32kHz
// 1.5kHz @ 24kHz
// 1kHz @ 16kHz
// 500Hz @ 8kHz
// 5512.5 Hz @ 88200Hz
// 2756.25 Hz @ 44100Hz
// 1837.5 Hz @ 29400Hz
// 1378.125 Hz @ 22050Hz
// 459.375 Hz @ 7350Hz
// Stereo, 16bit
for(i=0;i<12;i++){
i2s_set_param(&i2s_obj,CH_STEREO,test_rate_list[i],WL_16b);
// Start with fill all pages of DMA buffer
for (j=0;j<I2S_DMA_PAGE_NUM;j++) {
ptx_buf = i2s_get_tx_page(&i2s_obj);
if (ptx_buf) {
gen_sound_sample16((short*)ptx_buf, I2S_DMA_PAGE_SIZE/sizeof(short), 2);
i2s_send_page(&i2s_obj, (uint32_t*)ptx_buf);
}
}
wait_ms(5000); // delay 5 sec.
}
// Mono, 16bit
for(i=0;i<12;i++){
i2s_set_param(&i2s_obj,CH_MONO,test_rate_list[i],WL_16b);
for (j=0;j<I2S_DMA_PAGE_NUM;j++) {
ptx_buf = i2s_get_tx_page(&i2s_obj);
if (ptx_buf) {
gen_sound_sample16((short*)ptx_buf, I2S_DMA_PAGE_SIZE/sizeof(short), 1);
i2s_send_page(&i2s_obj, (uint32_t*)ptx_buf);
}
}
wait_ms(5000); // delay 5 sec.
}
// i2s_deinit(&i2s_obj);
i2s_disable(&i2s_obj);
alc5651_set_word_len(2);
alc5651_reg_dump();
i2s_enable(&i2s_obj);
// Stereo, 24bit
for(i=0;i<12;i++){
i2s_set_param(&i2s_obj,CH_STEREO,test_rate_list[i],WL_24b);
for (j=0;j<I2S_DMA_PAGE_NUM;j++) {
ptx_buf = i2s_get_tx_page(&i2s_obj);
if (ptx_buf) {
gen_sound_sample24((int*)ptx_buf, I2S_DMA_PAGE_SIZE/sizeof(int), 2);
i2s_send_page(&i2s_obj, (uint32_t*)ptx_buf);
}
}
wait_ms(5000); // delay 5 sec.
}
// Not Support Mono, 24bit
i2s_deinit(&i2s_obj);
#endif
while(1);
}