main_cpp file removed. SDHC support till 32GB

Dependencies:   FatFileSystem mbed

Dependents:   Spy_Robot_Complete

Fork of SDHCFileSystem by Klaus Bu

Committer:
kennyainny
Date:
Sun Dec 11 20:34:03 2016 +0000
Revision:
1:0cf4fe998c40
Parent:
0:90601632692f
troublesome 'main' file removed

Who changed what in which revision?

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