Dependencies:   mbed

Committer:
simon
Date:
Mon Dec 14 20:31:54 2009 +0000
Revision:
1:2dec995d53f8
Parent:
0:666a082cf50f
Child:
2:849162a1207f

        

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 1:2dec995d53f8 107 int SDFileSystem::disk_initialize() {
simon 1:2dec995d53f8 108
simon 1:2dec995d53f8 109 _spi.frequency(100000); // Set to 100kHz for initialisation
simon 1:2dec995d53f8 110
simon 1:2dec995d53f8 111 // Initialise the card by clocking it with cs = 1
simon 1:2dec995d53f8 112 _cs = 1;
simon 1:2dec995d53f8 113 for(int i=0; i<16; i++) {
simon 1:2dec995d53f8 114 _spi.write(0xFF);
simon 1:2dec995d53f8 115 }
simon 1:2dec995d53f8 116
simon 1:2dec995d53f8 117 // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
simon 1:2dec995d53f8 118 if(_cmd(0, 0) != 0x01) {
simon 1:2dec995d53f8 119 fprintf(stderr, "Not in idle state\n");
simon 1:2dec995d53f8 120 return 1;
simon 1:2dec995d53f8 121 }
simon 1:2dec995d53f8 122
simon 1:2dec995d53f8 123 // ACMD41 to give host capacity support (repeat until not busy)
simon 1:2dec995d53f8 124 // ACMD41 is application specific command, so we send APP_CMD (CMD55) beforehand
simon 1:2dec995d53f8 125 for(int i=0;; i++) {
simon 1:2dec995d53f8 126 _cmd(55, 0);
simon 1:2dec995d53f8 127 int response = _cmd(41, 0);
simon 1:2dec995d53f8 128 if(response == 0) {
simon 1:2dec995d53f8 129 break;
simon 1:2dec995d53f8 130 } else if(i > SD_COMMAND_TIMEOUT) {
simon 1:2dec995d53f8 131 fprintf(stderr, "Timeout waiting for card\n");
simon 1:2dec995d53f8 132 return 1;
simon 1:2dec995d53f8 133 }
simon 1:2dec995d53f8 134 }
simon 1:2dec995d53f8 135
simon 1:2dec995d53f8 136 _sectors = _sd_sectors();
simon 1:2dec995d53f8 137
simon 1:2dec995d53f8 138 // Set block length to 512 (CMD16)
simon 1:2dec995d53f8 139 if(_cmd(16, 512) != 0) {
simon 1:2dec995d53f8 140 fprintf(stderr, "Set block timeout\n");
simon 1:2dec995d53f8 141 return 1;
simon 1:2dec995d53f8 142 }
simon 1:2dec995d53f8 143
simon 1:2dec995d53f8 144 _spi.frequency(1000000); // Set to 1MHz for data transfer
simon 1:2dec995d53f8 145 return 0;
simon 1:2dec995d53f8 146 }
simon 1:2dec995d53f8 147
simon 1:2dec995d53f8 148 int SDFileSystem::disk_write(const char *buffer, int block_number) {
simon 1:2dec995d53f8 149 // set write address for single block (CMD24)
simon 1:2dec995d53f8 150 if(_cmd(24, block_number * 512) != 0) {
simon 1:2dec995d53f8 151 return 1;
simon 1:2dec995d53f8 152 }
simon 1:2dec995d53f8 153
simon 1:2dec995d53f8 154 // send the data block
simon 1:2dec995d53f8 155 _write(buffer, 512);
simon 1:2dec995d53f8 156 return 0;
simon 1:2dec995d53f8 157 }
simon 1:2dec995d53f8 158
simon 1:2dec995d53f8 159 int SDFileSystem::disk_read(char *buffer, int block_number) {
simon 1:2dec995d53f8 160 // set read address for single block (CMD17)
simon 1:2dec995d53f8 161 if(_cmd(17, block_number * 512) != 0) {
simon 1:2dec995d53f8 162 return 1;
simon 1:2dec995d53f8 163 }
simon 1:2dec995d53f8 164
simon 1:2dec995d53f8 165 // receive the data
simon 1:2dec995d53f8 166 _read(buffer, 512);
simon 1:2dec995d53f8 167 return 0;
simon 1:2dec995d53f8 168 }
simon 1:2dec995d53f8 169
simon 1:2dec995d53f8 170 int SDFileSystem::disk_status() { return 0; }
simon 1:2dec995d53f8 171 int SDFileSystem::disk_sync() { return 0; }
simon 1:2dec995d53f8 172 int SDFileSystem::disk_sectors() { return _sectors; }
simon 1:2dec995d53f8 173
simon 1:2dec995d53f8 174 // PRIVATE FUNCTIONS
simon 1:2dec995d53f8 175
simon 1:2dec995d53f8 176 int SDFileSystem::_cmd(int cmd, int arg) {
simon 1:2dec995d53f8 177 _cs = 0;
simon 1:2dec995d53f8 178
simon 1:2dec995d53f8 179 // send a command
simon 1:2dec995d53f8 180 _spi.write(0x40 | cmd);
simon 1:2dec995d53f8 181 _spi.write(arg >> 24);
simon 1:2dec995d53f8 182 _spi.write(arg >> 16);
simon 1:2dec995d53f8 183 _spi.write(arg >> 8);
simon 1:2dec995d53f8 184 _spi.write(arg >> 0);
simon 1:2dec995d53f8 185 _spi.write(0x95);
simon 1:2dec995d53f8 186
simon 1:2dec995d53f8 187 // wait for the repsonse (response[7] == 0)
simon 1:2dec995d53f8 188 for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
simon 1:2dec995d53f8 189 int response = _spi.write(0xFF);
simon 1:2dec995d53f8 190 if(!(response & 0x80)) {
simon 1:2dec995d53f8 191 _cs = 1;
simon 1:2dec995d53f8 192 _spi.write(0xFF);
simon 1:2dec995d53f8 193 return response;
simon 1:2dec995d53f8 194 }
simon 1:2dec995d53f8 195 }
simon 1:2dec995d53f8 196 _cs = 1;
simon 1:2dec995d53f8 197 _spi.write(0xFF);
simon 1:2dec995d53f8 198 return -1; // timeout
simon 1:2dec995d53f8 199 }
simon 1:2dec995d53f8 200
simon 1:2dec995d53f8 201 int SDFileSystem::_read(char *buffer, int length) {
simon 1:2dec995d53f8 202 _cs = 0;
simon 1:2dec995d53f8 203
simon 1:2dec995d53f8 204 // read until start byte (0xFF)
simon 1:2dec995d53f8 205 while(_spi.write(0xFF) != 0xFE);
simon 1:2dec995d53f8 206
simon 1:2dec995d53f8 207 // read data
simon 1:2dec995d53f8 208 for(int i=0; i<length; i++) {
simon 1:2dec995d53f8 209 buffer[i] = _spi.write(0xFF);
simon 1:2dec995d53f8 210 }
simon 1:2dec995d53f8 211 _spi.write(0xFF); // checksum
simon 1:2dec995d53f8 212 _spi.write(0xFF);
simon 1:2dec995d53f8 213
simon 1:2dec995d53f8 214 _cs = 1;
simon 1:2dec995d53f8 215 _spi.write(0xFF);
simon 1:2dec995d53f8 216 return 0;
simon 1:2dec995d53f8 217 }
simon 1:2dec995d53f8 218
simon 1:2dec995d53f8 219 int SDFileSystem::_write(const char *buffer, int length) {
simon 1:2dec995d53f8 220 _cs = 0;
simon 1:2dec995d53f8 221
simon 1:2dec995d53f8 222 // indicate start of block
simon 1:2dec995d53f8 223 _spi.write(0xFE);
simon 1:2dec995d53f8 224
simon 1:2dec995d53f8 225 // write the data
simon 1:2dec995d53f8 226 for(int i=0; i<length; i++) {
simon 1:2dec995d53f8 227 _spi.write(buffer[i]);
simon 1:2dec995d53f8 228 }
simon 1:2dec995d53f8 229
simon 1:2dec995d53f8 230 // write the checksum
simon 1:2dec995d53f8 231 _spi.write(0xFF);
simon 1:2dec995d53f8 232 _spi.write(0xFF);
simon 1:2dec995d53f8 233
simon 1:2dec995d53f8 234 // check the repsonse token
simon 1:2dec995d53f8 235 if((_spi.write(0xFF) & 0x1F) != 0x05) {
simon 1:2dec995d53f8 236 _cs = 1;
simon 1:2dec995d53f8 237 _spi.write(0xFF);
simon 1:2dec995d53f8 238 return 1;
simon 1:2dec995d53f8 239 }
simon 1:2dec995d53f8 240
simon 1:2dec995d53f8 241 // wait for write to finish
simon 1:2dec995d53f8 242 while(_spi.write(0xFF) == 0);
simon 1:2dec995d53f8 243
simon 1:2dec995d53f8 244 _cs = 1;
simon 1:2dec995d53f8 245 _spi.write(0xFF);
simon 1:2dec995d53f8 246 return 0;
simon 1:2dec995d53f8 247 }
simon 1:2dec995d53f8 248
simon 1:2dec995d53f8 249 static int ext_bits(char *data, int msb, int lsb) {
simon 1:2dec995d53f8 250 int bits = 0;
simon 1:2dec995d53f8 251 int size = 1 + msb - lsb;
simon 1:2dec995d53f8 252 for(int i=0; i<size; i++) {
simon 1:2dec995d53f8 253 int position = lsb + i;
simon 1:2dec995d53f8 254 int byte = 15 - (position >> 3);
simon 1:2dec995d53f8 255 int bit = position & 0x7;
simon 1:2dec995d53f8 256 int value = (data[byte] >> bit) & 1;
simon 1:2dec995d53f8 257 bits |= value << i;
simon 1:2dec995d53f8 258 }
simon 1:2dec995d53f8 259 return bits;
simon 1:2dec995d53f8 260 }
simon 1:2dec995d53f8 261
simon 1:2dec995d53f8 262 int SDFileSystem::_sd_sectors() {
simon 1:2dec995d53f8 263
simon 1:2dec995d53f8 264 // CMD9, Response R2 (R1 byte + 16-byte block read)
simon 1:2dec995d53f8 265 if(_cmd(9, 0) != 0) {
simon 1:2dec995d53f8 266 fprintf(stderr, "Didn't get a response from the disk\n");
simon 1:2dec995d53f8 267 return 0;
simon 1:2dec995d53f8 268 }
simon 1:2dec995d53f8 269
simon 1:2dec995d53f8 270 char csd[16];
simon 1:2dec995d53f8 271 if(_read(csd, 16) != 0) {
simon 1:2dec995d53f8 272 fprintf(stderr, "Couldn't read csd response from disk\n");
simon 1:2dec995d53f8 273 return 0;
simon 1:2dec995d53f8 274 }
simon 1:2dec995d53f8 275
simon 1:2dec995d53f8 276 // csd_structure : csd[127:126]
simon 1:2dec995d53f8 277 // c_size : csd[73:62]
simon 1:2dec995d53f8 278 // c_size_mult : csd[49:47]
simon 1:2dec995d53f8 279 // read_bl_len : csd[83:80]
simon 1:2dec995d53f8 280
simon 1:2dec995d53f8 281 int csd_structure = ext_bits(csd, 127, 126);
simon 1:2dec995d53f8 282 int c_size = ext_bits(csd, 73, 62);
simon 1:2dec995d53f8 283 int c_size_mult = ext_bits(csd, 49, 47);
simon 1:2dec995d53f8 284 int read_bl_len = ext_bits(csd, 83, 80);
simon 1:2dec995d53f8 285
simon 1:2dec995d53f8 286 if(csd_structure != 0) {
simon 1:2dec995d53f8 287 fprintf(stderr, "This disk tastes funny! I only know about type 0 CSD structures");
simon 1:2dec995d53f8 288 return 0;
simon 1:2dec995d53f8 289 }
simon 1:2dec995d53f8 290
simon 1:2dec995d53f8 291 int blocks = (c_size + 1) * (1 << (c_size_mult + 2));
simon 1:2dec995d53f8 292 int block_size = 1 << read_bl_len;
simon 1:2dec995d53f8 293
simon 1:2dec995d53f8 294 if(block_size != 512) {
simon 1:2dec995d53f8 295 fprintf(stderr, "This disk tastes funny! I only like 512-byte blocks");
simon 1:2dec995d53f8 296 return 0;
simon 1:2dec995d53f8 297 }
simon 1:2dec995d53f8 298
simon 1:2dec995d53f8 299 return blocks;
simon 1:2dec995d53f8 300 }