Bop-It Game

Dependencies:   TextLCD mbed ADXL345 VS1053b

Committer:
jdumond3
Date:
Wed Oct 19 18:04:48 2011 +0000
Revision:
0:d16ac399135a

        

Who changed what in which revision?

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