UBR CAN

Dependencies:   mbed

Fork of CAN_bus_reader_pub by Satoshi Nadakiti

Committer:
nadakiti
Date:
Wed Nov 09 04:59:01 2011 +0000
Revision:
0:78752077cc25

        

Who changed what in which revision?

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