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
00001 //mbed Microcontroller Library 00002 //SDCard Interface 00003 //Copyright 2010 00004 //Thomas Hamilton 00005 00006 #ifndef SDCardLibrary 00007 #define SDCardLibrary 00008 00009 #include "stdint.h" 00010 #include "mbed.h" 00011 #include "FATFileSystem.h" 00012 00013 class SDCard : public FATFileSystem 00014 { 00015 private: 00016 SPI DataLines; 00017 DigitalOut ChipSelect; 00018 //physical chip interface 00019 00020 unsigned int t; 00021 //timeout counter 00022 unsigned int Timeout; 00023 //timeout limit 00024 bool CRCMode; 00025 //low: CRCs disabled, high: CRCs enabled 00026 bool Capacity; 00027 //low: low-capacity, high: high-capacity 00028 bool Version; 00029 //low: version 1, high: version 2 00030 unsigned char Status; 00031 //0x00: Ready, 0x01: not initialized 00032 unsigned char FSR[64]; 00033 //function status register 00034 unsigned char CSD[16]; 00035 //card-specific data register 00036 unsigned char OCR[4]; 00037 //operating conditions register 00038 unsigned char DataCRCTable[512]; 00039 //CRC16 CCITT lookup table 00040 unsigned char CommandCRCTable[256]; 00041 //CRC7 SD command lookup table 00042 00043 unsigned char Initialize(); 00044 //complete all initialization operations 00045 void Command(unsigned char Index, 00046 unsigned int Argument, unsigned char* Response); 00047 //sends the SD command with the given Index and Argument to the SD 00048 //card and stores the SD Response 00049 void CommandCRC(unsigned char* IndexPtr, 00050 unsigned int* ArgumentPtr, unsigned char* Result); 00051 //calculates the SD proprietary CRC7 result of an SD command 00052 //(composed of a command index and argument) and stores the one- 00053 //byte solution in Result 00054 void DataCRC( 00055 unsigned short Length, unsigned char* Data, unsigned char* Result); 00056 //calculates the CRC16 CCITT result of the number of bytes in Data 00057 //given by Length and stores the two-byte solution in Result 00058 //assumes DataCRCTable has already been calculated 00059 void GenerateCRCTable(unsigned char Size, 00060 unsigned long long Generator, unsigned char* Table); 00061 //pre-calculates CRC results from the given Generator for efficient 00062 //checking; assumes pre-allocated Table is large enough to hold the 00063 //number of bytes given in Size 00064 00065 virtual unsigned char disk_initialize(); 00066 virtual unsigned char disk_status(); 00067 virtual unsigned char disk_read(unsigned char* buff, 00068 unsigned long sector, unsigned char count); 00069 virtual unsigned char disk_write(const unsigned char* buff, 00070 unsigned long sector, unsigned char count); 00071 virtual unsigned char disk_sync(); 00072 virtual unsigned long disk_sector_count(); 00073 virtual unsigned short disk_sector_size(); 00074 virtual unsigned long disk_block_size(); 00075 //FAT system virtual functions that are called by the FAT module 00076 00077 public: 00078 SDCard(PinName mosi, PinName miso, PinName sck, PinName cs, 00079 const char* DiskName); 00080 //constructor needs SPI pins, DigitalOut pins, and a directory name 00081 virtual ~SDCard(); 00082 //destructor deallocates tables and registers 00083 unsigned char Format(unsigned int AllocationUnit); 00084 //formats the card FAT with given AllocationUnit in sectors; the 00085 //maximum is 32768 sectors 00086 unsigned char Log(unsigned char Control, unsigned char Data); 00087 //multipurpose single-byte raw data-logging method with three modes 00088 //Control description 00089 // 0 synchronizes card and resets internal counter to 00090 // finalize read or write operations 00091 // 1 successively write Data to a raw byte address in order 00092 // starting at address 0 00093 // 2 successively read and return a raw data byte in order 00094 // starting at address 0 00095 //return byte from sync or write operations and input Data of sync 00096 //or read operations are not used; writing with this function will 00097 //deformat the drive 00098 unsigned char Write(unsigned int Address, unsigned char* Data); 00099 //write the first 512 byte sector of Data to the card at the given 00100 //Address 00101 unsigned char Write(unsigned int Address, 00102 unsigned char SectorCount, unsigned char* Data); 00103 //write the first given SectorCount of 512 byte sectors of Data to 00104 //the card at the given Address 00105 unsigned char Read(unsigned int Address, unsigned char* Data); 00106 //read the first 512 byte sector from card at the given Address 00107 //into Data 00108 unsigned char Read(unsigned int Address, 00109 unsigned char SectorCount, unsigned char* Data); 00110 //read the first given SectorCount of 512 byte sectors from the 00111 //card at the given Address into Data 00112 unsigned char SelectCRCMode(bool Mode); 00113 //toggle CRC mode; low: CRCs disabled, high: CRCs enabled, default: 00114 //CRCs disabled 00115 void SetTimeout(unsigned int Retries); 00116 //change the number of retries for interface functions; increase if 00117 //lines are unreliable; default: 1024, minimum: 1024 00118 }; 00119 00120 #endif
Generated on Wed Jul 13 2022 05:31:45 by 1.7.2