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