Under construction

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed

Fork of vs1011e by Erick Cardenas

Files at this revision

API Documentation at this revision

Comitter:
swilliams346
Date:
Mon Dec 12 14:31:10 2016 +0000
Parent:
0:8a46c5988d62
Commit message:
Under construction

Changed in this revision

4DGL-uLCD-SE.lib Show annotated file Show diff for this revision Revisions of this file
VS1002.cpp Show annotated file Show diff for this revision Revisions of this file
VS1002.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/4DGL-uLCD-SE.lib	Mon Dec 12 14:31:10 2016 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/4180_1/code/4DGL-uLCD-SE/#e39a44de229a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VS1002.cpp	Mon Dec 12 14:31:10 2016 +0000
@@ -0,0 +1,279 @@
+#include "VS1002.h"
+#include "mbed.h"
+AnalogIn rotary1(p19);
+DigitalIn pause1(p28);
+
+
+/* ==================================================================
+ * Constructor
+ * =================================================================*/
+VS1002::VS1002(
+PinName mmosi, PinName mmiso, PinName ssck, PinName ccs, const char *name,
+         PinName mosi, PinName miso, PinName sck, PinName cs, PinName rst,
+         PinName dreq, PinName dcs, PinName vol)
+    :
+     _sd(mmosi, mmiso, ssck, ccs, name),
+     _spi(mosi, miso, sck), 
+     _CS(cs), 
+     _RST(rst), 
+     _DREQ(dreq),
+     _DCS(dcs), 
+     _VOL(vol) {
+    
+    }    
+
+/*===================================================================
+ * Functions
+ *==================================================================*/
+ 
+void VS1002::cs_low(void)
+{
+    _CS = 0;                                
+}
+void VS1002::cs_high(void)
+{
+    _CS = 1;                                
+}
+void VS1002::dcs_low(void)
+{
+    _DCS = 0;
+}
+void VS1002::dcs_high(void)
+{
+    _DCS = 1;
+}
+void VS1002::sci_en(void)                    //SCI enable
+{
+    cs_high();
+    dcs_high();
+    cs_low();
+}
+void VS1002::sci_dis(void)                    //SCI disable
+{
+    cs_high();
+}
+void VS1002::sdi_en(void)                    //SDI enable
+{
+    dcs_high();
+    cs_high();
+    dcs_low();
+}
+void VS1002::sdi_dis(void)                    //SDI disable
+{
+    dcs_high();
+}
+void VS1002::reset(void)                    //hardware reset
+{
+    wait(0.01);
+    _RST = 0;
+    wait(0.01);
+    _RST = 1;
+    wait(0.10);
+}
+void VS1002::power_down(void)                //hardware and software reset
+{
+    cs_low();
+    reset();
+    sci_write(0x00, SM_PDOWN);
+    wait(0.01);
+    reset();
+}
+void VS1002::sci_initialise(void)
+{
+    _RST = 1;                                //no reset
+    _spi.format(8,0);                        //spi 8bit interface, steady state low
+    _spi.frequency(1000000);                //rising edge data record, freq. 1Mhz
+    
+    cs_low();
+    for(int i=0; i<4; i++)
+    {
+    _spi.write(0xFF);                        //clock the chip a bit
+    }
+    cs_high();
+    dcs_high();
+    wait_us(5);
+}
+void VS1002::sdi_initialise(void)
+{
+    _spi.format(8,0);
+    _spi.frequency(7000000);                //set to 7MHz
+    
+    cs_high();
+    dcs_high();
+}
+void VS1002::sci_write(unsigned char address, unsigned short int data)
+{
+    sci_en();                                //enables SCI/disables SDI
+    
+    while(!_DREQ);                            //wait unitl data request is high
+    _spi.write(0x02);                        //SCI write
+    _spi.write(address);                    //register address
+    _spi.write((data >> 8) & 0xFF);            //write out first half of data word
+    _spi.write(data & 0xFF);                //write out second half of data word
+    
+    sci_dis();                                //enables SDI/disables SCI
+    wait_us(5);
+}
+void VS1002::sdi_write(unsigned char datum)
+{
+    sdi_en();
+    while(!_DREQ);
+    _spi.write(datum);
+    sci_dis();
+}
+unsigned short int VS1002::read(unsigned short int address)
+{
+    cs_low();                                //enables SCI/disables SDI
+    
+    while(!_DREQ);                            //wait unitl data request is high
+    _spi.write(0x03);                        //SCI write
+    _spi.write(address);                    //register address
+    unsigned short int received = _spi.write(0x00);    //write out dummy byte
+    received <<= 8;
+    received += _spi.write(0x00);            //write out dummy byte
+    
+    cs_high();                                //enables SDI/disables SCI
+    
+    return received;                        //return received word
+}
+void VS1002::sine_test_activate(unsigned char wave)
+{
+    cs_high();                                //enables SDI/disables SCI
+    
+    while(!_DREQ);                            //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
+
+    cs_low();                                //enables SCI/disables SDI
+}
+void VS1002::sine_test_deactivate(void)
+{
+    cs_high();
+    
+    while(!_DREQ);
+    _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
+}
+void VS1002::volume(void)
+{
+ #ifdef FIXED_VOL
+    unsigned char volumize = (0 * 255); // FIXED VOL (not support volume input)
+ #else
+    unsigned char volumize = (_VOL * 255);
+ #endif
+    while(!_DREQ);
+    
+    unsigned short int attenuation = ((256 * volumize) + volumize);
+    sci_write(0x0B, attenuation);
+   
+}
+
+void VS1002::play_song(int song_number)
+{
+    /*====== Song Select ======*/
+    
+//    char list[10000] = {0};
+    char list[1000] = {0};
+    char str[16] = {"/sd/"};
+    unsigned int startplace = 0;
+    unsigned int endplace = 0;
+    unsigned int play = 0;
+    num_of_files = 0;
+
+    DIR *d;
+    struct dirent *p;
+    d = opendir("/sd");
+    if(d != NULL) 
+           {
+               while((p = readdir(d)) != NULL) 
+           {
+               strcat(list, "*");
+               strcat(list, p->d_name);
+               num_of_files++;
+           }
+    }
+    else 
+    {
+         perror("Could not open directory!");
+    }
+    strcat(list, "*");                                //terminating *
+    if(num_of_files < song_number)
+    {
+        return;
+    }
+    while(play != song_number)
+    {
+        char symbol = list[startplace];
+        startplace++;
+        if(symbol == 0x2A)                        //0x2A = "*"    
+        {
+            play++;
+        }                        
+    }
+    play = 0;
+    while(play != (song_number+1))
+    {
+        char symbol = list[endplace];
+        endplace++;    
+        if(symbol == 0x2A)                        //0x2A = "*"    
+        {
+            play++;
+        }
+    }
+
+    strncat(str, &list[startplace], endplace-startplace);
+    str[(endplace-startplace)+3] = '\0';
+
+//printf("list: %s\r\n",list); //debug      
+
+    /*====== File Transfer ======*/
+
+     // return if not MP3 file
+    if (!((strstr(str,"MP3")!=NULL)||(strstr(str,"mp3")!=NULL))) return;
+    // display filename.mp3 to the lcd screen
+    //lcd.printf("Now Playing: %s\r\n",str);
+    
+    FILE *song;
+    unsigned char array[512];
+        
+    song = fopen(str, "rb");
+    
+    if(!song) 
+    {
+        exit(1);
+    }
+   
+       while((!feof(song))&&!pause1)
+    {
+           fread(&array, 1, 512, song);
+           for(int i=0; i<512; i++)
+           {
+#ifndef FS_ONLY
+            sdi_write(array[i]);
+        //    printf(".");
+#endif
+           }
+#ifndef FS_ONLY
+            volume();
+#endif
+       }
+    for(int n=0; n<2048; n++)
+       {
+#ifndef FS_ONLY
+           sdi_write(0x00);
+#endif
+       }
+    fclose(song);                                //close the file
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/VS1002.h	Mon Dec 12 14:31:10 2016 +0000
@@ -0,0 +1,74 @@
+#ifndef VS1002_H
+#define VS1002_H
+
+//#define FS_ONLY
+#define FIXED_VOL
+
+#include "mbed.h"
+#include "SDFileSystem.h"
+#include "string"
+#include "string.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
+
+
+class VS1002  {
+
+public:
+//    VS1002(int _mmosi, int _mmiso, int _ssck, int _ccs, const char* _name,
+// int _mosi, int _miso, int _sck, int _cs, int _rst, int _dreq, int _dcs, int _vol);
+    VS1002(PinName _mmosi, PinName _mmiso, PinName _ssck, PinName _ccs, const char* _name, 
+    PinName _mosi, PinName _miso, PinName _sck, PinName _cs, PinName _rst, PinName _dreq,
+    PinName _dcs, PinName _vol);
+    
+    void cs_low(void); 
+    void cs_high(void);
+    void dcs_low(void);
+    void dcs_high(void);
+    void sci_en(void);
+    void sci_dis(void);
+    void sdi_en(void);
+    void sdi_dis(void);
+    
+    void sci_initialise(void);
+    void sdi_initialise(void);
+    void reset(void);
+    void power_down(void);
+ 
+    void sci_write(unsigned char, unsigned short int);
+    void sdi_write(unsigned char);
+    unsigned short int read(unsigned short int);
+    void sine_test_activate(unsigned char);
+    void volume(void);
+    void sine_test_deactivate(void);
+    void play_song(int);
+    
+    int num_of_files;
+    
+    DigitalIn _DREQ;
+    DigitalOut _RST;
+    AnalogIn _VOL;
+    
+protected:
+
+    SPI _spi;
+    DigitalOut _CS;
+    DigitalOut _DCS;
+    SDFileSystem _sd;
+
+};
+#endif
--- a/main.cpp	Sat Dec 10 23:20:32 2016 +0000
+++ b/main.cpp	Mon Dec 12 14:31:10 2016 +0000
@@ -1,18 +1,17 @@
 #include "mbed.h"
 #include "SDFileSystem.h"
+#include "uLCD_4DGL.h"
+#include "VS1002.h"
 
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
-DigitalOut led4(LED4);
+uLCD_4DGL uLCD(p28, p27, p30);
 
 DigitalIn Dreq(p26);
 DigitalOut XDCS(p25);
 DigitalOut reset(p23);
 DigitalOut XCS(p22);
 
-SPI mp3Board(p11, p12, p13);
-SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
+SPI mp3Board(p11, p12, p13); // mosi, miso, sclk
+SDFileSystem sd(p5, p6, p7, p8, "sd"); // DI, DO, SCK, CS
 
 int killLoop = 1;
 int mp3Chunk;
@@ -22,17 +21,67 @@
     fseek(fp, 0, SEEK_END);
     int size = ftell(fp);
     fseek(fp, 0, SEEK_SET);
-    
     return size;
 }
 
