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