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@6:ddf09d859ed7, 2011-01-16 (annotated)
- Committer:
- Blaze513
- Date:
- Sun Jan 16 09:20:30 2011 +0000
- Revision:
- 6:ddf09d859ed7
- Parent:
- 5:d85e20b6b904
gave access to Initialization function to FAT module.
added disk formatting functionality.
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 | 6:ddf09d859ed7 | 13 | class SDCard : public 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 | 6:ddf09d859ed7 | 19 | |
Blaze513 | 6:ddf09d859ed7 | 20 | unsigned int t; |
Blaze513 | 6:ddf09d859ed7 | 21 | //timeout counter |
Blaze513 | 6:ddf09d859ed7 | 22 | unsigned int Timeout; |
Blaze513 | 6:ddf09d859ed7 | 23 | //timeout limit |
Blaze513 | 6:ddf09d859ed7 | 24 | bool CRCMode; |
Blaze513 | 6:ddf09d859ed7 | 25 | //low: CRCs disabled, high: CRCs enabled |
Blaze513 | 6:ddf09d859ed7 | 26 | bool Capacity; |
Blaze513 | 6:ddf09d859ed7 | 27 | //low: low-capacity, high: high-capacity |
Blaze513 | 6:ddf09d859ed7 | 28 | bool Version; |
Blaze513 | 6:ddf09d859ed7 | 29 | //low: version 1, high: version 2 |
Blaze513 | 6:ddf09d859ed7 | 30 | unsigned char Status; |
Blaze513 | 6:ddf09d859ed7 | 31 | //0x00: Ready, 0x01: not initialized |
Blaze513 | 1:94c648931f84 | 32 | unsigned char FSR[64]; |
Blaze513 | 1:94c648931f84 | 33 | //function status register |
Blaze513 | 6:ddf09d859ed7 | 34 | unsigned char CSD[16]; |
Blaze513 | 6:ddf09d859ed7 | 35 | //card-specific data register |
Blaze513 | 6:ddf09d859ed7 | 36 | unsigned char OCR[4]; |
Blaze513 | 6:ddf09d859ed7 | 37 | //operating conditions register |
Blaze513 | 6:ddf09d859ed7 | 38 | unsigned char DataCRCTable[512]; |
Blaze513 | 6:ddf09d859ed7 | 39 | //CRC16 CCITT lookup table |
Blaze513 | 6:ddf09d859ed7 | 40 | unsigned char CommandCRCTable[256]; |
Blaze513 | 6:ddf09d859ed7 | 41 | //CRC7 SD command lookup table |
Blaze513 | 0:f3870f76a890 | 42 | |
Blaze513 | 1:94c648931f84 | 43 | unsigned char Initialize(); |
Blaze513 | 1:94c648931f84 | 44 | //complete all initialization operations |
Blaze513 | 5:d85e20b6b904 | 45 | void Command(unsigned char Index, |
Blaze513 | 5:d85e20b6b904 | 46 | unsigned int Argument, unsigned char* Response); |
Blaze513 | 6:ddf09d859ed7 | 47 | //sends the SD command with the given Index and Argument to the SD |
Blaze513 | 6:ddf09d859ed7 | 48 | //card and stores the SD Response |
Blaze513 | 5:d85e20b6b904 | 49 | void CommandCRC(unsigned char* IndexPtr, |
Blaze513 | 5:d85e20b6b904 | 50 | unsigned int* ArgumentPtr, unsigned char* Result); |
Blaze513 | 6:ddf09d859ed7 | 51 | //calculates the SD proprietary CRC7 result of an SD command |
Blaze513 | 6:ddf09d859ed7 | 52 | //(composed of a command index and argument) and stores the one- |
Blaze513 | 6:ddf09d859ed7 | 53 | //byte solution in Result |
Blaze513 | 6:ddf09d859ed7 | 54 | void DataCRC( |
Blaze513 | 6:ddf09d859ed7 | 55 | unsigned short Length, unsigned char* Data, unsigned char* Result); |
Blaze513 | 6:ddf09d859ed7 | 56 | //calculates the CRC16 CCITT result of the number of bytes in Data |
Blaze513 | 6:ddf09d859ed7 | 57 | //given by Length and stores the two-byte solution in Result |
Blaze513 | 5:d85e20b6b904 | 58 | //assumes DataCRCTable has already been calculated |
Blaze513 | 5:d85e20b6b904 | 59 | void GenerateCRCTable(unsigned char Size, |
Blaze513 | 5:d85e20b6b904 | 60 | unsigned long long Generator, unsigned char* Table); |
Blaze513 | 6:ddf09d859ed7 | 61 | //pre-calculates CRC results from the given Generator for efficient |
Blaze513 | 6:ddf09d859ed7 | 62 | //checking; assumes pre-allocated Table is large enough to hold the |
Blaze513 | 6:ddf09d859ed7 | 63 | //number of bytes given in Size |
Blaze513 | 0:f3870f76a890 | 64 | |
Blaze513 | 1:94c648931f84 | 65 | virtual unsigned char disk_initialize(); |
Blaze513 | 1:94c648931f84 | 66 | virtual unsigned char disk_status(); |
Blaze513 | 5:d85e20b6b904 | 67 | virtual unsigned char disk_read(unsigned char* buff, |
Blaze513 | 5:d85e20b6b904 | 68 | unsigned long sector, unsigned char count); |
Blaze513 | 5:d85e20b6b904 | 69 | virtual unsigned char disk_write(const unsigned char* buff, |
Blaze513 | 5:d85e20b6b904 | 70 | unsigned long sector, unsigned char count); |
Blaze513 | 1:94c648931f84 | 71 | virtual unsigned char disk_sync(); |
Blaze513 | 1:94c648931f84 | 72 | virtual unsigned long disk_sector_count(); |
Blaze513 | 1:94c648931f84 | 73 | virtual unsigned short disk_sector_size(); |
Blaze513 | 1:94c648931f84 | 74 | virtual unsigned long disk_block_size(); |
Blaze513 | 6:ddf09d859ed7 | 75 | //FAT system virtual functions that are called by the FAT module |
Blaze513 | 1:94c648931f84 | 76 | |
Blaze513 | 0:f3870f76a890 | 77 | public: |
Blaze513 | 5:d85e20b6b904 | 78 | SDCard(PinName mosi, PinName miso, PinName sck, PinName cs, |
Blaze513 | 5:d85e20b6b904 | 79 | const char* DiskName); |
Blaze513 | 6:ddf09d859ed7 | 80 | //constructor needs SPI pins, DigitalOut pins, and a directory name |
Blaze513 | 3:210eb67b260c | 81 | virtual ~SDCard(); |
Blaze513 | 6:ddf09d859ed7 | 82 | //destructor deallocates tables and registers |
Blaze513 | 6:ddf09d859ed7 | 83 | unsigned char Format(unsigned int AllocationUnit); |
Blaze513 | 6:ddf09d859ed7 | 84 | //formats the card FAT with given AllocationUnit in sectors; the |
Blaze513 | 6:ddf09d859ed7 | 85 | //maximum is 32768 sectors |
Blaze513 | 1:94c648931f84 | 86 | unsigned char Log(unsigned char Control, unsigned char Data); |
Blaze513 | 6:ddf09d859ed7 | 87 | //multipurpose single-byte raw data-logging method with three modes |
Blaze513 | 1:94c648931f84 | 88 | //Control description |
Blaze513 | 5:d85e20b6b904 | 89 | // 0 synchronizes card and resets internal counter to |
Blaze513 | 6:ddf09d859ed7 | 90 | // finalize read or write operations |
Blaze513 | 6:ddf09d859ed7 | 91 | // 1 successively write Data to a raw byte address in order |
Blaze513 | 6:ddf09d859ed7 | 92 | // starting at address 0 |
Blaze513 | 6:ddf09d859ed7 | 93 | // 2 successively read and return a raw data byte in order |
Blaze513 | 6:ddf09d859ed7 | 94 | // starting at address 0 |
Blaze513 | 6:ddf09d859ed7 | 95 | //return byte from sync or write operations and input Data of sync |
Blaze513 | 6:ddf09d859ed7 | 96 | //or read operations are not used; writing with this function will |
Blaze513 | 6:ddf09d859ed7 | 97 | //deformat the drive |
Blaze513 | 1:94c648931f84 | 98 | unsigned char Write(unsigned int Address, unsigned char* Data); |
Blaze513 | 6:ddf09d859ed7 | 99 | //write the first 512 byte sector of Data to the card at the given |
Blaze513 | 6:ddf09d859ed7 | 100 | //Address |
Blaze513 | 5:d85e20b6b904 | 101 | unsigned char Write(unsigned int Address, |
Blaze513 | 5:d85e20b6b904 | 102 | unsigned char SectorCount, unsigned char* Data); |
Blaze513 | 6:ddf09d859ed7 | 103 | //write the first given SectorCount of 512 byte sectors of Data to |
Blaze513 | 6:ddf09d859ed7 | 104 | //the card at the given Address |
Blaze513 | 1:94c648931f84 | 105 | unsigned char Read(unsigned int Address, unsigned char* Data); |
Blaze513 | 6:ddf09d859ed7 | 106 | //read the first 512 byte sector from card at the given Address |
Blaze513 | 6:ddf09d859ed7 | 107 | //into Data |
Blaze513 | 5:d85e20b6b904 | 108 | unsigned char Read(unsigned int Address, |
Blaze513 | 5:d85e20b6b904 | 109 | unsigned char SectorCount, unsigned char* Data); |
Blaze513 | 6:ddf09d859ed7 | 110 | //read the first given SectorCount of 512 byte sectors from the |
Blaze513 | 6:ddf09d859ed7 | 111 | //card at the given Address into Data |
Blaze513 | 1:94c648931f84 | 112 | unsigned char SelectCRCMode(bool Mode); |
Blaze513 | 6:ddf09d859ed7 | 113 | //toggle CRC mode; low: CRCs disabled, high: CRCs enabled, default: |
Blaze513 | 6:ddf09d859ed7 | 114 | //CRCs disabled |
Blaze513 | 3:210eb67b260c | 115 | void SetTimeout(unsigned int Retries); |
Blaze513 | 6:ddf09d859ed7 | 116 | //change the number of retries for interface functions; increase if |
Blaze513 | 6:ddf09d859ed7 | 117 | //lines are unreliable; default: 1024, minimum: 1024 |
Blaze513 | 0:f3870f76a890 | 118 | }; |
Blaze513 | 0:f3870f76a890 | 119 | |
Blaze513 | 0:f3870f76a890 | 120 | #endif |