Normal CS setting, send IR data from SD card module.

Dependencies:   mbed

Committer:
halfpitch
Date:
Sat Sep 03 17:10:24 2011 +0000
Revision:
0:011aabf9f436
Rev.A

Who changed what in which revision?

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