sxa

Dependents:   MP3333 B18_MP3_PLAYER B18_MP3_PLAYER B18_MP3_PLAYER

Committer:
PKnevermind
Date:
Wed Dec 09 08:32:13 2015 +0000
Revision:
3:934d5e72990a
Parent:
2:6f21eae5f456
VS1053

Who changed what in which revision?

UserRevisionLine numberNew contents of line
PKnevermind 0:928e5b21896c 1 /** \file vs10xx.c
PKnevermind 0:928e5b21896c 2 * Functions for interfacing with the mp3 player chip.
PKnevermind 0:928e5b21896c 3 * \todo safe rewind
PKnevermind 0:928e5b21896c 4 * \todo VS1003 WMA "wma-bytes-left" variable adjustment at ff/rew
PKnevermind 0:928e5b21896c 5 */
PKnevermind 0:928e5b21896c 6 #include "vs10xx.h"
PKnevermind 0:928e5b21896c 7
PKnevermind 0:928e5b21896c 8 /** Constructor of class VS1053. */
PKnevermind 0:928e5b21896c 9 vs10xx::vs10xx(PinName MOSI, PinName MISO, PinName SCLK, PinName XCS,
PKnevermind 0:928e5b21896c 10 PinName XDCS,PinName DREQ, PinName XRESET)
PKnevermind 0:928e5b21896c 11 :
PKnevermind 0:928e5b21896c 12 _spi(MOSI,MISO,SCLK),
PKnevermind 0:928e5b21896c 13 _XCS(XCS),
PKnevermind 0:928e5b21896c 14 _XDCS(XDCS),
PKnevermind 0:928e5b21896c 15 _DREQ(DREQ),
PKnevermind 0:928e5b21896c 16 _XRESET(XRESET)
PKnevermind 0:928e5b21896c 17 {
PKnevermind 0:928e5b21896c 18 _XCS = 1;
PKnevermind 0:928e5b21896c 19 _XDCS = 1;
PKnevermind 0:928e5b21896c 20 _XRESET = 1;
PKnevermind 0:928e5b21896c 21 }
PKnevermind 0:928e5b21896c 22
PKnevermind 0:928e5b21896c 23 /** Write the 16-bit value to VS10xx register*/
PKnevermind 0:928e5b21896c 24 void vs10xx::writeRegister(unsigned char addressbyte,unsigned int value)
PKnevermind 0:928e5b21896c 25 {
PKnevermind 0:928e5b21896c 26 _XCS = 1;
PKnevermind 0:928e5b21896c 27 while (!_DREQ);
PKnevermind 0:928e5b21896c 28 _XCS = 0;
PKnevermind 0:928e5b21896c 29 _spi.write(VS_WRITE_COMMAND);
PKnevermind 0:928e5b21896c 30 _spi.write(addressbyte);
PKnevermind 0:928e5b21896c 31 _spi.write(value >> 8);
PKnevermind 0:928e5b21896c 32 _spi.write(value & 0xFF);
PKnevermind 0:928e5b21896c 33 _XCS = 1;
PKnevermind 0:928e5b21896c 34 }
PKnevermind 0:928e5b21896c 35
PKnevermind 0:928e5b21896c 36 /** Read the 16-bit value of a VS10xx register */
PKnevermind 0:928e5b21896c 37 unsigned int vs10xx::readRegister (unsigned char addressbyte)
PKnevermind 0:928e5b21896c 38 {
PKnevermind 0:928e5b21896c 39 unsigned int resultvalue = 0;
PKnevermind 0:928e5b21896c 40
PKnevermind 0:928e5b21896c 41 _XCS = 1;
PKnevermind 0:928e5b21896c 42 while (!_DREQ);
PKnevermind 0:928e5b21896c 43 _XCS = 0;
PKnevermind 0:928e5b21896c 44 _spi.write(VS_READ_COMMAND);
PKnevermind 0:928e5b21896c 45 _spi.write((addressbyte));
PKnevermind 0:928e5b21896c 46 resultvalue = _spi.write(0XFF) << 8;
PKnevermind 0:928e5b21896c 47 resultvalue |= _spi.write(0XFF);
PKnevermind 0:928e5b21896c 48 _XCS = 1;
PKnevermind 0:928e5b21896c 49 return resultvalue;
PKnevermind 0:928e5b21896c 50 }
PKnevermind 0:928e5b21896c 51
PKnevermind 0:928e5b21896c 52 /** write data to VS10xx */
PKnevermind 0:928e5b21896c 53 void vs10xx::writeData(unsigned char *databuf,unsigned char n)
PKnevermind 0:928e5b21896c 54 {
PKnevermind 0:928e5b21896c 55 _XDCS = 1;
PKnevermind 0:928e5b21896c 56 _XDCS = 0;
PKnevermind 0:928e5b21896c 57 while (!_DREQ);
PKnevermind 0:928e5b21896c 58 while (n--)
PKnevermind 0:928e5b21896c 59 {
PKnevermind 0:928e5b21896c 60 _spi.write(*databuf++);
PKnevermind 0:928e5b21896c 61 }
PKnevermind 0:928e5b21896c 62 _XDCS = 1;
PKnevermind 0:928e5b21896c 63 }
PKnevermind 0:928e5b21896c 64
PKnevermind 0:928e5b21896c 65 void vs10xx::setFreq(int freq)
PKnevermind 0:928e5b21896c 66 {
PKnevermind 0:928e5b21896c 67 _spi.frequency(freq); //set freq for speed
PKnevermind 0:928e5b21896c 68 }
PKnevermind 0:928e5b21896c 69
PKnevermind 0:928e5b21896c 70 void vs10xx::setVolume(unsigned char vol)
PKnevermind 0:928e5b21896c 71 {
PKnevermind 0:928e5b21896c 72 writeRegister(SPI_VOL, vol*0x101); //Set volume level
PKnevermind 0:928e5b21896c 73 }
PKnevermind 0:928e5b21896c 74
PKnevermind 0:928e5b21896c 75 /** Soft Reset of VS10xx (Between songs) */
PKnevermind 0:928e5b21896c 76 void vs10xx::softReset()
PKnevermind 0:928e5b21896c 77 {
PKnevermind 0:928e5b21896c 78 _spi.frequency(1000000); //low speed
PKnevermind 0:928e5b21896c 79
PKnevermind 0:928e5b21896c 80 /* Soft Reset of VS10xx */
PKnevermind 0:928e5b21896c 81 writeRegister(SPI_MODE, 0x0804); /* Newmode, Reset, No L1-2 */
PKnevermind 0:928e5b21896c 82
PKnevermind 0:928e5b21896c 83 wait_ms(2); //delay
PKnevermind 0:928e5b21896c 84 while(!_DREQ);
PKnevermind 0:928e5b21896c 85
PKnevermind 0:928e5b21896c 86 /* A quick sanity check: write to two registers, then test if we
PKnevermind 0:928e5b21896c 87 get the same results. Note that if you use a too high SPI
PKnevermind 0:928e5b21896c 88 speed, the MSB is the most likely to fail when read again. */
PKnevermind 0:928e5b21896c 89 writeRegister(SPI_HDAT0, 0xABAD);
PKnevermind 0:928e5b21896c 90 writeRegister(SPI_HDAT1, 0x1DEA);
PKnevermind 0:928e5b21896c 91 if (readRegister(SPI_HDAT0) != 0xABAD || readRegister(SPI_HDAT1) != 0x1DEA) {
PKnevermind 0:928e5b21896c 92 printf("There is something wrong with VS10xx\n");
PKnevermind 0:928e5b21896c 93 }
PKnevermind 0:928e5b21896c 94
PKnevermind 0:928e5b21896c 95 writeRegister(SPI_CLOCKF,0XC000); //Set the clock
PKnevermind 0:928e5b21896c 96 writeRegister(SPI_AUDATA,0xbb81); //samplerate 48k,stereo
PKnevermind 0:928e5b21896c 97 writeRegister(SPI_BASS, 0x0055); //set accent
PKnevermind 2:6f21eae5f456 98 writeRegister(SPI_VOL, 0x0); //Set volume level
PKnevermind 0:928e5b21896c 99
PKnevermind 0:928e5b21896c 100 while (!_DREQ);
PKnevermind 0:928e5b21896c 101
PKnevermind 0:928e5b21896c 102 }
PKnevermind 0:928e5b21896c 103
PKnevermind 0:928e5b21896c 104 /** Reset VS10xx */
PKnevermind 0:928e5b21896c 105 void vs10xx::reset(){
PKnevermind 0:928e5b21896c 106
PKnevermind 0:928e5b21896c 107 _XRESET = 0;
PKnevermind 0:928e5b21896c 108 wait_ms(2); //it is a must
PKnevermind 0:928e5b21896c 109
PKnevermind 0:928e5b21896c 110 /* Send dummy SPI byte to initialize SPI */
PKnevermind 0:928e5b21896c 111 _spi.write(0xFF);
PKnevermind 0:928e5b21896c 112
PKnevermind 0:928e5b21896c 113 /* Un-reset VS10XX chip */
PKnevermind 0:928e5b21896c 114 _XCS = 1;
PKnevermind 0:928e5b21896c 115 _XDCS = 1;
PKnevermind 0:928e5b21896c 116 _XRESET = 1;
PKnevermind 0:928e5b21896c 117
PKnevermind 0:928e5b21896c 118 softReset(); //vs10xx soft reset.
PKnevermind 0:928e5b21896c 119
PKnevermind 0:928e5b21896c 120 //printf("\r\nVS10xx Init\r\n");
PKnevermind 0:928e5b21896c 121 }
PKnevermind 0:928e5b21896c 122
PKnevermind 0:928e5b21896c 123 /* Loads a plugin. */
PKnevermind 0:928e5b21896c 124 void vs10xx::loadPlugin(const unsigned short *plugin,int length) {
PKnevermind 0:928e5b21896c 125 int i = 0;
PKnevermind 0:928e5b21896c 126 while (i<length) {
PKnevermind 0:928e5b21896c 127 unsigned short addr, n, val;
PKnevermind 0:928e5b21896c 128 addr = plugin[i++];
PKnevermind 0:928e5b21896c 129 n = plugin[i++];
PKnevermind 0:928e5b21896c 130 if (n & 0x8000U) { /* RLE run, replicate n samples */
PKnevermind 0:928e5b21896c 131 n &= 0x7FFF;
PKnevermind 0:928e5b21896c 132 val = plugin[i++];
PKnevermind 0:928e5b21896c 133 while (n--) {
PKnevermind 0:928e5b21896c 134 writeRegister(addr, val);
PKnevermind 0:928e5b21896c 135 }
PKnevermind 0:928e5b21896c 136 } else { /* Copy run, copy n samples */
PKnevermind 0:928e5b21896c 137 while (n--) {
PKnevermind 0:928e5b21896c 138 val = plugin[i++];
PKnevermind 0:928e5b21896c 139 writeRegister(addr, val);
PKnevermind 0:928e5b21896c 140 }
PKnevermind 0:928e5b21896c 141 }
PKnevermind 0:928e5b21896c 142 }
PKnevermind 0:928e5b21896c 143 }
PKnevermind 0:928e5b21896c 144
PKnevermind 0:928e5b21896c 145
PKnevermind 0:928e5b21896c 146
PKnevermind 0:928e5b21896c 147