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