Dependencies:   mbed

Committer:
simon
Date:
Thu Nov 19 14:04:44 2009 +0000
Revision:
0:666a082cf50f
Child:
1:2dec995d53f8

        

Who changed what in which revision?

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