MP3 Player with touch panel interface and LCD display.
Dependencies: TextLCD mbed SDFileSystem
VS1002.cpp
00001 #include "VS1002.h" 00002 #include "mbed.h" 00003 00004 Serial pc(USBTX, USBRX); 00005 TextLCD lcd(p30, p29, p17, p18, p19, p20); 00006 00007 /* ================================================================== 00008 * Constructor 00009 * =================================================================*/ 00010 00011 VS1002::VS1002(PinName mmosi, PinName mmiso, PinName ssck, PinName ccs, const char *name, PinName mosi, PinName miso, PinName sck, PinName cs, PinName rst, PinName dreq, PinName dcs, PinName vol) 00012 : _sd(mmosi, mmiso, ssck, ccs, name), _spi(mosi, miso, sck), _CS(cs), _RST(rst), _DREQ(dreq), _DCS(dcs), _VOL(vol) { 00013 00014 } 00015 00016 /*=================================================================== 00017 * Functions 00018 *==================================================================*/ 00019 00020 void VS1002::cs_low(void) 00021 { 00022 _CS = 0; 00023 } 00024 void VS1002::cs_high(void) 00025 { 00026 _CS = 1; 00027 } 00028 void VS1002::dcs_low(void) 00029 { 00030 _DCS = 0; 00031 } 00032 void VS1002::dcs_high(void) 00033 { 00034 _DCS = 1; 00035 } 00036 void VS1002::sci_en(void) //SCI enable 00037 { 00038 cs_high(); 00039 dcs_high(); 00040 cs_low(); 00041 } 00042 void VS1002::sci_dis(void) //SCI disable 00043 { 00044 cs_high(); 00045 } 00046 void VS1002::sdi_en(void) //SDI enable 00047 { 00048 dcs_high(); 00049 cs_high(); 00050 dcs_low(); 00051 } 00052 void VS1002::sdi_dis(void) //SDI disable 00053 { 00054 dcs_high(); 00055 } 00056 void VS1002::reset(void) //hardware reset 00057 { 00058 wait(0.01); 00059 _RST = 0; 00060 wait(0.01); 00061 _RST = 1; 00062 wait(0.10); 00063 } 00064 void VS1002::power_down(void) //hardware and software reset 00065 { 00066 cs_low(); 00067 reset(); 00068 sci_write(0x00, SM_PDOWN); 00069 wait(0.01); 00070 reset(); 00071 } 00072 void VS1002::sci_initialise(void) 00073 { 00074 _RST = 1; //no reset 00075 _spi.format(8,0); //spi 8bit interface, steady state low 00076 _spi.frequency(1000000); //rising edge data record, freq. 1Mhz 00077 00078 cs_low(); 00079 for(int i=0; i<4; i++) 00080 { 00081 _spi.write(0xFF); //clock the chip a bit 00082 } 00083 cs_high(); 00084 dcs_high(); 00085 wait_us(5); 00086 } 00087 void VS1002::sdi_initialise(void) 00088 { 00089 _spi.format(8,0); 00090 _spi.frequency(7000000); //set to 7MHz 00091 00092 cs_high(); 00093 dcs_high(); 00094 } 00095 void VS1002::sci_write(unsigned char address, unsigned short int data) 00096 { 00097 sci_en(); //enables SCI/disables SDI 00098 00099 while(!_DREQ); //wait unitl data request is high 00100 _spi.write(0x02); //SCI write 00101 _spi.write(address); //register address 00102 _spi.write((data >> 8) & 0xFF); //write out first half of data word 00103 _spi.write(data & 0xFF); //write out second half of data word 00104 00105 sci_dis(); //enables SDI/disables SCI 00106 wait_us(5); 00107 } 00108 void VS1002::sdi_write(unsigned char datum) 00109 { 00110 sdi_en(); 00111 00112 while(!_DREQ); 00113 _spi.write(datum); 00114 00115 sci_dis(); 00116 } 00117 unsigned short int VS1002::read(unsigned short int address) 00118 { 00119 cs_low(); //enables SCI/disables SDI 00120 00121 while(!_DREQ); //wait unitl data request is high 00122 _spi.write(0x03); //SCI write 00123 _spi.write(address); //register address 00124 unsigned short int received = _spi.write(0x00); //write out dummy byte 00125 received <<= 8; 00126 received += _spi.write(0x00); //write out dummy byte 00127 00128 cs_high(); //enables SDI/disables SCI 00129 00130 return received; //return received word 00131 } 00132 void VS1002::sine_test_activate(unsigned char wave) 00133 { 00134 cs_high(); //enables SDI/disables SCI 00135 00136 while(!_DREQ); //wait unitl data request is high 00137 _spi.write(0x53); //SDI write 00138 _spi.write(0xEF); //SDI write 00139 _spi.write(0x6E); //SDI write 00140 _spi.write(wave); //SDI write 00141 _spi.write(0x00); //filler byte 00142 _spi.write(0x00); //filler byte 00143 _spi.write(0x00); //filler byte 00144 _spi.write(0x00); //filler byte 00145 00146 cs_low(); //enables SCI/disables SDI 00147 } 00148 void VS1002::sine_test_deactivate(void) 00149 { 00150 cs_high(); 00151 00152 while(!_DREQ); 00153 _spi.write(0x45); //SDI write 00154 _spi.write(0x78); //SDI write 00155 _spi.write(0x69); //SDI write 00156 _spi.write(0x74); //SDI write 00157 _spi.write(0x00); //filler byte 00158 _spi.write(0x00); //filler byte 00159 _spi.write(0x00); //filler byte 00160 _spi.write(0x00); //filler byte 00161 } 00162 00163 00164 void VS1002::volume(signed int left, signed int right) 00165 { 00166 while(_DREQ == 0); 00167 00168 unsigned short int _left = -left; //convert the decibel values into a format 00169 unsigned short int _right = -right; //readable by the chip cf. datasheet p.32 subsection 8.6.11 00170 _left *= 2; 00171 _right *= 2; 00172 unsigned short int attenuation = ((256 * _left) + _right); 00173 cs_low(); 00174 sci_write(0x0B, attenuation); //writeout these values 00175 cs_high(); 00176 } 00177 00178 void VS1002::play_song(int song_number) 00179 { 00180 /*====== Song Select ======*/ 00181 00182 char str[16] = "/sd/music/"; //folder where the songs are located 00183 sprintf(str+10,"%d",song_number); //appending song number to path of the file 00184 strcat(str,".mp3"); //appending .mp3 to file name 00185 FILE *song; 00186 unsigned char array[512]; //array for reading data from file 00187 bool play_new=false; // Variable to see if new_song has be assigned or not 00188 00189 song = fopen(str, "r"); // Open the music file in read mode 00190 00191 /* Printing to LCD the present status */ 00192 lcd.cls(); 00193 if(pause) 00194 lcd.printf("Paused "); 00195 if(mute) 00196 lcd.printf("Muted"); 00197 if(!mute && !pause) 00198 lcd.printf("Playing"); 00199 00200 lcd.printf("\n %d %s",new_song_number,song_name[new_song_number-1]); 00201 00202 if(!song) 00203 { 00204 lcd.printf("\n \n Error!!"); 00205 exit(1); 00206 } 00207 while(!feof(song)) 00208 { 00209 if(!pause) 00210 { 00211 00212 fread(&array, 1, 512, song); 00213 for(int i=0; i<512; i++) 00214 { 00215 sdi_write(array[i]); 00216 } 00217 volume(volume_set,volume_set); 00218 } 00219 if(new_song_number!=song_number) 00220 { 00221 play_new=true; 00222 break; 00223 } 00224 00225 00226 } 00227 00228 fclose(song); //close the file 00229 00230 if(!play_new) 00231 { 00232 new_song_number+=1; // Goto Next song on completion of one song 00233 if(new_song_number==7) 00234 new_song_number=1; 00235 play_new=false; 00236 } 00237 }
Generated on Wed Jul 13 2022 20:51:16 by 1.7.2