inisat_dev / Mbed 2 deprecated inisat4nucleo

Dependencies:   mbed Servo

Committer:
eric11fr
Date:
Thu Sep 03 21:45:56 2020 +0000
Revision:
0:b8bade04f24f
V1;

Who changed what in which revision?

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