The software I used to run my Sparkfun AVC 2011 entry, a 1:10 scale RC truck called \"Data Bus\". This is the final revision of the code as-run on April 23, 2011, the date of the contest, on the 3rd heat.

Dependencies:   TinyCHR6dm PinDetect mbed IncrementalEncoder Servo Schedule DebounceIn SimpleFilter LSM303DLH Steering

Committer:
shimniok
Date:
Wed Apr 27 19:23:43 2011 +0000
Revision:
0:9b27ebe1ce17
Initial release

Who changed what in which revision?

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