Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "MSCFileSystem.h" 00003 #include "SDHCFileSystem.h" 00004 #include "TextLCD.h" 00005 #include "i2s_irq_test.h" 00006 #include "lpc17xx_i2s.h" 00007 #include "lpc17xx_clkpwr.h" 00008 #include "aic23b_comm.h" 00009 #include "string" 00010 #include "_bitio.h" 00011 00012 #define _RGM_ADAPTIVE 1 /* 1:Adaptive 0:Constant */ 00013 00014 #define k_bits 0x07 // k_bits:0x09 to 0x0b? 00015 #define min_of_k 0x07 00016 #define max_of_k 0x0b /*for dynamic Rice-Golomb codings */ 00017 00018 DigitalOut led1(LED1); 00019 DigitalOut led2(LED2); 00020 DigitalOut led3(LED3); 00021 DigitalOut led4(LED4); 00022 Serial pc(USBTX, USBRX); // tx, rx 00023 I2C AIC23B(p9,p10); //sda,scl 00024 00025 TextLCD lcd(p24, p26, p27, p28, p29, p30); 00026 SDFileSystem sd(p11, p12, p13, p14, "sd");//p5,6,7,8 00027 //MSCFileSystem msc("msc"); // Mount flash drive under the name "msc" 00028 FILE *infp,*outfp,*playlist; 00029 00030 Ticker tick; 00031 Ticker fl_leds; 00032 00033 #define AudioBufRow 256 /* length of proc_r */ 00034 volatile short int AudioBuf[2][AudioBufRow]; /* Audio Data Buffer:AudioBuf[empty_c][proc_r] */ 00035 volatile BYTE empty_c=0; /* Column of AudioBuf */ 00036 volatile BYTE proc_r=0; /* Row of AudioBuf */ 00037 bool dac_semp; /* Write AudioBuf Complete? true:Yes, false:No */ 00038 00039 00040 int aic23b_send(int addr,char ctrl_address,char ctrl_data){ 00041 int flag; 00042 char cmd[2]; 00043 cmd[0]=ctrl_address; 00044 cmd[1]=ctrl_data; 00045 flag = AIC23B.write(addr, cmd, 2); 00046 pc.printf("ADDR=0x%x, CTRL_ADD=0x%x, CTRL_DATA=0x%x, RESP=0x%x\r\n",(addr&0x7f),cmd[0],cmd[1],flag); 00047 return (flag); 00048 } 00049 00050 int aic23b_init(void){ 00051 int flag=0; 00052 printf("*************************\r\nReset TLV320AIC23B\r\n*************************\r\n"); 00053 AIC23B.frequency(150000); 00054 flag = aic23b_send(AIC23B_ADDRESS,RESET_REFGISTER,RESET); 00055 wait(0.1); 00056 flag += aic23b_send(AIC23B_ADDRESS,POWER_DOWN_CONTROL,0); 00057 wait(0.1); 00058 flag += aic23b_send(AIC23B_ADDRESS,POWER_DOWN_CONTROL,1); 00059 wait(0.1); 00060 flag += aic23b_send(AIC23B_ADDRESS,DIGITAL_AUDIO_INTERFACE_FORMAT,(MASTER_MODE|INPUT_DATA_16_BIT_LENGTH|I2S_FORMAT)); 00061 wait(0.1); 00062 flag += aic23b_send(AIC23B_ADDRESS,SAMPLE_RATE_CONTROL,(SR_USB_44_1_KHZ_MODE| BOSR_USB_44_1_KHZ_MODE| USE_USB_CLOCK_44_1_KHZ_MODE)); 00063 wait(0.1); 00064 flag += aic23b_send(AIC23B_ADDRESS,ANALOG_AUDIO_PATH_CONTROL,0x10); 00065 wait(0.1); 00066 flag += aic23b_send(AIC23B_ADDRESS,DIGITAL_AUDIO_PATH_CONTROL,0); 00067 wait(0.1); 00068 flag += aic23b_send(AIC23B_ADDRESS,DIGITAL_INTERFACE_ACTIVATION,DIGITAL_INTERFACE_ACTIVE); 00069 wait(0.1); 00070 flag += aic23b_send(AIC23B_ADDRESS,LEFT_CHANNEL_HEADPHONE_VOLUME_CONTROL,LHV_VOLUME_DEFAULT); 00071 wait(0.1); 00072 flag += aic23b_send(AIC23B_ADDRESS,RIGHT_CHANNEL_HEADPHONE_VOLUME_CONTROL,RHV_VOLUME_DEFAULT); 00073 00074 if(!flag) { 00075 printf("*************************\r\nReset OK\r\n*************************\r\n"); 00076 return 0; 00077 } 00078 else{ 00079 printf("*************************\r\nReset FAILED\r\n*************************\r\n"); 00080 return -1; 00081 } 00082 } 00083 00084 void pl_led_flash(void) 00085 { 00086 if(led1){ 00087 led1 = 0; 00088 led2 = 1; 00089 return; 00090 } 00091 00092 if(led2){ 00093 led2 = 0; 00094 led3 = 1; 00095 return; 00096 } 00097 00098 if(led3){ 00099 led3 = 0; 00100 led4 = 1; 00101 return; 00102 } 00103 00104 if(led4){ 00105 led4 = 0; 00106 led1 = 1; 00107 return; 00108 } 00109 00110 00111 } 00112 00113 void dac_out(void) 00114 { 00115 volatile static short i,j; // AudioBuf[j][i] 00116 volatile uint32_t DBufLR; // Buffer Data to send I2S_TX 00117 uint16_t BufLPCnt; 00118 for(BufLPCnt=0 ; BufLPCnt < 9 ; BufLPCnt++ ) 00119 { 00120 if(i >= AudioBufRow-1) 00121 { 00122 if(dac_semp) /* colunm (empty_c) is full? true:Yes */ 00123 { 00124 i=0; 00125 empty_c =(empty_c==1)?0:1; /* empty_c:empty column of buffer AudioBuf, Because I2S_TX used these data */ 00126 j=(empty_c==1)?0:1; /* Change column which buffer is filled with data by function decode() */ 00127 dac_semp = false; /* false: empty_c is ready to get data from function decode() */ 00128 00129 }else{ /* Buffer is NOT ready to read? */ 00130 return; /* NO:return */ 00131 } 00132 } 00133 //Send I2S_TX 00134 if(I2S_GetLevel(LPC_I2S, I2S_TX_MODE)==TXFIFO_FULL)break; 00135 DBufLR = (0xffff0000 & (AudioBuf[j][i++]<<16))|(0x0000ffff & AudioBuf[j][i++]); 00136 I2S_Send(LPC_I2S,DBufLR); 00137 } 00138 } 00139 00140 void encode(long int n){ 00141 int zero_shift = 0; 00142 00143 if(n < 0){ 00144 putbit(0); // sign (put 0:if n as negative) 00145 n = -n; // n = abs(n) 00146 //printf("\t 0"); 00147 } 00148 else{ 00149 putbit(1); // sign (put 1:if n as positive) 00150 //printf("\t 1"); 00151 } 00152 zero_shift = (n >> (_lsb_k)); 00153 //printf("\t shift= %d",zero_shift); 00154 while(zero_shift > 0){ 00155 00156 zero_shift--; 00157 putbit(0); 00158 } // put n/(2^_lsb_k) 0's 00159 00160 putbit(1); // terminating "1" 00161 putbits(_lsb_k,rightbits(_lsb_k,n)); 00162 //printf("\t finish= %d \r\n",(n & ((1U<<_lsb_k)-1))); 00163 } 00164 00165 00166 00167 void decode(void) 00168 { 00169 volatile short i=0,j=0; 00170 00171 volatile long int diff=0,diff2=0; /* differential of previous data buffer */ 00172 volatile unsigned int buff_sign,zero_shift; /* sign buffer,register for unary codings */ 00173 volatile long int decode_buff; 00174 00175 volatile unsigned char k1= k_bits; /* Coding Parameter (init:k_bits) */ 00176 volatile unsigned char k2= k_bits; 00177 00178 init_bit_o(); 00179 init_bit_i(); /* init bit control function */ 00180 while(1){ 00181 00182 if(i!=empty_c) 00183 { 00184 if(j>=AudioBufRow-1) 00185 { 00186 i=empty_c; /* Choose empty buffer*/ 00187 j=0; 00188 } 00189 } 00190 00191 if (j<AudioBufRow-1) 00192 { 00193 /* decode */ 00194 if((buff_sign = getbit()) == OVERRUN)break; /*get sign*/ 00195 00196 zero_shift = 0; 00197 while(getbit()==0) 00198 zero_shift++; /* decode unary code */ 00199 00200 decode_buff = (signed int)(zero_shift*(1U<<k1)); /*decode Rice-Golomb code*/ 00201 decode_buff += getbits(k1); 00202 00203 if(!buff_sign)decode_buff =- decode_buff; /*add sign */ 00204 diff =(diff + decode_buff); /* get audio data */ 00205 00206 AudioBuf[i][j]=(short int)(diff); /* write AudioBuf */ 00207 j++; 00208 00209 00210 #if _RGM_ADAPTIVE 00211 /* calc k */ 00212 if(zero_shift > 2)k1=k1+1; 00213 if(zero_shift == 0)k1=k1-1; 00214 if(k1 < min_of_k)k1 = min_of_k; 00215 if(k1 > max_of_k)k1 = max_of_k; 00216 #endif 00217 00218 if((buff_sign = getbit()) == OVERRUN)break; /* get sign */ 00219 00220 zero_shift = 0; 00221 while(getbit()==0) 00222 zero_shift++; /* decode unary code */ 00223 00224 decode_buff = (signed int)(zero_shift*(1U<<k2)); /* decode Rice-Golomb code */ 00225 decode_buff += getbits(k2); 00226 00227 if(!buff_sign)decode_buff =- decode_buff; /* add sign */ 00228 diff2 =(diff2 + decode_buff); /* get Audio data */ 00229 00230 AudioBuf[i][j]=(short int)(diff2); /* Write Buffer */ 00231 j++; 00232 00233 #if _RGM_ADAPTIVE 00234 /* calc k */ 00235 if(zero_shift > 2)k2=k2+1; 00236 if(zero_shift == 0)k2=k2-1; 00237 if(k2 < min_of_k)k2 = min_of_k; 00238 if(k2 > max_of_k)k2 = max_of_k; 00239 #endif 00240 00241 } 00242 if(j>=AudioBufRow-1){ /* AudioBuf is filled with data? */ 00243 dac_semp = true; 00244 } 00245 } 00246 } 00247 00248 00249 int main() { 00250 00251 char s[256]; 00252 char* p; 00253 00254 lcd.cls(); 00255 lcd.locate(0,0); 00256 lcd.printf("I2S Codec:"); 00257 mbed_i2s_init(); //DAC:44100Hz sampling ,16bit ,Stereo ,MCLK Disable, TLV320AIC23B=Master 00258 if(aic23b_init()==0){ 00259 lcd.locate(12,0); 00260 lcd.printf("OK"); 00261 }else{ 00262 lcd.locate(12,0); 00263 lcd.printf("NG"); 00264 } 00265 Buffer_Init(); 00266 pc.printf("CCLKCFG= %d \r\n",LPC_SC->CCLKCFG); 00267 pc.printf("I2S Send start.\r\n"); 00268 led1=1; 00269 lcd.locate(0,1); 00270 lcd.printf("> Please Wait..."); 00271 00272 if ( NULL == (playlist = fopen( "/sd/play.txt", "r" )) ) { 00273 printf( "\r\nError: The Playlist file cannot be accessed\r\n" ); 00274 return -1; 00275 } 00276 00277 00278 while(1){ 00279 if((fgets( s, 256, playlist ))==NULL){ 00280 fseek(playlist, 0L, SEEK_SET); 00281 fgets( s, 256, playlist ); 00282 } 00283 p=strchr(s,'\n'); 00284 if(p!=NULL){ 00285 *p='\0'; 00286 00287 } //remove CR code for Macintosh text-file 00288 p=strchr(s,'\r'); 00289 if(p!=NULL){ 00290 *p='\0'; 00291 00292 } //remove LF code for Linux & Windows text-file 00293 printf("\r\n open file:%s \r\n",s); // cut '\n' symbol 00294 lcd.locate(0,1); 00295 lcd.printf("> "); 00296 s[15]='\0'; 00297 lcd.locate(1,1); 00298 lcd.printf("%s",s); 00299 if ( NULL == (infp = fopen( s, "r" )) ) { 00300 printf( "\r\nError: The message file cannot be accessed\r\n" ); 00301 return -1; 00302 } 00303 00304 fseek(infp, 0L, SEEK_SET); 00305 tick.attach_us(&dac_out, 181); //set 44.1kHz/8(word FIFO) sampling data 00306 fl_leds.attach(&pl_led_flash,1); 00307 00308 00309 decode(); 00310 00311 fclose( infp ); 00312 infp = NULL; 00313 tick.detach(); 00314 fl_leds.detach(); 00315 lcd.locate(0,1); 00316 lcd.printf("> Please Wait..."); 00317 } 00318 } 00319 00320
Generated on Tue Jul 12 2022 20:16:18 by
1.7.2