Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Thu May 09 14:52:19 2019 +0000
Revision:
104:17040265b7b4
Parent:
51:387249f9b333
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

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