Normal CS setting data for saving IR data.

Dependencies:   mbed

Committer:
halfpitch
Date:
Sat Sep 03 17:06:30 2011 +0000
Revision:
0:558646c716d0
Rev.A

Who changed what in which revision?

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