changes for speed up.

Dependencies:   FATFileSystemEx

Dependents:   nlgplay nlgplay_130

Fork of SDFileSystem by Mbed

Revision:
7:3701e9942a16
Parent:
4:e6b0a24df912
--- a/SDFileSystem.cpp	Wed Aug 20 00:29:53 2014 +0000
+++ b/SDFileSystem.cpp	Mon Feb 08 05:49:06 2016 +0000
@@ -117,14 +117,16 @@
 #include "mbed_debug.h"
 
 // SD card data clock speed (def. 1000000)
-#define SD_SPEED 8000000
+#define SD_SPEED 12000000
+//#define SD_SPEED 24000000
 
 #define SD_COMMAND_TIMEOUT 5000
 
 #define SD_DBG             0
 
 SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) :
-    FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs) {
+    FATFileSystem(name), _cs(cs) {
+    _spi = new SPI(mosi, miso, sclk),
     _cs = 1;
 }
 
@@ -142,20 +144,25 @@
 #define SDCARD_V2   2 //!< v2.x High Capacity
 #define SDCARD_V2HC 3 //!< Not recognised as an SD Card
 
+SPI *SDFileSystem::getSPI(void)
+{
+    return _spi;
+}
+
 int SDFileSystem::initialise_card() {
     // Set to 100kHz for initialisation, and clock card with cs = 1
-    _spi.frequency(100000);
+    _spi->frequency(100000);
     _cs = 1;
     for (int i = 0; i < 16; i++) {
-        _spi.write(0xFF);
+        _spi->write(0xFF);
     }
-    
+
     // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
     if (_cmd(0, 0) != R1_IDLE_STATE) {
         debug("No disk, or could not put SD card in to SPI idle state\n");
         return SDCARD_FAIL;
     }
-    
+
     // send CMD8 to determine whther it is ver 2.x
     int r = _cmd8();
     if (r == R1_IDLE_STATE) {
@@ -177,7 +184,7 @@
             return SDCARD_V1;
         }
     }
-    
+
     debug("Timeout waiting for v1.x card\n");
     return SDCARD_FAIL;
 }
@@ -194,7 +201,7 @@
             return SDCARD_V2;
         }
     }
-    
+
     debug("Timeout waiting for v2.x card\n");
     return SDCARD_FAIL;
 }
@@ -203,14 +210,14 @@
     int i = initialise_card();
     debug_if(SD_DBG, "init card = %d\n", i);
     _sectors = _sd_sectors();
-    
+
     // Set block length to 512 (CMD16)
     if (_cmd(16, 512) != 0) {
         debug("Set 512-byte block timed out\n");
         return 1;
     }
-    
-    _spi.frequency(SD_SPEED); // Set to 1MHz for data transfer
+
+    _spi->frequency(SD_SPEED); // Set to 1MHz for data transfer
     return 0;
 }
 
@@ -219,7 +226,7 @@
     if (_cmd(24, block_number * cdv) != 0) {
         return 1;
     }
-    
+
     // send the data block
     _write(buffer, 512);
     return 0;
@@ -230,7 +237,7 @@
     if (_cmd(17, block_number * cdv) != 0) {
         return 1;
     }
-    
+
     // receive the data
     _read(buffer, 512);
     return 0;
@@ -244,48 +251,48 @@
 // PRIVATE FUNCTIONS
 int SDFileSystem::_cmd(int cmd, int arg) {
     _cs = 0;
-    
+
     // send a command
-    _spi.write(0x40 | cmd);
-    _spi.write(arg >> 24);
-    _spi.write(arg >> 16);
-    _spi.write(arg >> 8);
-    _spi.write(arg >> 0);
-    _spi.write(0x95);
-    
+    _spi->write(0x40 | cmd);
+    _spi->write(arg >> 24);
+    _spi->write(arg >> 16);
+    _spi->write(arg >> 8);
+    _spi->write(arg >> 0);
+    _spi->write(0x95);
+
     // wait for the repsonse (response[7] == 0)
     for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
-        int response = _spi.write(0xFF);
+        int response = _spi->write(0xFF);
         if (!(response & 0x80)) {
             _cs = 1;
-            _spi.write(0xFF);
+            _spi->write(0xFF);
             return response;
         }
     }
     _cs = 1;
