Tick Tock / Mbed 2 deprecated CANcan

Dependencies:   SDHCFileSystem mbed

Committer:
TickTock
Date:
Mon Nov 19 04:44:11 2012 +0000
Revision:
0:1596b8644523
Initial revision - just getting hw up and running

Who changed what in which revision?

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