A File System interface that incorporates both SD and USB support. It was difficult to find a ready-to-go library, but the right set of pieces from others, and a few modifications, created this.

Dependencies:   FatFileSystemCpp USBHostLite

Dependents:   m3PI_TP_POPS_II2015v0 m3PI_TP_POPS_II2015v0 ourproject m3PI_TP_SETI ... more

Committer:
bouaziz
Date:
Sun Nov 10 21:01:29 2013 +0000
Revision:
2:5bc5d2b70622
Parent:
1:19a776ced714
Just simple modification to maje this lib usable by our students Polytech Paris Sud Undergraduate School  in university of Paris Sud Orsay;

Who changed what in which revision?

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