Smart pole

Dependencies:   FTPClient SDFileSystem WIZnetInterface mbed

Fork of FTP_Streaming_Music_Player_WIZwiki-W7500 by justin kim

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