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