strange

Dependencies:   FATFileSystem

Fork of SDFileSystem by mbed official

Files at this revision

API Documentation at this revision

Comitter:
rproctor6
Date:
Wed Sep 30 20:05:44 2015 +0000
Parent:
2:c8f66dc765d4
Commit message:
kevin;

Changed in this revision

SDFileSystem.cpp Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.h Show annotated file Show diff for this revision Revisions of this file
--- a/SDFileSystem.cpp	Thu Nov 29 10:56:21 2012 +0000
+++ b/SDFileSystem.cpp	Wed Sep 30 20:05:44 2015 +0000
@@ -104,7 +104,7 @@
  *
  * Single Block Read and Write
  * ---------------------------
- *
+ 
  * Block transfers have a byte header, followed by the data, followed
  * by a 16-bit CRC. In our case, the data will always be 512 bytes.
  *
@@ -119,9 +119,11 @@
 
 #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) {
+SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName cd, const char* name) :
+    FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs), _cd(cd)
+{
     _cs = 1;
+    _cd.mode(PullUp);
 }
 
 #define R1_IDLE_STATE           (1 << 0)
@@ -141,21 +143,33 @@
 #define SDCARD_V1   1
 #define SDCARD_V2   2
 #define SDCARD_V2HC 3
-
-int SDFileSystem::initialise_card() {
+/*int SDFileSystem::SD_inserted()
+{
+    if (_cd == 0) {
+        return 0;
+    }
+    else if (_cd == 1) {
+    return 1;
+        }
+        */
+int SDFileSystem::hasCard(){
+return _cd.read();
+}
+int SDFileSystem::initialise_card()
+{
     // Set to 100kHz for initialisation, and clock card with cs = 1
     _spi.frequency(100000);
     _cs = 1;
     for (int i = 0; i < 16; i++) {
         _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) {
@@ -168,7 +182,8 @@
     }
 }
 
-int SDFileSystem::initialise_card_v1() {
+int SDFileSystem::initialise_card_v1()
+{
     for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
         _cmd(55, 0);
         if (_cmd(41, 0) == 0) {
@@ -177,12 +192,13 @@
             return SDCARD_V1;
         }
     }
-    
+
     debug("Timeout waiting for v1.x card\n");
     return SDCARD_FAIL;
 }
 
-int SDFileSystem::initialise_card_v2() {
+int SDFileSystem::initialise_card_v2()
+{
     for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
         wait_ms(50);
         _cmd58();
@@ -194,57 +210,70 @@
             return SDCARD_V2;
         }
     }
-    
+
     debug("Timeout waiting for v2.x card\n");
     return SDCARD_FAIL;
 }
 
