USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Tue Dec 06 12:07:12 2011 +0000
Revision:
14:757226626acb
Parent:
13:32b8a767cf0e
Child:
16:c753717bfd4d
protection enabled when usb cable plugged. filesystem has no access when plugged

Who changed what in which revision?

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