VS1002 MP3 Decoder
VS1002¶
This is an MP3 decoder breakout board availiable from http://www.sparkfun.com/commerce/product_info.php?products_id=519 .
Mission Statement¶
The goal is to be able to stream music from an SD Card via the MBED and send it to the VS1002 MP3 Decoder hopefully ending up with some nice music to listen to.
MP3 Decoder Basic Interface¶
The VS1002 uses SPI Serial Peripheral Interface which is supported in the mbed library. This can be further split up into SCI Serial Control Interface and SDI Serial Data Interface, SCI is used for controlling the decoder hardware and sending out commands and SDI is for sending the actual data to be decoded or placed in registers. SPI can be described as a 4-pin interface; MOSI - Master Output, Slave Input, MISO - Master Input, Slave Input, SCK - Serial Clock, CS - Chip Select. The VS1002 has the interesting function that it has 2 Chip Selects for SCI and SDI, however it is possible to combine these onto one pin, which is what I have done in my code. CS low enables SCI and CS high enables SDI, however, in order to initate a data transfer there must be transition of the state of the CS pin, whereupon the first bit to be read by the VS1002 will be at the first rising edge on the SCK line.
Pinout¶
TRS Jack
MBED VS1002 Breakout MBED TRS Earphone Jack NC MICP VIN-----------o (40) VOUT /\ NC MICN RIGHT-----------------------------------------\ |__|-\ i.e.(15) o----------RST GBUF----------------------------------------\ \--|__| \ i.e.(16) o----------DREQ LEFT--------------------------------------\ \___| | | NC GPIO 2 GPIO 1 NC \ | | | NC GPIO 3 GPIO 0 NC \__________| i.e.(17) o----------BSYNC SO-----------o (12) miso \ NC TX SI-----------o (11) mosi }SPI Bus NC RX SCLK-----------o (13) sck / GND (1) o----------GND CS-----------o i.e.(14)
Beginners' Code¶
The first task is to get a the VS1002 to talk to the MBED and run a preliminary sine wave test. On top I have included a volume control function, which works by taking the attenuation for both left and right channels in -dB i.e. volume(-20, -40) means left channel attenuation = -20dB and right channel attenuation = -40dB. Here is the example code.
/projects/cookbook/svn/VS1002MP3Player/cookbook_examples/Sinewavetest.cpp
Now for a .wav file¶
Sine waves are nice because they tell you that the VS1002 is actually working but, what is really nice is to be able to play something with a bit more variation. The next program includes being able to stream data from a file on the SDCard into an array on the MBED and then to send it to the VS1002. This naturally has to be done as fast as possible to optimise the quality of the sound heard. Despite this, there is a limit at which the speed of the data transmission does not matter because none of the information is being lost. The major problem which audio systems suffer is that they are tempermental and sometimes very subjective. Certain frequencies of data transmission, despite being fast enough, can lead to horrid sound quality whereas others are just better. My SDCard Micro is a Transcend and running at 2GHz! and the VS1002 at 7MHz. The SD Card may seem unnecessarily fast but it is really to free up extra time for the MBED to fulfill other operations in the meantime. Note that the last pin in the constructor is the volume control - an AnalogIn.
#include "mbed.h" #include "VS1002.h" VS1002 mp3(5, 6, 7, 8, "sd", 11, 12, 13, 18, 15, 16, 17, 20); //5, 6, 7, 8, "sd", - SDCard connections //11, 12, 13, 18, 15, 16, 17, - VS1002 connections int main () //20 - volume pin { /*============================================================ * MP3 Initialising *==========================================================*/ mp3._RST = 1; mp3.cs_high(); //chip disabled mp3.sci_initialise(); //initialise MBED mp3.sci_write(0x00,(SM_SDINEW+SM_STREAM+SM_DIFF)); //enable data streaming mp3.sci_write(0x03, 0x9800); mp3.sdi_initialise(); //chip enabled /*============================================================ * This is the good part *==========================================================*/ mp3.play_song("redreded.mp3"); return EXIT_SUCCESS; }
This is great but I'd rather just say that I wish to play the songs all in a row or randomly, so I assign each track a number automatically depending on how they are ordered on the SDCard and when I wish to play track I simply say...
mp3.play_song(TRACK NUMBER HERE); }}} i.e. {{{ #!cpp mp3.play_song(6);
This means that if I put the function in an IF loop I can increment the track number each cycle and run through several tracks, the application possibilities go on and on...