Dependencies:   mbed

Committer:
simon
Date:
Mon Dec 14 22:32:28 2009 +0000
Revision:
2:849162a1207f
Parent:
1:2dec995d53f8
Child:
3:68ef62208d4d

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 1:2dec995d53f8 1 /* mbed Microcontroller Library - SDFileSystem
simon 1:2dec995d53f8 2 * Copyright (c) 2008-2009, sford
simon 1:2dec995d53f8 3 *
simon 1:2dec995d53f8 4 * Introduction
simon 1:2dec995d53f8 5 * ------------
simon 1:2dec995d53f8 6 * SD and MMC cards support a number of interfaces, but common to them all
simon 1:2dec995d53f8 7 * is one based on SPI. This is the one I'm implmenting because it means
simon 1:2dec995d53f8 8 * it is much more portable even though not so performant, and we already
simon 1:2dec995d53f8 9 * have the mbed SPI Interface!
simon 1:2dec995d53f8 10 *
simon 1:2dec995d53f8 11 * The main reference I'm using is Chapter 7, "SPI Mode" of:
simon 1:2dec995d53f8 12 * http://www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
simon 1:2dec995d53f8 13 *
simon 1:2dec995d53f8 14 * SPI Startup
simon 1:2dec995d53f8 15 * -----------
simon 1:2dec995d53f8 16 * The SD card powers up in SD mode. The SPI interface mode is selected by
simon 1:2dec995d53f8 17 * asserting CS low and sending the reset command (CMD0). The card will
simon 1:2dec995d53f8 18 * respond with a (R1) response.
simon 1:2dec995d53f8 19 *
simon 1:2dec995d53f8 20 * CMD8 is optionally sent to determine the voltage range supported, and
simon 1:2dec995d53f8 21 * indirectly determine whether it is a version 1.x SD/non-SD card or
simon 1:2dec995d53f8 22 * version 2.x. I'll just ignore this for now.
simon 1:2dec995d53f8 23 *
simon 1:2dec995d53f8 24 * ACMD41 is repeatedly issued to initialise the card, until "in idle"
simon 1:2dec995d53f8 25 * (bit 0) of the R1 response goes to '0', indicating it is initialised.
simon 1:2dec995d53f8 26 *
simon 1:2dec995d53f8 27 * You should also indicate whether the host supports High Capicity cards,
simon 1:2dec995d53f8 28 * and check whether the card is high capacity - i'll also ignore this
simon 1:2dec995d53f8 29 *
simon 1:2dec995d53f8 30 * SPI Protocol
simon 1:2dec995d53f8 31 * ------------
simon 1:2dec995d53f8 32 * The SD SPI protocol is based on transactions made up of 8-bit words, with
simon 1:2dec995d53f8 33 * the host starting every bus transaction by asserting the CS signal low. The
simon 1:2dec995d53f8 34 * card always responds to commands, data blocks and errors.
simon 1:2dec995d53f8 35 *
simon 1:2dec995d53f8 36 * The protocol supports a CRC, but by default it is off (except for the
simon 1:2dec995d53f8 37 * first reset CMD0, where the CRC can just be pre-calculated, and CMD8)
simon 1:2dec995d53f8 38 * I'll leave the CRC off I think!
simon 1:2dec995d53f8 39 *
simon 1:2dec995d53f8 40 * Standard capacity cards have variable data block sizes, whereas High
simon 1:2dec995d53f8 41 * Capacity cards fix the size of data block to 512 bytes. I'll therefore
simon 1:2dec995d53f8 42 * just always use the Standard Capacity cards with a block size of 512 bytes.
simon 1:2dec995d53f8 43 * This is set with CMD16.
simon 1:2dec995d53f8 44 *
simon 1:2dec995d53f8 45 * You can read and write single blocks (CMD17, CMD25) or multiple blocks
simon 1:2dec995d53f8 46 * (CMD18, CMD25). For simplicity, I'll just use single block accesses. When
simon 1:2dec995d53f8 47 * the card gets a read command, it responds with a response token, and then
simon 1:2dec995d53f8 48 * a data token or an error.
simon 1:2dec995d53f8 49 *
simon 1:2dec995d53f8 50 * SPI Command Format
simon 1:2dec995d53f8 51 * ------------------
simon 1:2dec995d53f8 52 * Commands are 6-bytes long, containing the command, 32-bit argument, and CRC.
simon 1:2dec995d53f8 53 *
simon 1:2dec995d53f8 54 * +---------------+------------+------------+-----------+----------+--------------+
simon 1:2dec995d53f8 55 * | 01 | cmd[5:0] | arg[31:24] | arg[23:16] | arg[15:8] | arg[7:0] | crc[6:0] | 1 |
simon 1:2dec995d53f8 56 * +---------------+------------+------------+-----------+----------+--------------+
simon 1:2dec995d53f8 57 *
simon 1:2dec995d53f8 58 * As I'm not using CRC, I can fix that byte to what is needed for CMD0 (0x95)
simon 1:2dec995d53f8 59 *
simon 1:2dec995d53f8 60 * All Application Specific commands shall be preceded with APP_CMD (CMD55).
simon 1:2dec995d53f8 61 *
simon 1:2dec995d53f8 62 * SPI Response Format
simon 1:2dec995d53f8 63 * -------------------
simon 1:2dec995d53f8 64 * The main response format (R1) is a status byte (normally zero). Key flags:
simon 1:2dec995d53f8 65 * idle - 1 if the card is in an idle state/initialising
simon 1:2dec995d53f8 66 * cmd - 1 if an illegal command code was detected
simon 1:2dec995d53f8 67 *
simon 1:2dec995d53f8 68 * +-------------------------------------------------+
simon 1:2dec995d53f8 69 * R1 | 0 | arg | addr | seq | crc | cmd | erase | idle |
simon 1:2dec995d53f8 70 * +-------------------------------------------------+
simon 1:2dec995d53f8 71 *
simon 1:2dec995d53f8 72 * R1b is the same, except it is followed by a busy signal (zeros) until
simon 1:2dec995d53f8 73 * the first non-zero byte when it is ready again.
simon 1:2dec995d53f8 74 *
simon 1:2dec995d53f8 75 * Data Response Token
simon 1:2dec995d53f8 76 * -------------------
simon 1:2dec995d53f8 77 * Every data block written to the card is acknowledged by a byte
simon 1:2dec995d53f8 78 * response token
simon 1:2dec995d53f8 79 *
simon 1:2dec995d53f8 80 * +----------------------+
simon 1:2dec995d53f8 81 * | xxx | 0 | status | 1 |
simon 1:2dec995d53f8 82 * +----------------------+
simon 1:2dec995d53f8 83 * 010 - OK!
simon 1:2dec995d53f8 84 * 101 - CRC Error
simon 1:2dec995d53f8 85 * 110 - Write Error
simon 1:2dec995d53f8 86 *
simon 1:2dec995d53f8 87 * Single Block Read and Write
simon 1:2dec995d53f8 88 * ---------------------------
simon 1:2dec995d53f8 89 *
simon 1:2dec995d53f8 90 * Block transfers have a byte header, followed by the data, followed
simon 1:2dec995d53f8 91 * by a 16-bit CRC. In our case, the data will always be 512 bytes.
simon 1:2dec995d53f8 92 *
simon 1:2dec995d53f8 93 * +------+---------+---------+- - - -+---------+-----------+----------+
simon 1:2dec995d53f8 94 * | 0xFE | data[0] | data[1] | | data[n] | crc[15:8] | crc[7:0] |
simon 1:2dec995d53f8 95 * +------+---------+---------+- - - -+---------+-----------+----------+
simon 1:2dec995d53f8 96 */
simon 1:2dec995d53f8 97
simon 1:2dec995d53f8 98 #include "SDFileSystem.h"
simon 1:2dec995d53f8 99
simon 1:2dec995d53f8 100 #define SD_COMMAND_TIMEOUT 5000
simon 1:2dec995d53f8 101
simon 1:2dec995d53f8 102 SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) :
simon 1:2dec995d53f8 103 FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs) {
simon 1:2dec995d53f8 104 _cs = 1;
simon 1:2dec995d53f8 105 }
simon 1:2dec995d53f8 106
simon 2:849162a1207f 107 #define R1_IDLE_STATE (1 << 0)
simon 2:849162a1207f 108 #define R1_ERASE_RESET (1 << 1)
simon 2:849162a1207f 109 #define R1_ILLEGAL_COMMAND (1 << 2)
simon 2:849162a1207f 110 #define R1_COM_CRC_ERROR (1 << 3)
simon 2:849162a1207f 111 #define R1_ERASE_SEQUENCE_ERROR (1 << 4)
simon 2:849162a1207f 112 #define R1_ADDRESS_ERROR (1 << 5)
simon 2:849162a1207f 113 #define R1_PARAMETER_ERROR (1 << 6)
simon 2:849162a1207f 114
simon 1:2dec995d53f8 115 int SDFileSystem::disk_initialize() {
simon 1:2dec995d53f8 116
simon 1:2dec995d53f8 117 _spi.frequency(100000); // Set to 100kHz for initialisation
simon 1:2dec995d53f8 118
simon 1:2dec995d53f8 119 // Initialise the card by clocking it with cs = 1
simon 1:2dec995d53f8 120 _cs = 1;
simon 1:2dec995d53f8 121 for(int i=0; i<16; i++) {
simon 1:2dec995d53f8 122 _spi.write(0xFF);
simon 1:2dec995d53f8 123 }
simon 1:2dec995d53f8 124
simon 1:2dec995d53f8 125 // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
simon 2:849162a1207f 126 if(_cmd(0, 0) != R1_IDLE_STATE) {
simon 2:849162a1207f 127 fprintf(stderr, "No disk, or could not put SD card in to SPI idle state\n");
simon 1:2dec995d53f8 128 return 1;
simon 1:2dec995d53f8 129 }
simon 2:849162a1207f 130
simon 2:849162a1207f 131 int r = _cmd8();
simon 2:849162a1207f 132 if(r == R1_IDLE_STATE) {
simon 2:849162a1207f 133 printf("SD version 2.x\n");
simon 2:849162a1207f 134 } else if(r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
simon 2:849162a1207f 135 printf("SD version 1.x\n");
simon 2:849162a1207f 136 } else {
simon 2:849162a1207f 137 error("Unknown SD version\n");
simon 2:849162a1207f 138 }
simon 2:849162a1207f 139
simon 1:2dec995d53f8 140 // ACMD41 to give host capacity support (repeat until not busy)
simon 1:2dec995d53f8 141 // ACMD41 is application specific command, so we send APP_CMD (CMD55) beforehand
simon 1:2dec995d53f8 142 for(int i=0;; i++) {
simon 1:2dec995d53f8 143 _cmd(55, 0);
simon 1:2dec995d53f8 144 int response = _cmd(41, 0);
simon 1:2dec995d53f8 145 if(response == 0) {
simon 1:2dec995d53f8 146 break;
simon 1:2dec995d53f8 147 } else if(i > SD_COMMAND_TIMEOUT) {
simon 1:2dec995d53f8 148 fprintf(stderr, "Timeout waiting for card\n");
simon 1:2dec995d53f8 149 return 1;
simon 1:2dec995d53f8 150 }
simon 1:2dec995d53f8 151 }
simon 1:2dec995d53f8 152
simon 2:849162a1207f 153 printf("OK\n");
simon 2:849162a1207f 154
simon 1:2dec995d53f8 155 _sectors = _sd_sectors();
simon 1:2dec995d53f8 156
simon 1:2dec995d53f8 157 // Set block length to 512 (CMD16)
simon 1:2dec995d53f8 158 if(_cmd(16, 512) != 0) {
simon 1:2dec995d53f8 159 fprintf(stderr, "Set block timeout\n");
simon 1:2dec995d53f8 160 return 1;
simon 1:2dec995d53f8 161 }
simon 1:2dec995d53f8 162
simon 1:2dec995d53f8 163 _spi.frequency(1000000); // Set to 1MHz for data transfer
simon 1:2dec995d53f8 164 return 0;
simon 1:2dec995d53f8 165 }
simon 1:2dec995d53f8 166
simon 1:2dec995d53f8 167 int SDFileSystem::disk_write(const char *buffer, int block_number) {
simon 1:2dec995d53f8 168 // set write address for single block (CMD24)
simon 1:2dec995d53f8 169 if(_cmd(24, block_number * 512) != 0) {
simon 1:2dec995d53f8 170 return 1;
simon 1:2dec995d53f8 171 }
simon 1:2dec995d53f8 172
simon 1:2dec995d53f8 173 // send the data block
simon 1:2dec995d53f8 174 _write(buffer, 512);
simon 1:2dec995d53f8 175 return 0;
simon 1:2dec995d53f8 176 }
simon 1:2dec995d53f8 177
simon 1:2dec995d53f8 178 int SDFileSystem::disk_read(char *buffer, int block_number) {
simon 1:2dec995d53f8 179 // set read address for single block (CMD17)
simon 1:2dec995d53f8 180 if(_cmd(17, block_number * 512) != 0) {
simon 1:2dec995d53f8 181 return 1;
simon 1:2dec995d53f8 182 }
simon 1:2dec995d53f8 183
simon 1:2dec995d53f8 184 // receive the data
simon 1:2dec995d53f8 185 _read(buffer, 512);
simon 1:2dec995d53f8 186 return 0;
simon 1:2dec995d53f8 187 }
simon 1:2dec995d53f8 188
simon 1:2dec995d53f8 189 int SDFileSystem::disk_status() { return 0; }
simon 1:2dec995d53f8 190 int SDFileSystem::disk_sync() { return 0; }
simon 1:2dec995d53f8 191 int SDFileSystem::disk_sectors() { return _sectors; }
simon 1:2dec995d53f8 192
simon 1:2dec995d53f8 193 // PRIVATE FUNCTIONS
simon 1:2dec995d53f8 194
simon 1:2dec995d53f8 195 int SDFileSystem::_cmd(int cmd, int arg) {
simon 1:2dec995d53f8 196 _cs = 0;
simon 1:2dec995d53f8 197
simon 1:2dec995d53f8 198 // send a command
simon 1:2dec995d53f8 199 _spi.write(0x40 | cmd);
simon 1:2dec995d53f8 200 _spi.write(arg >> 24);
simon 1:2dec995d53f8 201 _spi.write(arg >> 16);
simon 1:2dec995d53f8 202 _spi.write(arg >> 8);
simon 1:2dec995d53f8 203 _spi.write(arg >> 0);
simon 1:2dec995d53f8 204 _spi.write(0x95);
simon 1:2dec995d53f8 205
simon 1:2dec995d53f8 206 // wait for the repsonse (response[7] == 0)
simon 1:2dec995d53f8 207 for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
simon 1:2dec995d53f8 208 int response = _spi.write(0xFF);
simon 1:2dec995d53f8 209 if(!(response & 0x80)) {
simon 1:2dec995d53f8 210 _cs = 1;
simon 1:2dec995d53f8 211 _spi.write(0xFF);
simon 1:2dec995d53f8 212 return response;
simon 1:2dec995d53f8 213 }
simon 1:2dec995d53f8 214 }
simon 1:2dec995d53f8 215 _cs = 1;
simon 1:2dec995d53f8 216 _spi.write(0xFF);
simon 1:2dec995d53f8 217 return -1; // timeout
simon 1:2dec995d53f8 218 }
simon 1:2dec995d53f8 219
simon 2:849162a1207f 220 int SDFileSystem::_cmd8() {
simon 2:849162a1207f 221 _cs = 0;
simon 2:849162a1207f 222
simon 2:849162a1207f 223 // send a command
simon 2:849162a1207f 224 _spi.write(0x40 | 8); // CMD8
simon 2:849162a1207f 225 _spi.write(0x00); // reserved
simon 2:849162a1207f 226 _spi.write(0x00); // reserved
simon 2:849162a1207f 227 _spi.write(0x01); // 3.3v
simon 2:849162a1207f 228 _spi.write(0xAA); // check pattern
simon 2:849162a1207f 229 _spi.write(0x87); // crc
simon 2:849162a1207f 230
simon 2:849162a1207f 231 // wait for the repsonse (response[7] == 0)
simon 2:849162a1207f 232 for(int i=0; i<SD_COMMAND_TIMEOUT * 1000; i++) {
simon 2:849162a1207f 233 char response[5];
simon 2:849162a1207f 234 response[0] = _spi.write(0xFF);
simon 2:849162a1207f 235 if(!(response[0] & 0x80)) {
simon 2:849162a1207f 236 for(int j=1; j<5; j++) {
simon 2:849162a1207f 237 response[i] = _spi.write(0xFF);
simon 2:849162a1207f 238 }
simon 2:849162a1207f 239 _cs = 1;
simon 2:849162a1207f 240 _spi.write(0xFF);
simon 2:849162a1207f 241 return response[0];
simon 2:849162a1207f 242 }
simon 2:849162a1207f 243 }
simon 2:849162a1207f 244 _cs = 1;
simon 2:849162a1207f 245 _spi.write(0xFF);
simon 2:849162a1207f 246 return -1; // timeout
simon 2:849162a1207f 247 }
simon 2:849162a1207f 248
simon 1:2dec995d53f8 249 int SDFileSystem::_read(char *buffer, int length) {
simon 1:2dec995d53f8 250 _cs = 0;
simon 1:2dec995d53f8 251
simon 1:2dec995d53f8 252 // read until start byte (0xFF)
simon 1:2dec995d53f8 253 while(_spi.write(0xFF) != 0xFE);
simon 1:2dec995d53f8 254
simon 1:2dec995d53f8 255 // read data
simon 1:2dec995d53f8 256 for(int i=0; i<length; i++) {
simon 1:2dec995d53f8 257 buffer[i] = _spi.write(0xFF);
simon 1:2dec995d53f8 258 }
simon 1:2dec995d53f8 259 _spi.write(0xFF); // checksum
simon 1:2dec995d53f8 260 _spi.write(0xFF);
simon 1:2dec995d53f8 261
simon 1:2dec995d53f8 262 _cs = 1;
simon 1:2dec995d53f8 263 _spi.write(0xFF);
simon 1:2dec995d53f8 264 return 0;
simon 1:2dec995d53f8 265 }
simon 1:2dec995d53f8 266
simon 1:2dec995d53f8 267 int SDFileSystem::_write(const char *buffer, int length) {
simon 1:2dec995d53f8 268 _cs = 0;
simon 1:2dec995d53f8 269
simon 1:2dec995d53f8 270 // indicate start of block
simon 1:2dec995d53f8 271 _spi.write(0xFE);
simon 1:2dec995d53f8 272
simon 1:2dec995d53f8 273 // write the data
simon 1:2dec995d53f8 274 for(int i=0; i<length; i++) {
simon 1:2dec995d53f8 275 _spi.write(buffer[i]);
simon 1:2dec995d53f8 276 }
simon 1:2dec995d53f8 277
simon 1:2dec995d53f8 278 // write the checksum
simon 1:2dec995d53f8 279 _spi.write(0xFF);
simon 1:2dec995d53f8 280 _spi.write(0xFF);
simon 1:2dec995d53f8 281
simon 1:2dec995d53f8 282 // check the repsonse token
simon 1:2dec995d53f8 283 if((_spi.write(0xFF) & 0x1F) != 0x05) {
simon 1:2dec995d53f8 284 _cs = 1;
simon 1:2dec995d53f8 285 _spi.write(0xFF);
simon 1:2dec995d53f8 286 return 1;
simon 1:2dec995d53f8 287 }
simon 1:2dec995d53f8 288
simon 1:2dec995d53f8 289 // wait for write to finish
simon 1:2dec995d53f8 290 while(_spi.write(0xFF) == 0);
simon 1:2dec995d53f8 291
simon 1:2dec995d53f8 292 _cs = 1;
simon 1:2dec995d53f8 293 _spi.write(0xFF);
simon 1:2dec995d53f8 294 return 0;
simon 1:2dec995d53f8 295 }
simon 1:2dec995d53f8 296
simon 1:2dec995d53f8 297 static int ext_bits(char *data, int msb, int lsb) {
simon 1:2dec995d53f8 298 int bits = 0;
simon 1:2dec995d53f8 299 int size = 1 + msb - lsb;
simon 1:2dec995d53f8 300 for(int i=0; i<size; i++) {
simon 1:2dec995d53f8 301 int position = lsb + i;
simon 1:2dec995d53f8 302 int byte = 15 - (position >> 3);
simon 1:2dec995d53f8 303 int bit = position & 0x7;
simon 1:2dec995d53f8 304 int value = (data[byte] >> bit) & 1;
simon 1:2dec995d53f8 305 bits |= value << i;
simon 1:2dec995d53f8 306 }
simon 1:2dec995d53f8 307 return bits;
simon 1:2dec995d53f8 308 }
simon 1:2dec995d53f8 309
simon 1:2dec995d53f8 310 int SDFileSystem::_sd_sectors() {
simon 1:2dec995d53f8 311
simon 1:2dec995d53f8 312 // CMD9, Response R2 (R1 byte + 16-byte block read)
simon 1:2dec995d53f8 313 if(_cmd(9, 0) != 0) {
simon 1:2dec995d53f8 314 fprintf(stderr, "Didn't get a response from the disk\n");
simon 1:2dec995d53f8 315 return 0;
simon 1:2dec995d53f8 316 }
simon 1:2dec995d53f8 317
simon 1:2dec995d53f8 318 char csd[16];
simon 1:2dec995d53f8 319 if(_read(csd, 16) != 0) {
simon 1:2dec995d53f8 320 fprintf(stderr, "Couldn't read csd response from disk\n");
simon 1:2dec995d53f8 321 return 0;
simon 1:2dec995d53f8 322 }
simon 1:2dec995d53f8 323
simon 1:2dec995d53f8 324 // csd_structure : csd[127:126]
simon 1:2dec995d53f8 325 // c_size : csd[73:62]
simon 1:2dec995d53f8 326 // c_size_mult : csd[49:47]
simon 1:2dec995d53f8 327 // read_bl_len : csd[83:80]
simon 1:2dec995d53f8 328
simon 1:2dec995d53f8 329 int csd_structure = ext_bits(csd, 127, 126);
simon 1:2dec995d53f8 330 int c_size = ext_bits(csd, 73, 62);
simon 1:2dec995d53f8 331 int c_size_mult = ext_bits(csd, 49, 47);
simon 1:2dec995d53f8 332 int read_bl_len = ext_bits(csd, 83, 80);
simon 1:2dec995d53f8 333
simon 1:2dec995d53f8 334 if(csd_structure != 0) {
simon 1:2dec995d53f8 335 fprintf(stderr, "This disk tastes funny! I only know about type 0 CSD structures");
simon 1:2dec995d53f8 336 return 0;
simon 1:2dec995d53f8 337 }
simon 1:2dec995d53f8 338
simon 1:2dec995d53f8 339 int blocks = (c_size + 1) * (1 << (c_size_mult + 2));
simon 1:2dec995d53f8 340 int block_size = 1 << read_bl_len;
simon 1:2dec995d53f8 341
simon 1:2dec995d53f8 342 if(block_size != 512) {
simon 1:2dec995d53f8 343 fprintf(stderr, "This disk tastes funny! I only like 512-byte blocks");
simon 1:2dec995d53f8 344 return 0;
simon 1:2dec995d53f8 345 }
simon 1:2dec995d53f8 346
simon 1:2dec995d53f8 347 return blocks;
simon 1:2dec995d53f8 348 }