Mark Schwarzer / SDFileSystem

Dependents:   Schwarzer_A7_1

Committer:
markschwarzer
Date:
Mon Nov 09 14:25:13 2020 +0000
Revision:
0:964d386ab059
logged data in SD card

Who changed what in which revision?

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