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.
SDCard.h@1:94c648931f84, 2010-08-07 (annotated)
- Committer:
- Blaze513
- Date:
- Sat Aug 07 18:32:30 2010 +0000
- Revision:
- 1:94c648931f84
- Parent:
- 0:f3870f76a890
- Child:
- 3:210eb67b260c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Blaze513 | 0:f3870f76a890 | 1 | #ifndef SDCardLibrary |
Blaze513 | 0:f3870f76a890 | 2 | #define SDCardLibrary |
Blaze513 | 0:f3870f76a890 | 3 | |
Blaze513 | 0:f3870f76a890 | 4 | #include "stdint.h" |
Blaze513 | 0:f3870f76a890 | 5 | #include "mbed.h" |
Blaze513 | 1:94c648931f84 | 6 | #include "FATFileSystem.h" |
Blaze513 | 0:f3870f76a890 | 7 | |
Blaze513 | 1:94c648931f84 | 8 | class SDCard : private FATFileSystem |
Blaze513 | 0:f3870f76a890 | 9 | { |
Blaze513 | 0:f3870f76a890 | 10 | private: |
Blaze513 | 0:f3870f76a890 | 11 | SPI DataLines; |
Blaze513 | 0:f3870f76a890 | 12 | DigitalOut ChipSelect; |
Blaze513 | 1:94c648931f84 | 13 | //physical chip interface |
Blaze513 | 0:f3870f76a890 | 14 | unsigned char CommandCRCTable[256]; |
Blaze513 | 0:f3870f76a890 | 15 | //CRC7 lookup table |
Blaze513 | 0:f3870f76a890 | 16 | unsigned char DataCRCTable[512]; |
Blaze513 | 0:f3870f76a890 | 17 | //CRC CCITT lookup table |
Blaze513 | 0:f3870f76a890 | 18 | unsigned char OCR[4]; |
Blaze513 | 0:f3870f76a890 | 19 | //operating condition register |
Blaze513 | 0:f3870f76a890 | 20 | unsigned char CSD[16]; |
Blaze513 | 0:f3870f76a890 | 21 | //card-specific data register |
Blaze513 | 1:94c648931f84 | 22 | unsigned char FSR[64]; |
Blaze513 | 1:94c648931f84 | 23 | //function status register |
Blaze513 | 0:f3870f76a890 | 24 | bool Version; |
Blaze513 | 0:f3870f76a890 | 25 | //card version, low for 1, high for 2 |
Blaze513 | 0:f3870f76a890 | 26 | bool Capacity; |
Blaze513 | 0:f3870f76a890 | 27 | //low for low-capacity, high for high-capacity |
Blaze513 | 0:f3870f76a890 | 28 | bool CRCMode; |
Blaze513 | 0:f3870f76a890 | 29 | //low to disable CRCs, high to enable CRCs |
Blaze513 | 0:f3870f76a890 | 30 | unsigned char Workspace[5]; |
Blaze513 | 1:94c648931f84 | 31 | //all-purpose work area |
Blaze513 | 1:94c648931f84 | 32 | unsigned int Timeout; |
Blaze513 | 1:94c648931f84 | 33 | unsigned int t; |
Blaze513 | 1:94c648931f84 | 34 | //timeout limit and counter |
Blaze513 | 0:f3870f76a890 | 35 | |
Blaze513 | 1:94c648931f84 | 36 | unsigned char Initialize(); |
Blaze513 | 1:94c648931f84 | 37 | //complete all initialization operations |
Blaze513 | 0:f3870f76a890 | 38 | void Command(unsigned char Index, unsigned int Argument, unsigned char* Response); |
Blaze513 | 0:f3870f76a890 | 39 | //sends command to the SD card |
Blaze513 | 1:94c648931f84 | 40 | void CommandCRC(unsigned char* IndexPtr, unsigned int* ArgumentPtr, unsigned char* Result); |
Blaze513 | 0:f3870f76a890 | 41 | void DataCRC(unsigned short Length, unsigned char* Data, unsigned char* Result); |
Blaze513 | 0:f3870f76a890 | 42 | //calculates the CRC result of the input with a lookup table |
Blaze513 | 0:f3870f76a890 | 43 | void GenerateCRCTable(unsigned char Size, unsigned long long Generator, unsigned char* Table); |
Blaze513 | 0:f3870f76a890 | 44 | //pre-calculates CRC results for efficient checking |
Blaze513 | 0:f3870f76a890 | 45 | |
Blaze513 | 1:94c648931f84 | 46 | virtual unsigned char disk_initialize(); |
Blaze513 | 1:94c648931f84 | 47 | virtual unsigned char disk_status(); |
Blaze513 | 1:94c648931f84 | 48 | virtual unsigned char disk_read(unsigned char* buff, unsigned long sector, unsigned char count); |
Blaze513 | 1:94c648931f84 | 49 | virtual unsigned char disk_write(const unsigned char* buff, unsigned long sector, unsigned char count); |
Blaze513 | 1:94c648931f84 | 50 | virtual unsigned char disk_sync(); |
Blaze513 | 1:94c648931f84 | 51 | virtual unsigned long disk_sector_count(); |
Blaze513 | 1:94c648931f84 | 52 | virtual unsigned short disk_sector_size(); |
Blaze513 | 1:94c648931f84 | 53 | virtual unsigned long disk_block_size(); |
Blaze513 | 1:94c648931f84 | 54 | //FAT system virtual function definitions, called by FAT module |
Blaze513 | 1:94c648931f84 | 55 | |
Blaze513 | 0:f3870f76a890 | 56 | public: |
Blaze513 | 1:94c648931f84 | 57 | SDCard(PinName mosi, PinName miso, PinName sck, PinName cs, const char* DiskName); |
Blaze513 | 1:94c648931f84 | 58 | //constructor requires SPI and DigitalOut pins, and a directory name |
Blaze513 | 1:94c648931f84 | 59 | unsigned char Log(unsigned char Control, unsigned char Data); |
Blaze513 | 1:94c648931f84 | 60 | //multipurpose raw data-logging method with three modes of operation |
Blaze513 | 1:94c648931f84 | 61 | //Control description |
Blaze513 | 1:94c648931f84 | 62 | // 0 synchronizes card and resets internal counter to finalize I/O operations |
Blaze513 | 1:94c648931f84 | 63 | // 1 successive write input Data to a raw data byte in order starting at address 0 |
Blaze513 | 1:94c648931f84 | 64 | // 2 successively read and return a raw data byte in order starting at address 0 |
Blaze513 | 1:94c648931f84 | 65 | //return data from sync or write operations, and input data of sync or read operations are |
Blaze513 | 1:94c648931f84 | 66 | //"don't care" bits |
Blaze513 | 1:94c648931f84 | 67 | unsigned char Write(unsigned int Address, unsigned char* Data); |
Blaze513 | 1:94c648931f84 | 68 | unsigned char Write(unsigned int Address, unsigned char SectorCount, unsigned char* Data); |
Blaze513 | 1:94c648931f84 | 69 | //write data sectors to the card |
Blaze513 | 1:94c648931f84 | 70 | unsigned char Read(unsigned int Address, unsigned char* Data); |
Blaze513 | 1:94c648931f84 | 71 | unsigned char Read(unsigned int Address, unsigned char SectorCount, unsigned char* Data); |
Blaze513 | 1:94c648931f84 | 72 | //read data sectors from the card |
Blaze513 | 1:94c648931f84 | 73 | unsigned char SelectCRCMode(bool Mode); |
Blaze513 | 1:94c648931f84 | 74 | //turn CRC mode on or off, default is off |
Blaze513 | 0:f3870f76a890 | 75 | }; |
Blaze513 | 0:f3870f76a890 | 76 | |
Blaze513 | 0:f3870f76a890 | 77 | #endif |