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