Description

Dependents:   RoboPanorama

Fork of SDFileSystem by Simon Ford

Committer:
lndv3
Date:
Fri Oct 12 16:41:32 2012 +0000
Revision:
1:97e0c0200e51
Parent:
0:b1ddfc9a9b25
Commit.

Who changed what in which revision?

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