Playing MP3 with Music Shield and WIZwiki-W7500
Dependencies: FTPClient javakysSDFileSystem2 WIZnetInterface mbed
Prerequisite
This example is for playing MP3 file stored in SD card on WIZwiki-W7500 and updating MP3 files from server to SD card via FTP protocol.
To implement this function, you need a Platform board, network Interface board, MP3 decoder board and SD card. Below are what we used.
- WIZwiki-W7500 from WIZnet (Platform board and Ethernet I/F board)
- Music shield from Seeed Studio
- SD card
Hardware Configuration
WIZwiki-W7500 Pin map
SPI1 for SD Card
SPI1 on WIZwiki-W7100 is for reading from or writing to SD card and pins for SPI1 are PB_0, PB_1, PB_2 and PB_3.
SPI0 and other control pins for MP3 decoder
WIZwiki-W7500 communicates to MP3 decoder on Music Shield via SPI0 pins of which consists of PC_12, D11, D12 and D13.
And PC_13, PC_14 and PC-15 are used for DCS, DREQ and RST, respectively.
InterruptIn pins for 5-Way Navigation Switch
D3, D4, D5, D6 and D7 are connected to 5 way navigation switch on Music Shield.
When user pushes the switch to one way, a relevant pin is grounded so that he or she should make it set high at the beginning.
Software
SPI Initialization
//Declaration in VS1002.cpp 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) : _DREQ(dreq), _RST(rst), _spi(mosi, miso, sck), _CS(cs), _DCS(dcs), _sd(mmosi, mmiso, ssck, ccs, name) { } //Initialization in main.cpp VS1002 mp3(PB_3, PB_2, PB_1, PB_0,"sdc",D11, D12 ,D13, PC_12, PC_15, PC_14, PC_13);
Mapping 5-Way Navigation Switch into InterruptIn pins
main.cpp
InterruptIn K_VU(D3); // Volume UP Key InterruptIn K_VD(D7); // Volume Down Key InterruptIn K_FW(D4); // Foward Key InterruptIn K_BW(D6); // Backward Key InterruptIn K_ONOFF(D5); //Play/Resume or Pause Key
Additional codes due to mbed library bug of WIZwiki-W7500
main.cpp
//Operating Clock Frequency Set *(volatile uint32_t *)(0x41001014) = 0x0060100; //Set all InterruptIn pins to Internal PullUp *(volatile uint32_t *)(0x41003000) = 0x00000002; //D6 *(volatile uint32_t *)(0x41003004) = 0x00000002; //D5 *(volatile uint32_t *)(0x41003008) = 0x00000002; //D4 *(volatile uint32_t *)(0x41003080) = 0x00000002; //D3 *(volatile uint32_t *)(0x41003098) = 0x00000002; //D7
Caution
This example can play only MP3 files with up to 192KHz sample rate//
VS1002.cpp
- Committer:
- javakys
- Date:
- 2017-03-30
- Revision:
- 2:cea9f6564641
- Parent:
- 1:fc4e0c572992
File content as of revision 2:cea9f6564641:
#include "VS1002.h" #include "mbed.h" Serial pc(USBTX, USBRX); /* ================================================================== * Constructor * =================================================================*/ 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) : _DREQ(dreq), _RST(rst), _spi(mosi, miso, sck), _CS(cs), _DCS(dcs), _sd(mmosi, mmiso, ssck, ccs, name) { } /*=================================================================== * Functions *==================================================================*/ void VS1002::cs_low(void) { _CS = 0; } void VS1002::cs_high(void) { _CS = 1; } void VS1002::dcs_low(void) { _DCS = 0; } void VS1002::dcs_high(void) { _DCS = 1; } void VS1002::sci_en(void) //SCI enable { cs_high(); dcs_high(); cs_low(); } void VS1002::sci_dis(void) //SCI disable { cs_high(); } void VS1002::sdi_en(void) //SDI enable { dcs_high(); cs_high(); dcs_low(); } void VS1002::sdi_dis(void) //SDI disable { dcs_high(); } void VS1002::reset(void) //hardware reset { wait(0.01); _RST = 0; wait(0.01); _RST = 1; wait(0.10); } void VS1002::power_down(void) //hardware and software reset { cs_low(); reset(); sci_write(0x00, SM_PDOWN); wait(0.01); reset(); } void VS1002::sci_initialise(void) { _RST = 1; //no reset _spi.format(8,0); //spi 8bit interface, steady state low _spi.frequency(2000000); //rising edge data record, freq. 1Mhz cs_low(); for(int i=0; i<4; i++) { _spi.write(0xFF); //clock the chip a bit } cs_high(); dcs_high(); wait_us(5); } void VS1002::sdi_initialise(void) { _spi.format(8,0); _spi.frequency(7000000); //set to 7MHz cs_high(); dcs_high(); } void VS1002::sci_write(unsigned char address, unsigned short int data) { sci_en(); //enables SCI/disables SDI while(!_DREQ); //wait unitl data request is high _spi.write(0x02); //SCI write _spi.write(address); //register address _spi.write((data >> 8) & 0xFF); //write out first half of data word _spi.write(data & 0xFF); //write out second half of data word sci_dis(); //enables SDI/disables SCI wait_us(5); } void VS1002::sdi_write(unsigned char datum) { sdi_en(); while(!_DREQ); _spi.write(datum); sci_dis(); } unsigned short int VS1002::read(unsigned short int address) { cs_low(); //enables SCI/disables SDI while(!_DREQ); //wait unitl data request is high _spi.write(0x03); //SCI write _spi.write(address); //register address unsigned short int received = _spi.write(0x00); //write out dummy byte received <<= 8; received += _spi.write(0x00); //write out dummy byte cs_high(); //enables SDI/disables SCI return received; //return received word } void VS1002::sine_test_activate(unsigned char wave) { cs_high(); //enables SDI/disables SCI while(!_DREQ); //wait unitl data request is high _spi.write(0x53); //SDI write _spi.write(0xEF); //SDI write _spi.write(0x6E); //SDI write _spi.write(wave); //SDI write _spi.write(0x00); //filler byte _spi.write(0x00); //filler byte _spi.write(0x00); //filler byte _spi.write(0x00); //filler byte cs_low(); //enables SCI/disables SDI } void VS1002::sine_test_deactivate(void) { cs_high(); while(!_DREQ); _spi.write(0x45); //SDI write _spi.write(0x78); //SDI write _spi.write(0x69); //SDI write _spi.write(0x74); //SDI write _spi.write(0x00); //filler byte _spi.write(0x00); //filler byte _spi.write(0x00); //filler byte _spi.write(0x00); //filler byte } void VS1002::volume(signed int left, signed int right) { while(_DREQ == 0); unsigned short int _left = -left; //convert the decibel values into a format unsigned short int _right = -right; //readable by the chip cf. datasheet p.32 subsection 8.6.11 _left *= 2; _right *= 2; unsigned short int attenuation = ((256 * _left) + _right); cs_low(); sci_write(0x0B, attenuation); //writeout these values cs_high(); } void VS1002::play_song(int song_number) { /*====== Song Select ======*/ char str[16]; //folder where the songs are located sprintf(str,"/sdc/%d",song_number); //appending song number to path of the file strcat(str,".mp3"); //appending .mp3 to file name FILE *song; unsigned char array[512]; //array for reading data from file bool play_new=false; // Variable to see if new_song has be assigned or not song = fopen(str, "r"); // Open the music file in read mode if(!song) { new_song_number+=1; // Goto Next song on completion of one song if(new_song_number==10) new_song_number=1; fclose(song); //close the file return; } while(!feof(song)) { if(!pause) { fread(&array, 1, 512, song); for(int i=0; i<512; i++) { sdi_write(array[i]); } volume(volume_set,volume_set); } if(new_song_number!=song_number) { play_new=true; break; } } fclose(song); //close the file if(!play_new) { new_song_number+=1; // Goto Next song on completion of one song if(new_song_number==10) new_song_number=1; play_new=false; } }