Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Lab3_1 RTOS-VS1053b-mp3_semaphore RTOS-VS1053b-mp3_v01
VS1053.h@1:ced2c297cc1b, 2010-12-16 (annotated)
- Committer:
- christi_s
- Date:
- Thu Dec 16 21:54:03 2010 +0000
- Revision:
- 1:ced2c297cc1b
- Parent:
- 0:7728d9c6c487
- Child:
- 2:5bab956cb59e
add volume and balance control
add last patches, apps from vlsi
fix spi timing problems
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 | #ifndef VS1053_H |
| christi_s | 0:7728d9c6c487 | 30 | #define VS1053_H |
| christi_s | 0:7728d9c6c487 | 31 | |
| christi_s | 1:ced2c297cc1b | 32 | |
| christi_s | 0:7728d9c6c487 | 33 | // ---------------------------------------------------------------------------- |
| christi_s | 0:7728d9c6c487 | 34 | // Extended settings |
| christi_s | 0:7728d9c6c487 | 35 | // ---------------------------------------------------------------------------- |
| christi_s | 1:ced2c297cc1b | 36 | // Enable debug output (Output -> printf ...) |
| christi_s | 1:ced2c297cc1b | 37 | // -------------------------------------------------------------------------- |
| christi_s | 1:ced2c297cc1b | 38 | #define DEBUG |
| christi_s | 0:7728d9c6c487 | 39 | // Patches, Addons |
| christi_s | 0:7728d9c6c487 | 40 | // -------------------------------------------------------------------------- |
| christi_s | 0:7728d9c6c487 | 41 | //#define VS1053_PATCH_1_4_FLAC |
| christi_s | 1:ced2c297cc1b | 42 | //#define VS1053_PATCH_1_5 |
| christi_s | 1:ced2c297cc1b | 43 | #define VS1053_PATCH_1_5_FLAC |
| christi_s | 1:ced2c297cc1b | 44 | //#define VS1053_SPECANA |
| christi_s | 1:ced2c297cc1b | 45 | //#define VS1053B_PCM_RECORDER_0_9 |
| christi_s | 0:7728d9c6c487 | 46 | // ---------------------------------------------------------------------------- |
| christi_s | 0:7728d9c6c487 | 47 | |
| christi_s | 1:ced2c297cc1b | 48 | #if defined(VS1053_PATCH_1_4_FLAC) && defined(VS1053_PATCH_1_5) && defined(VS1053_PATCH_1_5_FLAC) && defined(VS1053_SPECANA) && defined(VS1053B_PCM_RECORDER_0_9) |
| christi_s | 1:ced2c297cc1b | 49 | #error "VS1053: Exclusive use of patch and app versions." |
| christi_s | 1:ced2c297cc1b | 50 | #endif |
| christi_s | 1:ced2c297cc1b | 51 | #if defined(VS1053_PATCH_1_4_FLAC) || defined(VS1053_PATCH_1_5) || defined(VS1053_PATCH_1_5_FLAC) || defined(VS1053_SPECANA) || defined(VS1053B_PCM_RECORDER_0_9) |
| christi_s | 1:ced2c297cc1b | 52 | #define VS_PATCH |
| christi_s | 0:7728d9c6c487 | 53 | #endif |
| christi_s | 0:7728d9c6c487 | 54 | |
| christi_s | 1:ced2c297cc1b | 55 | #include "mbed.h" |
| christi_s | 0:7728d9c6c487 | 56 | |
| christi_s | 1:ced2c297cc1b | 57 | #define DEFAULT_BALANCE_DIFERENCE_LEFT_RIGHT 0.0f |
| christi_s | 1:ced2c297cc1b | 58 | #define DEFAULT_VOLUME -40.0f |
| christi_s | 0:7728d9c6c487 | 59 | |
| christi_s | 0:7728d9c6c487 | 60 | // SCI register address assignment |
| christi_s | 0:7728d9c6c487 | 61 | #define SCI_MODE 0x0 |
| christi_s | 0:7728d9c6c487 | 62 | #define SCI_STATUS 0x1 |
| christi_s | 0:7728d9c6c487 | 63 | #define SCI_BASS 0x2 |
| christi_s | 0:7728d9c6c487 | 64 | #define SCI_CLOCKF 0x3 |
| christi_s | 0:7728d9c6c487 | 65 | #define SCI_DECODE_TOME 0x4 |
| christi_s | 0:7728d9c6c487 | 66 | #define SCI_AUDATA 0x5 |
| christi_s | 0:7728d9c6c487 | 67 | #define SCI_WRAM 0x6 |
| christi_s | 0:7728d9c6c487 | 68 | #define SCI_WRAMADDR 0x7 |
| christi_s | 0:7728d9c6c487 | 69 | #define SCI_HDAT0 0x8 |
| christi_s | 0:7728d9c6c487 | 70 | #define SCI_HDAT1 0x9 |
| christi_s | 0:7728d9c6c487 | 71 | #define SCI_AIADDR 0xA |
| christi_s | 0:7728d9c6c487 | 72 | #define SCI_VOL 0xB |
| christi_s | 0:7728d9c6c487 | 73 | #define SCI_AICTRL0 0xC |
| christi_s | 0:7728d9c6c487 | 74 | #define SCI_AICTRL1 0xD |
| christi_s | 0:7728d9c6c487 | 75 | #define SCI_AICTRL2 0xE |
| christi_s | 0:7728d9c6c487 | 76 | #define SCI_AICTRL3 0xF |
| christi_s | 0:7728d9c6c487 | 77 | |
| christi_s | 0:7728d9c6c487 | 78 | |
| christi_s | 0:7728d9c6c487 | 79 | //SCI_MODE register bits as of p.38 of the datasheet |
| christi_s | 0:7728d9c6c487 | 80 | #define SM_DIFF 0x0001 |
| christi_s | 0:7728d9c6c487 | 81 | #define SM_LAYER12 0x0002 |
| christi_s | 0:7728d9c6c487 | 82 | #define SM_RESET 0x0004 |
| christi_s | 0:7728d9c6c487 | 83 | #define SM_CANCEL 0x0008 |
| christi_s | 0:7728d9c6c487 | 84 | #define SM_EARSPEAKER_LO 0x0010 |
| christi_s | 0:7728d9c6c487 | 85 | #define SM_TESTS 0x0020 |
| christi_s | 0:7728d9c6c487 | 86 | #define SM_STREAM 0x0040 |
| christi_s | 0:7728d9c6c487 | 87 | #define SM_EARSPEAKER_HI 0x0080 |
| christi_s | 0:7728d9c6c487 | 88 | #define SM_DACT 0x0100 |
| christi_s | 0:7728d9c6c487 | 89 | #define SM_SDIORD 0x0200 |
| christi_s | 0:7728d9c6c487 | 90 | #define SM_SDISHARE 0x0400 |
| christi_s | 0:7728d9c6c487 | 91 | #define SM_SDINEW 0x0800 |
| christi_s | 0:7728d9c6c487 | 92 | #define SM_ADPCM 0x1000 |
| christi_s | 0:7728d9c6c487 | 93 | #define SM_B13 0x2000 |
| christi_s | 0:7728d9c6c487 | 94 | #define SM_LINE1 0x4000 |
| christi_s | 0:7728d9c6c487 | 95 | #define SM_CLK_RANGE 0x8000 |
| christi_s | 0:7728d9c6c487 | 96 | |
| christi_s | 0:7728d9c6c487 | 97 | //SCI_CLOCKF register bits as of p.42 of the datasheet |
| christi_s | 0:7728d9c6c487 | 98 | #define SC_MULT_XTALI 0x0000 |
| christi_s | 0:7728d9c6c487 | 99 | #define SC_MULT_XTALIx20 0x2000 |
| christi_s | 0:7728d9c6c487 | 100 | #define SC_MULT_XTALIx25 0x4000 |
| christi_s | 0:7728d9c6c487 | 101 | #define SC_MULT_XTALIx30 0x6000 |
| christi_s | 0:7728d9c6c487 | 102 | #define SC_MULT_XTALIx35 0x8000 |
| christi_s | 0:7728d9c6c487 | 103 | #define SC_MULT_XTALIx40 0xA000 |
| christi_s | 0:7728d9c6c487 | 104 | #define SC_MULT_XTALIx45 0xC000 |
| christi_s | 0:7728d9c6c487 | 105 | #define SC_MULT_XTALIx50 0xE000 |
| christi_s | 0:7728d9c6c487 | 106 | // |
| christi_s | 0:7728d9c6c487 | 107 | #define SC_ADD_NOMOD 0x0000 |
| christi_s | 0:7728d9c6c487 | 108 | #define SC_ADD_10x 0x0800 |
| christi_s | 0:7728d9c6c487 | 109 | #define SC_ADD_15x 0x1000 |
| christi_s | 0:7728d9c6c487 | 110 | #define SC_ADD_20x 0x1800 |
| christi_s | 0:7728d9c6c487 | 111 | |
| christi_s | 0:7728d9c6c487 | 112 | |
| christi_s | 0:7728d9c6c487 | 113 | // Extra Parameter in X memory (refer to p.58 of the datasheet) |
| christi_s | 0:7728d9c6c487 | 114 | #define para_chipID_0 0x1E00 |
| christi_s | 0:7728d9c6c487 | 115 | #define para_chipID_1 0x1E01 |
| christi_s | 0:7728d9c6c487 | 116 | #define para_version 0x1E02 |
| christi_s | 0:7728d9c6c487 | 117 | #define para_config1 0x1E03 |
| christi_s | 0:7728d9c6c487 | 118 | #define para_playSpeed 0x1E04 |
| christi_s | 0:7728d9c6c487 | 119 | #define para_byteRate 0x1E05 |
| christi_s | 0:7728d9c6c487 | 120 | #define para_endFillByte 0x1E06 |
| christi_s | 0:7728d9c6c487 | 121 | // |
| christi_s | 0:7728d9c6c487 | 122 | #define para_positionMsec_0 0x1E27 |
| christi_s | 0:7728d9c6c487 | 123 | #define para_positionMsec_1 0x1E28 |
| christi_s | 0:7728d9c6c487 | 124 | #define para_resync 0x1E29 |
| christi_s | 0:7728d9c6c487 | 125 | |
| christi_s | 0:7728d9c6c487 | 126 | |
| christi_s | 1:ced2c297cc1b | 127 | /** Class for VS1053 - Ogg Vorbis / MP3 / AAC / WMA / FLAC / MIDI Audio Codec Chip |
| christi_s | 1:ced2c297cc1b | 128 | * |
| christi_s | 1:ced2c297cc1b | 129 | * |
| christi_s | 1:ced2c297cc1b | 130 | */ |
| christi_s | 0:7728d9c6c487 | 131 | class VS1053 { |
| christi_s | 0:7728d9c6c487 | 132 | |
| christi_s | 0:7728d9c6c487 | 133 | public: |
| christi_s | 1:ced2c297cc1b | 134 | /** Create a vs1053b object. |
| christi_s | 1:ced2c297cc1b | 135 | * |
| christi_s | 1:ced2c297cc1b | 136 | * @param mosi SPI Master Out, Slave In pin to vs1053b |
| christi_s | 1:ced2c297cc1b | 137 | * @param miso SPI Master In, Slave Out pin to vs1053b |
| christi_s | 1:ced2c297cc1b | 138 | * @param sck SPI Clock pin to vs1053b |
| christi_s | 1:ced2c297cc1b | 139 | * @param cs Pin to vs1053b control chip select |
| christi_s | 1:ced2c297cc1b | 140 | * @param rst Pin to vs1053b reset |
| christi_s | 1:ced2c297cc1b | 141 | * @param dreq Pin to vs1053b data request |
| christi_s | 1:ced2c297cc1b | 142 | * @param dcs Pin to vs1053b data chip select |
| christi_s | 1:ced2c297cc1b | 143 | */ |
| christi_s | 0:7728d9c6c487 | 144 | VS1053( |
| christi_s | 1:ced2c297cc1b | 145 | PinName mosi, |
| christi_s | 1:ced2c297cc1b | 146 | PinName miso, |
| christi_s | 1:ced2c297cc1b | 147 | PinName sck, |
| christi_s | 1:ced2c297cc1b | 148 | PinName cs, |
| christi_s | 1:ced2c297cc1b | 149 | PinName rst, |
| christi_s | 1:ced2c297cc1b | 150 | PinName dreq, |
| christi_s | 1:ced2c297cc1b | 151 | PinName dcs |
| christi_s | 0:7728d9c6c487 | 152 | ); |
| christi_s | 0:7728d9c6c487 | 153 | |
| christi_s | 1:ced2c297cc1b | 154 | /** Reset the vs1053b. (hardware reset) |
| christi_s | 1:ced2c297cc1b | 155 | * |
| christi_s | 1:ced2c297cc1b | 156 | */ |
| christi_s | 1:ced2c297cc1b | 157 | void reset(void); |
| christi_s | 1:ced2c297cc1b | 158 | |
| christi_s | 1:ced2c297cc1b | 159 | void power_down(void); |
| christi_s | 1:ced2c297cc1b | 160 | |
| christi_s | 1:ced2c297cc1b | 161 | /** Write data stream to vs1053b |
| christi_s | 1:ced2c297cc1b | 162 | * |
| christi_s | 1:ced2c297cc1b | 163 | * @param data Data to be sent to vs1053b |
| christi_s | 1:ced2c297cc1b | 164 | * @param length Length of data |
| christi_s | 1:ced2c297cc1b | 165 | */ |
| christi_s | 1:ced2c297cc1b | 166 | void writeStream(unsigned char* data, unsigned int length); |
| christi_s | 1:ced2c297cc1b | 167 | |
| christi_s | 1:ced2c297cc1b | 168 | #if 0 |
| christi_s | 1:ced2c297cc1b | 169 | void putcStream(unsigned char); |
| christi_s | 1:ced2c297cc1b | 170 | #endif |
| christi_s | 1:ced2c297cc1b | 171 | //void stop(void); |
| christi_s | 1:ced2c297cc1b | 172 | void terminateStream(void); |
| christi_s | 1:ced2c297cc1b | 173 | |
| christi_s | 1:ced2c297cc1b | 174 | /** Initialize the vs1053b device |
| christi_s | 1:ced2c297cc1b | 175 | * |
| christi_s | 1:ced2c297cc1b | 176 | * @return |
| christi_s | 1:ced2c297cc1b | 177 | */ |
| christi_s | 1:ced2c297cc1b | 178 | bool initialize(void); |
| christi_s | 1:ced2c297cc1b | 179 | |
| christi_s | 1:ced2c297cc1b | 180 | void setVolume(float); |
| christi_s | 1:ced2c297cc1b | 181 | float getVolume(); |
| christi_s | 1:ced2c297cc1b | 182 | |
| christi_s | 1:ced2c297cc1b | 183 | void setBalance(float); |
| christi_s | 1:ced2c297cc1b | 184 | float getBalance(); |
| christi_s | 1:ced2c297cc1b | 185 | |
| christi_s | 1:ced2c297cc1b | 186 | |
| christi_s | 1:ced2c297cc1b | 187 | void setPlaySpeed(unsigned short speed); |
| christi_s | 1:ced2c297cc1b | 188 | |
| christi_s | 1:ced2c297cc1b | 189 | protected: |
| christi_s | 1:ced2c297cc1b | 190 | unsigned short int wram_read(unsigned short int); |
| christi_s | 1:ced2c297cc1b | 191 | void wram_write(unsigned short int, unsigned short int); |
| christi_s | 1:ced2c297cc1b | 192 | void write_plugin(const unsigned short* , unsigned int); |
| christi_s | 0:7728d9c6c487 | 193 | void cs_low(void); |
| christi_s | 0:7728d9c6c487 | 194 | void cs_high(void); |
| christi_s | 0:7728d9c6c487 | 195 | void dcs_low(void); |
| christi_s | 0:7728d9c6c487 | 196 | void dcs_high(void); |
| christi_s | 0:7728d9c6c487 | 197 | void sci_en(void); |
| christi_s | 0:7728d9c6c487 | 198 | void sci_dis(void); |
| christi_s | 0:7728d9c6c487 | 199 | void sdi_en(void); |
| christi_s | 0:7728d9c6c487 | 200 | void sdi_dis(void); |
| christi_s | 0:7728d9c6c487 | 201 | |
| christi_s | 1:ced2c297cc1b | 202 | void spi_initialise(void); |
| christi_s | 1:ced2c297cc1b | 203 | |
| christi_s | 0:7728d9c6c487 | 204 | void sdi_initialise(void); |
| christi_s | 0:7728d9c6c487 | 205 | |
| christi_s | 0:7728d9c6c487 | 206 | void sci_write(unsigned char, unsigned short int); |
| christi_s | 0:7728d9c6c487 | 207 | void sdi_write(unsigned char); |
| christi_s | 0:7728d9c6c487 | 208 | unsigned short int sci_read(unsigned short int); |
| christi_s | 0:7728d9c6c487 | 209 | void sine_test_activate(unsigned char); |
| christi_s | 0:7728d9c6c487 | 210 | |
| christi_s | 0:7728d9c6c487 | 211 | void sine_test_deactivate(void); |
| christi_s | 0:7728d9c6c487 | 212 | |
| christi_s | 1:ced2c297cc1b | 213 | void changeVolume(void); |
| christi_s | 1:ced2c297cc1b | 214 | |
| christi_s | 1:ced2c297cc1b | 215 | SPI _spi; |
| christi_s | 1:ced2c297cc1b | 216 | DigitalOut _CS; |
| christi_s | 1:ced2c297cc1b | 217 | DigitalOut _DCS; |
| christi_s | 0:7728d9c6c487 | 218 | DigitalIn _DREQ; |
| christi_s | 0:7728d9c6c487 | 219 | DigitalOut _RST; |
| christi_s | 1:ced2c297cc1b | 220 | |
| christi_s | 1:ced2c297cc1b | 221 | // variables to save volume, values in db |
| christi_s | 1:ced2c297cc1b | 222 | float _balance; |
| christi_s | 1:ced2c297cc1b | 223 | float _volume; |
| christi_s | 0:7728d9c6c487 | 224 | |
| christi_s | 0:7728d9c6c487 | 225 | }; |
| christi_s | 0:7728d9c6c487 | 226 | #endif |