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