SD for ECE 4180
Fork of SDFileSystem by
Embed:
(wiki syntax)
Show/hide line numbers
SDFileSystem.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2012 ARM Limited 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00020 * SOFTWARE. 00021 */ 00022 /* Introduction 00023 * ------------ 00024 * SD and MMC cards support a number of interfaces, but common to them all 00025 * is one based on SPI. This is the one I'm implmenting because it means 00026 * it is much more portable even though not so performant, and we already 00027 * have the mbed SPI Interface! 00028 * 00029 * The main reference I'm using is Chapter 7, "SPI Mode" of: 00030 * http://www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf 00031 * 00032 * SPI Startup 00033 * ----------- 00034 * The SD card powers up in SD mode. The SPI interface mode is selected by 00035 * asserting CS low and sending the reset command (CMD0). The card will 00036 * respond with a (R1) response. 00037 * 00038 * CMD8 is optionally sent to determine the voltage range supported, and 00039 * indirectly determine whether it is a version 1.x SD/non-SD card or 00040 * version 2.x. I'll just ignore this for now. 00041 * 00042 * ACMD41 is repeatedly issued to initialise the card, until "in idle" 00043 * (bit 0) of the R1 response goes to '0', indicating it is initialised. 00044 * 00045 * You should also indicate whether the host supports High Capicity cards, 00046 * and check whether the card is high capacity - i'll also ignore this 00047 * 00048 * SPI Protocol 00049 * ------------ 00050 * The SD SPI protocol is based on transactions made up of 8-bit words, with 00051 * the host starting every bus transaction by asserting the CS signal low. The 00052 * card always responds to commands, data blocks and errors. 00053 * 00054 * The protocol supports a CRC, but by default it is off (except for the 00055 * first reset CMD0, where the CRC can just be pre-calculated, and CMD8) 00056 * I'll leave the CRC off I think! 00057 * 00058 * Standard capacity cards have variable data block sizes, whereas High 00059 * Capacity cards fix the size of data block to 512 bytes. I'll therefore 00060 * just always use the Standard Capacity cards with a block size of 512 bytes. 00061 * This is set with CMD16. 00062 * 00063 * You can read and write single blocks (CMD17, CMD25) or multiple blocks 00064 * (CMD18, CMD25). For simplicity, I'll just use single block accesses. When 00065 * the card gets a read command, it responds with a response token, and then 00066 * a data token or an error. 00067 * 00068 * SPI Command Format 00069 * ------------------ 00070 * Commands are 6-bytes long, containing the command, 32-bit argument, and CRC. 00071 * 00072 * +---------------+------------+------------+-----------+----------+--------------+ 00073 * | 01 | cmd[5:0] | arg[31:24] | arg[23:16] | arg[15:8] | arg[7:0] | crc[6:0] | 1 | 00074 * +---------------+------------+------------+-----------+----------+--------------+ 00075 * 00076 * As I'm not using CRC, I can fix that byte to what is needed for CMD0 (0x95) 00077 * 00078 * All Application Specific commands shall be preceded with APP_CMD (CMD55). 00079 * 00080 * SPI Response Format 00081 * ------------------- 00082 * The main response format (R1) is a status byte (normally zero). Key flags: 00083 * idle - 1 if the card is in an idle state/initialising 00084 * cmd - 1 if an illegal command code was detected 00085 * 00086 * +-------------------------------------------------+ 00087 * R1 | 0 | arg | addr | seq | crc | cmd | erase | idle | 00088 * +-------------------------------------------------+ 00089 * 00090 * R1b is the same, except it is followed by a busy signal (zeros) until 00091 * the first non-zero byte when it is ready again. 00092 * 00093 * Data Response Token 00094 * ------------------- 00095 * Every data block written to the card is acknowledged by a byte 00096 * response token 00097 * 00098 * +----------------------+ 00099 * | xxx | 0 | status | 1 | 00100 * +----------------------+ 00101 * 010 - OK! 00102 * 101 - CRC Error 00103 * 110 - Write Error 00104 * 00105 * Single Block Read and Write 00106 * --------------------------- 00107 * 00108 * Block transfers have a byte header, followed by the data, followed 00109 * by a 16-bit CRC. In our case, the data will always be 512 bytes. 00110 * 00111 * +------+---------+---------+- - - -+---------+-----------+----------+ 00112 * | 0xFE | data[0] | data[1] | | data[n] | crc[15:8] | crc[7:0] | 00113 * +------+---------+---------+- - - -+---------+-----------+----------+ 00114 */ 00115 #include "SDFileSystem.h" 00116 #include "mbed.h" 00117 #include "mbed_debug.h" 00118 00119 #define SD_COMMAND_TIMEOUT 5000 00120 00121 #define SD_DBG 0 00122 00123 SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName cd, const char* name) : 00124 FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs), _cd(cd){ 00125 _cs = 1; 00126 _cd.mode(PullUp); 00127 } 00128 00129 #define R1_IDLE_STATE (1 << 0) 00130 #define R1_ERASE_RESET (1 << 1) 00131 #define R1_ILLEGAL_COMMAND (1 << 2) 00132 #define R1_COM_CRC_ERROR (1 << 3) 00133 #define R1_ERASE_SEQUENCE_ERROR (1 << 4) 00134 #define R1_ADDRESS_ERROR (1 << 5) 00135 #define R1_PARAMETER_ERROR (1 << 6) 00136 00137 // Types 00138 // - v1.x Standard Capacity 00139 // - v2.x Standard Capacity 00140 // - v2.x High Capacity 00141 // - Not recognised as an SD Card 00142 #define SDCARD_FAIL 0 00143 #define SDCARD_V1 1 00144 #define SDCARD_V2 2 00145 #define SDCARD_V2HC 3 00146 00147 int SDFileSystem::initialise_card() { 00148 // Set to 100kHz for initialisation, and clock card with cs = 1 00149 _spi.frequency(100000); 00150 _cs = 1; 00151 for (int i = 0; i < 16; i++) { 00152 _spi.write(0xFF); 00153 } 00154 00155 // send CMD0, should return with all zeros except IDLE STATE set (bit 0) 00156 if (_cmd(0, 0) != R1_IDLE_STATE) { 00157 debug("No disk, or could not put SD card in to SPI idle state\n"); 00158 return SDCARD_FAIL; 00159 } 00160 00161 // send CMD8 to determine whther it is ver 2.x 00162 int r = _cmd8(); 00163 if (r == R1_IDLE_STATE) { 00164 return initialise_card_v2(); 00165 } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) { 00166 return initialise_card_v1(); 00167 } else { 00168 debug("Not in idle state after sending CMD8 (not an SD card?)\n"); 00169 return SDCARD_FAIL; 00170 } 00171 } 00172 00173 int SDFileSystem::initialise_card_v1() { 00174 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { 00175 _cmd(55, 0); 00176 if (_cmd(41, 0) == 0) { 00177 cdv = 512; 00178 debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r"); 00179 return SDCARD_V1; 00180 } 00181 } 00182 00183 debug("Timeout waiting for v1.x card\n"); 00184 return SDCARD_FAIL; 00185 } 00186 00187 bool SDFileSystem::SD_inserted() 00188 { 00189 if(_cd) { 00190 return(true); 00191 } 00192 else 00193 {return(false);} 00194 } 00195 int SDFileSystem::initialise_card_v2() { 00196 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { 00197 wait_ms(50); 00198 _cmd58(); 00199 _cmd(55, 0); 00200 if (_cmd(41, 0x40000000) == 0) { 00201 _cmd58(); 00202 debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r"); 00203 cdv = 1; 00204 return SDCARD_V2; 00205 } 00206 } 00207 00208 debug("Timeout waiting for v2.x card\n"); 00209 return SDCARD_FAIL; 00210 } 00211 00212 int SDFileSystem::disk_initialize() { 00213 int i = initialise_card(); 00214 debug_if(SD_DBG, "init card = %d\n", i); 00215 _sectors = _sd_sectors(); 00216 00217 // Set block length to 512 (CMD16) 00218 if (_cmd(16, 512) != 0) { 00219 debug("Set 512-byte block timed out\n"); 00220 return 1; 00221 } 00222 00223 _spi.frequency(1000000); // Set to 1MHz for data transfer 00224 return 0; 00225 } 00226 00227 int SDFileSystem::disk_write(const uint8_t *buffer, uint64_t block_number) { 00228 // set write address for single block (CMD24) 00229 if (_cmd(24, block_number * cdv) != 0) { 00230 return 1; 00231 } 00232 00233 // send the data block 00234 _write(buffer, 512); 00235 return 0; 00236 } 00237 00238 int SDFileSystem::disk_read(uint8_t *buffer, uint64_t block_number) { 00239 // set read address for single block (CMD17) 00240 if (_cmd(17, block_number * cdv) != 0) { 00241 return 1; 00242 } 00243 00244 // receive the data 00245 _read(buffer, 512); 00246 return 0; 00247 } 00248 00249 int SDFileSystem::disk_status() { return 0; } 00250 int SDFileSystem::disk_sync() { return 0; } 00251 uint64_t SDFileSystem::disk_sectors() { return _sectors; } 00252 00253 00254 // PRIVATE FUNCTIONS 00255 int SDFileSystem::_cmd(int cmd, int arg) { 00256 _cs = 0; 00257 00258 // send a command 00259 _spi.write(0x40 | cmd); 00260 _spi.write(arg >> 24); 00261 _spi.write(arg >> 16); 00262 _spi.write(arg >> 8); 00263 _spi.write(arg >> 0); 00264 _spi.write(0x95); 00265 00266 // wait for the repsonse (response[7] == 0) 00267 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { 00268 int response = _spi.write(0xFF); 00269 if (!(response & 0x80)) { 00270 _cs = 1; 00271 _spi.write(0xFF); 00272 return response; 00273 } 00274 } 00275 _cs = 1; 00276 _spi.write(0xFF); 00277 return -1; // timeout 00278 } 00279 int SDFileSystem::_cmdx(int cmd, int arg) { 00280 _cs = 0; 00281 00282 // send a command 00283 _spi.write(0x40 | cmd); 00284 _spi.write(arg >> 24); 00285 _spi.write(arg >> 16); 00286 _spi.write(arg >> 8); 00287 _spi.write(arg >> 0); 00288 _spi.write(0x95); 00289 00290 // wait for the repsonse (response[7] == 0) 00291 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { 00292 int response = _spi.write(0xFF); 00293 if (!(response & 0x80)) { 00294 return response; 00295 } 00296 } 00297 _cs = 1; 00298 _spi.write(0xFF); 00299 return -1; // timeout 00300 } 00301 00302 00303 int SDFileSystem::_cmd58() { 00304 _cs = 0; 00305 int arg = 0; 00306 00307 // send a command 00308 _spi.write(0x40 | 58); 00309 _spi.write(arg >> 24); 00310 _spi.write(arg >> 16); 00311 _spi.write(arg >> 8); 00312 _spi.write(arg >> 0); 00313 _spi.write(0x95); 00314 00315 // wait for the repsonse (response[7] == 0) 00316 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { 00317 int response = _spi.write(0xFF); 00318 if (!(response & 0x80)) { 00319 int ocr = _spi.write(0xFF) << 24; 00320 ocr |= _spi.write(0xFF) << 16; 00321 ocr |= _spi.write(0xFF) << 8; 00322 ocr |= _spi.write(0xFF) << 0; 00323 _cs = 1; 00324 _spi.write(0xFF); 00325 return response; 00326 } 00327 } 00328 _cs = 1; 00329 _spi.write(0xFF); 00330 return -1; // timeout 00331 } 00332 00333 int SDFileSystem::_cmd8() { 00334 _cs = 0; 00335 00336 // send a command 00337 _spi.write(0x40 | 8); // CMD8 00338 _spi.write(0x00); // reserved 00339 _spi.write(0x00); // reserved 00340 _spi.write(0x01); // 3.3v 00341 _spi.write(0xAA); // check pattern 00342 _spi.write(0x87); // crc 00343 00344 // wait for the repsonse (response[7] == 0) 00345 for (int i = 0; i < SD_COMMAND_TIMEOUT * 1000; i++) { 00346 char response[5]; 00347 response[0] = _spi.write(0xFF); 00348 if (!(response[0] & 0x80)) { 00349 for (int j = 1; j < 5; j++) { 00350 response[i] = _spi.write(0xFF); 00351 } 00352 _cs = 1; 00353 _spi.write(0xFF); 00354 return response[0]; 00355 } 00356 } 00357 _cs = 1; 00358 _spi.write(0xFF); 00359 return -1; // timeout 00360 } 00361 00362 int SDFileSystem::_read(uint8_t *buffer, uint32_t length) { 00363 _cs = 0; 00364 00365 // read until start byte (0xFF) 00366 while (_spi.write(0xFF) != 0xFE); 00367 00368 // read data 00369 for (int i = 0; i < length; i++) { 00370 buffer[i] = _spi.write(0xFF); 00371 } 00372 _spi.write(0xFF); // checksum 00373 _spi.write(0xFF); 00374 00375 _cs = 1; 00376 _spi.write(0xFF); 00377 return 0; 00378 } 00379 00380 int SDFileSystem::_write(const uint8_t*buffer, uint32_t length) { 00381 _cs = 0; 00382 00383 // indicate start of block 00384 _spi.write(0xFE); 00385 00386 // write the data 00387 for (int i = 0; i < length; i++) { 00388 _spi.write(buffer[i]); 00389 } 00390 00391 // write the checksum 00392 _spi.write(0xFF); 00393 _spi.write(0xFF); 00394 00395 // check the response token 00396 if ((_spi.write(0xFF) & 0x1F) != 0x05) { 00397 _cs = 1; 00398 _spi.write(0xFF); 00399 return 1; 00400 } 00401 00402 // wait for write to finish 00403 while (_spi.write(0xFF) == 0); 00404 00405 _cs = 1; 00406 _spi.write(0xFF); 00407 return 0; 00408 } 00409 00410 static uint32_t ext_bits(unsigned char *data, int msb, int lsb) { 00411 uint32_t bits = 0; 00412 uint32_t size = 1 + msb - lsb; 00413 for (int i = 0; i < size; i++) { 00414 uint32_t position = lsb + i; 00415 uint32_t byte = 15 - (position >> 3); 00416 uint32_t bit = position & 0x7; 00417 uint32_t value = (data[byte] >> bit) & 1; 00418 bits |= value << i; 00419 } 00420 return bits; 00421 } 00422 00423 uint64_t SDFileSystem::_sd_sectors() { 00424 uint32_t c_size, c_size_mult, read_bl_len; 00425 uint32_t block_len, mult, blocknr, capacity; 00426 uint32_t hc_c_size; 00427 uint64_t blocks; 00428 00429 // CMD9, Response R2 (R1 byte + 16-byte block read) 00430 if (_cmdx(9, 0) != 0) { 00431 debug("Didn't get a response from the disk\n"); 00432 return 0; 00433 } 00434 00435 uint8_t csd[16]; 00436 if (_read(csd, 16) != 0) { 00437 debug("Couldn't read csd response from disk\n"); 00438 return 0; 00439 } 00440 00441 // csd_structure : csd[127:126] 00442 // c_size : csd[73:62] 00443 // c_size_mult : csd[49:47] 00444 // read_bl_len : csd[83:80] - the *maximum* read block length 00445 00446 int csd_structure = ext_bits(csd, 127, 126); 00447 00448 switch (csd_structure) { 00449 case 0: 00450 cdv = 512; 00451 c_size = ext_bits(csd, 73, 62); 00452 c_size_mult = ext_bits(csd, 49, 47); 00453 read_bl_len = ext_bits(csd, 83, 80); 00454 00455 block_len = 1 << read_bl_len; 00456 mult = 1 << (c_size_mult + 2); 00457 blocknr = (c_size + 1) * mult; 00458 capacity = blocknr * block_len; 00459 blocks = capacity / 512; 00460 debug_if(SD_DBG, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks); 00461 break; 00462 00463 case 1: 00464 cdv = 1; 00465 hc_c_size = ext_bits(csd, 63, 48); 00466 blocks = (hc_c_size+1)*1024; 00467 debug_if(SD_DBG, "\n\rSDHC Card \n\rhc_c_size: %d\n\rcapacity: %lld \n\rsectors: %lld\n\r", hc_c_size, blocks*512, blocks); 00468 break; 00469 00470 default: 00471 debug("CSD struct unsupported\r\n"); 00472 return 0; 00473 }; 00474 return blocks; 00475 }
Generated on Wed Aug 10 2022 15:14:06 by 1.7.2