temp

Dependencies:   mbed SDFileSystem MS5607 ADXL345_I2C FATFileSystem

Committer:
IKobayashi
Date:
Mon Mar 16 23:37:42 2020 +0900
Revision:
0:c88c3b616c00
copy

Who changed what in which revision?

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