modified for VS1033

Dependents:   11U68_MP3Player with TFTLCD 11U68_MP3Player-with-TFTLCD Mp3_1

Fork of VS1033 by en 129

Revision:
7:b61cd12eabc5
Parent:
6:1f57fbd3aeb5
Child:
8:171effe49517
--- 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();
+}