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