Changes to allow hardware camera trigger

Dependencies:   mbed

Fork of GPS_Incremental by james kain

Committer:
dannyman939
Date:
Tue Mar 19 02:17:40 2013 +0000
Revision:
0:c746ee34feae
Basic functionality, Chris Version 0.0

Who changed what in which revision?

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