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