SD Card Interface class. Log raw data bytes to memory addresses of your choice, or format the card and use the FAT file system to write files.

Dependencies:   mbed

Revision:
6:ddf09d859ed7
Parent:
5:d85e20b6b904
--- a/SDCard.h	Sat Jan 15 05:58:22 2011 +0000
+++ b/SDCard.h	Sun Jan 16 09:20:30 2011 +0000
@@ -10,54 +10,57 @@
 #include "mbed.h"
 #include "FATFileSystem.h"
 
-class SDCard : private FATFileSystem
+class SDCard : public FATFileSystem
 {
     private:
         SPI DataLines;
         DigitalOut ChipSelect;
             //physical chip interface
-        unsigned char CommandCRCTable[256];
-            //CRC7 lookup table
-        unsigned char DataCRCTable[512];
-            //CRC16 CCITT lookup table
-        unsigned char OCR[4];
-            //operating conditions register
-        unsigned char CSD[16];
-            //card-specific data register
+
+        unsigned int t;
+            //timeout counter
+        unsigned int Timeout;
+            //timeout limit
+        bool CRCMode;
+            //low: CRCs disabled, high: CRCs enabled
+        bool Capacity;
+            //low: low-capacity, high: high-capacity
+        bool Version;
+            //low: version 1, high: version 2
+        unsigned char Status;
+            //0x00: Ready, 0x01: not initialized
         unsigned char FSR[64];
             //function status register
-        bool Version;
-            //card version, low for 1, high for 2
-        bool Capacity;
-            //low for low-capacity, high for high-capacity
-        bool CRCMode;
-            //low to disable CRCs, high to enable CRCs
-        unsigned int Timeout;
-            //timeout limit
-        unsigned int t;
-            //timeout counter
-        unsigned char Workspace[5];
-            //all-purpose work area
+        unsigned char CSD[16];
+            //card-specific data register
+        unsigned char OCR[4];
+            //operating conditions register
+        unsigned char DataCRCTable[512];
+            //CRC16 CCITT lookup table
+        unsigned char CommandCRCTable[256];
+            //CRC7 SD command lookup table
 
         unsigned char Initialize();
             //complete all initialization operations
         void Command(unsigned char Index,
             unsigned int Argument, unsigned char* Response);
-            //sends command to the SD card
+            //sends the SD command with the given Index and Argument to the SD
+            //card and stores the SD Response
         void CommandCRC(unsigned char* IndexPtr,
             unsigned int* ArgumentPtr, unsigned char* Result);
-            //calculates the SD card proprietary CRC7 result of an SD card
-            //command, records one-byte solution in Result
-        void DataCRC(unsigned short Length,
-            unsigned char* Data, unsigned char* Result);
-            //calculates the CRC16 result of the number of bytes in Data
-            //given by Length and stores the two-byte solution in Result,
+            //calculates the SD proprietary CRC7 result of an SD command
+            //(composed of a command index and argument) and stores the one-
+            //byte solution in Result
+        void DataCRC(
+            unsigned short Length, unsigned char* Data, unsigned char* Result);
+            //calculates the CRC16 CCITT result of the number of bytes in Data
+            //given by Length and stores the two-byte solution in Result
             //assumes DataCRCTable has already been calculated
         void GenerateCRCTable(unsigned char Size,
             unsigned long long Generator, unsigned char* Table);
-            //pre-calculates CRC results for efficient checking, assumes
-            //pre-allocated Table is large enough to hold the number of
-            //bytes given in Size
+            //pre-calculates CRC results from the given Generator for efficient
+            //checking; assumes pre-allocated Table is large enough to hold the
+            //number of bytes given in Size
 
         virtual unsigned char disk_initialize();
         virtual unsigned char disk_status();
@@ -69,41 +72,49 @@
         virtual unsigned long disk_sector_count();
         virtual unsigned short disk_sector_size();
         virtual unsigned long disk_block_size();
-            //FAT system virtual function definitions, called by FAT module
+            //FAT system virtual functions that are called by the FAT module
 
     public:
         SDCard(PinName mosi, PinName miso, PinName sck, PinName cs,
             const char* DiskName);
-            //constructor requires SPI and DigitalOut pins, and a
-            //directory name
+            //constructor needs SPI pins, DigitalOut pins, and a directory name
         virtual ~SDCard();
-            //destructor deallocates tables and workspace
+            //destructor deallocates tables and registers
+        unsigned char Format(unsigned int AllocationUnit);
+            //formats the card FAT with given AllocationUnit in sectors; the
+            //maximum is 32768 sectors
         unsigned char Log(unsigned char Control, unsigned char Data);
-            //multipurpose raw data-logging method with three modes
+            //multipurpose single-byte raw data-logging method with three modes
             //Control   description
             //  0       synchronizes card and resets internal counter to
-            //              finalize I/O operations
-            //  1       successively write input to a raw data byte in
-            //              order starting at address 0
-            //  2       successively read and return a raw data byte in
-            //              order starting at address 0
-            //return data from sync or write operations, and input
-            //data of sync or read operations are stuff bits
+            //              finalize read or write operations
+            //  1       successively write Data to a raw byte address in order
+            //              starting at address 0
+            //  2       successively read and return a raw data byte in order
+            //              starting at address 0
+            //return byte from sync or write operations and input Data of sync
+            //or read operations are not used; writing with this function will
+            //deformat the drive
         unsigned char Write(unsigned int Address, unsigned char* Data);
-            //write single 512B sector to card at Address from Data
+            //write the first 512 byte sector of Data to the card at the given
+            //Address
         unsigned char Write(unsigned int Address,
             unsigned char SectorCount, unsigned char* Data);
-            //write SectorCount 512B sectors to card at Address from Data
+            //write the first given SectorCount of 512 byte sectors of Data to
+            //the card at the given Address
         unsigned char Read(unsigned int Address, unsigned char* Data);
-            //read single 512B sector from card at Address into Data
+            //read the first 512 byte sector from card at the given Address
+            //into Data
         unsigned char Read(unsigned int Address,
             unsigned char SectorCount,  unsigned char* Data);
-            //read SectorCount 512B sectors from card at Address into Data
+            //read the first given SectorCount of 512 byte sectors from the
+            //card at the given Address into Data
         unsigned char SelectCRCMode(bool Mode);
-            //toggle CRC mode; high for on or low for off, default is off
+            //toggle CRC mode; low: CRCs disabled, high: CRCs enabled, default:
+            //CRCs disabled
         void SetTimeout(unsigned int Retries);
-            //change the number of retries for interface functions;
-            //increase if lines are unreliable; default is 1024
+            //change the number of retries for interface functions; increase if
+            //lines are unreliable; default: 1024, minimum: 1024
 };
 
 #endif
\ No newline at end of file