-int SDFileSystem::disk_initialize() {
+int SDFileSystem::disk_initialize()
+{
     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(1000000); // Set to 1MHz for data transfer
     return 0;
 }
 
-int SDFileSystem::disk_write(const uint8_t *buffer, uint64_t block_number) {
+int SDFileSystem::disk_write(const uint8_t *buffer, uint64_t block_number)
+{
     // set write address for single block (CMD24)
     if (_cmd(24, block_number * cdv) != 0) {
         return 1;
     }
-    
+
     // send the data block
     _write(buffer, 512);
     return 0;
 }
 
-int SDFileSystem::disk_read(uint8_t *buffer, uint64_t block_number) {
+int SDFileSystem::disk_read(uint8_t *buffer, uint64_t block_number)
+{
     // set read address for single block (CMD17)
     if (_cmd(17, block_number * cdv) != 0) {
         return 1;
     }
-    
+
     // receive the data
     _read(buffer, 512);
     return 0;
 }
 
-int SDFileSystem::disk_status() { return 0; }
-int SDFileSystem::disk_sync() { return 0; }
-uint64_t SDFileSystem::disk_sectors() { return _sectors; }
+int SDFileSystem::disk_status()
+{
+    return 0;
+}
+int SDFileSystem::disk_sync()
+{
+    return 0;
+}
+uint64_t SDFileSystem::disk_sectors()
+{
+    return _sectors;
+}
 
 
 // PRIVATE FUNCTIONS
-int SDFileSystem::_cmd(int cmd, int arg) {
+int SDFileSystem::_cmd(int cmd, int arg)
+{
     _cs = 0;
-    
+
     // send a command
     _spi.write(0x40 | cmd);
     _spi.write(arg >> 24);
@@ -252,7 +281,7 @@
     _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);
@@ -266,9 +295,10 @@
     _spi.write(0xFF);
     return -1; // timeout
 }
-int SDFileSystem::_cmdx(int cmd, int arg) {
+int SDFileSystem::_cmdx(int cmd, int arg)
+{
     _cs = 0;
-    
+
     // send a command
     _spi.write(0x40 | cmd);
     _spi.write(arg >> 24);
@@ -276,7 +306,7 @@
     _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);
@@ -290,10 +320,11 @@
 }
 
 
-int SDFileSystem::_cmd58() {
+int SDFileSystem::_cmd58()
+{
     _cs = 0;
     int arg = 0;
-    
+
     // send a command
     _spi.write(0x40 | 58);
     _spi.write(arg >> 24);
@@ -301,7 +332,7 @@
     _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);
@@ -320,9 +351,10 @@
     return -1; // timeout
 }
 
-int SDFileSystem::_cmd8() {
+int SDFileSystem::_cmd8()
+{
     _cs = 0;
-    
+
     // send a command
     _spi.write(0x40 | 8); // CMD8
     _spi.write(0x00);     // reserved
@@ -330,7 +362,7 @@
     _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];
@@ -349,55 +381,58 @@
     return -1; // timeout
 }
 
-int SDFileSystem::_read(uint8_t *buffer, uint32_t length) {
+int SDFileSystem::_read(uint8_t *buffer, uint32_t length)
+{
     _cs = 0;
-    
+
     // read until start byte (0xFF)
     while (_spi.write(0xFF) != 0xFE);
-    
+
     // read data
     for (int i = 0; i < length; i++) {
         buffer[i] = _spi.write(0xFF);
     }
     _spi.write(0xFF); // checksum
     _spi.write(0xFF);
-    
+
     _cs = 1;
     _spi.write(0xFF);
     return 0;
 }
 
-int SDFileSystem::_write(const uint8_t*buffer, uint32_t length) {
+int SDFileSystem::_write(const uint8_t*buffer, uint32_t length)
+{
     _cs = 0;
-    
+
     // indicate start of block
     _spi.write(0xFE);
-    
+
     // write the data
     for (int i = 0; i < length; i++) {
         _spi.write(buffer[i]);
     }
-    
+
     // write the checksum
     _spi.write(0xFF);
     _spi.write(0xFF);
-    
+
     // check the response token
     if ((_spi.write(0xFF) & 0x1F) != 0x05) {
         _cs = 1;
         _spi.write(0xFF);
         return 1;
     }
-    
+
     // wait for write to finish
     while (_spi.write(0xFF) == 0);
-    
+
     _cs = 1;
     _spi.write(0xFF);
     return 0;
 }
 
-static uint32_t ext_bits(unsigned char *data, int msb, int lsb) {
+static uint32_t ext_bits(unsigned char *data, int msb, int lsb)
+{
     uint32_t bits = 0;
     uint32_t size = 1 + msb - lsb;
     for (int i = 0; i < size; i++) {
@@ -410,38 +445,39 @@
     return bits;
 }
 
-uint64_t SDFileSystem::_sd_sectors() {
+uint64_t SDFileSystem::_sd_sectors()
+{
     uint32_t c_size, c_size_mult, read_bl_len;
     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 +485,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;
--- a/SDFileSystem.h	Thu Nov 29 10:56:21 2012 +0000
+++ b/SDFileSystem.h	Wed Sep 30 20:05:44 2015 +0000
@@ -51,13 +51,15 @@
      * @param cs   DigitalOut pin used as SD Card chip select
      * @param name The name used to access the virtual filesystem
      */
-    SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name);
+    SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName cd, const char* name);
+    int SD_inserted();
     virtual int disk_initialize();
     virtual int disk_status();
     virtual int disk_read(uint8_t * buffer, uint64_t block_number);
     virtual int disk_write(const uint8_t * buffer, uint64_t block_number);
     virtual int disk_sync();
     virtual uint64_t disk_sectors();
+    int hasCard();
 
 protected:
 
@@ -76,6 +78,7 @@
     
     SPI _spi;
     DigitalOut _cs;
+    DigitalIn _cd;
     int cdv;
 };