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