Copy of "Simon Ford".

Committer:
ds074704261
Date:
Sun Nov 04 08:57:35 2012 +0000
Revision:
1:f060966d5bf9
Parent:
0:cb288fc30586
Copied on 09 Aug 2012.

Who changed what in which revision?

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