Lab Checkoff

Dependencies:   SDFileSystem TextLCD mbed-rtos mbed wave_player FATFileSystem

Committer:
doubster
Date:
Wed Nov 13 20:00:28 2013 +0000
Revision:
0:67dbd54e60d4
Lab Checkoff

Who changed what in which revision?

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