Add the RTOS processing. for the Network radio streaming receiver.
Fork of VS1053b by
VS1053.cpp@8:5e4a21202223, 2011-01-12 (annotated)
- Committer:
- christi_s
- Date:
- Wed Jan 12 18:30:28 2011 +0000
- Revision:
- 8:5e4a21202223
- Parent:
- 6:eed008905159
- Child:
- 10:114ac02a3875
change buffer functions const unsigned char* -> const char*
change ctor -> create buffer external; you can have more than one VS1053b with different buffer sizes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
christi_s | 1:ced2c297cc1b | 1 | /* mbed VLSI VS1053b library |
christi_s | 1:ced2c297cc1b | 2 | * Copyright (c) 2010 Christian Schmiljun |
christi_s | 1:ced2c297cc1b | 3 | * |
christi_s | 1:ced2c297cc1b | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
christi_s | 1:ced2c297cc1b | 5 | * of this software and associated documentation files (the "Software"), to deal |
christi_s | 1:ced2c297cc1b | 6 | * in the Software without restriction, including without limitation the rights |
christi_s | 1:ced2c297cc1b | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
christi_s | 1:ced2c297cc1b | 8 | * copies of the Software, and to permit persons to whom the Software is |
christi_s | 1:ced2c297cc1b | 9 | * furnished to do so, subject to the following conditions: |
christi_s | 1:ced2c297cc1b | 10 | * |
christi_s | 1:ced2c297cc1b | 11 | * The above copyright notice and this permission notice shall be included in |
christi_s | 1:ced2c297cc1b | 12 | * all copies or substantial portions of the Software. |
christi_s | 1:ced2c297cc1b | 13 | * |
christi_s | 1:ced2c297cc1b | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
christi_s | 1:ced2c297cc1b | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
christi_s | 1:ced2c297cc1b | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
christi_s | 1:ced2c297cc1b | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
christi_s | 1:ced2c297cc1b | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
christi_s | 1:ced2c297cc1b | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
christi_s | 1:ced2c297cc1b | 20 | * THE SOFTWARE. |
christi_s | 1:ced2c297cc1b | 21 | */ |
christi_s | 1:ced2c297cc1b | 22 | |
christi_s | 1:ced2c297cc1b | 23 | /* This code based on: |
christi_s | 1:ced2c297cc1b | 24 | * mbeduino_MP3_Shield_MP3Player |
christi_s | 1:ced2c297cc1b | 25 | * http://mbed.org/users/xshige/programs/mbeduino_MP3_Shield_MP3Player/lgcx63 |
christi_s | 1:ced2c297cc1b | 26 | * 2010-10-16 |
christi_s | 1:ced2c297cc1b | 27 | */ |
christi_s | 1:ced2c297cc1b | 28 | |
christi_s | 0:7728d9c6c487 | 29 | #include "VS1053.h" |
christi_s | 0:7728d9c6c487 | 30 | #include "mbed.h" |
christi_s | 0:7728d9c6c487 | 31 | |
christi_s | 0:7728d9c6c487 | 32 | // patch binarys |
christi_s | 0:7728d9c6c487 | 33 | #include "Patches/VS1053b_patch_1_5.c" |
christi_s | 0:7728d9c6c487 | 34 | #include "Patches/VS1053b_patch_1_5_flac.c" |
christi_s | 0:7728d9c6c487 | 35 | #include "Patches/VS1053b_patch_1_4_flac.c" |
christi_s | 1:ced2c297cc1b | 36 | #include "Patches/VS1053b_specana_0_9.c" |
christi_s | 1:ced2c297cc1b | 37 | #include "Patches/VS1053b_pcm_recorder_0_9.c" |
christi_s | 0:7728d9c6c487 | 38 | |
christi_s | 6:eed008905159 | 39 | const char VS1053::_sampleRateTable[4][4] = { |
christi_s | 6:eed008905159 | 40 | 11, 12, 8, 0, |
christi_s | 6:eed008905159 | 41 | 11, 12, 8, 0, |
christi_s | 6:eed008905159 | 42 | 22, 24, 16, 0, |
christi_s | 6:eed008905159 | 43 | 44, 48, 32, 0 |
christi_s | 6:eed008905159 | 44 | }; |
christi_s | 4:cacb5e663fa9 | 45 | |
christi_s | 0:7728d9c6c487 | 46 | /* ================================================================== |
christi_s | 0:7728d9c6c487 | 47 | * Constructor |
christi_s | 0:7728d9c6c487 | 48 | * =================================================================*/ |
christi_s | 0:7728d9c6c487 | 49 | VS1053::VS1053( |
christi_s | 0:7728d9c6c487 | 50 | PinName mosi, PinName miso, PinName sck, PinName cs, PinName rst, |
christi_s | 8:5e4a21202223 | 51 | PinName dreq, PinName dcs, char* buffer, int buffer_size) |
christi_s | 0:7728d9c6c487 | 52 | : |
christi_s | 0:7728d9c6c487 | 53 | _spi(mosi, miso, sck), |
christi_s | 0:7728d9c6c487 | 54 | _CS(cs), |
christi_s | 0:7728d9c6c487 | 55 | _RST(rst), |
christi_s | 4:cacb5e663fa9 | 56 | _DCS(dcs), |
christi_s | 4:cacb5e663fa9 | 57 | _DREQ(dreq), |
christi_s | 4:cacb5e663fa9 | 58 | _DREQ_INTERUPT_IN(dreq) |
christi_s | 2:5bab956cb59e | 59 | { |
christi_s | 1:ced2c297cc1b | 60 | _volume = DEFAULT_VOLUME; |
christi_s | 1:ced2c297cc1b | 61 | _balance = DEFAULT_BALANCE_DIFERENCE_LEFT_RIGHT; |
christi_s | 2:5bab956cb59e | 62 | _sb_amplitude = DEFAULT_BASS_AMPLITUDE; |
christi_s | 2:5bab956cb59e | 63 | _sb_freqlimit = DEFAULT_BASS_FREQUENCY; |
christi_s | 2:5bab956cb59e | 64 | _st_amplitude = DEFAULT_TREBLE_AMPLITUDE; |
christi_s | 4:cacb5e663fa9 | 65 | _st_freqlimit = DEFAULT_TREBLE_FREQUENCY; |
christi_s | 8:5e4a21202223 | 66 | _buffer = buffer; |
christi_s | 8:5e4a21202223 | 67 | BUFFER_SIZE = buffer_size; |
christi_s | 4:cacb5e663fa9 | 68 | _DREQ_INTERUPT_IN.mode(PullDown); |
christi_s | 4:cacb5e663fa9 | 69 | INTERRUPT_HANDLER_DISABLE; |
christi_s | 4:cacb5e663fa9 | 70 | bufferReset(); |
christi_s | 0:7728d9c6c487 | 71 | } |
christi_s | 0:7728d9c6c487 | 72 | |
christi_s | 8:5e4a21202223 | 73 | |
christi_s | 0:7728d9c6c487 | 74 | /*=================================================================== |
christi_s | 0:7728d9c6c487 | 75 | * Functions |
christi_s | 0:7728d9c6c487 | 76 | *==================================================================*/ |
christi_s | 0:7728d9c6c487 | 77 | |
christi_s | 0:7728d9c6c487 | 78 | void VS1053::cs_low(void) { |
christi_s | 0:7728d9c6c487 | 79 | _CS = 0; |
christi_s | 0:7728d9c6c487 | 80 | } |
christi_s | 0:7728d9c6c487 | 81 | void VS1053::cs_high(void) { |
christi_s | 0:7728d9c6c487 | 82 | _CS = 1; |
christi_s | 0:7728d9c6c487 | 83 | } |
christi_s | 0:7728d9c6c487 | 84 | void VS1053::dcs_low(void) { |
christi_s | 0:7728d9c6c487 | 85 | _DCS = 0; |
christi_s | 0:7728d9c6c487 | 86 | |
christi_s | 0:7728d9c6c487 | 87 | } |
christi_s | 0:7728d9c6c487 | 88 | void VS1053::dcs_high(void) { |
christi_s | 0:7728d9c6c487 | 89 | _DCS = 1; |
christi_s | 0:7728d9c6c487 | 90 | } |
christi_s | 0:7728d9c6c487 | 91 | void VS1053::sci_en(void) { //SCI enable |
christi_s | 0:7728d9c6c487 | 92 | cs_high(); |
christi_s | 0:7728d9c6c487 | 93 | dcs_high(); |
christi_s | 0:7728d9c6c487 | 94 | cs_low(); |
christi_s | 0:7728d9c6c487 | 95 | } |
christi_s | 0:7728d9c6c487 | 96 | void VS1053::sci_dis(void) { //SCI disable |
christi_s | 0:7728d9c6c487 | 97 | cs_high(); |
christi_s | 0:7728d9c6c487 | 98 | } |
christi_s | 0:7728d9c6c487 | 99 | void VS1053::sdi_en(void) { //SDI enable |
christi_s | 0:7728d9c6c487 | 100 | dcs_high(); |
christi_s | 0:7728d9c6c487 | 101 | cs_high(); |
christi_s | 0:7728d9c6c487 | 102 | dcs_low(); |
christi_s | 0:7728d9c6c487 | 103 | } |
christi_s | 0:7728d9c6c487 | 104 | void VS1053::sdi_dis(void) { //SDI disable |
christi_s | 0:7728d9c6c487 | 105 | dcs_high(); |
christi_s | 0:7728d9c6c487 | 106 | } |
christi_s | 0:7728d9c6c487 | 107 | void VS1053::reset(void) { //hardware reset |
christi_s | 4:cacb5e663fa9 | 108 | INTERRUPT_HANDLER_DISABLE; |
christi_s | 0:7728d9c6c487 | 109 | wait_ms(10); |
christi_s | 0:7728d9c6c487 | 110 | _RST = 0; |
christi_s | 0:7728d9c6c487 | 111 | wait_ms(5); |
christi_s | 0:7728d9c6c487 | 112 | _RST = 1; |
christi_s | 0:7728d9c6c487 | 113 | wait_ms(10); |
christi_s | 0:7728d9c6c487 | 114 | } |
christi_s | 0:7728d9c6c487 | 115 | void VS1053::power_down(void) { //hardware and software reset |
christi_s | 0:7728d9c6c487 | 116 | cs_low(); |
christi_s | 0:7728d9c6c487 | 117 | reset(); |
christi_s | 0:7728d9c6c487 | 118 | // sci_write(0x00, SM_PDOWN); |
christi_s | 0:7728d9c6c487 | 119 | sci_write(0x00, 0x10); // tempo |
christi_s | 0:7728d9c6c487 | 120 | wait(0.01); |
christi_s | 0:7728d9c6c487 | 121 | reset(); |
christi_s | 0:7728d9c6c487 | 122 | } |
christi_s | 1:ced2c297cc1b | 123 | void VS1053::spi_initialise(void) { |
christi_s | 0:7728d9c6c487 | 124 | _RST = 1; //no reset |
christi_s | 0:7728d9c6c487 | 125 | _spi.format(8,0); //spi 8bit interface, steady state low |
christi_s | 0:7728d9c6c487 | 126 | // _spi.frequency(1000000); //rising edge data record, freq. 1Mhz |
christi_s | 0:7728d9c6c487 | 127 | _spi.frequency(2000000); //rising edge data record, freq. 2Mhz |
christi_s | 0:7728d9c6c487 | 128 | |
christi_s | 0:7728d9c6c487 | 129 | |
christi_s | 0:7728d9c6c487 | 130 | cs_low(); |
christi_s | 0:7728d9c6c487 | 131 | for (int i=0; i<4; i++) { |
christi_s | 0:7728d9c6c487 | 132 | _spi.write(0xFF); //clock the chip a bit |
christi_s | 0:7728d9c6c487 | 133 | } |
christi_s | 0:7728d9c6c487 | 134 | cs_high(); |
christi_s | 0:7728d9c6c487 | 135 | dcs_high(); |
christi_s | 0:7728d9c6c487 | 136 | wait_us(5); |
christi_s | 0:7728d9c6c487 | 137 | } |
christi_s | 0:7728d9c6c487 | 138 | void VS1053::sdi_initialise(void) { |
christi_s | 1:ced2c297cc1b | 139 | _spi.frequency(8000000); //set to 8 MHz to make fast transfer |
christi_s | 0:7728d9c6c487 | 140 | cs_high(); |
christi_s | 0:7728d9c6c487 | 141 | dcs_high(); |
christi_s | 0:7728d9c6c487 | 142 | } |
christi_s | 0:7728d9c6c487 | 143 | void VS1053::sci_write(unsigned char address, unsigned short int data) { |
christi_s | 4:cacb5e663fa9 | 144 | // TODO disable all interrupts |
christi_s | 4:cacb5e663fa9 | 145 | __disable_irq(); |
christi_s | 0:7728d9c6c487 | 146 | sci_en(); //enables SCI/disables SDI |
christi_s | 0:7728d9c6c487 | 147 | |
christi_s | 0:7728d9c6c487 | 148 | while (!_DREQ); //wait unitl data request is high |
christi_s | 0:7728d9c6c487 | 149 | _spi.write(0x02); //SCI write |
christi_s | 0:7728d9c6c487 | 150 | _spi.write(address); //register address |
christi_s | 0:7728d9c6c487 | 151 | _spi.write((data >> 8) & 0xFF); //write out first half of data word |
christi_s | 0:7728d9c6c487 | 152 | _spi.write(data & 0xFF); //write out second half of data word |
christi_s | 0:7728d9c6c487 | 153 | |
christi_s | 0:7728d9c6c487 | 154 | sci_dis(); //enables SDI/disables SCI |
christi_s | 0:7728d9c6c487 | 155 | wait_us(5); |
christi_s | 4:cacb5e663fa9 | 156 | |
christi_s | 4:cacb5e663fa9 | 157 | // TODO enable all interrupts |
christi_s | 4:cacb5e663fa9 | 158 | __enable_irq(); |
christi_s | 0:7728d9c6c487 | 159 | } |
christi_s | 4:cacb5e663fa9 | 160 | void VS1053::sdi_write(unsigned char datum) { |
christi_s | 4:cacb5e663fa9 | 161 | |
christi_s | 0:7728d9c6c487 | 162 | sdi_en(); |
christi_s | 0:7728d9c6c487 | 163 | |
christi_s | 0:7728d9c6c487 | 164 | while (!_DREQ); |
christi_s | 0:7728d9c6c487 | 165 | _spi.write(datum); |
christi_s | 0:7728d9c6c487 | 166 | |
christi_s | 4:cacb5e663fa9 | 167 | sdi_dis(); |
christi_s | 0:7728d9c6c487 | 168 | } |
christi_s | 1:ced2c297cc1b | 169 | unsigned short VS1053::sci_read(unsigned short int address) { |
christi_s | 4:cacb5e663fa9 | 170 | // TODO disable all interrupts |
christi_s | 4:cacb5e663fa9 | 171 | __disable_irq(); |
christi_s | 4:cacb5e663fa9 | 172 | |
christi_s | 0:7728d9c6c487 | 173 | cs_low(); //enables SCI/disables SDI |
christi_s | 0:7728d9c6c487 | 174 | |
christi_s | 0:7728d9c6c487 | 175 | while (!_DREQ); //wait unitl data request is high |
christi_s | 0:7728d9c6c487 | 176 | _spi.write(0x03); //SCI write |
christi_s | 0:7728d9c6c487 | 177 | _spi.write(address); //register address |
christi_s | 0:7728d9c6c487 | 178 | unsigned short int received = _spi.write(0x00); //write out dummy byte |
christi_s | 0:7728d9c6c487 | 179 | received <<= 8; |
christi_s | 4:cacb5e663fa9 | 180 | received |= _spi.write(0x00); //write out dummy byte |
christi_s | 0:7728d9c6c487 | 181 | |
christi_s | 0:7728d9c6c487 | 182 | cs_high(); //enables SDI/disables SCI |
christi_s | 0:7728d9c6c487 | 183 | |
christi_s | 4:cacb5e663fa9 | 184 | // TODO enable all interrupts |
christi_s | 4:cacb5e663fa9 | 185 | __enable_irq(); |
christi_s | 0:7728d9c6c487 | 186 | return received; //return received word |
christi_s | 0:7728d9c6c487 | 187 | } |
christi_s | 0:7728d9c6c487 | 188 | void VS1053::sine_test_activate(unsigned char wave) { |
christi_s | 0:7728d9c6c487 | 189 | cs_high(); //enables SDI/disables SCI |
christi_s | 0:7728d9c6c487 | 190 | |
christi_s | 0:7728d9c6c487 | 191 | while (!_DREQ); //wait unitl data request is high |
christi_s | 0:7728d9c6c487 | 192 | _spi.write(0x53); //SDI write |
christi_s | 0:7728d9c6c487 | 193 | _spi.write(0xEF); //SDI write |
christi_s | 0:7728d9c6c487 | 194 | _spi.write(0x6E); //SDI write |
christi_s | 0:7728d9c6c487 | 195 | _spi.write(wave); //SDI write |
christi_s | 0:7728d9c6c487 | 196 | _spi.write(0x00); //filler byte |
christi_s | 0:7728d9c6c487 | 197 | _spi.write(0x00); //filler byte |
christi_s | 0:7728d9c6c487 | 198 | _spi.write(0x00); //filler byte |
christi_s | 0:7728d9c6c487 | 199 | _spi.write(0x00); //filler byte |
christi_s | 0:7728d9c6c487 | 200 | |
christi_s | 0:7728d9c6c487 | 201 | cs_low(); //enables SCI/disables SDI |
christi_s | 0:7728d9c6c487 | 202 | } |
christi_s | 0:7728d9c6c487 | 203 | void VS1053::sine_test_deactivate(void) { |
christi_s | 0:7728d9c6c487 | 204 | cs_high(); |
christi_s | 0:7728d9c6c487 | 205 | |
christi_s | 0:7728d9c6c487 | 206 | while (!_DREQ); |
christi_s | 0:7728d9c6c487 | 207 | _spi.write(0x45); //SDI write |
christi_s | 0:7728d9c6c487 | 208 | _spi.write(0x78); //SDI write |
christi_s | 0:7728d9c6c487 | 209 | _spi.write(0x69); //SDI write |
christi_s | 0:7728d9c6c487 | 210 | _spi.write(0x74); //SDI write |
christi_s | 0:7728d9c6c487 | 211 | _spi.write(0x00); //filler byte |
christi_s | 0:7728d9c6c487 | 212 | _spi.write(0x00); //filler byte |
christi_s | 0:7728d9c6c487 | 213 | _spi.write(0x00); //filler byte |
christi_s | 0:7728d9c6c487 | 214 | _spi.write(0x00); //filler byte |
christi_s | 0:7728d9c6c487 | 215 | } |
christi_s | 0:7728d9c6c487 | 216 | |
christi_s | 0:7728d9c6c487 | 217 | unsigned short int VS1053::wram_read(unsigned short int address) { |
christi_s | 0:7728d9c6c487 | 218 | unsigned short int tmp1,tmp2; |
christi_s | 0:7728d9c6c487 | 219 | sci_write(SCI_WRAMADDR,address); |
christi_s | 0:7728d9c6c487 | 220 | tmp1=sci_read(SCI_WRAM); |
christi_s | 0:7728d9c6c487 | 221 | sci_write(SCI_WRAMADDR,address); |
christi_s | 0:7728d9c6c487 | 222 | tmp2=sci_read(SCI_WRAM); |
christi_s | 0:7728d9c6c487 | 223 | if (tmp1==tmp2) return tmp1; |
christi_s | 0:7728d9c6c487 | 224 | sci_write(SCI_WRAMADDR,address); |
christi_s | 0:7728d9c6c487 | 225 | tmp1=sci_read(SCI_WRAM); |
christi_s | 0:7728d9c6c487 | 226 | if (tmp1==tmp2) return tmp1; |
christi_s | 0:7728d9c6c487 | 227 | sci_write(SCI_WRAMADDR,address); |
christi_s | 0:7728d9c6c487 | 228 | tmp1=sci_read(SCI_WRAM); |
christi_s | 0:7728d9c6c487 | 229 | if (tmp1==tmp2) return tmp1; |
christi_s | 0:7728d9c6c487 | 230 | return tmp1; |
christi_s | 0:7728d9c6c487 | 231 | } |
christi_s | 0:7728d9c6c487 | 232 | |
christi_s | 0:7728d9c6c487 | 233 | void VS1053::wram_write(unsigned short int address, unsigned short int data) { |
christi_s | 0:7728d9c6c487 | 234 | sci_write(SCI_WRAMADDR,address); |
christi_s | 0:7728d9c6c487 | 235 | sci_write(SCI_WRAM,data); |
christi_s | 0:7728d9c6c487 | 236 | return; |
christi_s | 0:7728d9c6c487 | 237 | } |
christi_s | 0:7728d9c6c487 | 238 | |
christi_s | 1:ced2c297cc1b | 239 | void VS1053::setPlaySpeed(unsigned short speed) |
christi_s | 1:ced2c297cc1b | 240 | { |
christi_s | 1:ced2c297cc1b | 241 | wram_write(para_playSpeed, speed); |
christi_s | 2:5bab956cb59e | 242 | DEBUGOUT("VS1053b: Change speed. New speed: %d\r\n", speed); |
christi_s | 1:ced2c297cc1b | 243 | } |
christi_s | 0:7728d9c6c487 | 244 | |
christi_s | 0:7728d9c6c487 | 245 | void VS1053::terminateStream(void) { |
christi_s | 4:cacb5e663fa9 | 246 | while(bufferCount() > 0) |
christi_s | 4:cacb5e663fa9 | 247 | ; |
christi_s | 4:cacb5e663fa9 | 248 | DEBUGOUT("VS1053b: Song terminating..\r\n"); |
christi_s | 1:ced2c297cc1b | 249 | // send at least 2052 bytes of endFillByte[7:0]. |
christi_s | 1:ced2c297cc1b | 250 | // read endFillByte (0 .. 15) from wram |
christi_s | 1:ced2c297cc1b | 251 | unsigned short endFillByte=wram_read(para_endFillByte); |
christi_s | 1:ced2c297cc1b | 252 | // clear endFillByte (8 .. 15) |
christi_s | 1:ced2c297cc1b | 253 | endFillByte = endFillByte ^0x00FF; |
christi_s | 1:ced2c297cc1b | 254 | for (int n = 0; n < 2052; n++) |
christi_s | 1:ced2c297cc1b | 255 | sdi_write(endFillByte); |
christi_s | 1:ced2c297cc1b | 256 | |
christi_s | 1:ced2c297cc1b | 257 | // set SCI MODE bit SM CANCEL |
christi_s | 1:ced2c297cc1b | 258 | unsigned short sciModeByte = sci_read(SCI_MODE); |
christi_s | 1:ced2c297cc1b | 259 | sciModeByte |= SM_CANCEL; |
christi_s | 1:ced2c297cc1b | 260 | sci_write(SCI_MODE, sciModeByte); |
christi_s | 1:ced2c297cc1b | 261 | |
christi_s | 1:ced2c297cc1b | 262 | // send up 2048 bytes of endFillByte[7:0]. |
christi_s | 1:ced2c297cc1b | 263 | for (int i = 0; i < 64; i++) |
christi_s | 1:ced2c297cc1b | 264 | { |
christi_s | 1:ced2c297cc1b | 265 | // send at least 32 bytes of endFillByte[7:0] |
christi_s | 1:ced2c297cc1b | 266 | for (int n = 0; n < 32; n++) |
christi_s | 1:ced2c297cc1b | 267 | sdi_write(endFillByte); |
christi_s | 1:ced2c297cc1b | 268 | // read SCI MODE; if SM CANCEL is still set, repeat |
christi_s | 1:ced2c297cc1b | 269 | sciModeByte = sci_read(SCI_MODE); |
christi_s | 1:ced2c297cc1b | 270 | if ((sciModeByte & SM_CANCEL) == 0x0000) |
christi_s | 1:ced2c297cc1b | 271 | { |
christi_s | 1:ced2c297cc1b | 272 | break; |
christi_s | 1:ced2c297cc1b | 273 | } |
christi_s | 1:ced2c297cc1b | 274 | } |
christi_s | 1:ced2c297cc1b | 275 | |
christi_s | 1:ced2c297cc1b | 276 | if ((sciModeByte & SM_CANCEL) == 0x0000) |
christi_s | 1:ced2c297cc1b | 277 | { |
christi_s | 2:5bab956cb59e | 278 | DEBUGOUT("VS1053b: Song sucessfully sent. Terminating OK\r\n"); |
christi_s | 2:5bab956cb59e | 279 | DEBUGOUT("VS1053b: SCI MODE = %#x, SM_CANCEL = %#x\r\n", sciModeByte, sciModeByte & SM_CANCEL); |
christi_s | 6:eed008905159 | 280 | sci_write(SCI_DECODE_TIME, 0x0000); |
christi_s | 1:ced2c297cc1b | 281 | } |
christi_s | 1:ced2c297cc1b | 282 | else |
christi_s | 1:ced2c297cc1b | 283 | { |
christi_s | 2:5bab956cb59e | 284 | DEBUGOUT("VS1053b: SM CANCEL hasn't cleared after sending 2048 bytes, do software reset\r\n"); |
christi_s | 6:eed008905159 | 285 | DEBUGOUT("VS1053b: SCI MODE = %#x, SM_CANCEL = %#x\r\n", sciModeByte, sciModeByte & SM_CANCEL); |
christi_s | 1:ced2c297cc1b | 286 | initialize(); |
christi_s | 1:ced2c297cc1b | 287 | } |
christi_s | 0:7728d9c6c487 | 288 | } |
christi_s | 0:7728d9c6c487 | 289 | |
christi_s | 0:7728d9c6c487 | 290 | void VS1053::write_plugin(const unsigned short *plugin, unsigned int len) { |
christi_s | 0:7728d9c6c487 | 291 | unsigned int i; |
christi_s | 0:7728d9c6c487 | 292 | unsigned short addr, n, val; |
christi_s | 0:7728d9c6c487 | 293 | |
christi_s | 0:7728d9c6c487 | 294 | for (i=0; i<len;) { |
christi_s | 0:7728d9c6c487 | 295 | addr = plugin[i++]; |
christi_s | 0:7728d9c6c487 | 296 | n = plugin[i++]; |
christi_s | 0:7728d9c6c487 | 297 | if (n & 0x8000U) { //RLE run, replicate n samples |
christi_s | 0:7728d9c6c487 | 298 | n &= 0x7FFF; |
christi_s | 0:7728d9c6c487 | 299 | val = plugin[i++]; |
christi_s | 0:7728d9c6c487 | 300 | while (n--) { |
christi_s | 0:7728d9c6c487 | 301 | sci_write(addr,val); |
christi_s | 0:7728d9c6c487 | 302 | } |
christi_s | 0:7728d9c6c487 | 303 | } else { //copy run, copy n sample |
christi_s | 0:7728d9c6c487 | 304 | while (n--) { |
christi_s | 0:7728d9c6c487 | 305 | val = plugin[i++]; |
christi_s | 0:7728d9c6c487 | 306 | sci_write(addr,val); |
christi_s | 0:7728d9c6c487 | 307 | } |
christi_s | 0:7728d9c6c487 | 308 | } |
christi_s | 0:7728d9c6c487 | 309 | } |
christi_s | 0:7728d9c6c487 | 310 | |
christi_s | 0:7728d9c6c487 | 311 | return; |
christi_s | 0:7728d9c6c487 | 312 | } |
christi_s | 0:7728d9c6c487 | 313 | |
christi_s | 0:7728d9c6c487 | 314 | |
christi_s | 1:ced2c297cc1b | 315 | bool VS1053::initialize(void) { |
christi_s | 0:7728d9c6c487 | 316 | _RST = 1; |
christi_s | 0:7728d9c6c487 | 317 | cs_high(); //chip disabled |
christi_s | 1:ced2c297cc1b | 318 | spi_initialise(); //initialise MBED |
christi_s | 1:ced2c297cc1b | 319 | |
christi_s | 1:ced2c297cc1b | 320 | sci_write(SCI_MODE, (SM_SDINEW+SM_RESET)); // set mode reg. |
christi_s | 0:7728d9c6c487 | 321 | wait_ms(10); |
christi_s | 1:ced2c297cc1b | 322 | |
christi_s | 1:ced2c297cc1b | 323 | #ifdef DEBUG |
christi_s | 1:ced2c297cc1b | 324 | unsigned int info = wram_read(para_chipID_0); |
christi_s | 2:5bab956cb59e | 325 | DEBUGOUT("VS1053b: ChipID_0:%04X\r\n", info); |
christi_s | 1:ced2c297cc1b | 326 | info = wram_read(para_chipID_1); |
christi_s | 2:5bab956cb59e | 327 | DEBUGOUT("VS1053b: ChipID_1:%04X\r\n", info); |
christi_s | 1:ced2c297cc1b | 328 | info = wram_read(para_version); |
christi_s | 2:5bab956cb59e | 329 | DEBUGOUT("VS1053b: Structure version:%04X\r\n", info); |
christi_s | 0:7728d9c6c487 | 330 | #endif |
christi_s | 1:ced2c297cc1b | 331 | |
christi_s | 0:7728d9c6c487 | 332 | //get chip version, set clock multiplier and load patch |
christi_s | 1:ced2c297cc1b | 333 | int i = (sci_read(SCI_STATUS) & 0xF0) >> 4; |
christi_s | 0:7728d9c6c487 | 334 | if (i == 4) { |
christi_s | 2:5bab956cb59e | 335 | |
christi_s | 2:5bab956cb59e | 336 | DEBUGOUT("VS1053b: Installed Chip is: VS1053\r\n"); |
christi_s | 2:5bab956cb59e | 337 | |
christi_s | 1:ced2c297cc1b | 338 | sci_write(SCI_CLOCKF, (SC_MULT_XTALIx50)); |
christi_s | 1:ced2c297cc1b | 339 | wait_ms(10); |
christi_s | 0:7728d9c6c487 | 340 | #ifdef VS_PATCH |
christi_s | 0:7728d9c6c487 | 341 | // loading patch |
christi_s | 1:ced2c297cc1b | 342 | write_plugin(vs1053b_patch, sizeof(vs1053b_patch)/2); |
christi_s | 2:5bab956cb59e | 343 | |
christi_s | 2:5bab956cb59e | 344 | DEBUGOUT("VS1053b: Patch is loaded.\r\n"); |
christi_s | 2:5bab956cb59e | 345 | DEBUGOUT("VS1053b: Patch size:%d bytes\r\n",sizeof(vs1053b_patch)); |
christi_s | 1:ced2c297cc1b | 346 | |
christi_s | 1:ced2c297cc1b | 347 | #endif // VS_PATCH |
christi_s | 1:ced2c297cc1b | 348 | } |
christi_s | 1:ced2c297cc1b | 349 | else |
christi_s | 1:ced2c297cc1b | 350 | { |
christi_s | 2:5bab956cb59e | 351 | DEBUGOUT("VS1053b: Not Supported Chip\r\n"); |
christi_s | 1:ced2c297cc1b | 352 | return false; |
christi_s | 1:ced2c297cc1b | 353 | } |
christi_s | 1:ced2c297cc1b | 354 | |
christi_s | 1:ced2c297cc1b | 355 | // change spi to higher speed |
christi_s | 1:ced2c297cc1b | 356 | sdi_initialise(); |
christi_s | 1:ced2c297cc1b | 357 | changeVolume(); |
christi_s | 4:cacb5e663fa9 | 358 | changeBass(); |
christi_s | 4:cacb5e663fa9 | 359 | _isIdle = true; |
christi_s | 1:ced2c297cc1b | 360 | return true; |
christi_s | 0:7728d9c6c487 | 361 | } |
christi_s | 0:7728d9c6c487 | 362 | |
christi_s | 1:ced2c297cc1b | 363 | void VS1053::setVolume(float vol) |
christi_s | 1:ced2c297cc1b | 364 | { |
christi_s | 1:ced2c297cc1b | 365 | if (vol > -0.5) |
christi_s | 1:ced2c297cc1b | 366 | _volume = -0.5; |
christi_s | 1:ced2c297cc1b | 367 | else |
christi_s | 1:ced2c297cc1b | 368 | _volume = vol; |
christi_s | 1:ced2c297cc1b | 369 | |
christi_s | 1:ced2c297cc1b | 370 | changeVolume(); |
christi_s | 1:ced2c297cc1b | 371 | } |
christi_s | 1:ced2c297cc1b | 372 | |
christi_s | 1:ced2c297cc1b | 373 | float VS1053::getVolume(void) |
christi_s | 1:ced2c297cc1b | 374 | { |
christi_s | 1:ced2c297cc1b | 375 | return _volume; |
christi_s | 0:7728d9c6c487 | 376 | } |
christi_s | 1:ced2c297cc1b | 377 | |
christi_s | 1:ced2c297cc1b | 378 | void VS1053::setBalance(float balance) |
christi_s | 1:ced2c297cc1b | 379 | { |
christi_s | 1:ced2c297cc1b | 380 | _balance = balance; |
christi_s | 1:ced2c297cc1b | 381 | |
christi_s | 1:ced2c297cc1b | 382 | changeVolume(); |
christi_s | 1:ced2c297cc1b | 383 | } |
christi_s | 1:ced2c297cc1b | 384 | |
christi_s | 1:ced2c297cc1b | 385 | float VS1053::getBalance(void) |
christi_s | 1:ced2c297cc1b | 386 | { |
christi_s | 1:ced2c297cc1b | 387 | return _balance; |
christi_s | 1:ced2c297cc1b | 388 | } |
christi_s | 1:ced2c297cc1b | 389 | |
christi_s | 1:ced2c297cc1b | 390 | void VS1053::changeVolume(void) |
christi_s | 1:ced2c297cc1b | 391 | { |
christi_s | 1:ced2c297cc1b | 392 | // volume calculation |
christi_s | 1:ced2c297cc1b | 393 | unsigned short volCalced = (((char)(_volume / -0.5f)) << 8) + (char)((_volume - _balance) / -0.5f); |
christi_s | 1:ced2c297cc1b | 394 | |
christi_s | 2:5bab956cb59e | 395 | sci_write(SCI_VOL, volCalced); |
christi_s | 2:5bab956cb59e | 396 | |
christi_s | 2:5bab956cb59e | 397 | DEBUGOUT("VS1053b: Change volume to %#x (%f, Balance = %f)\r\n", volCalced, _volume, _balance); |
christi_s | 2:5bab956cb59e | 398 | } |
christi_s | 2:5bab956cb59e | 399 | |
christi_s | 2:5bab956cb59e | 400 | int VS1053::getTrebleFrequency(void) |
christi_s | 2:5bab956cb59e | 401 | { |
christi_s | 2:5bab956cb59e | 402 | return _st_freqlimit * 1000; |
christi_s | 2:5bab956cb59e | 403 | } |
christi_s | 2:5bab956cb59e | 404 | |
christi_s | 2:5bab956cb59e | 405 | |
christi_s | 2:5bab956cb59e | 406 | void VS1053::setTrebleFrequency(int frequency) |
christi_s | 2:5bab956cb59e | 407 | { |
christi_s | 2:5bab956cb59e | 408 | frequency /= 1000; |
christi_s | 2:5bab956cb59e | 409 | |
christi_s | 2:5bab956cb59e | 410 | if(frequency < 1) |
christi_s | 2:5bab956cb59e | 411 | { |
christi_s | 2:5bab956cb59e | 412 | frequency = 1; |
christi_s | 2:5bab956cb59e | 413 | } |
christi_s | 2:5bab956cb59e | 414 | else if(frequency > 15) |
christi_s | 2:5bab956cb59e | 415 | { |
christi_s | 2:5bab956cb59e | 416 | frequency = 15; |
christi_s | 2:5bab956cb59e | 417 | } |
christi_s | 2:5bab956cb59e | 418 | _st_freqlimit = frequency; |
christi_s | 2:5bab956cb59e | 419 | changeBass(); |
christi_s | 2:5bab956cb59e | 420 | } |
christi_s | 2:5bab956cb59e | 421 | |
christi_s | 2:5bab956cb59e | 422 | int VS1053::getTrebleAmplitude(void) |
christi_s | 2:5bab956cb59e | 423 | { |
christi_s | 2:5bab956cb59e | 424 | return _st_amplitude; |
christi_s | 2:5bab956cb59e | 425 | } |
christi_s | 2:5bab956cb59e | 426 | |
christi_s | 2:5bab956cb59e | 427 | void VS1053::setTrebleAmplitude(int amplitude) |
christi_s | 2:5bab956cb59e | 428 | { |
christi_s | 2:5bab956cb59e | 429 | if(amplitude < -8) |
christi_s | 2:5bab956cb59e | 430 | { |
christi_s | 2:5bab956cb59e | 431 | amplitude = -8; |
christi_s | 2:5bab956cb59e | 432 | } |
christi_s | 2:5bab956cb59e | 433 | else if(amplitude > 7) |
christi_s | 2:5bab956cb59e | 434 | { |
christi_s | 2:5bab956cb59e | 435 | amplitude = 7; |
christi_s | 2:5bab956cb59e | 436 | } |
christi_s | 2:5bab956cb59e | 437 | _st_amplitude = amplitude; |
christi_s | 2:5bab956cb59e | 438 | changeBass(); |
christi_s | 2:5bab956cb59e | 439 | } |
christi_s | 1:ced2c297cc1b | 440 | |
christi_s | 2:5bab956cb59e | 441 | int VS1053::getBassFrequency(void) |
christi_s | 2:5bab956cb59e | 442 | { |
christi_s | 2:5bab956cb59e | 443 | return _sb_freqlimit * 10; |
christi_s | 2:5bab956cb59e | 444 | } |
christi_s | 2:5bab956cb59e | 445 | |
christi_s | 2:5bab956cb59e | 446 | void VS1053::setBassFrequency(int frequency) |
christi_s | 2:5bab956cb59e | 447 | { |
christi_s | 2:5bab956cb59e | 448 | frequency /= 10; |
christi_s | 2:5bab956cb59e | 449 | |
christi_s | 2:5bab956cb59e | 450 | if(frequency < 2) |
christi_s | 2:5bab956cb59e | 451 | { |
christi_s | 2:5bab956cb59e | 452 | frequency = 2; |
christi_s | 2:5bab956cb59e | 453 | } |
christi_s | 2:5bab956cb59e | 454 | else if(frequency > 15) |
christi_s | 2:5bab956cb59e | 455 | { |
christi_s | 2:5bab956cb59e | 456 | frequency = 15; |
christi_s | 2:5bab956cb59e | 457 | } |
christi_s | 2:5bab956cb59e | 458 | _sb_freqlimit = frequency; |
christi_s | 2:5bab956cb59e | 459 | changeBass(); |
christi_s | 2:5bab956cb59e | 460 | } |
christi_s | 2:5bab956cb59e | 461 | |
christi_s | 2:5bab956cb59e | 462 | int VS1053::getBassAmplitude(void) |
christi_s | 2:5bab956cb59e | 463 | { |
christi_s | 2:5bab956cb59e | 464 | return _sb_amplitude; |
christi_s | 1:ced2c297cc1b | 465 | } |
christi_s | 2:5bab956cb59e | 466 | |
christi_s | 2:5bab956cb59e | 467 | void VS1053::setBassAmplitude(int amplitude) |
christi_s | 2:5bab956cb59e | 468 | { |
christi_s | 2:5bab956cb59e | 469 | if(amplitude < -15) |
christi_s | 2:5bab956cb59e | 470 | { |
christi_s | 2:5bab956cb59e | 471 | amplitude = -15; |
christi_s | 2:5bab956cb59e | 472 | } |
christi_s | 2:5bab956cb59e | 473 | else if(amplitude > 0) |
christi_s | 2:5bab956cb59e | 474 | { |
christi_s | 2:5bab956cb59e | 475 | amplitude = 0; |
christi_s | 2:5bab956cb59e | 476 | } |
christi_s | 2:5bab956cb59e | 477 | _sb_amplitude = amplitude; |
christi_s | 2:5bab956cb59e | 478 | changeBass(); |
christi_s | 2:5bab956cb59e | 479 | } |
christi_s | 2:5bab956cb59e | 480 | |
christi_s | 2:5bab956cb59e | 481 | void VS1053::changeBass(void) |
christi_s | 2:5bab956cb59e | 482 | { |
christi_s | 2:5bab956cb59e | 483 | unsigned short bassCalced = ((_st_amplitude & 0x0f) << 12) |
christi_s | 2:5bab956cb59e | 484 | | ((_st_freqlimit & 0x0f) << 8) |
christi_s | 2:5bab956cb59e | 485 | | ((_sb_amplitude & 0x0f) << 4) |
christi_s | 2:5bab956cb59e | 486 | | ((_sb_freqlimit & 0x0f) << 0); |
christi_s | 2:5bab956cb59e | 487 | |
christi_s | 2:5bab956cb59e | 488 | sci_write(SCI_BASS, bassCalced); |
christi_s | 2:5bab956cb59e | 489 | |
christi_s | 2:5bab956cb59e | 490 | DEBUGOUT("VS1053b: Change bass settings to:\r\n") |
christi_s | 2:5bab956cb59e | 491 | DEBUGOUT("VS1053b: --Treble: Amplitude=%i, Frequency=%i\r\n", getTrebleAmplitude(), getTrebleFrequency()); |
christi_s | 2:5bab956cb59e | 492 | DEBUGOUT("VS1053b: --Bass: Amplitude=%i, Frequency=%i\r\n", getBassAmplitude(), getBassFrequency()); |
christi_s | 2:5bab956cb59e | 493 | } |
christi_s | 4:cacb5e663fa9 | 494 | |
christi_s | 4:cacb5e663fa9 | 495 | /*=================================================================== |
christi_s | 4:cacb5e663fa9 | 496 | * Buffer handling |
christi_s | 4:cacb5e663fa9 | 497 | *==================================================================*/ |
christi_s | 8:5e4a21202223 | 498 | |
christi_s | 8:5e4a21202223 | 499 | unsigned int VS1053::bufferLength(void) |
christi_s | 8:5e4a21202223 | 500 | { |
christi_s | 8:5e4a21202223 | 501 | return BUFFER_SIZE; |
christi_s | 8:5e4a21202223 | 502 | } |
christi_s | 4:cacb5e663fa9 | 503 | |
christi_s | 4:cacb5e663fa9 | 504 | unsigned char VS1053::bufferGetByte(void) |
christi_s | 4:cacb5e663fa9 | 505 | { |
christi_s | 4:cacb5e663fa9 | 506 | unsigned char retVal = 0x00; |
christi_s | 4:cacb5e663fa9 | 507 | if (bufferCount() > 0x00) |
christi_s | 4:cacb5e663fa9 | 508 | { |
christi_s | 4:cacb5e663fa9 | 509 | retVal = *_bufferReadPointer++; |
christi_s | 4:cacb5e663fa9 | 510 | if (_bufferReadPointer >= _buffer + BUFFER_SIZE) |
christi_s | 4:cacb5e663fa9 | 511 | { |
christi_s | 4:cacb5e663fa9 | 512 | _bufferReadPointer = _buffer; |
christi_s | 4:cacb5e663fa9 | 513 | } |
christi_s | 4:cacb5e663fa9 | 514 | } |
christi_s | 4:cacb5e663fa9 | 515 | return retVal; |
christi_s | 4:cacb5e663fa9 | 516 | } |
christi_s | 4:cacb5e663fa9 | 517 | |
christi_s | 8:5e4a21202223 | 518 | bool VS1053::bufferSetByte(char c) |
christi_s | 4:cacb5e663fa9 | 519 | { |
christi_s | 4:cacb5e663fa9 | 520 | if (bufferFree() > 0x00) |
christi_s | 4:cacb5e663fa9 | 521 | { |
christi_s | 4:cacb5e663fa9 | 522 | *_bufferWritePointer++ = c; |
christi_s | 4:cacb5e663fa9 | 523 | if (_bufferWritePointer >= _buffer + BUFFER_SIZE) |
christi_s | 4:cacb5e663fa9 | 524 | { |
christi_s | 4:cacb5e663fa9 | 525 | _bufferWritePointer = _buffer; |
christi_s | 4:cacb5e663fa9 | 526 | } |
christi_s | 4:cacb5e663fa9 | 527 | return true; |
christi_s | 4:cacb5e663fa9 | 528 | } |
christi_s | 4:cacb5e663fa9 | 529 | return false; |
christi_s | 4:cacb5e663fa9 | 530 | } |
christi_s | 4:cacb5e663fa9 | 531 | |
christi_s | 8:5e4a21202223 | 532 | bool VS1053::bufferPutStream(const char *s, unsigned int length) |
christi_s | 4:cacb5e663fa9 | 533 | { |
christi_s | 4:cacb5e663fa9 | 534 | if (bufferFree() >= length) |
christi_s | 4:cacb5e663fa9 | 535 | { |
christi_s | 4:cacb5e663fa9 | 536 | while (length--) |
christi_s | 4:cacb5e663fa9 | 537 | { |
christi_s | 4:cacb5e663fa9 | 538 | *_bufferWritePointer++ = *s++; |
christi_s | 4:cacb5e663fa9 | 539 | if (_bufferWritePointer >= _buffer + BUFFER_SIZE) |
christi_s | 4:cacb5e663fa9 | 540 | { |
christi_s | 4:cacb5e663fa9 | 541 | _bufferWritePointer = _buffer; |
christi_s | 4:cacb5e663fa9 | 542 | } |
christi_s | 4:cacb5e663fa9 | 543 | } |
christi_s | 4:cacb5e663fa9 | 544 | return true; |
christi_s | 4:cacb5e663fa9 | 545 | } |
christi_s | 4:cacb5e663fa9 | 546 | return false; |
christi_s | 4:cacb5e663fa9 | 547 | } |
christi_s | 4:cacb5e663fa9 | 548 | |
christi_s | 4:cacb5e663fa9 | 549 | unsigned int VS1053::bufferFree(void) |
christi_s | 4:cacb5e663fa9 | 550 | { |
christi_s | 4:cacb5e663fa9 | 551 | if(_bufferReadPointer > _bufferWritePointer) |
christi_s | 4:cacb5e663fa9 | 552 | { |
christi_s | 4:cacb5e663fa9 | 553 | return _bufferReadPointer - _bufferWritePointer - 1; |
christi_s | 4:cacb5e663fa9 | 554 | } |
christi_s | 4:cacb5e663fa9 | 555 | else if(_bufferReadPointer < _bufferWritePointer) |
christi_s | 4:cacb5e663fa9 | 556 | { |
christi_s | 4:cacb5e663fa9 | 557 | return BUFFER_SIZE - (_bufferWritePointer - _bufferReadPointer) - 1; |
christi_s | 4:cacb5e663fa9 | 558 | } |
christi_s | 4:cacb5e663fa9 | 559 | return BUFFER_SIZE - 1; |
christi_s | 4:cacb5e663fa9 | 560 | } |
christi_s | 4:cacb5e663fa9 | 561 | |
christi_s | 4:cacb5e663fa9 | 562 | unsigned int VS1053::bufferCount(void) |
christi_s | 4:cacb5e663fa9 | 563 | { |
christi_s | 4:cacb5e663fa9 | 564 | return BUFFER_SIZE - bufferFree() - 1; |
christi_s | 4:cacb5e663fa9 | 565 | } |
christi_s | 4:cacb5e663fa9 | 566 | |
christi_s | 4:cacb5e663fa9 | 567 | void VS1053::bufferReset(void) |
christi_s | 4:cacb5e663fa9 | 568 | { |
christi_s | 4:cacb5e663fa9 | 569 | _bufferReadPointer = _buffer; |
christi_s | 4:cacb5e663fa9 | 570 | _bufferWritePointer = _buffer; |
christi_s | 4:cacb5e663fa9 | 571 | } |
christi_s | 4:cacb5e663fa9 | 572 | |
christi_s | 4:cacb5e663fa9 | 573 | |
christi_s | 4:cacb5e663fa9 | 574 | void VS1053::dataRequestHandler(void) |
christi_s | 4:cacb5e663fa9 | 575 | { |
christi_s | 4:cacb5e663fa9 | 576 | if (_isIdle && _DREQ) |
christi_s | 4:cacb5e663fa9 | 577 | { |
christi_s | 4:cacb5e663fa9 | 578 | _isIdle = false; |
christi_s | 4:cacb5e663fa9 | 579 | // write buffer to vs1053b |
christi_s | 4:cacb5e663fa9 | 580 | unsigned length = bufferCount(); |
christi_s | 4:cacb5e663fa9 | 581 | int i = 0; |
christi_s | 4:cacb5e663fa9 | 582 | sdi_en(); |
christi_s | 4:cacb5e663fa9 | 583 | |
christi_s | 4:cacb5e663fa9 | 584 | while (length > 0) |
christi_s | 4:cacb5e663fa9 | 585 | { |
christi_s | 4:cacb5e663fa9 | 586 | int l2 = (length > 32) ? 32 : length; |
christi_s | 4:cacb5e663fa9 | 587 | //DEBUGOUT("L2: %i\r\n", l2); |
christi_s | 4:cacb5e663fa9 | 588 | for( ; l2 != 0; l2--) |
christi_s | 4:cacb5e663fa9 | 589 | { |
christi_s | 4:cacb5e663fa9 | 590 | _spi.write(bufferGetByte()); |
christi_s | 4:cacb5e663fa9 | 591 | } |
christi_s | 4:cacb5e663fa9 | 592 | |
christi_s | 4:cacb5e663fa9 | 593 | length -= l2; |
christi_s | 4:cacb5e663fa9 | 594 | |
christi_s | 4:cacb5e663fa9 | 595 | if (!_DREQ || i > 4) |
christi_s | 4:cacb5e663fa9 | 596 | break; |
christi_s | 4:cacb5e663fa9 | 597 | i++; |
christi_s | 4:cacb5e663fa9 | 598 | } |
christi_s | 4:cacb5e663fa9 | 599 | |
christi_s | 4:cacb5e663fa9 | 600 | sdi_dis(); |
christi_s | 4:cacb5e663fa9 | 601 | |
christi_s | 4:cacb5e663fa9 | 602 | _isIdle = true; |
christi_s | 4:cacb5e663fa9 | 603 | } |
christi_s | 4:cacb5e663fa9 | 604 | } |
christi_s | 4:cacb5e663fa9 | 605 | |
christi_s | 4:cacb5e663fa9 | 606 | void VS1053::play(void) |
christi_s | 4:cacb5e663fa9 | 607 | { |
christi_s | 4:cacb5e663fa9 | 608 | INTERRUPT_HANDLER_ENABLE; |
christi_s | 4:cacb5e663fa9 | 609 | DEBUGOUT("VS1053b: Play.\r\n"); |
christi_s | 4:cacb5e663fa9 | 610 | } |
christi_s | 4:cacb5e663fa9 | 611 | |
christi_s | 4:cacb5e663fa9 | 612 | void VS1053::pause(void) |
christi_s | 4:cacb5e663fa9 | 613 | { |
christi_s | 4:cacb5e663fa9 | 614 | INTERRUPT_HANDLER_DISABLE; |
christi_s | 4:cacb5e663fa9 | 615 | DEBUGOUT("VS1053b: Pause.\r\n"); |
christi_s | 4:cacb5e663fa9 | 616 | } |
christi_s | 4:cacb5e663fa9 | 617 | |
christi_s | 4:cacb5e663fa9 | 618 | void VS1053::stop(void) |
christi_s | 4:cacb5e663fa9 | 619 | { |
christi_s | 4:cacb5e663fa9 | 620 | INTERRUPT_HANDLER_DISABLE; |
christi_s | 4:cacb5e663fa9 | 621 | __disable_irq(); |
christi_s | 4:cacb5e663fa9 | 622 | DEBUGOUT("VS1053b: Song stoping..\r\n"); |
christi_s | 4:cacb5e663fa9 | 623 | while(!_isIdle) |
christi_s | 4:cacb5e663fa9 | 624 | ; |
christi_s | 4:cacb5e663fa9 | 625 | |
christi_s | 4:cacb5e663fa9 | 626 | // set SCI MODE bit SM CANCEL |
christi_s | 4:cacb5e663fa9 | 627 | unsigned short sciModeByte = sci_read(SCI_MODE); |
christi_s | 4:cacb5e663fa9 | 628 | sciModeByte |= SM_CANCEL; |
christi_s | 4:cacb5e663fa9 | 629 | sci_write(SCI_MODE, sciModeByte); |
christi_s | 4:cacb5e663fa9 | 630 | |
christi_s | 4:cacb5e663fa9 | 631 | // send up 2048 bytes of audio data. |
christi_s | 4:cacb5e663fa9 | 632 | for (int i = 0; i < 64; i++) |
christi_s | 4:cacb5e663fa9 | 633 | { |
christi_s | 4:cacb5e663fa9 | 634 | // send at least 32 bytes of audio data |
christi_s | 4:cacb5e663fa9 | 635 | int z = bufferCount(); |
christi_s | 4:cacb5e663fa9 | 636 | if (z > 32) |
christi_s | 4:cacb5e663fa9 | 637 | z = 32; |
christi_s | 4:cacb5e663fa9 | 638 | for (int n = 0; n < z; n++) |
christi_s | 4:cacb5e663fa9 | 639 | { |
christi_s | 4:cacb5e663fa9 | 640 | _spi.write(bufferGetByte()); |
christi_s | 4:cacb5e663fa9 | 641 | } |
christi_s | 4:cacb5e663fa9 | 642 | // read SCI MODE; if SM CANCEL is still set, repeat |
christi_s | 4:cacb5e663fa9 | 643 | sciModeByte = sci_read(SCI_MODE); |
christi_s | 4:cacb5e663fa9 | 644 | if ((sciModeByte & SM_CANCEL) == 0x0000) |
christi_s | 4:cacb5e663fa9 | 645 | { |
christi_s | 4:cacb5e663fa9 | 646 | break; |
christi_s | 4:cacb5e663fa9 | 647 | } |
christi_s | 4:cacb5e663fa9 | 648 | } |
christi_s | 4:cacb5e663fa9 | 649 | |
christi_s | 4:cacb5e663fa9 | 650 | if ((sciModeByte & SM_CANCEL) == 0x0000) |
christi_s | 4:cacb5e663fa9 | 651 | { |
christi_s | 4:cacb5e663fa9 | 652 | // send at least 2052 bytes of endFillByte[7:0]. |
christi_s | 4:cacb5e663fa9 | 653 | // read endFillByte (0 .. 15) from wram |
christi_s | 4:cacb5e663fa9 | 654 | unsigned short endFillByte=wram_read(para_endFillByte); |
christi_s | 4:cacb5e663fa9 | 655 | // clear endFillByte (8 .. 15) |
christi_s | 4:cacb5e663fa9 | 656 | endFillByte = endFillByte ^0x00FF; |
christi_s | 4:cacb5e663fa9 | 657 | for (int n = 0; n < 2052; n++) |
christi_s | 4:cacb5e663fa9 | 658 | sdi_write(endFillByte); |
christi_s | 4:cacb5e663fa9 | 659 | DEBUGOUT("VS1053b: Song sucessfully stopped.\r\n"); |
christi_s | 4:cacb5e663fa9 | 660 | DEBUGOUT("VS1053b: SCI MODE = %#x, SM_CANCEL = %#x\r\n", sciModeByte, sciModeByte & SM_CANCEL); |
christi_s | 6:eed008905159 | 661 | sci_write(SCI_DECODE_TIME, 0x0000); |
christi_s | 4:cacb5e663fa9 | 662 | } |
christi_s | 4:cacb5e663fa9 | 663 | else |
christi_s | 4:cacb5e663fa9 | 664 | { |
christi_s | 4:cacb5e663fa9 | 665 | DEBUGOUT("VS1053b: SM CANCEL hasn't cleared after sending 2048 bytes, do software reset\r\n"); |
christi_s | 6:eed008905159 | 666 | DEBUGOUT("VS1053b: SCI MODE = %#x, SM_CANCEL = %#x\r\n", sciModeByte, sciModeByte & SM_CANCEL); |
christi_s | 4:cacb5e663fa9 | 667 | initialize(); |
christi_s | 4:cacb5e663fa9 | 668 | } |
christi_s | 4:cacb5e663fa9 | 669 | |
christi_s | 4:cacb5e663fa9 | 670 | bufferReset(); |
christi_s | 4:cacb5e663fa9 | 671 | __enable_irq(); |
christi_s | 6:eed008905159 | 672 | } |
christi_s | 6:eed008905159 | 673 | |
christi_s | 6:eed008905159 | 674 | void VS1053::getAudioInfo(AudioInfo* aInfo) |
christi_s | 6:eed008905159 | 675 | { |
christi_s | 6:eed008905159 | 676 | // volume calculation |
christi_s | 6:eed008905159 | 677 | unsigned short hdat0 = sci_read(SCI_HDAT0); |
christi_s | 6:eed008905159 | 678 | unsigned short hdat1 = sci_read(SCI_HDAT1); |
christi_s | 6:eed008905159 | 679 | |
christi_s | 6:eed008905159 | 680 | DEBUGOUT("VS1053b: Audio info\r\n"); |
christi_s | 6:eed008905159 | 681 | |
christi_s | 6:eed008905159 | 682 | AudioInfo* retVal = aInfo; |
christi_s | 6:eed008905159 | 683 | retVal->type = UNKNOWN; |
christi_s | 6:eed008905159 | 684 | |
christi_s | 6:eed008905159 | 685 | if (hdat1 == 0x7665) |
christi_s | 6:eed008905159 | 686 | { |
christi_s | 6:eed008905159 | 687 | // audio is WAV |
christi_s | 6:eed008905159 | 688 | retVal->type = WAV; |
christi_s | 6:eed008905159 | 689 | } |
christi_s | 6:eed008905159 | 690 | else if (hdat1 == 0x4154 || hdat1 == 0x4144 || hdat1 == 0x4D34 ) |
christi_s | 6:eed008905159 | 691 | { |
christi_s | 6:eed008905159 | 692 | // audio is AAC |
christi_s | 6:eed008905159 | 693 | retVal->type = AAC; |
christi_s | 6:eed008905159 | 694 | } |
christi_s | 6:eed008905159 | 695 | else if (hdat1 == 0x574D ) |
christi_s | 6:eed008905159 | 696 | { |
christi_s | 6:eed008905159 | 697 | // audio is WMA |
christi_s | 6:eed008905159 | 698 | retVal->type = WMA; |
christi_s | 6:eed008905159 | 699 | } |
christi_s | 6:eed008905159 | 700 | else if (hdat1 == 0x4D54 ) |
christi_s | 6:eed008905159 | 701 | { |
christi_s | 6:eed008905159 | 702 | // audio is MIDI |
christi_s | 6:eed008905159 | 703 | retVal->type = MIDI; |
christi_s | 6:eed008905159 | 704 | } |
christi_s | 6:eed008905159 | 705 | else if (hdat1 == 0x4F76 ) |
christi_s | 6:eed008905159 | 706 | { |
christi_s | 6:eed008905159 | 707 | // audio is OGG VORBIS |
christi_s | 6:eed008905159 | 708 | retVal->type = OGG_VORBIS; |
christi_s | 6:eed008905159 | 709 | } |
christi_s | 6:eed008905159 | 710 | else if (hdat1 >= 0xFFE0 && hdat1 <= 0xFFFF) |
christi_s | 6:eed008905159 | 711 | { |
christi_s | 6:eed008905159 | 712 | // audio is mp3 |
christi_s | 6:eed008905159 | 713 | retVal->type = MP3; |
christi_s | 6:eed008905159 | 714 | |
christi_s | 6:eed008905159 | 715 | DEBUGOUT("VS1053b: Audio is mp3\r\n"); |
christi_s | 6:eed008905159 | 716 | retVal->ext.mp3.id = (MP3_ID)((hdat1 >> 3) & 0x0003); |
christi_s | 6:eed008905159 | 717 | switch((hdat1 >> 1) & 0x0003) |
christi_s | 6:eed008905159 | 718 | { |
christi_s | 6:eed008905159 | 719 | case 3: |
christi_s | 6:eed008905159 | 720 | retVal->ext.mp3.layer = 1; |
christi_s | 6:eed008905159 | 721 | break; |
christi_s | 6:eed008905159 | 722 | case 2: |
christi_s | 6:eed008905159 | 723 | retVal->ext.mp3.layer = 2; |
christi_s | 6:eed008905159 | 724 | break; |
christi_s | 6:eed008905159 | 725 | case 1: |
christi_s | 6:eed008905159 | 726 | retVal->ext.mp3.layer = 3; |
christi_s | 6:eed008905159 | 727 | break; |
christi_s | 6:eed008905159 | 728 | default: |
christi_s | 6:eed008905159 | 729 | retVal->ext.mp3.layer = 0; |
christi_s | 6:eed008905159 | 730 | break; |
christi_s | 6:eed008905159 | 731 | } |
christi_s | 6:eed008905159 | 732 | retVal->ext.mp3.protrectBit = (hdat1 >> 0) & 0x0001; |
christi_s | 6:eed008905159 | 733 | |
christi_s | 6:eed008905159 | 734 | char srate = (hdat0 >> 10) & 0x0003; |
christi_s | 6:eed008905159 | 735 | retVal->ext.mp3.kSampleRate = _sampleRateTable[retVal->ext.mp3.id][srate]; |
christi_s | 6:eed008905159 | 736 | |
christi_s | 6:eed008905159 | 737 | retVal->ext.mp3.padBit = (hdat0 >> 9) & 0x0001; |
christi_s | 6:eed008905159 | 738 | retVal->ext.mp3.mode =(MP3_MODE)((hdat0 >> 6) & 0x0003); |
christi_s | 6:eed008905159 | 739 | retVal->ext.mp3.extension = (hdat0 >> 4) & 0x0003; |
christi_s | 6:eed008905159 | 740 | retVal->ext.mp3.copyright = (hdat0 >> 3) & 0x0001; |
christi_s | 6:eed008905159 | 741 | retVal->ext.mp3.original = (hdat0 >> 2) & 0x0001; |
christi_s | 6:eed008905159 | 742 | retVal->ext.mp3.emphasis = (hdat0 >> 0) & 0x0003; |
christi_s | 6:eed008905159 | 743 | |
christi_s | 6:eed008905159 | 744 | DEBUGOUT("VS1053b: ID: %i, Layer: %i, Samplerate: %i, Mode: %i\r\n", retVal->ext.mp3.id, retVal->ext.mp3.layer, retVal->ext.mp3.kSampleRate, retVal->ext.mp3.mode); |
christi_s | 6:eed008905159 | 745 | } |
christi_s | 6:eed008905159 | 746 | |
christi_s | 6:eed008905159 | 747 | // read byteRate |
christi_s | 6:eed008905159 | 748 | unsigned short byteRate = wram_read(para_byteRate); |
christi_s | 6:eed008905159 | 749 | retVal->kBitRate = (byteRate * 8) / 1000; |
christi_s | 6:eed008905159 | 750 | DEBUGOUT("VS1053b: BitRate: %i kBit/s\r\n", retVal->kBitRate); |
christi_s | 6:eed008905159 | 751 | |
christi_s | 6:eed008905159 | 752 | // decode time |
christi_s | 6:eed008905159 | 753 | retVal->decodeTime = sci_read(SCI_DECODE_TIME); |
christi_s | 6:eed008905159 | 754 | DEBUGOUT("VS1053b: Decodetime: %i s\r\n", retVal->decodeTime); |
christi_s | 6:eed008905159 | 755 | |
christi_s | 8:5e4a21202223 | 756 | } |