-    _spi.write(0xFF);
+    _spi->write(0xFF);
     return -1; // timeout
 }
 int SDFileSystem::_cmdx(int cmd, int arg) {
     _cs = 0;
-    
+
     // send a command
-    _spi.write(0x40 | cmd);
-    _spi.write(arg >> 24);
-    _spi.write(arg >> 16);
-    _spi.write(arg >> 8);
-    _spi.write(arg >> 0);
-    _spi.write(0x95);
-    
+    _spi->write(0x40 | cmd);
+    _spi->write(arg >> 24);
+    _spi->write(arg >> 16);
+    _spi->write(arg >> 8);
+    _spi->write(arg >> 0);
+    _spi->write(0x95);
+
     // wait for the repsonse (response[7] == 0)
     for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
-        int response = _spi.write(0xFF);
+        int response = _spi->write(0xFF);
         if (!(response & 0x80)) {
             return response;
         }
     }
     _cs = 1;
-    _spi.write(0xFF);
+    _spi->write(0xFF);
     return -1; // timeout
 }
 
@@ -293,107 +300,107 @@
 int SDFileSystem::_cmd58() {
     _cs = 0;
     int arg = 0;
-    
+
     // send a command
-    _spi.write(0x40 | 58);
-    _spi.write(arg >> 24);
-    _spi.write(arg >> 16);
-    _spi.write(arg >> 8);
-    _spi.write(arg >> 0);
-    _spi.write(0x95);
-    
+    _spi->write(0x40 | 58);
+    _spi->write(arg >> 24);
+    _spi->write(arg >> 16);
+    _spi->write(arg >> 8);
+    _spi->write(arg >> 0);
+    _spi->write(0x95);
+
     // wait for the repsonse (response[7] == 0)
     for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
-        int response = _spi.write(0xFF);
+        int response = _spi->write(0xFF);
         if (!(response & 0x80)) {
-            int ocr = _spi.write(0xFF) << 24;
-            ocr |= _spi.write(0xFF) << 16;
-            ocr |= _spi.write(0xFF) << 8;
-            ocr |= _spi.write(0xFF) << 0;
+            int ocr = _spi->write(0xFF) << 24;
+            ocr |= _spi->write(0xFF) << 16;
+            ocr |= _spi->write(0xFF) << 8;
+            ocr |= _spi->write(0xFF) << 0;
             _cs = 1;
-            _spi.write(0xFF);
+            _spi->write(0xFF);
             return response;
         }
     }
     _cs = 1;
