SD library

Committer:
el14afma
Date:
Wed Apr 27 06:51:28 2016 +0000
Revision:
0:5ffb234d134c
-Save highscore and settings on a sd card

Who changed what in which revision?

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