Natthaphon Sudadech / Mbed 2 deprecated PJ04_MASTER

Dependencies:   mbed

Fork of VS1053 by SGMP Coperations

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers vs10xx.cpp Source File

vs10xx.cpp

00001 /** \file vs10xx.c
00002  * Functions for interfacing with the mp3 player chip.
00003  * \todo safe rewind
00004  * \todo VS1003 WMA "wma-bytes-left" variable adjustment at ff/rew
00005  */
00006 #include "vs10xx.h"
00007 
00008 /** Constructor of class VS1053. */
00009 vs10xx::vs10xx(PinName MOSI, PinName MISO, PinName SCLK, PinName XCS, 
00010                PinName XDCS,PinName DREQ, PinName XRESET)
00011                 :  
00012                 _spi(MOSI,MISO,SCLK),
00013                 _XCS(XCS),
00014                 _XDCS(XDCS),
00015                 _DREQ(DREQ),
00016                 _XRESET(XRESET)    
00017 {
00018     _XCS = 1;
00019     _XDCS = 1;
00020     _XRESET = 1;
00021 }
00022 
00023 /** Write the 16-bit value to VS10xx register*/
00024 void vs10xx::writeRegister(unsigned char addressbyte,unsigned int value)
00025 {
00026     _XCS = 1;
00027     while (!_DREQ);
00028     _XCS = 0;
00029     _spi.write(VS_WRITE_COMMAND);
00030     _spi.write(addressbyte);
00031     _spi.write(value >> 8);
00032     _spi.write(value & 0xFF);
00033     _XCS = 1;
00034 }
00035 
00036 /** Read the 16-bit value of a VS10xx register */
00037 unsigned int vs10xx::readRegister (unsigned char addressbyte)
00038 {
00039     unsigned int resultvalue = 0;
00040     
00041     _XCS = 1;
00042     while (!_DREQ);
00043     _XCS = 0;
00044     _spi.write(VS_READ_COMMAND);
00045     _spi.write((addressbyte));
00046     resultvalue = _spi.write(0XFF) << 8;
00047     resultvalue |= _spi.write(0XFF); 
00048     _XCS = 1;
00049     return resultvalue;
00050 }
00051 
00052 /** write data to VS10xx  */
00053 void vs10xx::writeData(unsigned char *databuf,unsigned char n)
00054 {
00055     _XDCS = 1;
00056     _XDCS = 0;
00057     while (!_DREQ);
00058     while (n--)
00059     {
00060         _spi.write(*databuf++);
00061     }
00062     _XDCS = 1;
00063 }
00064 
00065 void vs10xx::setFreq(int freq)
00066 {
00067     _spi.frequency(freq);        //set freq for speed
00068 }
00069 
00070 void vs10xx::setVolume(unsigned char vol)
00071 {
00072     writeRegister(SPI_VOL, vol*0x101);     //Set volume level
00073 }
00074 
00075 /** Soft Reset of VS10xx (Between songs) */
00076 void vs10xx::softReset()
00077 {
00078     _spi.frequency(1000000);          //low speed
00079     
00080     /* Soft Reset of VS10xx */
00081     writeRegister(SPI_MODE, 0x0804); /* Newmode, Reset, No L1-2 */
00082     
00083     wait_ms(2);         //delay
00084     while(!_DREQ);
00085     
00086     /* A quick sanity check: write to two registers, then test if we
00087      get the same results. Note that if you use a too high SPI
00088      speed, the MSB is the most likely to fail when read again. */
00089     writeRegister(SPI_HDAT0, 0xABAD);
00090     writeRegister(SPI_HDAT1, 0x1DEA);
00091     if (readRegister(SPI_HDAT0) != 0xABAD || readRegister(SPI_HDAT1) != 0x1DEA) {
00092         printf("There is something wrong with VS10xx\n");
00093     }
00094     
00095     writeRegister(SPI_CLOCKF,0XC000);   //Set the clock
00096     writeRegister(SPI_AUDATA,0xbb81);   //samplerate 48k,stereo
00097     writeRegister(SPI_BASS, 0x0055);    //set accent
00098     writeRegister(SPI_VOL, 0x4040);     //Set volume level
00099         
00100     while (!_DREQ);
00101     
00102 }
00103 
00104 /** Reset VS10xx */
00105 void vs10xx::reset(){
00106 
00107     _XRESET = 0;
00108     wait_ms(2);  //it is a must
00109     
00110     /* Send dummy SPI byte to initialize SPI */
00111     _spi.write(0xFF);
00112 
00113     /* Un-reset VS10XX chip */
00114     _XCS = 1;
00115     _XDCS = 1;
00116     _XRESET = 1;
00117 
00118     softReset(); //vs10xx soft reset.
00119 
00120    // printf("\r\nVS10xx Init\r\n");
00121 }
00122 
00123 /*     Loads a plugin.       */
00124 void vs10xx::loadPlugin(const unsigned short *plugin,int length) {
00125     int i = 0;
00126     while (i<length) {
00127         unsigned short addr, n, val;
00128         addr = plugin[i++];
00129         n = plugin[i++];
00130         if (n & 0x8000U) { /* RLE run, replicate n samples */
00131             n &= 0x7FFF;
00132             val = plugin[i++];
00133             while (n--) {
00134                 writeRegister(addr, val);
00135             }
00136         } else {           /* Copy run, copy n samples */
00137             while (n--) {
00138                 val = plugin[i++];
00139                 writeRegister(addr, val);
00140             }
00141         }
00142     }
00143 }
00144 
00145 
00146 
00147 
00148