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