modified for VS1033
Dependents: 11U68_MP3Player with TFTLCD 11U68_MP3Player-with-TFTLCD Mp3_1
Fork of VS1033 by
Revision 7:b61cd12eabc5, committed 2015-07-11
- Comitter:
- nameless129
- Date:
- Sat Jul 11 19:42:28 2015 +0000
- Parent:
- 6:1f57fbd3aeb5
- Child:
- 8:171effe49517
- Commit message:
- first commit;
Changed in this revision
VS1053.cpp | Show annotated file Show diff for this revision Revisions of this file |
VS1053.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/VS1053.cpp Fri Dec 20 21:34:03 2013 +0000 +++ b/VS1053.cpp Sat Jul 11 19:42:28 2015 +0000 @@ -18,8 +18,7 @@ dreq(dreqPin), rst(rstPin) { - spi.format(8, 0); - spi.frequency(spiFrequency); + useSpiFreq = spiFrequency; // Initialize outputs cs = 1; @@ -31,49 +30,101 @@ VS1053::~VS1053() { } +//SCI enable +void VS1053::sci_en(void) +{ + cs = 1; + bsync = 1; + cs = 0; +} + +//SCI disable +void VS1053::sci_dis(void) +{ + cs = 1; +} + +//SDI enable +void VS1053::sdi_en(void) +{ + bsync = 1; + cs = 1; + bsync = 0; +} + +//SDI disable +void VS1053::sdi_dis(void) +{ + bsync = 1; +} + /** Make a hardware reset by hitting VS1053's RESET pin. */ void VS1053::hardwareReset() { rst = 0; - wait(.05); + wait(0.1); rst = 1; - wait(.05); + wait(0.1); } -/** Send a data byte to VS1053. */ -void VS1053::sendDataByte(uint8_t data) { - while (!dreq); - bsync = 0; - spi.write(data); +//Serial Command Interface(SCI) init +void VS1053::sci_init() +{ + spi.format(8, 0); + spi.frequency(1000000); + + cs = 0; + for(int i=0; i<4; i++) + { + spi.write(0xFF); //clock the chip a bit + } + cs = 1; + bsync = 1; + wait_us(5); + writeReg(SCI_MODE,(SM_SDINEW)); + writeReg(SCI_CLOCKF, 0x9800); +} + +//Serial Data Interface(SDI) init +void VS1053::sdi_init() +{ + spi.format(8, 0); + spi.frequency(useSpiFreq); + + cs = 1; bsync = 1; } -/** Send a data block specified as a pointer to VS1053. +/** SDI Send a data byte to VS1053. */ +void VS1053::sendDataByte(uint8_t data) { + sdi_en(); + + while (!dreq); + spi.write(data); + + sdi_dis(); +} + +/** SDI Send a data block specified as a pointer to VS1053. * @return Data length successfully sent. */ size_t VS1053::sendDataBlock(uint8_t* data, size_t length) { size_t n, sizeSent = 0; if (!data || !length) return 0; + sdi_en(); while (length) { n = length < 32 ? length : 32; while (!dreq); - bsync = 0; - for (uint32_t i = 0; i < n; i++) { + for (uint32_t i = 0; i < n; i++) + { spi.write(*data++); sizeSent++; length--; } - bsync = 1; } + sdi_dis(); return sizeSent; } -/** Change VS1053's PLL setting for speedup. */ -void VS1053::clockUp() { - // Set CLKI to 43.0-55.3 MHz - writeReg(SCI_CLOCKF, 0x8800); // SC_MULT=4 (3.5x), SC_ADD=1 (+1.0x) - wait(0.01); -} - /** Send cancel request to VS1053. * @return Zero at failure, non-zero at success. */ @@ -114,13 +165,13 @@ length = 2052; while (length) { n = length < 32 ? length : 32; + sdi_en(); while (!dreq); - bsync = 0; for (uint32_t i = 0; i < n; i++) { spi.write(endFillByte); length--; } - bsync = 1; + sdi_dis(); } // Check if both HDAT0 and HDAT1 are cleared return readReg(SCI_HDAT0) == 0x0000 && readReg(SCI_HDAT1) == 0x0000; @@ -132,15 +183,13 @@ if (addr > 0x0f) { return; } - + sci_en(); while (!dreq); - cs = 0; spi.write(0x02); // Send a "Write SCI" instruction (02h), spi.write(addr); // target address, spi.write(word >> 8); // high byte, spi.write(word & 0xff); // then low byte - while (!dreq); - cs = 1; + sci_dis(); } /** Read an SCI (Serial Control Interface) register entry. @@ -153,14 +202,53 @@ if (addr > 0x0f) { return 0x0000; } - + cs = 0; while (!dreq); - cs = 0; spi.write(0x03); // Send a "Read SCI" instruction (03h) spi.write(addr); // and target address word = spi.write(0xff) << 8; // Receive high byte with dummy data FFh word |= spi.write(0xff); // Receive low byte - while (!dreq); cs = 1; return word; } + +void VS1053::sine_test_activate(unsigned char wave) +{ + hardwareReset(); + spi.format(8, 0); + spi.frequency(1000000); + writeReg(SCI_MODE,(SM_SDINEW+SM_TESTS)); + sdi_en(); + + while(dreq == 0) + { + } //wait unitl data request is high + spi.write(0x53); //SDI write + spi.write(0xEF); //SDI write + spi.write(0x6E); //SDI write + spi.write(wave); //SDI write + spi.write(0x00); //filler byte + spi.write(0x00); //filler byte + spi.write(0x00); //filler byte + spi.write(0x00); //filler byte + + sdi_dis(); +} +void VS1053::sine_test_deactivate(void) +{ + sdi_en(); + + while(dreq == 0) + { + } + spi.write(0x45); //SDI write + spi.write(0x78); //SDI write + spi.write(0x69); //SDI write + spi.write(0x74); //SDI write + spi.write(0x00); //filler byte + spi.write(0x00); //filler byte + spi.write(0x00); //filler byte + spi.write(0x00); //filler byte + + sdi_dis(); +}
--- a/VS1053.h Fri Dec 20 21:34:03 2013 +0000 +++ b/VS1053.h Sat Jul 11 19:42:28 2015 +0000 @@ -7,6 +7,26 @@ #ifndef KAYX_VS1053_H_ #define KAYX_VS1053_H_ +//SCI_MODE register bits as of p.26 of the datasheet +#define SM_DIFF 0x0001 +#define SM_SETTOZERO 0x0002 +#define SM_RESET 0x0004 +#define SM_OUTOFWAV 0x0008 +#define SM_PDOWN 0x0010 +#define SM_TESTS 0x0020 +#define SM_STREAM 0x0040 +#define SM_PLUSV 0x0080 +#define SM_DACT 0x0100 +#define SM_SDIORD 0x0200 +#define SM_SDISHARE 0x0400 +#define SM_SDINEW 0x0800 +#define SM_ADPCM 0x1000 +#define SM_ADPCM_HP 0x2000 + +#define SineWave_10k (0x1D) +#define SineWave_1k (0xA8) + + /** Class VS1053. Drives VLSI's mp3/midi codec chip. */ class VS1053 { private: @@ -36,18 +56,26 @@ VS1053(PinName mosiPin, PinName misoPin, PinName sckPin, PinName csPin, PinName bsyncPin, PinName dreqPin, PinName rstPin, - uint32_t spiFrequency=1000000); + uint32_t spiFrequency=7000000); ~VS1053(); void hardwareReset(); void sendDataByte(uint8_t data); size_t sendDataBlock(uint8_t* data, size_t length); - void clockUp(); + void sci_init(); + void sdi_init(); bool sendCancel(); bool stop(); + void sine_test_activate(unsigned char wave); + void sine_test_deactivate(); private: void writeReg(uint8_t, uint16_t); uint16_t readReg(uint8_t); + uint32_t useSpiFreq; + void sci_en(); + void sci_dis(); + void sdi_en(); + void sdi_dis(); }; #endif