USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Sun Dec 11 15:07:59 2011 +0000
Revision:
17:364ef42e502d
OK: will write doc

Who changed what in which revision?

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