![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Arduino MP3 Shield MP3Player This program comes from http://mbed.org/cookbook/VS1002-MP3-Decoder (Very small modification done to fit latest libs)
Dependencies: mbed SDFileSystem
VS1002.cpp@0:8eab2c229d83, 2010-10-03 (annotated)
- Committer:
- xshige
- Date:
- Sun Oct 03 16:56:38 2010 +0000
- Revision:
- 0:8eab2c229d83
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xshige | 0:8eab2c229d83 | 1 | #include "VS1002.h" |
xshige | 0:8eab2c229d83 | 2 | #include "mbed.h" |
xshige | 0:8eab2c229d83 | 3 | |
xshige | 0:8eab2c229d83 | 4 | |
xshige | 0:8eab2c229d83 | 5 | Serial pc(USBTX, USBRX); |
xshige | 0:8eab2c229d83 | 6 | |
xshige | 0:8eab2c229d83 | 7 | /* ================================================================== |
xshige | 0:8eab2c229d83 | 8 | * Constructor |
xshige | 0:8eab2c229d83 | 9 | * =================================================================*/ |
xshige | 0:8eab2c229d83 | 10 | VS1002::VS1002( |
xshige | 0:8eab2c229d83 | 11 | PinName mmosi, PinName mmiso, PinName ssck, PinName ccs, const char *name, |
xshige | 0:8eab2c229d83 | 12 | PinName mosi, PinName miso, PinName sck, PinName cs, PinName rst, |
xshige | 0:8eab2c229d83 | 13 | PinName dreq, PinName dcs, PinName vol) |
xshige | 0:8eab2c229d83 | 14 | : |
xshige | 0:8eab2c229d83 | 15 | _sd(mmosi, mmiso, ssck, ccs, name), |
xshige | 0:8eab2c229d83 | 16 | _spi(mosi, miso, sck), |
xshige | 0:8eab2c229d83 | 17 | _CS(cs), |
xshige | 0:8eab2c229d83 | 18 | _RST(rst), |
xshige | 0:8eab2c229d83 | 19 | _DREQ(dreq), |
xshige | 0:8eab2c229d83 | 20 | _DCS(dcs), |
xshige | 0:8eab2c229d83 | 21 | _VOL(vol) { |
xshige | 0:8eab2c229d83 | 22 | |
xshige | 0:8eab2c229d83 | 23 | } |
xshige | 0:8eab2c229d83 | 24 | |
xshige | 0:8eab2c229d83 | 25 | /*=================================================================== |
xshige | 0:8eab2c229d83 | 26 | * Functions |
xshige | 0:8eab2c229d83 | 27 | *==================================================================*/ |
xshige | 0:8eab2c229d83 | 28 | |
xshige | 0:8eab2c229d83 | 29 | void VS1002::cs_low(void) |
xshige | 0:8eab2c229d83 | 30 | { |
xshige | 0:8eab2c229d83 | 31 | _CS = 0; |
xshige | 0:8eab2c229d83 | 32 | } |
xshige | 0:8eab2c229d83 | 33 | void VS1002::cs_high(void) |
xshige | 0:8eab2c229d83 | 34 | { |
xshige | 0:8eab2c229d83 | 35 | _CS = 1; |
xshige | 0:8eab2c229d83 | 36 | } |
xshige | 0:8eab2c229d83 | 37 | void VS1002::dcs_low(void) |
xshige | 0:8eab2c229d83 | 38 | { |
xshige | 0:8eab2c229d83 | 39 | _DCS = 0; |
xshige | 0:8eab2c229d83 | 40 | } |
xshige | 0:8eab2c229d83 | 41 | void VS1002::dcs_high(void) |
xshige | 0:8eab2c229d83 | 42 | { |
xshige | 0:8eab2c229d83 | 43 | _DCS = 1; |
xshige | 0:8eab2c229d83 | 44 | } |
xshige | 0:8eab2c229d83 | 45 | void VS1002::sci_en(void) //SCI enable |
xshige | 0:8eab2c229d83 | 46 | { |
xshige | 0:8eab2c229d83 | 47 | cs_high(); |
xshige | 0:8eab2c229d83 | 48 | dcs_high(); |
xshige | 0:8eab2c229d83 | 49 | cs_low(); |
xshige | 0:8eab2c229d83 | 50 | } |
xshige | 0:8eab2c229d83 | 51 | void VS1002::sci_dis(void) //SCI disable |
xshige | 0:8eab2c229d83 | 52 | { |
xshige | 0:8eab2c229d83 | 53 | cs_high(); |
xshige | 0:8eab2c229d83 | 54 | } |
xshige | 0:8eab2c229d83 | 55 | void VS1002::sdi_en(void) //SDI enable |
xshige | 0:8eab2c229d83 | 56 | { |
xshige | 0:8eab2c229d83 | 57 | dcs_high(); |
xshige | 0:8eab2c229d83 | 58 | cs_high(); |
xshige | 0:8eab2c229d83 | 59 | dcs_low(); |
xshige | 0:8eab2c229d83 | 60 | } |
xshige | 0:8eab2c229d83 | 61 | void VS1002::sdi_dis(void) //SDI disable |
xshige | 0:8eab2c229d83 | 62 | { |
xshige | 0:8eab2c229d83 | 63 | dcs_high(); |
xshige | 0:8eab2c229d83 | 64 | } |
xshige | 0:8eab2c229d83 | 65 | void VS1002::reset(void) //hardware reset |
xshige | 0:8eab2c229d83 | 66 | { |
xshige | 0:8eab2c229d83 | 67 | wait(0.01); |
xshige | 0:8eab2c229d83 | 68 | _RST = 0; |
xshige | 0:8eab2c229d83 | 69 | wait(0.01); |
xshige | 0:8eab2c229d83 | 70 | _RST = 1; |
xshige | 0:8eab2c229d83 | 71 | wait(0.10); |
xshige | 0:8eab2c229d83 | 72 | } |
xshige | 0:8eab2c229d83 | 73 | void VS1002::power_down(void) //hardware and software reset |
xshige | 0:8eab2c229d83 | 74 | { |
xshige | 0:8eab2c229d83 | 75 | cs_low(); |
xshige | 0:8eab2c229d83 | 76 | reset(); |
xshige | 0:8eab2c229d83 | 77 | sci_write(0x00, SM_PDOWN); |
xshige | 0:8eab2c229d83 | 78 | wait(0.01); |
xshige | 0:8eab2c229d83 | 79 | reset(); |
xshige | 0:8eab2c229d83 | 80 | } |
xshige | 0:8eab2c229d83 | 81 | void VS1002::sci_initialise(void) |
xshige | 0:8eab2c229d83 | 82 | { |
xshige | 0:8eab2c229d83 | 83 | _RST = 1; //no reset |
xshige | 0:8eab2c229d83 | 84 | _spi.format(8,0); //spi 8bit interface, steady state low |
xshige | 0:8eab2c229d83 | 85 | _spi.frequency(1000000); //rising edge data record, freq. 1Mhz |
xshige | 0:8eab2c229d83 | 86 | |
xshige | 0:8eab2c229d83 | 87 | cs_low(); |
xshige | 0:8eab2c229d83 | 88 | for(int i=0; i<4; i++) |
xshige | 0:8eab2c229d83 | 89 | { |
xshige | 0:8eab2c229d83 | 90 | _spi.write(0xFF); //clock the chip a bit |
xshige | 0:8eab2c229d83 | 91 | } |
xshige | 0:8eab2c229d83 | 92 | cs_high(); |
xshige | 0:8eab2c229d83 | 93 | dcs_high(); |
xshige | 0:8eab2c229d83 | 94 | wait_us(5); |
xshige | 0:8eab2c229d83 | 95 | } |
xshige | 0:8eab2c229d83 | 96 | void VS1002::sdi_initialise(void) |
xshige | 0:8eab2c229d83 | 97 | { |
xshige | 0:8eab2c229d83 | 98 | _spi.format(8,0); |
xshige | 0:8eab2c229d83 | 99 | _spi.frequency(7000000); //set to 7MHz |
xshige | 0:8eab2c229d83 | 100 | |
xshige | 0:8eab2c229d83 | 101 | cs_high(); |
xshige | 0:8eab2c229d83 | 102 | dcs_high(); |
xshige | 0:8eab2c229d83 | 103 | } |
xshige | 0:8eab2c229d83 | 104 | void VS1002::sci_write(unsigned char address, unsigned short int data) |
xshige | 0:8eab2c229d83 | 105 | { |
xshige | 0:8eab2c229d83 | 106 | sci_en(); //enables SCI/disables SDI |
xshige | 0:8eab2c229d83 | 107 | |
xshige | 0:8eab2c229d83 | 108 | while(!_DREQ); //wait unitl data request is high |
xshige | 0:8eab2c229d83 | 109 | _spi.write(0x02); //SCI write |
xshige | 0:8eab2c229d83 | 110 | _spi.write(address); //register address |
xshige | 0:8eab2c229d83 | 111 | _spi.write((data >> 8) & 0xFF); //write out first half of data word |
xshige | 0:8eab2c229d83 | 112 | _spi.write(data & 0xFF); //write out second half of data word |
xshige | 0:8eab2c229d83 | 113 | |
xshige | 0:8eab2c229d83 | 114 | sci_dis(); //enables SDI/disables SCI |
xshige | 0:8eab2c229d83 | 115 | wait_us(5); |
xshige | 0:8eab2c229d83 | 116 | } |
xshige | 0:8eab2c229d83 | 117 | void VS1002::sdi_write(unsigned char datum) |
xshige | 0:8eab2c229d83 | 118 | { |
xshige | 0:8eab2c229d83 | 119 | sdi_en(); |
xshige | 0:8eab2c229d83 | 120 | |
xshige | 0:8eab2c229d83 | 121 | while(!_DREQ); |
xshige | 0:8eab2c229d83 | 122 | _spi.write(datum); |
xshige | 0:8eab2c229d83 | 123 | |
xshige | 0:8eab2c229d83 | 124 | sci_dis(); |
xshige | 0:8eab2c229d83 | 125 | } |
xshige | 0:8eab2c229d83 | 126 | unsigned short int VS1002::read(unsigned short int address) |
xshige | 0:8eab2c229d83 | 127 | { |
xshige | 0:8eab2c229d83 | 128 | cs_low(); //enables SCI/disables SDI |
xshige | 0:8eab2c229d83 | 129 | |
xshige | 0:8eab2c229d83 | 130 | while(!_DREQ); //wait unitl data request is high |
xshige | 0:8eab2c229d83 | 131 | _spi.write(0x03); //SCI write |
xshige | 0:8eab2c229d83 | 132 | _spi.write(address); //register address |
xshige | 0:8eab2c229d83 | 133 | unsigned short int received = _spi.write(0x00); //write out dummy byte |
xshige | 0:8eab2c229d83 | 134 | received <<= 8; |
xshige | 0:8eab2c229d83 | 135 | received += _spi.write(0x00); //write out dummy byte |
xshige | 0:8eab2c229d83 | 136 | |
xshige | 0:8eab2c229d83 | 137 | cs_high(); //enables SDI/disables SCI |
xshige | 0:8eab2c229d83 | 138 | |
xshige | 0:8eab2c229d83 | 139 | return received; //return received word |
xshige | 0:8eab2c229d83 | 140 | } |
xshige | 0:8eab2c229d83 | 141 | void VS1002::sine_test_activate(unsigned char wave) |
xshige | 0:8eab2c229d83 | 142 | { |
xshige | 0:8eab2c229d83 | 143 | cs_high(); //enables SDI/disables SCI |
xshige | 0:8eab2c229d83 | 144 | |
xshige | 0:8eab2c229d83 | 145 | while(!_DREQ); //wait unitl data request is high |
xshige | 0:8eab2c229d83 | 146 | _spi.write(0x53); //SDI write |
xshige | 0:8eab2c229d83 | 147 | _spi.write(0xEF); //SDI write |
xshige | 0:8eab2c229d83 | 148 | _spi.write(0x6E); //SDI write |
xshige | 0:8eab2c229d83 | 149 | _spi.write(wave); //SDI write |
xshige | 0:8eab2c229d83 | 150 | _spi.write(0x00); //filler byte |
xshige | 0:8eab2c229d83 | 151 | _spi.write(0x00); //filler byte |
xshige | 0:8eab2c229d83 | 152 | _spi.write(0x00); //filler byte |
xshige | 0:8eab2c229d83 | 153 | _spi.write(0x00); //filler byte |
xshige | 0:8eab2c229d83 | 154 | |
xshige | 0:8eab2c229d83 | 155 | cs_low(); //enables SCI/disables SDI |
xshige | 0:8eab2c229d83 | 156 | } |
xshige | 0:8eab2c229d83 | 157 | void VS1002::sine_test_deactivate(void) |
xshige | 0:8eab2c229d83 | 158 | { |
xshige | 0:8eab2c229d83 | 159 | cs_high(); |
xshige | 0:8eab2c229d83 | 160 | |
xshige | 0:8eab2c229d83 | 161 | while(!_DREQ); |
xshige | 0:8eab2c229d83 | 162 | _spi.write(0x45); //SDI write |
xshige | 0:8eab2c229d83 | 163 | _spi.write(0x78); //SDI write |
xshige | 0:8eab2c229d83 | 164 | _spi.write(0x69); //SDI write |
xshige | 0:8eab2c229d83 | 165 | _spi.write(0x74); //SDI write |
xshige | 0:8eab2c229d83 | 166 | _spi.write(0x00); //filler byte |
xshige | 0:8eab2c229d83 | 167 | _spi.write(0x00); //filler byte |
xshige | 0:8eab2c229d83 | 168 | _spi.write(0x00); //filler byte |
xshige | 0:8eab2c229d83 | 169 | _spi.write(0x00); //filler byte |
xshige | 0:8eab2c229d83 | 170 | } |
xshige | 0:8eab2c229d83 | 171 | void VS1002::volume(void) |
xshige | 0:8eab2c229d83 | 172 | { |
xshige | 0:8eab2c229d83 | 173 | #ifdef FIXED_VOL |
xshige | 0:8eab2c229d83 | 174 | unsigned char volumize = (0 * 255); // FIXED VOL (not support volume input) |
xshige | 0:8eab2c229d83 | 175 | #else |
xshige | 0:8eab2c229d83 | 176 | unsigned char volumize = (_VOL * 255); |
xshige | 0:8eab2c229d83 | 177 | #endif |
xshige | 0:8eab2c229d83 | 178 | while(!_DREQ); |
xshige | 0:8eab2c229d83 | 179 | |
xshige | 0:8eab2c229d83 | 180 | unsigned short int attenuation = ((256 * volumize) + volumize); |
xshige | 0:8eab2c229d83 | 181 | sci_write(0x0B, attenuation); |
xshige | 0:8eab2c229d83 | 182 | } |
xshige | 0:8eab2c229d83 | 183 | |
xshige | 0:8eab2c229d83 | 184 | void VS1002::play_song(int song_number) |
xshige | 0:8eab2c229d83 | 185 | { |
xshige | 0:8eab2c229d83 | 186 | /*====== Song Select ======*/ |
xshige | 0:8eab2c229d83 | 187 | |
xshige | 0:8eab2c229d83 | 188 | // char list[10000] = {0}; |
xshige | 0:8eab2c229d83 | 189 | char list[1000] = {0}; |
xshige | 0:8eab2c229d83 | 190 | char str[16] = {"/sd/"}; |
xshige | 0:8eab2c229d83 | 191 | unsigned int startplace = 0; |
xshige | 0:8eab2c229d83 | 192 | unsigned int endplace = 0; |
xshige | 0:8eab2c229d83 | 193 | unsigned int play = 0; |
xshige | 0:8eab2c229d83 | 194 | num_of_files = 0; |
xshige | 0:8eab2c229d83 | 195 | |
xshige | 0:8eab2c229d83 | 196 | DIR *d; |
xshige | 0:8eab2c229d83 | 197 | struct dirent *p; |
xshige | 0:8eab2c229d83 | 198 | d = opendir("/sd"); |
xshige | 0:8eab2c229d83 | 199 | if(d != NULL) |
xshige | 0:8eab2c229d83 | 200 | { |
xshige | 0:8eab2c229d83 | 201 | while((p = readdir(d)) != NULL) |
xshige | 0:8eab2c229d83 | 202 | { |
xshige | 0:8eab2c229d83 | 203 | strcat(list, "*"); |
xshige | 0:8eab2c229d83 | 204 | strcat(list, p->d_name); |
xshige | 0:8eab2c229d83 | 205 | num_of_files++; |
xshige | 0:8eab2c229d83 | 206 | } |
xshige | 0:8eab2c229d83 | 207 | } |
xshige | 0:8eab2c229d83 | 208 | else |
xshige | 0:8eab2c229d83 | 209 | { |
xshige | 0:8eab2c229d83 | 210 | perror("Could not open directory!"); |
xshige | 0:8eab2c229d83 | 211 | } |
xshige | 0:8eab2c229d83 | 212 | strcat(list, "*"); //terminating * |
xshige | 0:8eab2c229d83 | 213 | if(num_of_files < song_number) |
xshige | 0:8eab2c229d83 | 214 | { |
xshige | 0:8eab2c229d83 | 215 | return; |
xshige | 0:8eab2c229d83 | 216 | } |
xshige | 0:8eab2c229d83 | 217 | while(play != song_number) |
xshige | 0:8eab2c229d83 | 218 | { |
xshige | 0:8eab2c229d83 | 219 | char symbol = list[startplace]; |
xshige | 0:8eab2c229d83 | 220 | startplace++; |
xshige | 0:8eab2c229d83 | 221 | if(symbol == 0x2A) //0x2A = "*" |
xshige | 0:8eab2c229d83 | 222 | { |
xshige | 0:8eab2c229d83 | 223 | play++; |
xshige | 0:8eab2c229d83 | 224 | } |
xshige | 0:8eab2c229d83 | 225 | } |
xshige | 0:8eab2c229d83 | 226 | play = 0; |
xshige | 0:8eab2c229d83 | 227 | while(play != (song_number+1)) |
xshige | 0:8eab2c229d83 | 228 | { |
xshige | 0:8eab2c229d83 | 229 | char symbol = list[endplace]; |
xshige | 0:8eab2c229d83 | 230 | endplace++; |
xshige | 0:8eab2c229d83 | 231 | if(symbol == 0x2A) //0x2A = "*" |
xshige | 0:8eab2c229d83 | 232 | { |
xshige | 0:8eab2c229d83 | 233 | play++; |
xshige | 0:8eab2c229d83 | 234 | } |
xshige | 0:8eab2c229d83 | 235 | } |
xshige | 0:8eab2c229d83 | 236 | |
xshige | 0:8eab2c229d83 | 237 | strncat(str, &list[startplace], endplace-startplace); |
xshige | 0:8eab2c229d83 | 238 | str[(endplace-startplace)+3] = '\0'; |
xshige | 0:8eab2c229d83 | 239 | |
xshige | 0:8eab2c229d83 | 240 | //printf("list: %s\r\n",list); //debug |
xshige | 0:8eab2c229d83 | 241 | |
xshige | 0:8eab2c229d83 | 242 | /*====== File Transfer ======*/ |
xshige | 0:8eab2c229d83 | 243 | |
xshige | 0:8eab2c229d83 | 244 | // return if not MP3 file |
xshige | 0:8eab2c229d83 | 245 | if (!((strstr(str,"MP3")!=NULL)||(strstr(str,"mp3")!=NULL))) return; |
xshige | 0:8eab2c229d83 | 246 | // display filename.mp3 |
xshige | 0:8eab2c229d83 | 247 | printf("Now Playing: %s\r\n",str); |
xshige | 0:8eab2c229d83 | 248 | |
xshige | 0:8eab2c229d83 | 249 | FILE *song; |
xshige | 0:8eab2c229d83 | 250 | unsigned char array[512]; |
xshige | 0:8eab2c229d83 | 251 | |
xshige | 0:8eab2c229d83 | 252 | song = fopen(str, "rb"); |
xshige | 0:8eab2c229d83 | 253 | |
xshige | 0:8eab2c229d83 | 254 | if(!song) |
xshige | 0:8eab2c229d83 | 255 | { |
xshige | 0:8eab2c229d83 | 256 | exit(1); |
xshige | 0:8eab2c229d83 | 257 | } |
xshige | 0:8eab2c229d83 | 258 | |
xshige | 0:8eab2c229d83 | 259 | while(!feof(song)) |
xshige | 0:8eab2c229d83 | 260 | { |
xshige | 0:8eab2c229d83 | 261 | fread(&array, 1, 512, song); |
xshige | 0:8eab2c229d83 | 262 | for(int i=0; i<512; i++) |
xshige | 0:8eab2c229d83 | 263 | { |
xshige | 0:8eab2c229d83 | 264 | #ifndef FS_ONLY |
xshige | 0:8eab2c229d83 | 265 | sdi_write(array[i]); |
xshige | 0:8eab2c229d83 | 266 | // printf("."); |
xshige | 0:8eab2c229d83 | 267 | #endif |
xshige | 0:8eab2c229d83 | 268 | } |
xshige | 0:8eab2c229d83 | 269 | #ifndef FS_ONLY |
xshige | 0:8eab2c229d83 | 270 | volume(); |
xshige | 0:8eab2c229d83 | 271 | #endif |
xshige | 0:8eab2c229d83 | 272 | } |
xshige | 0:8eab2c229d83 | 273 | for(int n=0; n<2048; n++) |
xshige | 0:8eab2c229d83 | 274 | { |
xshige | 0:8eab2c229d83 | 275 | #ifndef FS_ONLY |
xshige | 0:8eab2c229d83 | 276 | sdi_write(0x00); |
xshige | 0:8eab2c229d83 | 277 | #endif |
xshige | 0:8eab2c229d83 | 278 | } |
xshige | 0:8eab2c229d83 | 279 | fclose(song); //close the file |
xshige | 0:8eab2c229d83 | 280 | } |