A version of the USBMSD_SD library that doesn't try to connect in the constructor. I made this change to support an operation mode where USB MSD can be enabled later in program operation without hanging.

Dependents:   Rocket

Fork of USBMSD_SD by Greg Steiert

Committer:
samux
Date:
Sun Dec 11 15:17:54 2011 +0000
Revision:
0:de50a209c5a9
Child:
1:923991b026e7

        

Who changed what in which revision?

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