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.
Dependencies: FatFileSystem HTTPClient NetServices RPG SDFileSystem TextLCD mbed spxml
Fork of Arduino_MP3_Shield_MP3Player_UI by
VS1002.cpp
00001 #include "VS1002.h" 00002 #include "mbed.h" 00003 #include "TextLCD.h" 00004 AnalogIn rotary1(p19); 00005 DigitalIn pause1(p28); 00006 TextLCD lcd(p10, p18, p24, p23, p22, p21, TextLCD::LCD16x2 ); // rs, e, d0-d3 00007 00008 00009 /* ================================================================== 00010 * Constructor 00011 * =================================================================*/ 00012 VS1002::VS1002( 00013 PinName mmosi, PinName mmiso, PinName ssck, PinName ccs, const char *name, 00014 PinName mosi, PinName miso, PinName sck, PinName cs, PinName rst, 00015 PinName dreq, PinName dcs, PinName vol) 00016 : 00017 _sd(mmosi, mmiso, ssck, ccs, name), 00018 _spi(mosi, miso, sck), 00019 _CS(cs), 00020 _RST(rst), 00021 _DREQ(dreq), 00022 _DCS(dcs), 00023 _VOL(vol) 00024 { 00025 00026 } 00027 00028 /*=================================================================== 00029 * Functions 00030 *==================================================================*/ 00031 00032 void VS1002::cs_low(void) 00033 { 00034 _CS = 0; 00035 } 00036 void VS1002::cs_high(void) 00037 { 00038 _CS = 1; 00039 } 00040 void VS1002::dcs_low(void) 00041 { 00042 _DCS = 0; 00043 } 00044 void VS1002::dcs_high(void) 00045 { 00046 _DCS = 1; 00047 } 00048 void VS1002::sci_en(void) //SCI enable 00049 { 00050 cs_high(); 00051 dcs_high(); 00052 cs_low(); 00053 } 00054 void VS1002::sci_dis(void) //SCI disable 00055 { 00056 cs_high(); 00057 } 00058 void VS1002::sdi_en(void) //SDI enable 00059 { 00060 dcs_high(); 00061 cs_high(); 00062 dcs_low(); 00063 } 00064 void VS1002::sdi_dis(void) //SDI disable 00065 { 00066 dcs_high(); 00067 } 00068 void VS1002::reset(void) //hardware reset 00069 { 00070 wait(0.01); 00071 _RST = 0; 00072 wait(0.01); 00073 _RST = 1; 00074 wait(0.10); 00075 } 00076 void VS1002::power_down(void) //hardware and software reset 00077 { 00078 cs_low(); 00079 reset(); 00080 sci_write(0x00, SM_PDOWN); 00081 wait(0.01); 00082 reset(); 00083 } 00084 void VS1002::sci_initialise(void) 00085 { 00086 _RST = 1; //no reset 00087 _spi.format(8,0); //spi 8bit interface, steady state low 00088 _spi.frequency(1000000); //rising edge data record, freq. 1Mhz 00089 00090 cs_low(); 00091 for(int i=0; i<4; i++) { 00092 _spi.write(0xFF); //clock the chip a bit 00093 } 00094 cs_high(); 00095 dcs_high(); 00096 wait_us(5); 00097 } 00098 void VS1002::sdi_initialise(void) 00099 { 00100 _spi.format(8,0); 00101 _spi.frequency(7000000); //set to 7MHz 00102 00103 cs_high(); 00104 dcs_high(); 00105 } 00106 void VS1002::sci_write(unsigned char address, unsigned short int data) 00107 { 00108 sci_en(); //enables SCI/disables SDI 00109 00110 while(!_DREQ); //wait unitl data request is high 00111 _spi.write(0x02); //SCI write 00112 _spi.write(address); //register address 00113 _spi.write((data >> 8) & 0xFF); //write out first half of data word 00114 _spi.write(data & 0xFF); //write out second half of data word 00115 00116 sci_dis(); //enables SDI/disables SCI 00117 wait_us(5); 00118 } 00119 void VS1002::sdi_write(unsigned char datum) 00120 { 00121 sdi_en(); 00122 00123 while(!_DREQ); 00124 _spi.write(datum); 00125 00126 sci_dis(); 00127 } 00128 unsigned short int VS1002::read(unsigned short int address) 00129 { 00130 cs_low(); //enables SCI/disables SDI 00131 00132 while(!_DREQ); //wait unitl data request is high 00133 _spi.write(0x03); //SCI write 00134 _spi.write(address); //register address 00135 unsigned short int received = _spi.write(0x00); //write out dummy byte 00136 received <<= 8; 00137 received += _spi.write(0x00); //write out dummy byte 00138 00139 cs_high(); //enables SDI/disables SCI 00140 00141 return received; //return received word 00142 } 00143 void VS1002::sine_test_activate(unsigned char wave) 00144 { 00145 cs_high(); //enables SDI/disables SCI 00146 00147 while(!_DREQ); //wait unitl data request is high 00148 _spi.write(0x53); //SDI write 00149 _spi.write(0xEF); //SDI write 00150 _spi.write(0x6E); //SDI write 00151 _spi.write(wave); //SDI write 00152 _spi.write(0x00); //filler byte 00153 _spi.write(0x00); //filler byte 00154 _spi.write(0x00); //filler byte 00155 _spi.write(0x00); //filler byte 00156 00157 cs_low(); //enables SCI/disables SDI 00158 } 00159 void VS1002::sine_test_deactivate(void) 00160 { 00161 cs_high(); 00162 00163 while(!_DREQ); 00164 _spi.write(0x45); //SDI write 00165 _spi.write(0x78); //SDI write 00166 _spi.write(0x69); //SDI write 00167 _spi.write(0x74); //SDI write 00168 _spi.write(0x00); //filler byte 00169 _spi.write(0x00); //filler byte 00170 _spi.write(0x00); //filler byte 00171 _spi.write(0x00); //filler byte 00172 } 00173 void VS1002::volume(void) 00174 { 00175 #ifdef FIXED_VOL 00176 unsigned char volumize = (0 * 255); // FIXED VOL (not support volume input) 00177 #else 00178 unsigned char volumize = (_VOL * 255); 00179 #endif 00180 while(!_DREQ); 00181 00182 unsigned short int attenuation = ((256 * volumize) + volumize); 00183 sci_write(0x0B, attenuation); 00184 00185 } 00186 00187 void VS1002::play_song(int song_number) 00188 { 00189 /*====== Song Select ======*/ 00190 00191 // char list[10000] = {0}; 00192 char list[1000] = {0}; 00193 char str[16] = {"/sd/"}; 00194 unsigned int startplace = 0; 00195 unsigned int endplace = 0; 00196 unsigned int play = 0; 00197 num_of_files = 0; 00198 00199 DIR *d; 00200 struct dirent *p; 00201 d = opendir("/sd"); 00202 if(d != NULL) { 00203 while((p = readdir(d)) != NULL) { 00204 strcat(list, "*"); 00205 strcat(list, p->d_name); 00206 num_of_files++; 00207 } 00208 } else { 00209 perror("Could not open directory!"); 00210 } 00211 strcat(list, "*"); //terminating * 00212 if(num_of_files < song_number) { 00213 return; 00214 } 00215 while(play != song_number) { 00216 char symbol = list[startplace]; 00217 startplace++; 00218 if(symbol == 0x2A) { //0x2A = "*" 00219 play++; 00220 } 00221 } 00222 play = 0; 00223 while(play != (song_number+1)) { 00224 char symbol = list[endplace]; 00225 endplace++; 00226 if(symbol == 0x2A) { //0x2A = "*" 00227 play++; 00228 } 00229 } 00230 00231 strncat(str, &list[startplace], endplace-startplace); 00232 str[(endplace-startplace)+3] = '\0'; 00233 00234 //printf("list: %s\r\n",list); //debug 00235 00236 /*====== File Transfer ======*/ 00237 00238 // return if not MP3 file 00239 if (!((strstr(str,"MP3")!=NULL)||(strstr(str,"mp3")!=NULL))) return; 00240 // display filename.mp3 to the lcd screen 00241 lcd.printf("Now Playing: %s\r\n",str); 00242 00243 FILE *song; 00244 unsigned char array[512]; 00245 00246 song = fopen(str, "rb"); 00247 00248 if(!song) { 00249 exit(1); 00250 } 00251 00252 while((!feof(song))&&!pause1) { 00253 fread(&array, 1, 512, song); 00254 for(int i=0; i<512; i++) { 00255 #ifndef FS_ONLY 00256 sdi_write(array[i]); 00257 // printf("."); 00258 #endif 00259 } 00260 #ifndef FS_ONLY 00261 volume(); 00262 #endif 00263 } 00264 for(int n=0; n<2048; n++) { 00265 #ifndef FS_ONLY 00266 sdi_write(0x00); 00267 #endif 00268 } 00269 fclose(song); //close the file 00270 lcd.printf("Done Playing: %s\r\n\n",str); 00271 lcd.printf(" \n"); 00272 }
Generated on Mon Jul 18 2022 19:26:35 by
