modified for VS1033

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

Fork of VS1033 by en 129

Revision:
10:a8594af40a50
Parent:
9:1dd0ecaac8e5
Child:
11:da08a7b0947d
--- a/VS1053.cpp	Thu Jul 16 18:17:42 2015 +0000
+++ b/VS1053.cpp	Fri Jul 24 11:05:54 2015 +0000
@@ -9,12 +9,12 @@
 
 /** Constructor of class VS1053. */
 VS1053::VS1053(PinName mosiPin, PinName misoPin, PinName sckPin,
-               PinName csPin, PinName bsyncPin, PinName dreqPin,
+               PinName csPin, PinName dcsPin, PinName dreqPin,
                PinName rstPin, uint32_t spiFrequency)
 :
     spi(mosiPin, misoPin, sckPin),
     cs(csPin),
-    bsync(bsyncPin),
+    dcs(dcsPin),
     dreq(dreqPin),
     rst(rstPin)
 {
@@ -22,7 +22,7 @@
 
     // Initialize outputs
     cs = 1;
-    bsync = 1;
+    dcs = 1;
     rst = 1;
 }
 
@@ -33,29 +33,29 @@
 //SCI enable
 void VS1053::sci_en(void)
 {
-    cs = 1;
-    bsync = 1;
+    dcs = 1;
     cs = 0;
 }
 
 //SCI disable
 void VS1053::sci_dis(void)
 {
+    dcs = 1;
     cs = 1;
 }
 
 //SDI enable
 void VS1053::sdi_en(void)
 {
-    bsync = 1;
     cs = 1;
-    bsync = 0;
+    dcs = 0;
 }
 
 //SDI disable
 void VS1053::sdi_dis(void)
 {
-    bsync = 1;
+    cs = 1;
+    dcs = 1;
 }
 
 /** Make a hardware reset by hitting VS1053's RESET pin. */
@@ -78,7 +78,7 @@
         spi.write(0xFF);                        //clock the chip a bit
     }
     cs = 1;
-    bsync = 1;
+    dcs = 1;
     wait_us(5);
     writeReg(SCI_MODE,(SM_SDINEW));
     writeReg(SCI_CLOCKF, 0x9800);
@@ -90,8 +90,11 @@
     spi.format(8, 0);
     spi.frequency(useSpiFreq);
 
+    writeReg(SCI_CLOCKF, 0xc000);  // SC_MULT=6 (4.0x)
+    wait(0.01);
+
     cs = 1;
-    bsync = 1;
+    dcs = 1;
 }
 
 void VS1053::VolControl(uint16_t vol)
@@ -110,6 +113,13 @@
     sdi_dis();
 }
 
+bool VS1053::checkDREQ()
+{
+    bool dreq_return = 0;
+    dreq_return = dreq;
+    return dreq_return;
+}
+
 /** SDI Send a data block specified as a pointer to VS1053.
  *  @return Data length successfully sent.
  */
@@ -120,9 +130,9 @@
     sdi_en();
     while (length) {
         n = length < 32 ? length : 32;
-        while (!dreq);
         for (uint32_t i = 0; i < n; i++)
         {
+            while (!dreq);
             spi.write(*data++);
             sizeSent++; length--;
         }
@@ -156,16 +166,16 @@
        
     // Send 0 2056 times
     length = 257;
+    sdi_en();
     while (length) {
         n = length < 32 ? length : 32;
-        sdi_en();
-        while (!dreq);
         for (uint32_t i = 0; i < n; i++) {
+            while (!dreq);
             spi.write(0x00);
             length--;
         }
-        sdi_dis();
     }
+    sdi_dis();
     // Check if both HDAT0 and HDAT1 are cleared
     return readReg(SCI_HDAT0) == 0x0000 && readReg(SCI_HDAT1) == 0x0000;
 }
@@ -195,13 +205,13 @@
     if (addr > 0x0f) {
         return 0x0000;
     }
-    cs = 0;
+    sci_en();
     while (!dreq);
     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
-    cs = 1;
+    sci_dis();
     return word;
 }