Test session

Dependencies:   FatFileSystem MCP23017 WattBob_TextLCD mbed

Fork of Assignment_2_herpe by Xavier Herpe

Committer:
xherpe
Date:
Thu Mar 08 16:32:35 2012 +0000
Revision:
0:aaddc17011a9

        

Who changed what in which revision?

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