khoa phan test

Dependencies:   4DGL-uLCD-SE MMA8452 mbed wave_player

Fork of rpg_game_shell by ECE 2035 TA

Committer:
kphan9
Date:
Sun Apr 22 02:27:42 2018 +0000
Revision:
2:2f32fe4dadab
Parent:
0:35660d7952f7
test

Who changed what in which revision?

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