USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Sun Dec 11 15:42:23 2011 +0000
Revision:
19:148a0b8d23bc
add disk_status to check if init and write protection

Who changed what in which revision?

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