.

Committer:
PloyLL
Date:
Sun Dec 04 00:06:01 2016 +0000
Revision:
0:bb49bb2c6197
mp3_interrupt

Who changed what in which revision?

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