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