FTP Streaming Music Player with WIZwiki-W7500

Dependencies:   FTPClient SDFileSystem TextLCD WIZnetInterface mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers VS1002.cpp Source File

VS1002.cpp

00001 #include "VS1002.h"
00002 #include "mbed.h"
00003 
00004 Serial pc(USBTX, USBRX);
00005 TextLCD lcd(D8, D9, D0, D1, D2, D15);
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)
00012     : _DREQ(dreq), _RST(rst), _spi(mosi, miso, sck), _CS(cs), _DCS(dcs), _sd(mmosi, mmiso, ssck, ccs, name) {
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     char str[16];        //folder where the songs are located
00182     sprintf(str,"/sdc/%d",song_number);   //appending song number to path of the file
00183     strcat(str,".mp3");                 //appending .mp3 to file name
00184     FILE *song;
00185     unsigned char array[512];           //array for reading data from file
00186     bool play_new=false;                // Variable to see if new_song has be assigned or not
00187     song = fopen(str, "r");    // Open the music file in read mode
00188     /* Printing to LCD the present status */
00189      lcd.cls();
00190   if(pause) 
00191     lcd.printf("     Paused ");
00192   if(mute) 
00193    lcd.printf("Muted");
00194   if(!mute && !pause)
00195    lcd.printf("     Playing..."); 
00196 
00197   lcd.printf("\n %d %s",new_song_number,song_name[new_song_number-1]);
00198    
00199     if(!song) 
00200     {
00201         lcd.printf("\n \n Error!!");
00202         new_song_number+=1;               // Goto Next song on completion of one song
00203         if(new_song_number==10)
00204             new_song_number=1;
00205     }
00206       while(!feof(song))
00207     {
00208             if(!pause)
00209            {
00210           
00211            fread(&array, 1, 512, song);           
00212            for(int i=0; i<512; i++)
00213            {
00214                 sdi_write(array[i]);
00215            }
00216            volume(volume_set,volume_set);
00217            }
00218           if(new_song_number!=song_number)
00219            {
00220             play_new=true;
00221              break;
00222             }
00223              
00224              
00225           }
00226     
00227            fclose(song);                              //close the file
00228 
00229       if(!play_new)
00230       {
00231      new_song_number+=1;               // Goto Next song on completion of one song
00232      if(new_song_number==10)
00233       new_song_number=1; 
00234      play_new=false;                   
00235      }
00236 }
00237