dd

Dependencies:   C12832 LM75B mbed

Committer:
pfe
Date:
Tue Apr 21 10:16:20 2015 +0000
Revision:
0:05a20e3e3179
dd

Who changed what in which revision?

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