-    _spi.write(0xFF);
+    _spi->write(0xFF);
     return -1; // timeout
 }
 
 int SDFileSystem::_cmd8() {
     _cs = 0;
-    
+
     // send a command
-    _spi.write(0x40 | 8); // CMD8
-    _spi.write(0x00);     // reserved
-    _spi.write(0x00);     // reserved
-    _spi.write(0x01);     // 3.3v
-    _spi.write(0xAA);     // check pattern
-    _spi.write(0x87);     // crc
-    
+    _spi->write(0x40 | 8); // CMD8
+    _spi->write(0x00);     // reserved
+    _spi->write(0x00);     // reserved
+    _spi->write(0x01);     // 3.3v
+    _spi->write(0xAA);     // check pattern
+    _spi->write(0x87);     // crc
+
     // wait for the repsonse (response[7] == 0)
     for (int i = 0; i < SD_COMMAND_TIMEOUT * 1000; i++) {
         char response[5];
-        response[0] = _spi.write(0xFF);
+        response[0] = _spi->write(0xFF);
         if (!(response[0] & 0x80)) {
             for (int j = 1; j < 5; j++) {
-                response[i] = _spi.write(0xFF);
+                response[i] = _spi->write(0xFF);
             }
             _cs = 1;
-            _spi.write(0xFF);
+            _spi->write(0xFF);
             return response[0];
         }
     }
     _cs = 1;
-    _spi.write(0xFF);
+    _spi->write(0xFF);
     return -1; // timeout
 }
 
 int SDFileSystem::_read(uint8_t *buffer, uint32_t length) {
     _cs = 0;
-    
+
     // read until start byte (0xFF)
-    while (_spi.write(0xFF) != 0xFE);
-    
+    while (_spi->write(0xFF) != 0xFE);
+
     // read data
     for (int i = 0; i < length; i++) {
-        buffer[i] = _spi.write(0xFF);
+        buffer[i] = _spi->write(0xFF);
     }
-    _spi.write(0xFF); // checksum
-    _spi.write(0xFF);
-    
+    _spi->write(0xFF); // checksum
+    _spi->write(0xFF);
+
     _cs = 1;
-    _spi.write(0xFF);
+    _spi->write(0xFF);
     return 0;
 }
 
 int SDFileSystem::_write(const uint8_t*buffer, uint32_t length) {
     _cs = 0;
-    
+
     // indicate start of block
-    _spi.write(0xFE);
-    
+    _spi->write(0xFE);
+
     // write the data
     for (int i = 0; i < length; i++) {
-        _spi.write(buffer[i]);
+        _spi->write(buffer[i]);
     }
-    
+
     // write the checksum
-    _spi.write(0xFF);
-    _spi.write(0xFF);
-    
+    _spi->write(0xFF);
+    _spi->write(0xFF);
+
     // check the response token
-    if ((_spi.write(0xFF) & 0x1F) != 0x05) {
+    if ((_spi->write(0xFF) & 0x1F) != 0x05) {
         _cs = 1;
-        _spi.write(0xFF);
+        _spi->write(0xFF);
         return 1;
     }
-    
+
     // wait for write to finish
-    while (_spi.write(0xFF) == 0);
-    
+    while (_spi->write(0xFF) == 0);
+
     _cs = 1;
-    _spi.write(0xFF);
+    _spi->write(0xFF);
     return 0;
 }
 
@@ -415,33 +422,33 @@
     uint32_t block_len, mult, blocknr, capacity;
     uint32_t hc_c_size;
     uint64_t blocks;
-    
+
     // CMD9, Response R2 (R1 byte + 16-byte block read)
     if (_cmdx(9, 0) != 0) {
         debug("Didn't get a response from the disk\n");
         return 0;
     }
-    
+
     uint8_t csd[16];
     if (_read(csd, 16) != 0) {
         debug("Couldn't read csd response from disk\n");
         return 0;
     }
-    
+
     // csd_structure : csd[127:126]
     // c_size        : csd[73:62]
     // c_size_mult   : csd[49:47]
     // read_bl_len   : csd[83:80] - the *maximum* read block length
-    
+
     int csd_structure = ext_bits(csd, 127, 126);
-    
+
     switch (csd_structure) {
         case 0:
             cdv = 512;
             c_size = ext_bits(csd, 73, 62);
             c_size_mult = ext_bits(csd, 49, 47);
             read_bl_len = ext_bits(csd, 83, 80);
-            
+
             block_len = 1 << read_bl_len;
             mult = 1 << (c_size_mult + 2);
             blocknr = (c_size + 1) * mult;
@@ -449,14 +456,14 @@
             blocks = capacity / 512;
             debug_if(SD_DBG, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks);
             break;
-        
+
         case 1:
             cdv = 1;
             hc_c_size = ext_bits(csd, 63, 48);
             blocks = (hc_c_size+1)*1024;
             debug_if(SD_DBG, "\n\rSDHC Card \n\rhc_c_size: %d\n\rcapacity: %lld \n\rsectors: %lld\n\r", hc_c_size, blocks*512, blocks);
             break;
-        
+
         default:
             debug("CSD struct unsupported\r\n");
             return 0;