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