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: 4DGL-uLCD-SE SDFileSystem TextLCD mbed wave_player
VS1002.cpp
00001 #include "VS1002.h" 00002 #include "mbed.h" 00003 #include "uLCD_4DGL.h" 00004 00005 //Serial pc(USBTX, USBRX); 00006 uLCD_4DGL uLCD2(p28, p27, p29); 00007 00008 /* ================================================================== 00009 * Constructor 00010 * =================================================================*/ 00011 00012 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) 00013 : _sd(mmosi, mmiso, ssck, ccs, name), _spi(mosi, miso, sck), _CS(cs), _RST(rst), _DREQ(dreq), _DCS(dcs), _VOL(vol) { 00014 00015 } 00016 00017 /*=================================================================== 00018 * Functions 00019 *==================================================================*/ 00020 00021 void VS1002::cs_low(void) 00022 { 00023 _CS = 0; 00024 } 00025 void VS1002::cs_high(void) 00026 { 00027 _CS = 1; 00028 } 00029 void VS1002::dcs_low(void) 00030 { 00031 _DCS = 0; 00032 } 00033 void VS1002::dcs_high(void) 00034 { 00035 _DCS = 1; 00036 } 00037 void VS1002::sci_en(void) //SCI enable 00038 { 00039 cs_high(); 00040 dcs_high(); 00041 cs_low(); 00042 } 00043 void VS1002::sci_dis(void) //SCI disable 00044 { 00045 cs_high(); 00046 } 00047 void VS1002::sdi_en(void) //SDI enable 00048 { 00049 dcs_high(); 00050 cs_high(); 00051 dcs_low(); 00052 } 00053 void VS1002::sdi_dis(void) //SDI disable 00054 { 00055 dcs_high(); 00056 } 00057 void VS1002::reset(void) //hardware reset 00058 { 00059 wait(0.01); 00060 _RST = 0; 00061 wait(0.01); 00062 _RST = 1; 00063 wait(0.10); 00064 } 00065 void VS1002::power_down(void) //hardware and software reset 00066 { 00067 cs_low(); 00068 reset(); 00069 sci_write(0x00, SM_PDOWN); 00070 wait(0.01); 00071 reset(); 00072 } 00073 void VS1002::sci_initialise(void) 00074 { 00075 _RST = 1; //no reset 00076 _spi.format(8,0); //spi 8bit interface, steady state low 00077 _spi.frequency(1000000); //rising edge data record, freq. 1Mhz 00078 00079 cs_low(); 00080 for(int i=0; i<4; i++) 00081 { 00082 _spi.write(0xFF); //clock the chip a bit 00083 } 00084 cs_high(); 00085 dcs_high(); 00086 wait_us(5); 00087 } 00088 void VS1002::sdi_initialise(void) 00089 { 00090 _spi.format(8,0); 00091 _spi.frequency(7000000); //set to 7MHz 00092 00093 cs_high(); 00094 dcs_high(); 00095 } 00096 void VS1002::sci_write(unsigned char address, unsigned short int data) 00097 { 00098 sci_en(); //enables SCI/disables SDI 00099 00100 while(!_DREQ); //wait unitl data request is high 00101 _spi.write(0x02); //SCI write 00102 _spi.write(address); //register address 00103 _spi.write((data >> 8) & 0xFF); //write out first half of data word 00104 _spi.write(data & 0xFF); //write out second half of data word 00105 00106 sci_dis(); //enables SDI/disables SCI 00107 wait_us(5); 00108 } 00109 void VS1002::sdi_write(unsigned char datum) 00110 { 00111 sdi_en(); 00112 00113 while(!_DREQ); 00114 _spi.write(datum); 00115 00116 sci_dis(); 00117 } 00118 unsigned short int VS1002::read(unsigned short int address) 00119 { 00120 cs_low(); //enables SCI/disables SDI 00121 00122 while(!_DREQ); //wait unitl data request is high 00123 _spi.write(0x03); //SCI write 00124 _spi.write(address); //register address 00125 unsigned short int received = _spi.write(0x00); //write out dummy byte 00126 received <<= 8; 00127 received += _spi.write(0x00); //write out dummy byte 00128 00129 cs_high(); //enables SDI/disables SCI 00130 00131 return received; //return received word 00132 } 00133 void VS1002::sine_test_activate(unsigned char wave) 00134 { 00135 cs_high(); //enables SDI/disables SCI 00136 00137 while(!_DREQ); //wait unitl data request is high 00138 _spi.write(0x53); //SDI write 00139 _spi.write(0xEF); //SDI write 00140 _spi.write(0x6E); //SDI write 00141 _spi.write(wave); //SDI write 00142 _spi.write(0x00); //filler byte 00143 _spi.write(0x00); //filler byte 00144 _spi.write(0x00); //filler byte 00145 _spi.write(0x00); //filler byte 00146 00147 cs_low(); //enables SCI/disables SDI 00148 } 00149 void VS1002::sine_test_deactivate(void) 00150 { 00151 cs_high(); 00152 00153 while(!_DREQ); 00154 _spi.write(0x45); //SDI write 00155 _spi.write(0x78); //SDI write 00156 _spi.write(0x69); //SDI write 00157 _spi.write(0x74); //SDI write 00158 _spi.write(0x00); //filler byte 00159 _spi.write(0x00); //filler byte 00160 _spi.write(0x00); //filler byte 00161 _spi.write(0x00); //filler byte 00162 } 00163 00164 00165 void VS1002::volume(signed int left, signed int right) 00166 { 00167 while(_DREQ == 0); 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::update_LCD(string print_file) 00179 { 00180 uLCD2.cls(); 00181 if(pause){ 00182 uLCD2.printf("Paused "); 00183 uLCD2.printf(print_file.c_str());} 00184 if(mute){ 00185 uLCD2.printf("Muted "); 00186 uLCD2.printf(print_file.c_str());} 00187 if(!mute && !pause){ 00188 uLCD2.printf("Playing "); 00189 uLCD2.printf(print_file.c_str()); } 00190 00191 uLCD2.locate(0,4); 00192 if(fade==0) 00193 uLCD2.printf("Fade: %s ",print_fade); 00194 else 00195 uLCD2.printf("Fade: %s %i % ",print_fade,fade_percent); 00196 if(t!=oldt) 00197 { 00198 uLCD2.locate(0,8); 00199 if(t<10) 00200 uLCD2.printf("%i:0%i",m,t); 00201 else 00202 uLCD2.printf("%i:%i",m,t); 00203 oldt=t; 00204 } 00205 //uLCD2.printf("\n %d %s",new_song_number,song_name[new_song_number-1]); 00206 } 00207 00208 void VS1002::play_song(string song, string print_file, int song_number) 00209 { 00210 /*====== Song Select ======*/ 00211 00212 FILE *song2play; 00213 unsigned char array[512]; //array for reading data from file 00214 bool play_new=false; // Variable to see if new_song has be assigned or not 00215 00216 song2play = fopen(song.c_str(), "r"); // Open the music file in read mode 00217 00218 //Printing to LCD the present status 00219 update_LCD(print_file); 00220 t=0; 00221 m=0; 00222 if(!song2play) 00223 { 00224 uLCD2.printf("\n \n Error!!"); 00225 exit(1); 00226 } 00227 while(!feof(song2play)) 00228 { 00229 if(!pause) 00230 { 00231 fread(&array, 1, 512, song2play); 00232 for(int i=0; i<512; i++) 00233 { 00234 sdi_write(array[i]); 00235 } 00236 volume(volume_left,volume_right); 00237 } 00238 if(t!=oldt) 00239 { 00240 uLCD2.locate(0,8); 00241 if(t<10) 00242 uLCD2.printf("%i:0%i",m,t); 00243 else 00244 uLCD2.printf("%i:%i",m,t); 00245 oldt=t; 00246 } 00247 if(fade!=old_fade) 00248 { 00249 uLCD2.locate(0,4); 00250 if(fade==0) 00251 uLCD2.printf("Fade: %s ",print_fade); 00252 else 00253 uLCD2.printf("Fade: %s %i % ",print_fade,fade_percent); 00254 00255 old_fade=fade; 00256 } 00257 if(stop) 00258 { 00259 play_new=true; 00260 break; 00261 } 00262 if((pause!=old_pause)||(mute!=old_mute)){ 00263 old_pause=pause; 00264 old_mute=mute; 00265 update_LCD(print_file); 00266 uLCD2.locate(0,8); 00267 if(t<10) 00268 uLCD2.printf("%i:0%i",m,t); 00269 else 00270 uLCD2.printf("%i:%i",m,t); 00271 } 00272 if(new_song_number!=song_number) 00273 { 00274 play_new=true; 00275 break; 00276 } 00277 } 00278 fclose(song2play); //close the file 00279 00280 if(!play_new) 00281 { 00282 new_song_number+=1; // Goto Next song on completion of one song 00283 if(new_song_number==size) 00284 new_song_number=0; 00285 play_new=false; 00286 } 00287 00288 }
Generated on Tue Jul 19 2022 14:10:21 by
1.7.2