nnnnn

Fork of SDFileSystem by Simon Ford

Committer:
simon
Date:
Thu Jun 03 16:32:50 2010 +0000
Revision:
0:b1ddfc9a9b25
Child:
3:a66a7f783ef0

        

Who changed what in which revision?

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