+void sdi_write(unsigned char datum)
+{
+    XCS = 1;  
+    XDCS = 1;
+    XCS = 0;   
+    while(!Dreq);
+    mp3Board.write(datum);
+    XCS = 1;
+}
+
+/*
+void sdi_write(unsigned char datum)
+{
+    _CS = 1;  
+    _DCS = 1;
+    _CS = 0;   
+    while(!_DREQ);
+    _spi.write(datum);
+    _CS = 1;
+}
+////////////////////////////////////////////////////////////////////////////
+VS1002::VS1002(
+PinName mmosi, PinName mmiso, PinName ssck, PinName ccs, const char *name,
+         PinName mosi, PinName miso, PinName sck, PinName cs, PinName rst,
+         PinName dreq, PinName dcs, PinName vol)
+    :
+     _sd(mmosi, mmiso, ssck, ccs, name),
+     _spi(mosi, miso, sck), 
+     _CS(cs), 
+     _RST(rst), 
+     _DREQ(dreq),
+     _DCS(dcs), 
+     _VOL(vol) {
+    
+    }    */
+///////////////////////////////////////////////////////////////////////////
+
 int main() {
-    led1=0;
-    led2=0;
-    led3=0;
-    led4=0;
+    reset = 1;
+    //Sine test // So, the simplified procedure is:
+    XDCS = 1; //1) Always keep XDCS at logical 1.
+    XCS = 0; //2) Set XCS to 0, 
+    mp3Board.write(0x02); //0x02 is the write opcode
+    mp3Board.write(0x00); //0x00 is the address of the sci mode register
+    mp3Board.write(0x0A); //0x0A (11 in dec) is the SM_SDINEW bit    //send SCI command to set SCI_MODE with SM_SHARED and SM_SDINEW set to 1, 
+    XCS = 1;    //then set XCS back to 1.
+    mp3Board.write(0x53); //3) While XCS is 1, send sine test activation bytes, e.g. 0x53 0xef 0x6e 0x7e 0x00 0x00 0x00 0x00.
+    mp3Board.write(0xEF);
+    mp3Board.write(0x6E);
+    mp3Board.write(0x44);
+    mp3Board.write(0x00);
+    mp3Board.write(0x00);
+    mp3Board.write(0x00);
+    mp3Board.write(0x00);
     
     
+    /*
+    mp3Board.frequency(12000000);
     XCS=1;
     XCS=0; //pull xcs low to begin sci write. Sets mode to vs1002, 
     mp3Board.write(0x02); //0x02 is the write opcode
@@ -40,35 +89,81 @@
     mp3Board.write(0x0A); //0x0A (11 in dec) is the SM_SDINEW bit
     XCS=1; //pulled high to end write sequence
     
-    led1=1;
+    uLCD.locate(0,0);
+    uLCD.printf("Initialized...");
     
     FILE *mp3_file;
-    //printf("\n\n\nHello, wave world!\n");
-    mp3_file=fopen("/sd/Putties.mp3","rb");
-    
-    XDCS = 1;
-    XDCS = 0;
-    
-    
-    /*
-    fseek(mp3_file, 0, SEEK_END);
-    fileSize=ftell(mp3_file);
-    fseek(mp3_file, 0, SEEK_SET);
-    */
-    
-    fileSize=getFileSize(mp3_file);
+    unsigned char mp3Chunk[512];
+    mp3_file = fopen("/sd/GGW.mp3","rb");
     
-    led2=1;
-    
-    fread(&mp3Chunk, 1, 100, mp3_file);
-    led3=1;
-    mp3Board.write(mp3Chunk);
-    
-        
-    
-    fclose(mp3_file);
-    XDCS=1;
-    
-    led4=1;
-    
-}
+    if (mp3_file !=NULL)
+    {
+        uLCD.locate(0,1);
+        uLCD.printf("File found");
+        XDCS = 1;
+        XDCS = 0;
+        fileSize=getFileSize(mp3_file);
+            uLCD.locate(0,3);
+            uLCD.printf("Filesize: ");
+            uLCD.locate(0,4);
+            uLCD.printf(" %i", fileSize);
+        //fread(&mp3Chunk, 1, 1, mp3_file);
+            uLCD.locate(0,5);
+            uLCD.printf("mp3 file: ");
+            uLCD.locate(0,6);
+            uLCD.printf(" %i", mp3_file);
+            uLCD.locate(0,7);
+            //uLCD.printf("mp3 chunk: ");
+            //uLCD.locate(0,8);
+            //uLCD.printf(" %i", mp3Chunk);
+        //would have funtion here passing the 8 bits to arm assembler code, hence the necessatity for 8 btis only!
+        //mp3Board.write(mp3Chunk);
+            uLCD.locate(0,10);
+            uLCD.printf("File writing...");
+//////////////////////////////////////////////////////////////////////////////
+        fread(&mp3Chunk, 1, 512, mp3_file);
+        for(int i=0; i<512; i++)
+        {
+            #ifndef FS_ONLY
+            //mp3Board.write(mp3Chunk[i]);
+            sdi_write(mp3Chunk[i]);
+                uLCD.locate(0,11);
+                uLCD.printf("... %i", i);
+                uLCD.locate(0,12);
+                uLCD.printf(".. %i", mp3Chunk[i]);
+            #endif
+        }
+        #ifndef FS_ONLY
+            //Volume set to 0 dB
+            XCS = 1; 
+            XDCS = 1;
+            XCS = 0;                               //enables SCI/disables SDI
+            while(!Dreq);                            //wait unitl data request is high
+            mp3Board.write(0x02);                        //SCI write
+            mp3Board.write(0x0B);                    //register address
+            mp3Board.write((0 >> 8) & 0xFF);            //write out first half of data word
+            mp3Board.write(0 & 0xFF);                //write out second half of data word
+            XCS = 1;                                //enables SDI/disables SCI
+            wait_us(5);
+        #endif
+        for(int n=0; n<2048; n++)
+        {
+            #ifndef FS_ONLY
+            sdi_write(0x00);
+                uLCD.locate(0,13);
+                uLCD.printf("... %i", n);
+            #endif
+        }
+////////////////////////////////////////////////////////////////////////   
+        fclose(mp3_file);
+            XDCS=1;
+            uLCD.locate(0,14);
+            uLCD.printf("File closing."); 
+    }
+    else
+    {
+        uLCD.locate(0,2);
+        uLCD.printf("File not found");
+    }
+    return 0;*/
+}
\ No newline at end of file