SDFileSystem-Smartes

Dependents:   Demo_NucleoF4xx_SD_Card soundthing

Committer:
FelipeVR
Date:
Tue Jun 12 18:15:57 2018 +0000
Revision:
0:70ff1cadb69a
SD Smartest;

Who changed what in which revision?

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