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.
Diff: SDCard.h
- Revision:
- 5:d85e20b6b904
- Parent:
- 4:9a5878d316d5
- Child:
- 6:ddf09d859ed7
--- a/SDCard.h Fri Aug 27 00:59:28 2010 +0000 +++ b/SDCard.h Sat Jan 15 05:58:22 2011 +0000 @@ -19,7 +19,7 @@ unsigned char CommandCRCTable[256]; //CRC7 lookup table unsigned char DataCRCTable[512]; - //CRC CCITT lookup table + //CRC16 CCITT lookup table unsigned char OCR[4]; //operating conditions register unsigned char CSD[16]; @@ -32,31 +32,39 @@ //low for low-capacity, high for high-capacity bool CRCMode; //low to disable CRCs, high to enable CRCs - unsigned char Workspace[5]; - //all-purpose work area unsigned int Timeout; //timeout limit unsigned int t; //timeout counter + unsigned char Workspace[5]; + //all-purpose work area unsigned char Initialize(); //complete all initialization operations - void Command(unsigned char Index, unsigned int Argument, unsigned char* Response); + void Command(unsigned char Index, + unsigned int Argument, unsigned char* Response); //sends command to the SD card - void CommandCRC(unsigned char* IndexPtr, unsigned int* ArgumentPtr, unsigned char* Result); - //calculates the command CRC7 result with a lookup table - void DataCRC(unsigned short Length, unsigned char* Data, unsigned char* Result); - //calculates the CRC16 result of the input with a lookup table - void GenerateCRCTable( - unsigned char Size, unsigned long long Generator, unsigned char* Table); - //pre-calculates CRC results for efficient checking + 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, + //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 virtual unsigned char disk_initialize(); virtual unsigned char disk_status(); - virtual unsigned char disk_read( - unsigned char* buff, unsigned long sector, unsigned char count); - virtual unsigned char disk_write( - const unsigned char* buff, unsigned long sector, unsigned char count); + virtual unsigned char disk_read(unsigned char* buff, + unsigned long sector, unsigned char count); + virtual unsigned char disk_write(const unsigned char* buff, + unsigned long sector, unsigned char count); virtual unsigned char disk_sync(); virtual unsigned long disk_sector_count(); virtual unsigned short disk_sector_size(); @@ -64,26 +72,35 @@ //FAT system virtual function definitions, called by 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 + SDCard(PinName mosi, PinName miso, PinName sck, PinName cs, + const char* DiskName); + //constructor requires SPI and DigitalOut pins, and a + //directory name virtual ~SDCard(); //destructor deallocates tables and workspace unsigned char Log(unsigned char Control, unsigned char Data); - //multipurpose raw data-logging method with three modes of operation + //multipurpose 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 + // 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 "don't care" bits + //data of sync or read operations are stuff bits unsigned char Write(unsigned int Address, unsigned char* Data); - unsigned char Write(unsigned int Address, unsigned char SectorCount, unsigned char* Data); - //write data sectors to the card + //write single 512B sector to card at Address from Data + unsigned char Write(unsigned int Address, + unsigned char SectorCount, unsigned char* Data); + //write SectorCount 512B sectors to card at Address from Data unsigned char Read(unsigned int Address, unsigned char* Data); - unsigned char Read(unsigned int Address, unsigned char SectorCount, unsigned char* Data); - //read data sectors from the card + //read single 512B sector from card at 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 unsigned char SelectCRCMode(bool Mode); - //turn CRC mode on or off, default is off + //toggle CRC mode; high for on or low for off, default is off void SetTimeout(unsigned int Retries); //change the number of retries for interface functions; //increase if lines are unreliable; default is 1024