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