Content originally posted in LPCWare by praveen.9123 on Tue May 20 22:36:31 MST 2014
Hi Bavarian,
These is the actual code for I2S configuration which works for below 48Kbps(bit-rate) and 16Khz sampling frequency or 24Khz Sampling frequency with little delay in it, but works fine.
If you don't mind check the below code whether it is correct or not.
When i used for above 48kbps the Delay is more i don't know why it happens. Please help me to play mp3 stream data with more than 48kbps(bit-rate), It will be helpful to me.
/* Configure I2S for Audio Format input */
Status Chip_I2S_Config(LPC_I2S_Type *I2Sx, uint8_t TRMode, Chip_I2S_Audio_Format_Type *audio_format)
{
uint32_t pClk;
uint32_t x, y;
uint64_t divider;
uint16_t dif;
uint16_t x_divide = 0, y_divide = 0;
uint32_t N;
uint16_t err, ErrorOptimal = 0xFFFF;
pClk = (uint64_t)Chip_Clock_GetRate(CLK_APB1_I2S);
/* divider is a fixed point number with 16 fractional bits */
divider = (((uint64_t)(audio_format->SampleRate) * 2 * (audio_format->WordWidth) * 2) << 16) / pClk;
/* find N that make x/y <= 1 -> divider <= 2^16 */
for (N = 64; N > 0; N--) {
if ((divider * N) < (1 << 16)) {
break;
}
}
if (N == 0) {
return ERROR;
}
divider *= N;
for (y = 255; y > 0; y--) {
x = y * divider;
if (x & (0xFF000000)) {
continue;
}
dif = x & 0xFFFF;
if (dif > 0x8000) {
err = 0x10000 - dif;
}
else {
err = dif;
}
if (err == 0) {
y_divide = y;
break;
}
else if (err < ErrorOptimal) {
ErrorOptimal = err;
y_divide = y;
}
}
x_divide = ((uint64_t)y_divide * (audio_format->SampleRate) * 2 * (audio_format->WordWidth) * N * 2) / pClk;
if (x_divide >= 256) {
x_divide = 0xFF;
}
if (x_divide == 0) {
x_divide = 1;
}
if (audio_format->WordWidth <= 8) {
IP_I2S_SetWordWidth(I2Sx, TRMode, I2S_WORDWIDTH_8);
}
else if (audio_format->WordWidth <= 16) {
IP_I2S_SetWordWidth(I2Sx, TRMode, I2S_WORDWIDTH_16);
}
else {
IP_I2S_SetWordWidth(I2Sx, TRMode, I2S_WORDWIDTH_32);
}
IP_I2S_SetMono(I2Sx, TRMode, (audio_format->ChannelNumber) == 1 ? I2S_MONO : I2S_STEREO);
IP_I2S_SetMasterSlaveMode(I2Sx, TRMode, I2S_MASTER_MODE);
IP_I2S_SetWS_Halfperiod(I2Sx, TRMode, audio_format->WordWidth - 1);
IP_I2S_ModeConfig(I2Sx, TRMode, I2S_TXMODE_CLKSEL(0), !I2S_TXMODE_4PIN_ENABLE, !I2S_TXMODE_MCENA);
IP_I2S_SetBitRate(I2Sx, TRMode, N - 1);
IP_I2S_SetXYDivider(I2Sx, TRMode, x_divide, y_divide);
return SUCCESS;
}
Best Regards,
Praveen