Stand-alone HMD demo system built on mbed FRDM-K64F.

Dependencies:   SDFileSystem mbed FATFileSystem

Committer:
maruishi
Date:
Tue Jul 29 11:41:59 2014 +0000
Revision:
0:9bbe50de69d8
FRDM-K64F based 3D Head-mount-display for Interface(CQ Pub)

Who changed what in which revision?

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