Dependencies:   mbed

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