Tuesday, September 21, 2010

Arduino and SparkFun MP3 shield test code

For the speakers project I'm currently working in, I got an mp3 shield for the arduino from sparkfun. I'm currently exploring the VS1053D decoder chip. To that goal, I wrote the attached testing code that will put the chip to test mode and output a sine wave sound with adjustable pitch. I noticed that there's not much info available on the internet to make this shield work, so I'll make make code available as I go (check also the google code pages for this project.

#include SPI.h ---> put brackets here and delete this comment
int CS_pin = 9;
int DREQ_pin = 3;
byte received;
void setup() {
  pinMode(CS_pin, OUTPUT);
  pinMode(DREQ_pin, INPUT);
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  //CPOL = 0, CPHA = 1 
  //see http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_Numbers
  //and decoder chip datasheet
  SPI.setDataMode(SPI_MODE1);
  //max SDI clock freq = CLKI/7 and (datasheet) CLKI = 36.864, hence max clock = 5MHz
  //SPI clock arduino = 16MHz. 16/ 4 = 4MHz -- ok!
  SPI.setClockDivider(SPI_CLOCK_DIV4);
  initialize();
}


void loop(){
  digitalWrite(CS_pin, HIGH);
  chip_write(0x00, 0x0c20); // sets sci_mode register, SM_SDINEW, SM_SDISHARE
                          // SM_TESTS.  pg 25, 26
  chip_sineTest(0xAA);   // test tone frequency (pg 35)
  
  for (;;){}
}
void chip_write (unsigned int address, word data){
    byte aux;
    digitalWrite(CS_pin, LOW);
    SPI.transfer(0x02);  //write command
    SPI.transfer(address); //SDI_MODE register
    //extract and send higher byte of data
    aux = data >> 8;
    SPI.transfer(aux);
    //extract and send lower byte of data
    aux = data & 0b11111111;
    SPI.transfer(aux);
    //wait for the chip to finish executing command
    while (!DREQ_pin){};
    digitalWrite(CS_pin, HIGH);
}
void chip_sineTest(int pitch){
   digitalWrite(CS_pin, HIGH);
   SPI.transfer(0x53);
   SPI.transfer(0xEF);
   SPI.transfer(0x6E);
   SPI.transfer(pitch);
   SPI.transfer(0);
   SPI.transfer(0);
   SPI.transfer(0);
   SPI.transfer(0);    
}
void initialize(){
   
}

No comments: