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@2:cea9f6564641, 2017-03-30 (annotated)
- Committer:
- javakys
- Date:
- Thu Mar 30 10:29:51 2017 +0000
- Revision:
- 2:cea9f6564641
- Parent:
- 1:fc4e0c572992
Function Description Update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
javakys | 1:fc4e0c572992 | 1 | #include "VS1002.h" |
javakys | 1:fc4e0c572992 | 2 | #include "mbed.h" |
javakys | 1:fc4e0c572992 | 3 | |
javakys | 1:fc4e0c572992 | 4 | Serial pc(USBTX, USBRX); |
javakys | 1:fc4e0c572992 | 5 | |
javakys | 1:fc4e0c572992 | 6 | /* ================================================================== |
javakys | 1:fc4e0c572992 | 7 | * Constructor |
javakys | 1:fc4e0c572992 | 8 | * =================================================================*/ |
javakys | 1:fc4e0c572992 | 9 | |
javakys | 1:fc4e0c572992 | 10 | 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) |
javakys | 1:fc4e0c572992 | 11 | : _DREQ(dreq), _RST(rst), _spi(mosi, miso, sck), _CS(cs), _DCS(dcs), _sd(mmosi, mmiso, ssck, ccs, name) { |
javakys | 1:fc4e0c572992 | 12 | |
javakys | 1:fc4e0c572992 | 13 | } |
javakys | 1:fc4e0c572992 | 14 | |
javakys | 1:fc4e0c572992 | 15 | /*=================================================================== |
javakys | 1:fc4e0c572992 | 16 | * Functions |
javakys | 1:fc4e0c572992 | 17 | *==================================================================*/ |
javakys | 1:fc4e0c572992 | 18 | |
javakys | 1:fc4e0c572992 | 19 | void VS1002::cs_low(void) |
javakys | 1:fc4e0c572992 | 20 | { |
javakys | 1:fc4e0c572992 | 21 | _CS = 0; |
javakys | 1:fc4e0c572992 | 22 | } |
javakys | 1:fc4e0c572992 | 23 | void VS1002::cs_high(void) |
javakys | 1:fc4e0c572992 | 24 | { |
javakys | 1:fc4e0c572992 | 25 | _CS = 1; |
javakys | 1:fc4e0c572992 | 26 | } |
javakys | 1:fc4e0c572992 | 27 | void VS1002::dcs_low(void) |
javakys | 1:fc4e0c572992 | 28 | { |
javakys | 1:fc4e0c572992 | 29 | _DCS = 0; |
javakys | 1:fc4e0c572992 | 30 | } |
javakys | 1:fc4e0c572992 | 31 | void VS1002::dcs_high(void) |
javakys | 1:fc4e0c572992 | 32 | { |
javakys | 1:fc4e0c572992 | 33 | _DCS = 1; |
javakys | 1:fc4e0c572992 | 34 | } |
javakys | 1:fc4e0c572992 | 35 | void VS1002::sci_en(void) //SCI enable |
javakys | 1:fc4e0c572992 | 36 | { |
javakys | 1:fc4e0c572992 | 37 | cs_high(); |
javakys | 1:fc4e0c572992 | 38 | dcs_high(); |
javakys | 1:fc4e0c572992 | 39 | cs_low(); |
javakys | 1:fc4e0c572992 | 40 | } |
javakys | 1:fc4e0c572992 | 41 | void VS1002::sci_dis(void) //SCI disable |
javakys | 1:fc4e0c572992 | 42 | { |
javakys | 1:fc4e0c572992 | 43 | cs_high(); |
javakys | 1:fc4e0c572992 | 44 | } |
javakys | 1:fc4e0c572992 | 45 | void VS1002::sdi_en(void) //SDI enable |
javakys | 1:fc4e0c572992 | 46 | { |
javakys | 1:fc4e0c572992 | 47 | dcs_high(); |
javakys | 1:fc4e0c572992 | 48 | cs_high(); |
javakys | 1:fc4e0c572992 | 49 | dcs_low(); |
javakys | 1:fc4e0c572992 | 50 | } |
javakys | 1:fc4e0c572992 | 51 | void VS1002::sdi_dis(void) //SDI disable |
javakys | 1:fc4e0c572992 | 52 | { |
javakys | 1:fc4e0c572992 | 53 | dcs_high(); |
javakys | 1:fc4e0c572992 | 54 | } |
javakys | 1:fc4e0c572992 | 55 | void VS1002::reset(void) //hardware reset |
javakys | 1:fc4e0c572992 | 56 | { |
javakys | 1:fc4e0c572992 | 57 | wait(0.01); |
javakys | 1:fc4e0c572992 | 58 | _RST = 0; |
javakys | 1:fc4e0c572992 | 59 | wait(0.01); |
javakys | 1:fc4e0c572992 | 60 | _RST = 1; |
javakys | 1:fc4e0c572992 | 61 | wait(0.10); |
javakys | 1:fc4e0c572992 | 62 | } |
javakys | 1:fc4e0c572992 | 63 | void VS1002::power_down(void) //hardware and software reset |
javakys | 1:fc4e0c572992 | 64 | { |
javakys | 1:fc4e0c572992 | 65 | cs_low(); |
javakys | 1:fc4e0c572992 | 66 | reset(); |
javakys | 1:fc4e0c572992 | 67 | sci_write(0x00, SM_PDOWN); |
javakys | 1:fc4e0c572992 | 68 | wait(0.01); |
javakys | 1:fc4e0c572992 | 69 | reset(); |
javakys | 1:fc4e0c572992 | 70 | } |
javakys | 1:fc4e0c572992 | 71 | void VS1002::sci_initialise(void) |
javakys | 1:fc4e0c572992 | 72 | { |
javakys | 1:fc4e0c572992 | 73 | _RST = 1; //no reset |
javakys | 1:fc4e0c572992 | 74 | _spi.format(8,0); //spi 8bit interface, steady state low |
javakys | 1:fc4e0c572992 | 75 | _spi.frequency(2000000); //rising edge data record, freq. 1Mhz |
javakys | 1:fc4e0c572992 | 76 | |
javakys | 1:fc4e0c572992 | 77 | cs_low(); |
javakys | 1:fc4e0c572992 | 78 | for(int i=0; i<4; i++) |
javakys | 1:fc4e0c572992 | 79 | { |
javakys | 1:fc4e0c572992 | 80 | _spi.write(0xFF); //clock the chip a bit |
javakys | 1:fc4e0c572992 | 81 | } |
javakys | 1:fc4e0c572992 | 82 | cs_high(); |
javakys | 1:fc4e0c572992 | 83 | dcs_high(); |
javakys | 1:fc4e0c572992 | 84 | wait_us(5); |
javakys | 1:fc4e0c572992 | 85 | } |
javakys | 1:fc4e0c572992 | 86 | void VS1002::sdi_initialise(void) |
javakys | 1:fc4e0c572992 | 87 | { |
javakys | 1:fc4e0c572992 | 88 | _spi.format(8,0); |
javakys | 1:fc4e0c572992 | 89 | _spi.frequency(7000000); //set to 7MHz |
javakys | 1:fc4e0c572992 | 90 | |
javakys | 1:fc4e0c572992 | 91 | cs_high(); |
javakys | 1:fc4e0c572992 | 92 | dcs_high(); |
javakys | 1:fc4e0c572992 | 93 | } |
javakys | 1:fc4e0c572992 | 94 | void VS1002::sci_write(unsigned char address, unsigned short int data) |
javakys | 1:fc4e0c572992 | 95 | { |
javakys | 1:fc4e0c572992 | 96 | sci_en(); //enables SCI/disables SDI |
javakys | 1:fc4e0c572992 | 97 | |
javakys | 1:fc4e0c572992 | 98 | while(!_DREQ); //wait unitl data request is high |
javakys | 1:fc4e0c572992 | 99 | _spi.write(0x02); //SCI write |
javakys | 1:fc4e0c572992 | 100 | _spi.write(address); //register address |
javakys | 1:fc4e0c572992 | 101 | _spi.write((data >> 8) & 0xFF); //write out first half of data word |
javakys | 1:fc4e0c572992 | 102 | _spi.write(data & 0xFF); //write out second half of data word |
javakys | 1:fc4e0c572992 | 103 | |
javakys | 1:fc4e0c572992 | 104 | sci_dis(); //enables SDI/disables SCI |
javakys | 1:fc4e0c572992 | 105 | wait_us(5); |
javakys | 1:fc4e0c572992 | 106 | } |
javakys | 1:fc4e0c572992 | 107 | void VS1002::sdi_write(unsigned char datum) |
javakys | 1:fc4e0c572992 | 108 | { |
javakys | 1:fc4e0c572992 | 109 | sdi_en(); |
javakys | 1:fc4e0c572992 | 110 | |
javakys | 1:fc4e0c572992 | 111 | while(!_DREQ); |
javakys | 1:fc4e0c572992 | 112 | _spi.write(datum); |
javakys | 1:fc4e0c572992 | 113 | |
javakys | 1:fc4e0c572992 | 114 | sci_dis(); |
javakys | 1:fc4e0c572992 | 115 | } |
javakys | 1:fc4e0c572992 | 116 | unsigned short int VS1002::read(unsigned short int address) |
javakys | 1:fc4e0c572992 | 117 | { |
javakys | 1:fc4e0c572992 | 118 | cs_low(); //enables SCI/disables SDI |
javakys | 1:fc4e0c572992 | 119 | |
javakys | 1:fc4e0c572992 | 120 | while(!_DREQ); //wait unitl data request is high |
javakys | 1:fc4e0c572992 | 121 | _spi.write(0x03); //SCI write |
javakys | 1:fc4e0c572992 | 122 | _spi.write(address); //register address |
javakys | 1:fc4e0c572992 | 123 | unsigned short int received = _spi.write(0x00); //write out dummy byte |
javakys | 1:fc4e0c572992 | 124 | received <<= 8; |
javakys | 1:fc4e0c572992 | 125 | received += _spi.write(0x00); //write out dummy byte |
javakys | 1:fc4e0c572992 | 126 | |
javakys | 1:fc4e0c572992 | 127 | cs_high(); //enables SDI/disables SCI |
javakys | 1:fc4e0c572992 | 128 | |
javakys | 1:fc4e0c572992 | 129 | return received; //return received word |
javakys | 1:fc4e0c572992 | 130 | } |
javakys | 1:fc4e0c572992 | 131 | void VS1002::sine_test_activate(unsigned char wave) |
javakys | 1:fc4e0c572992 | 132 | { |
javakys | 1:fc4e0c572992 | 133 | cs_high(); //enables SDI/disables SCI |
javakys | 1:fc4e0c572992 | 134 | |
javakys | 1:fc4e0c572992 | 135 | while(!_DREQ); //wait unitl data request is high |
javakys | 1:fc4e0c572992 | 136 | _spi.write(0x53); //SDI write |
javakys | 1:fc4e0c572992 | 137 | _spi.write(0xEF); //SDI write |
javakys | 1:fc4e0c572992 | 138 | _spi.write(0x6E); //SDI write |
javakys | 1:fc4e0c572992 | 139 | _spi.write(wave); //SDI write |
javakys | 1:fc4e0c572992 | 140 | _spi.write(0x00); //filler byte |
javakys | 1:fc4e0c572992 | 141 | _spi.write(0x00); //filler byte |
javakys | 1:fc4e0c572992 | 142 | _spi.write(0x00); //filler byte |
javakys | 1:fc4e0c572992 | 143 | _spi.write(0x00); //filler byte |
javakys | 1:fc4e0c572992 | 144 | |
javakys | 1:fc4e0c572992 | 145 | cs_low(); //enables SCI/disables SDI |
javakys | 1:fc4e0c572992 | 146 | } |
javakys | 1:fc4e0c572992 | 147 | void VS1002::sine_test_deactivate(void) |
javakys | 1:fc4e0c572992 | 148 | { |
javakys | 1:fc4e0c572992 | 149 | cs_high(); |
javakys | 1:fc4e0c572992 | 150 | |
javakys | 1:fc4e0c572992 | 151 | while(!_DREQ); |
javakys | 1:fc4e0c572992 | 152 | _spi.write(0x45); //SDI write |
javakys | 1:fc4e0c572992 | 153 | _spi.write(0x78); //SDI write |
javakys | 1:fc4e0c572992 | 154 | _spi.write(0x69); //SDI write |
javakys | 1:fc4e0c572992 | 155 | _spi.write(0x74); //SDI write |
javakys | 1:fc4e0c572992 | 156 | _spi.write(0x00); //filler byte |
javakys | 1:fc4e0c572992 | 157 | _spi.write(0x00); //filler byte |
javakys | 1:fc4e0c572992 | 158 | _spi.write(0x00); //filler byte |
javakys | 1:fc4e0c572992 | 159 | _spi.write(0x00); //filler byte |
javakys | 1:fc4e0c572992 | 160 | } |
javakys | 1:fc4e0c572992 | 161 | |
javakys | 1:fc4e0c572992 | 162 | |
javakys | 1:fc4e0c572992 | 163 | void VS1002::volume(signed int left, signed int right) |
javakys | 1:fc4e0c572992 | 164 | { |
javakys | 1:fc4e0c572992 | 165 | while(_DREQ == 0); |
javakys | 1:fc4e0c572992 | 166 | |
javakys | 1:fc4e0c572992 | 167 | unsigned short int _left = -left; //convert the decibel values into a format |
javakys | 1:fc4e0c572992 | 168 | unsigned short int _right = -right; //readable by the chip cf. datasheet p.32 subsection 8.6.11 |
javakys | 1:fc4e0c572992 | 169 | _left *= 2; |
javakys | 1:fc4e0c572992 | 170 | _right *= 2; |
javakys | 1:fc4e0c572992 | 171 | unsigned short int attenuation = ((256 * _left) + _right); |
javakys | 1:fc4e0c572992 | 172 | cs_low(); |
javakys | 1:fc4e0c572992 | 173 | sci_write(0x0B, attenuation); //writeout these values |
javakys | 1:fc4e0c572992 | 174 | cs_high(); |
javakys | 1:fc4e0c572992 | 175 | } |
javakys | 1:fc4e0c572992 | 176 | |
javakys | 1:fc4e0c572992 | 177 | void VS1002::play_song(int song_number) |
javakys | 1:fc4e0c572992 | 178 | { |
javakys | 1:fc4e0c572992 | 179 | /*====== Song Select ======*/ |
javakys | 1:fc4e0c572992 | 180 | char str[16]; //folder where the songs are located |
javakys | 1:fc4e0c572992 | 181 | sprintf(str,"/sdc/%d",song_number); //appending song number to path of the file |
javakys | 1:fc4e0c572992 | 182 | strcat(str,".mp3"); //appending .mp3 to file name |
javakys | 1:fc4e0c572992 | 183 | FILE *song; |
javakys | 1:fc4e0c572992 | 184 | unsigned char array[512]; //array for reading data from file |
javakys | 1:fc4e0c572992 | 185 | bool play_new=false; // Variable to see if new_song has be assigned or not |
javakys | 1:fc4e0c572992 | 186 | song = fopen(str, "r"); // Open the music file in read mode |
javakys | 1:fc4e0c572992 | 187 | |
javakys | 1:fc4e0c572992 | 188 | if(!song) |
javakys | 1:fc4e0c572992 | 189 | { |
javakys | 1:fc4e0c572992 | 190 | new_song_number+=1; // Goto Next song on completion of one song |
javakys | 1:fc4e0c572992 | 191 | if(new_song_number==10) |
javakys | 1:fc4e0c572992 | 192 | new_song_number=1; |
javakys | 1:fc4e0c572992 | 193 | |
javakys | 1:fc4e0c572992 | 194 | fclose(song); //close the file |
javakys | 1:fc4e0c572992 | 195 | |
javakys | 1:fc4e0c572992 | 196 | return; |
javakys | 1:fc4e0c572992 | 197 | } |
javakys | 1:fc4e0c572992 | 198 | |
javakys | 1:fc4e0c572992 | 199 | while(!feof(song)) |
javakys | 1:fc4e0c572992 | 200 | { |
javakys | 1:fc4e0c572992 | 201 | if(!pause) |
javakys | 1:fc4e0c572992 | 202 | { |
javakys | 1:fc4e0c572992 | 203 | |
javakys | 1:fc4e0c572992 | 204 | fread(&array, 1, 512, song); |
javakys | 1:fc4e0c572992 | 205 | for(int i=0; i<512; i++) |
javakys | 1:fc4e0c572992 | 206 | { |
javakys | 1:fc4e0c572992 | 207 | sdi_write(array[i]); |
javakys | 1:fc4e0c572992 | 208 | } |
javakys | 1:fc4e0c572992 | 209 | volume(volume_set,volume_set); |
javakys | 1:fc4e0c572992 | 210 | } |
javakys | 1:fc4e0c572992 | 211 | |
javakys | 1:fc4e0c572992 | 212 | if(new_song_number!=song_number) |
javakys | 1:fc4e0c572992 | 213 | { |
javakys | 1:fc4e0c572992 | 214 | play_new=true; |
javakys | 1:fc4e0c572992 | 215 | break; |
javakys | 1:fc4e0c572992 | 216 | } |
javakys | 1:fc4e0c572992 | 217 | |
javakys | 1:fc4e0c572992 | 218 | |
javakys | 1:fc4e0c572992 | 219 | } |
javakys | 1:fc4e0c572992 | 220 | |
javakys | 1:fc4e0c572992 | 221 | fclose(song); //close the file |
javakys | 1:fc4e0c572992 | 222 | |
javakys | 1:fc4e0c572992 | 223 | if(!play_new) |
javakys | 1:fc4e0c572992 | 224 | { |
javakys | 1:fc4e0c572992 | 225 | new_song_number+=1; // Goto Next song on completion of one song |
javakys | 1:fc4e0c572992 | 226 | if(new_song_number==10) |
javakys | 1:fc4e0c572992 | 227 | new_song_number=1; |
javakys | 1:fc4e0c572992 | 228 | play_new=false; |
javakys | 1:fc4e0c572992 | 229 | } |
javakys | 1:fc4e0c572992 | 230 | } |
javakys | 1:fc4e0c572992 | 231 |