FRA221_B18 / VS1053

Dependents:   MP3333 B18_MP3_PLAYER B18_MP3_PLAYER B18_MP3_PLAYER

Files at this revision

API Documentation at this revision

Comitter:
PKnevermind
Date:
Mon Dec 07 10:19:01 2015 +0000
Child:
1:4afda9d22e34
Commit message:
ygjhgj

Changed in this revision

player.cpp Show annotated file Show diff for this revision Revisions of this file
player.h Show annotated file Show diff for this revision Revisions of this file
vs10xx.cpp Show annotated file Show diff for this revision Revisions of this file
vs10xx.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/player.cpp	Mon Dec 07 10:19:01 2015 +0000
@@ -0,0 +1,104 @@
+#include "player.h"
+#include "SDFileSystem.h"
+
+SDFileSystem sd(D11, D12, D13, D10, "sd"); // the pinout on the mbed Cool
+vs10xx vs1053(D11, D12, D13, D4, D5, D3, D6);//mosi,miso,sclk,xcs,xdcs,dreq,xreset
+
+playerStatetype  playerState;
+ctrlStatetype ctrlState;
+static unsigned char fileBuf[32768];
+unsigned char *bufptr;
+
+char list[20][50];          //song list
+char index = 0;      //song play index
+char index_MAX;      //how many song in all
+unsigned char vlume = 0x40;   //vlume
+unsigned char vlumeflag = 0;  //set vlume flag
+
+void Player::begin(void)
+{
+    DirHandle *dir;
+    struct dirent *ptr;
+    FileHandle *fp;
+
+    vs1053.reset();
+    dir = opendir("/sd");
+    printf("\r\n**********playing list**********\r\n");
+    unsigned char i = 0,j=0;
+    while(((ptr = dir->readdir()) != NULL)&&(i <20)) {
+        if(strstr(ptr->d_name,".mp3")||strstr(ptr->d_name,".MP3")) {
+            fp =sd.open(ptr->d_name, O_RDONLY);
+            if(fp != NULL) {
+                char *byte = ptr->d_name;
+                j=0;
+                while(*byte) {
+                    list[i][j++]  = *byte++;
+                }
+                printf("%2d . %s\r\n", i,list[i++]);
+                fp->close();
+            }
+        }
+    }
+    index_MAX = i-1;
+    dir->closedir();
+    printf("\r\n");
+}
+
+/*  This function plays back an audio file.  */
+void Player::playFile(char *file)
+{
+    int bytes;        // How many bytes in buffer left
+    int n;
+
+    playerState = PS_PLAY;
+
+    vs1053.setFreq(24000000);     //hight speed
+    FileHandle *fp =sd.open(file, O_RDONLY);
+    if(fp == NULL) {
+        printf("Could not open %s\r\n",file);
+
+    } else {
+        printf("Playing %s ...\r\n",file);
+
+        /* Main playback loop */
+        while((bytes = fp->read(fileBuf,32768)) > 0) {
+            bufptr = fileBuf;
+
+            // actual audio data gets sent to VS10xx.
+            while(bytes > 0) {
+                n = (bytes < 1)?bytes:1;
+                vs1053.writeData(bufptr,n);
+                bytes -= n;
+                bufptr += n;
+            }
+        }
+        /*while(playerState == PS_PAUSE);         //Pause
+
+        if(vlumeflag) {                        //set vlume
+            vs1053.setFreq(12000000);     //low speed
+            vs1053.writeRegister(SPI_VOL, vlume*0x101);     //Set volume level
+            vs1053.setFreq(24000000);     //higth speed
+            vlumeflag = 0;  //clear flag;
+        }
+
+        if(playerState != PS_PLAY) {       //stop
+            fp->close();
+            vs1053.softReset();
+            return;
+        }*/
+        fp->close();
+        vs1053.softReset();
+        printf("[done!]\r\n");
+    }
+    if(index != index_MAX)index++;
+    else index = 0;
+}
+
+void Set32(unsigned char *d, unsigned int n)
+{
+    int i;
+    for (i=0; i<4; i++) {
+        *d++ = (unsigned char)n;
+        n >>= 8;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/player.h	Mon Dec 07 10:19:01 2015 +0000
@@ -0,0 +1,35 @@
+#ifndef PLAYER_H
+#define PLAYER_H
+#include "vs10xx.h"
+
+/** Playing states definations. */
+volatile typedef enum {
+  PS_STOP,	       // Player stop						 
+  PS_PLAY,         // Start to player                                    
+  PS_PAUSE,        //Pause play                                         
+  PS_RECORDING,    //Recording states                                   
+} playerStatetype;
+
+/** Control states definations. */
+volatile typedef enum
+{
+  CS_EMPTY = 0,      // Have no control                                     
+  CS_PLAYPAUSE,      // Play/pause button pressed                     
+  CS_RECORDING,      // Play/pause button long pressed               
+  CS_UP,             // Up button pressed                                                          
+  CS_DOWN,           // Down button pressed                                                       
+  CS_NEXT,           // Right button pressed                                                      
+  CS_PREV,           // Left button pressed                                                       
+} ctrlStatetype;
+
+class Player
+{
+public:
+  void begin(void);
+  void playFile(char *file);
+  void recordFile(char *file);
+private:
+    
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vs10xx.cpp	Mon Dec 07 10:19:01 2015 +0000
@@ -0,0 +1,147 @@
+/** \file vs10xx.c
+ * Functions for interfacing with the mp3 player chip.
+ * \todo safe rewind
+ * \todo VS1003 WMA "wma-bytes-left" variable adjustment at ff/rew
+ */
+#include "vs10xx.h"
+
+/** Constructor of class VS1053. */
+vs10xx::vs10xx(PinName MOSI, PinName MISO, PinName SCLK, PinName XCS, 
+               PinName XDCS,PinName DREQ, PinName XRESET)
+                :  
+                _spi(MOSI,MISO,SCLK),
+                _XCS(XCS),
+                _XDCS(XDCS),
+                _DREQ(DREQ),
+                _XRESET(XRESET)    
+{
+    _XCS = 1;
+    _XDCS = 1;
+    _XRESET = 1;
+}
+
+/** Write the 16-bit value to VS10xx register*/
+void vs10xx::writeRegister(unsigned char addressbyte,unsigned int value)
+{
+    _XCS = 1;
+    while (!_DREQ);
+	_XCS = 0;
+ 	_spi.write(VS_WRITE_COMMAND);
+ 	_spi.write(addressbyte);
+ 	_spi.write(value >> 8);
+ 	_spi.write(value & 0xFF);
+ 	_XCS = 1;
+}
+
+/** Read the 16-bit value of a VS10xx register */
+unsigned int vs10xx::readRegister (unsigned char addressbyte)
+{
+    unsigned int resultvalue = 0;
+    
+    _XCS = 1;
+    while (!_DREQ);
+	_XCS = 0;
+    _spi.write(VS_READ_COMMAND);
+    _spi.write((addressbyte));
+    resultvalue = _spi.write(0XFF) << 8;
+    resultvalue |= _spi.write(0XFF); 
+    _XCS = 1;
+    return resultvalue;
+}
+
+/** write data to VS10xx  */
+void vs10xx::writeData(unsigned char *databuf,unsigned char n)
+{
+    _XDCS = 1;
+    _XDCS = 0;
+    while (!_DREQ);
+    while (n--)
+    {
+        _spi.write(*databuf++);
+    }
+    _XDCS = 1;
+}
+
+void vs10xx::setFreq(int freq)
+{
+    _spi.frequency(freq);        //set freq for speed
+}
+
+void vs10xx::setVolume(unsigned char vol)
+{
+    writeRegister(SPI_VOL, vol*0x101);     //Set volume level
+}
+
+/** Soft Reset of VS10xx (Between songs) */
+void vs10xx::softReset()
+{
+    _spi.frequency(1000000);          //low speed
+    
+    /* Soft Reset of VS10xx */
+    writeRegister(SPI_MODE, 0x0804); /* Newmode, Reset, No L1-2 */
+    
+    wait_ms(2);         //delay
+    while(!_DREQ);
+    
+    /* A quick sanity check: write to two registers, then test if we
+     get the same results. Note that if you use a too high SPI
+     speed, the MSB is the most likely to fail when read again. */
+    writeRegister(SPI_HDAT0, 0xABAD);
+    writeRegister(SPI_HDAT1, 0x1DEA);
+    if (readRegister(SPI_HDAT0) != 0xABAD || readRegister(SPI_HDAT1) != 0x1DEA) {
+        printf("There is something wrong with VS10xx\n");
+    }
+    
+    writeRegister(SPI_CLOCKF,0XC000);   //Set the clock
+    writeRegister(SPI_AUDATA,0xbb81);   //samplerate 48k,stereo
+    writeRegister(SPI_BASS, 0x0055);    //set accent
+    writeRegister(SPI_VOL, 0x4040);     //Set volume level
+        
+    while (!_DREQ);
+    
+}
+
+/** Reset VS10xx */
+void vs10xx::reset(){
+
+    _XRESET = 0;
+    wait_ms(2);  //it is a must
+    
+    /* Send dummy SPI byte to initialize SPI */
+    _spi.write(0xFF);
+
+    /* Un-reset VS10XX chip */
+    _XCS = 1;
+    _XDCS = 1;
+    _XRESET = 1;
+
+    softReset(); //vs10xx soft reset.
+
+    //printf("\r\nVS10xx Init\r\n");
+}
+
+/*     Loads a plugin.       */
+void vs10xx::loadPlugin(const unsigned short *plugin,int length) {
+    int i = 0;
+    while (i<length) {
+        unsigned short addr, n, val;
+        addr = plugin[i++];
+        n = plugin[i++];
+        if (n & 0x8000U) { /* RLE run, replicate n samples */
+            n &= 0x7FFF;
+            val = plugin[i++];
+            while (n--) {
+                writeRegister(addr, val);
+            }
+        } else {           /* Copy run, copy n samples */
+            while (n--) {
+                val = plugin[i++];
+                writeRegister(addr, val);
+            }
+        }
+    }
+}
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vs10xx.h	Mon Dec 07 10:19:01 2015 +0000
@@ -0,0 +1,82 @@
+/** file vs10xx.h
+ * Headers for interfacing with the mp3 player chip.
+ * Interfacing the New Way, not handling BSYNC -> not compatible with VS1001.
+ */
+
+#ifndef VS10XX_H
+#define VS10XX_H
+
+#include "mbed.h"
+
+/** VS10xx SCI Write Command byte is 0x02 */
+#define VS_WRITE_COMMAND 0x02
+/** VS10xx SCI Read Command byte is 0x03 */
+#define VS_READ_COMMAND  0x03
+
+#define SPI_MODE	0x0   /**< VS10xx register */
+#define SPI_STATUS	0x1   /**< VS10xx register */
+#define SPI_BASS	0x2   /**< VS10xx register */
+#define SPI_CLOCKF	0x3   /**< VS10xx register */
+#define SPI_DECODE_TIME	0x4   /**< VS10xx register */
+#define SPI_AUDATA	0x5   /**< VS10xx register */
+#define SPI_WRAM	0x6   /**< VS10xx register */
+#define SPI_WRAMADDR	0x7   /**< VS10xx register */
+#define SPI_HDAT0	0x8   /**< VS10xx register */
+#define SPI_HDAT1	0x9   /**< VS10xx register */
+#define SPI_AIADDR	0xa   /**< VS10xx register */
+#define SPI_VOL		0xb   /**< VS10xx register */
+#define SPI_AICTRL0	0xc   /**< VS10xx register */
+#define SPI_AICTRL1	0xd   /**< VS10xx register */
+#define SPI_AICTRL2	0xe   /**< VS10xx register */
+#define SPI_AICTRL3	0xf   /**< VS10xx register */
+
+#define SM_DIFF           (1<< 0)
+#define SM_LAYER12        (1<< 1) /* VS1063, VS1053, VS1033, VS1011 */
+#define SM_RECORD_PATH    (1<< 1) /* VS1103 */
+#define SM_RESET          (1<< 2)
+#define SM_CANCEL         (1<< 3) /* VS1063, VS1053 */
+#define SM_OUTOFWAV       (1<< 3) /* VS1033, VS1003, VS1011 */
+#define SM_OUTOFMIDI      (1<< 3) /* VS1103 */
+#define SM_EARSPEAKER_LO  (1<< 4) /* VS1053, VS1033 */
+#define SM_PDOWN          (1<< 4) /* VS1003, VS1103 */
+#define SM_TESTS          (1<< 5)
+#define SM_STREAM         (1<< 6) /* VS1053, VS1033, VS1003, VS1011 */
+#define SM_ICONF          (1<< 6) /* VS1103 */
+#define SM_EARSPEAKER_HI  (1<< 7) /* VS1053, VS1033 */
+#define SM_DACT           (1<< 8)
+#define SM_SDIORD         (1<< 9)
+#define SM_SDISHARE       (1<<10)
+#define SM_SDINEW         (1<<11)
+#define SM_ENCODE         (1<<12) /* VS1063 */
+#define SM_ADPCM          (1<<12) /* VS1053, VS1033, VS1003 */
+#define SM_EARSPEAKER1103 (1<<12) /* VS1103 */
+#define SM_ADPCM_HP       (1<<13) /* VS1033, VS1003 */
+#define SM_LINE1          (1<<14) /* VS1063, VS1053 */
+#define SM_LINE_IN        (1<<14) /* VS1033, VS1003, VS1103 */
+#define SM_CLK_RANGE      (1<<15) /* VS1063, VS1053, VS1033 */
+#define SM_ADPCM_1103     (1<<15) /* VS1103 */
+
+class vs10xx
+{
+public:
+    vs10xx(PinName MOSI, PinName MISO, PinName SCLK, PinName XCS, 
+           PinName XDCS,PinName DREQ, PinName XRESET);
+    void writeRegister(unsigned char addressbyte, unsigned int value);
+    unsigned int readRegister(unsigned char addressbyte); /** Read the 16-bit value of a VS10xx register */
+    void writeData(unsigned char *databuf, unsigned char = 32); //write 32 bytes to vs1053
+   
+    void reset(void); //reset for vs10xx
+    void softReset(void); /* Soft Reset of VS10xx (Between songs) */
+    void setFreq(int freq);
+    void setVolume(unsigned char vol);
+    void loadPlugin(const unsigned short *plugin,int length);
+private:
+    SPI _spi;
+    DigitalOut  _XCS;
+    DigitalOut  _XDCS;  
+    DigitalIn   _DREQ;
+    DigitalOut  _XRESET;
+      
+};
+
+#endif