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.
Fork of VS1053b by
VS1053.h
00001 /* mbed VLSI VS1053b library 00002 * Copyright (c) 2010 Christian Schmiljun 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 * THE SOFTWARE. 00021 */ 00022 00023 /* This code based on: 00024 * mbeduino_MP3_Shield_MP3Player 00025 * http://mbed.org/users/xshige/programs/mbeduino_MP3_Shield_MP3Player/lgcx63 00026 * 2010-10-16 00027 */ 00028 00029 #ifndef _VS1053_H 00030 #define _VS1053_H 00031 00032 // ---------------------------------------------------------------------------- 00033 // Extended settings 00034 // ---------------------------------------------------------------------------- 00035 // Enable debug output (Output -> printf ...) 00036 // -------------------------------------------------------------------------- 00037 // #define DEBUG 00038 // #define DEBUGOUT (x,y...) printf(x, ##y); 00039 // Patches, Addons 00040 // -------------------------------------------------------------------------- 00041 // #define VS1053_PATCH_1_4_FLAC 00042 // #define VS1053_PATCH_1_5 00043 // #define VS1053_PATCH_1_5_FLAC 00044 // #define VS1053_SPECANA 00045 // #define VS1053B_PCM_RECORDER_0_9 00046 // ---------------------------------------------------------------------------- 00047 00048 #include "defines.h" 00049 #include "mbed.h" 00050 00051 #ifdef VS1053_RTOS 00052 #include "rtos.h" 00053 #define START_THREAD 1 00054 #endif 00055 00056 #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) 00057 #error "VS1053: Exclusive use of patch and app versions." 00058 #endif 00059 #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) 00060 #define VS_PATCH 00061 #endif 00062 00063 #ifndef DEBUGOUT 00064 #define DEBUGOUT(x,y...) 00065 #endif 00066 00067 00068 #define DEFAULT_BALANCE_DIFERENCE_LEFT_RIGHT 0.0f 00069 #define DEFAULT_VOLUME -40.0f 00070 #define DEFAULT_BASS_AMPLITUDE 5 // 0 - 15 dB 00071 #define DEFAULT_BASS_FREQUENCY 100 // 20 - 150 Hz 00072 #define DEFAULT_TREBLE_AMPLITUDE 0 // -8 - 7 dB 00073 #define DEFAULT_TREBLE_FREQUENCY 15000 //1000 - 15000 Hz 00074 00075 00076 00077 // SCI register address assignment 00078 #define SCI_MODE 0x00 00079 #define SCI_STATUS 0x01 00080 #define SCI_BASS 0x02 00081 #define SCI_CLOCKF 0x03 00082 #define SCI_DECODE_TIME 0x04 00083 #define SCI_AUDATA 0x05 00084 #define SCI_WRAM 0x06 00085 #define SCI_WRAMADDR 0x07 00086 #define SCI_HDAT0 0x08 00087 #define SCI_HDAT1 0x09 00088 #define SCI_AIADDR 0x0A 00089 #define SCI_VOL 0x0B 00090 #define SCI_AICTRL0 0x0C 00091 #define SCI_AICTRL1 0x0D 00092 #define SCI_AICTRL2 0x0E 00093 #define SCI_AICTRL3 0x0F 00094 00095 00096 //SCI_MODE register bits as of p.38 of the datasheet 00097 #define SM_DIFF 0x0001 00098 #define SM_LAYER12 0x0002 00099 #define SM_RESET 0x0004 00100 #define SM_CANCEL 0x0008 00101 #define SM_EARSPEAKER_LO 0x0010 00102 #define SM_TESTS 0x0020 00103 #define SM_STREAM 0x0040 00104 #define SM_EARSPEAKER_HI 0x0080 00105 #define SM_DACT 0x0100 00106 #define SM_SDIORD 0x0200 00107 #define SM_SDISHARE 0x0400 00108 #define SM_SDINEW 0x0800 00109 #define SM_ADPCM 0x1000 00110 #define SM_B13 0x2000 00111 #define SM_LINE1 0x4000 00112 #define SM_CLK_RANGE 0x8000 00113 00114 //SCI_CLOCKF register bits as of p.42 of the datasheet 00115 #define SC_ADD_NOMOD 0x0000 00116 #define SC_ADD_10x 0x0800 00117 #define SC_ADD_15x 0x1000 00118 #define SC_ADD_20x 0x1800 00119 #define SC_MULT_XTALI 0x0000 00120 #define SC_MULT_XTALIx20 0x2000 00121 #define SC_MULT_XTALIx25 0x4000 00122 #define SC_MULT_XTALIx30 0x6000 00123 #define SC_MULT_XTALIx35 0x8000 00124 #define SC_MULT_XTALIx40 0xA000 00125 #define SC_MULT_XTALIx45 0xC000 00126 #define SC_MULT_XTALIx50 0xE000 00127 00128 // Extra Parameter in X memory (refer to p.58 of the datasheet) 00129 #define para_chipID_0 0x1E00 00130 #define para_chipID_1 0x1E01 00131 #define para_version 0x1E02 00132 #define para_config1 0x1E03 00133 #define para_playSpeed 0x1E04 00134 #define para_byteRate 0x1E05 00135 #define para_endFillByte 0x1E06 00136 // 00137 #define para_positionMsec_0 0x1E27 00138 #define para_positionMsec_1 0x1E28 00139 #define para_resync 0x1E29 00140 00141 #ifdef VS1053_RTOS 00142 #define INTERRUPT_HANDLER_ENABLE _isPlay = true 00143 #define INTERRUPT_HANDLER_DISABLE _isPlay = false 00144 #else 00145 #define INTERRUPT_HANDLER_ENABLE _DREQ_INTERUPT_IN.rise(this, &VS1053::dataRequestHandler); timer.attach_us(this, &VS1053::dataRequestHandler, 1000) 00146 #define INTERRUPT_HANDLER_DISABLE _DREQ_INTERUPT_IN.rise(NULL); timer.detach() 00147 #endif 00148 00149 00150 /** Types of audio streams 00151 * 00152 */ 00153 enum AudioType 00154 { 00155 WAV, /*!< WAVE audio stream */ 00156 AAC, /*!< AAC audio stream (ADTS (.aac), MPEG2 ADIF (.aac) and MPEG4 AUDIO (.mp4 / .m4a / .3gp / .3g2)) */ 00157 WMA, /*!< Windows Media Audio (WMA) stream */ 00158 MIDI, /*!< Midi audio stream */ 00159 OGG_VORBIS, /*!< Ogg Vorbis audio stream */ 00160 MP3, /*!< MPEG Audio Layer */ 00161 UNKNOWN /*!< Unknown */ 00162 }; 00163 00164 typedef enum AudioType AudioType; 00165 00166 /** Types of MPEG Audio Layer stream IDs. 00167 * 00168 */ 00169 enum MP3_ID 00170 { 00171 MPG2_5a = 0, /*!< MPG 2.5, nonstandard, proprietary */ 00172 MPG2_5b = 1, /*!< MPG 2.5, nonstandard, proprietary */ 00173 MPG2_0 = 2, /*!< ISO 13818-3 MPG 2.0 */ 00174 MPG1_0 = 3 /*!< ISO 11172-3 MPG 1.0 */ 00175 }; 00176 00177 typedef enum MP3_ID MP3_ID; 00178 00179 /** Types of MPEG Audio Layer channel modes. 00180 * 00181 */ 00182 enum MP3_MODE 00183 { 00184 STEREO = 0, /*!< Stereo */ 00185 JOINT_STEREO = 1, /*!< Joint Stereo */ 00186 DUAL_CHANNEL = 2, /*!< Dual Channel */ 00187 MONO = 3 /*!< Mono */ 00188 }; 00189 00190 typedef enum MP3_MODE MP3_MODE; 00191 /** Struct for informations about audio streams. 00192 * 00193 */ 00194 typedef struct AudioInfo 00195 { 00196 AudioType type : 4; /*!< Type of the audio stream - important for the interpretation of the lower union */ 00197 unsigned short kBitRate ; /*!< Average bitrate of the audio stream - in kBit/s */ 00198 unsigned short decodeTime ; /*!< Decode time */ 00199 union { 00200 struct { 00201 MP3_ID id : 2; /*!< ID */ 00202 char layer : 2; /*!< Layer */ 00203 char protrectBit : 1; /*!< Protect bit, see p.44 of the datasheet */ 00204 char padBit : 1; /*!< Pad bit, see p.44 of the datasheet */ 00205 MP3_MODE mode : 2; /*!< Channel mode */ 00206 char extension : 2; /*!< Extension, see p.44 of the datasheet */ 00207 char copyright : 1; /*!< Copyright, see p.44 of the datasheet */ 00208 char original : 1; /*!< Original, see p.44 of the datasheet */ 00209 char emphasis : 2; /*!< Emphasis, see p.44 of the datasheet */ 00210 char kSampleRate : 6; /*!< Samplerate - in kHz (rounded) */ 00211 } mp3 ; /*!< MPEG Audio Layer */ 00212 struct { 00213 00214 } wma ; /*!< Windows Media Audio (WMA) stream */ 00215 struct { 00216 00217 } aac ; /*!< AAC audio stream */ 00218 struct { 00219 00220 } other ; /*!< Other */ 00221 } ext; 00222 00223 } AudioInfo; 00224 00225 /** Class for VS1053 - Ogg Vorbis / MP3 / AAC / WMA / FLAC / MIDI Audio Codec Chip. 00226 * Datasheet, see http://www.vlsi.fi/fileadmin/datasheets/vlsi/vs1053.pdf 00227 * 00228 * This code based on: 00229 * mbeduino_MP3_Shield_MP3Player 00230 * http://mbed.org/users/xshige/programs/mbeduino_MP3_Shield_MP3Player/lgcx63 00231 * 2010-10-16 00232 * 00233 * For the use of this class, a file "defines.h" must be created. 00234 * It controls debug output and vs1053 patches. 00235 * 00236 * defines.h: 00237 *@code 00238 * #ifndef _DEFINES_H 00239 * #define _DEFINES_H 00240 * // ---------------------------------------------------------------------------- 00241 * // debug output 00242 * // ---------------------------------------------------------------------------- 00243 * // optional 00244 * #define DEBUG 00245 * #ifdef DEBUG 00246 * # define DEBUGOUT(x,y...) printf(x, ##y); 00247 * #else 00248 * # define DEBUGOUT(x,y...) 00249 * #endif 00250 * // ---------------------------------------------------------------------------- 00251 * // VLSI VS1053b library, patch, apps 00252 * // ---------------------------------------------------------------------------- 00253 * // optional, ONLY ONE 00254 * //#define VS1053_PATCH_1_4_FLAC 00255 * //#define VS1053_PATCH_1_5 00256 * #define VS1053_PATCH_1_5_FLAC 00257 * //#define VS1053_SPECANA 00258 * //#define VS1053B_PCM_RECORDER_0_9 00259 * 00260 * #endif //_DEFINES_H 00261 *@endcode 00262 * 00263 * For a complete sample, see http://mbed.org/users/christi_s/programs/Lib_VS1053b 00264 * 00265 */ 00266 class VS1053 { 00267 00268 public: 00269 /** Create a vs1053b object. 00270 * 00271 * @param mosi 00272 * SPI Master Out, Slave In pin to vs1053b. 00273 * @param miso 00274 * SPI Master In, Slave Out pin to vs1053b. 00275 * @param sck 00276 * SPI Clock pin to vs1053b. 00277 * @param cs 00278 * Pin to vs1053b control chip select. 00279 * @param rst 00280 * Pin to vs1053b reset. 00281 * @param dreq 00282 * Pin to vs1053b data request. 00283 * @param dcs 00284 * Pin to vs1053b data chip select. 00285 * @param buffer 00286 * Array to cache audio data. 00287 * @param buffer_size 00288 * Length of the array. 00289 */ 00290 VS1053( 00291 PinName mosi, 00292 PinName miso, 00293 PinName sck, 00294 PinName cs, 00295 PinName rst, 00296 PinName dreq, 00297 PinName dcs, 00298 char* buffer, 00299 int buffer_size 00300 ); 00301 00302 /** Reset the vs1053b. (hardware reset) 00303 * 00304 */ 00305 void reset(void); 00306 00307 /** Stop the playback if the song is completed. 00308 * You must call this function for default playback. 00309 * 00310 */ 00311 void terminateStream(void); 00312 00313 /** Initialize the vs1053b device. 00314 * 00315 * @return 00316 * TRUE on success, FALSE on failure. 00317 */ 00318 bool initialize(void); 00319 00320 /** Set the volume. 00321 * 00322 * @param volume 00323 * Volume -0.5dB, -1.0dB, .. -64.0dB. 00324 */ 00325 void setVolume(float volume = DEFAULT_VOLUME); 00326 00327 /** Get the volume. 00328 * 00329 * @return 00330 * Return the volume in dB. 00331 */ 00332 float getVolume(); 00333 00334 /** Set the balance - volume difference between left-right. 00335 * 00336 * @param balance 00337 * Difference in dB. 00338 */ 00339 void setBalance(float balance = DEFAULT_BALANCE_DIFERENCE_LEFT_RIGHT); 00340 00341 /** Get the balance - volume difference between left-right. 00342 * 00343 * @return 00344 * Difference in dB. 00345 */ 00346 float getBalance(); 00347 00348 /** Get the treble frequency limit. 00349 * 00350 * @return 00351 * Frequenzy 1000, 2000 .. 15000Hz. 00352 */ 00353 int getTrebleFrequency(void); 00354 /** Set the treble frequency limit. 00355 * 00356 * @param frequency 00357 * Frequenzy 1000, 2000, .. 15000Hz. 00358 */ 00359 void setTrebleFrequency(int frequency = DEFAULT_TREBLE_FREQUENCY); 00360 00361 /** Get the treble amplitude. 00362 * 00363 * @return 00364 * Amplitude -8 .. 7dB (in 1.5dB steps); 0 = off. 00365 */ 00366 int getTrebleAmplitude(void); 00367 /** Set the treble amplitude. 00368 * 00369 * @param amplitude 00370 * Amplitude -8 .. 7dB (in 1.5dB steps); 0 = off. 00371 */ 00372 void setTrebleAmplitude(int amplitude = DEFAULT_TREBLE_AMPLITUDE); 00373 00374 /** Get the bass frequency limit. 00375 * 00376 * @return 00377 * Frequenzy 20, 30, .. 150Hz. 00378 */ 00379 int getBassFrequency(void); 00380 /** Set the bass frequency limit. 00381 * 00382 * @param frequency 00383 * Frequenzy 20, 30, .. 150Hz. 00384 */ 00385 void setBassFrequency(int frequency= DEFAULT_BASS_FREQUENCY); 00386 00387 /** Get the bass amplitude. 00388 * 00389 * @return 00390 * Amplitude 0 .. 15dB (in 1dB steps); 0 = off. 00391 */ 00392 int getBassAmplitude(void); 00393 /** Set the bass amplitude. 00394 * 00395 * @param amplitude 00396 * Amplitude 0 .. 15dB (in 1dB steps); 0 = off. 00397 */ 00398 void setBassAmplitude(int amplitude = DEFAULT_BASS_AMPLITUDE); 00399 00400 /** Set the speed of a playback. 00401 * 00402 * @param speed 00403 * Speed 0, 1, .. (0, 1 normal speed). 00404 * Speeds greater 2 are not recommended, buffer must be filled quickly enough. 00405 */ 00406 void setPlaySpeed(unsigned short speed); 00407 00408 /** Copy a byte into the audio data buffer. 00409 * 00410 * @param c 00411 * Data for the buffer. 00412 * 00413 * @return 00414 * TRUE on success; FALSE on failure, c isn't copied in the buffer. 00415 */ 00416 bool bufferSetByte(char c); 00417 00418 /** Copy a array of bytes into the audio data buffer. 00419 * 00420 * @param s 00421 * Data for the buffer. 00422 * 00423 * @param length 00424 * Size of data array. 00425 * 00426 * @return 00427 * TRUE on success; FALSE on failure, s isn't copied in the buffer. 00428 */ 00429 bool bufferPutStream(const char *s, unsigned int length); 00430 00431 /** Get the free space of the audio data buffer. 00432 * 00433 * @return 00434 * Space 0 .. BUFFER_SIZE - 1. 00435 */ 00436 unsigned int bufferFree(void); 00437 00438 /** Get the busy space of the audio data buffer. 00439 * 00440 * @return 00441 * Space 0 .. BUFFER_SIZE - 1. 00442 */ 00443 unsigned int bufferCount(void); 00444 00445 /** Complete length of the audio buffer. 00446 * 00447 * @return 00448 * Buffer length. 00449 */ 00450 unsigned int bufferLength(void); 00451 00452 /** Start playing audio from buffer. 00453 * 00454 */ 00455 void play(void); 00456 00457 /** Interrupt the playback. 00458 * 00459 */ 00460 void pause(void); 00461 00462 /** Stop the playback in the middle of a song. 00463 * After this call, you can now send the next audio file to buffer. 00464 * 00465 */ 00466 void stop(void); 00467 00468 /** Get information about played audio stream. 00469 * 00470 * @param aInfo 00471 * Return value for the informations. 00472 * 00473 */ 00474 void getAudioInfo(AudioInfo* aInfo); 00475 00476 protected: 00477 unsigned short int wram_read(unsigned short int); 00478 void wram_write(unsigned short int, unsigned short int); 00479 void write_plugin(const unsigned short* , unsigned int); 00480 void cs_low(void); 00481 void cs_high(void); 00482 void dcs_low(void); 00483 void dcs_high(void); 00484 void sci_en(void); 00485 void sci_dis(void); 00486 void sdi_en(void); 00487 void sdi_dis(void); 00488 00489 void spi_initialise(void); 00490 00491 void sdi_initialise(void); 00492 00493 void sci_write(unsigned char, unsigned short int); 00494 void sdi_write(unsigned char); 00495 unsigned short int sci_read(unsigned short int); 00496 void sine_test_activate(unsigned char); 00497 00498 void sine_test_deactivate(void); 00499 00500 void changeVolume(void); 00501 00502 00503 // TODO 00504 void power_down(void); 00505 00506 void changeBass(void); 00507 00508 unsigned char bufferGetByte(void); 00509 void bufferReset(void); 00510 #ifdef VS1053_RTOS 00511 static void dataRequestHandler_start(const void *args); 00512 #endif 00513 void dataRequestHandler(void); 00514 00515 SPI _spi; 00516 DigitalOut _CS; 00517 DigitalOut _RST; 00518 DigitalOut _DCS; 00519 DigitalIn _DREQ; 00520 #ifdef DEBUG 00521 DigitalOut _led; 00522 #endif 00523 00524 #ifdef VS1053_RTOS 00525 Thread _thread; 00526 #else 00527 InterruptIn _DREQ_INTERUPT_IN; 00528 #endif 00529 00530 char* _buffer; 00531 char* _bufferReadPointer; 00532 char* _bufferWritePointer; 00533 int BUFFER_SIZE; 00534 00535 bool _isIdle; 00536 bool _isPlay; 00537 00538 // variables to save 00539 // volume, values in db 00540 float _balance; 00541 float _volume; 00542 // bass enhancer settings 00543 int _sb_amplitude; 00544 int _sb_freqlimit; 00545 int _st_amplitude; 00546 int _st_freqlimit; 00547 00548 00549 Ticker timer; 00550 00551 static const char _sampleRateTable[4][4]; // _sampleRateTable[id][srate] 00552 00553 }; 00554 00555 #endif
Generated on Tue Jul 19 2022 01:19:15 by
1.7.2
