Web enabled thermostat demo using the Mission Cognition baseboard.

Dependencies:   NetServices mbed AvailableMemory

Committer:
jt
Date:
Mon Oct 03 21:45:56 2011 +0000
Revision:
0:2e82bfc9dc19
1.0

Who changed what in which revision?

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