Game for Leeds University Gamepad for the FRDM-K64F: Game is a RPG Horror Title.

Dependencies:   mbed FATFileSystem

Committer:
rottenegg
Date:
Fri May 10 21:25:27 2019 +0000
Revision:
26:716bcd47f3ca
Parent:
3:63cdd5cab431
FINAL_SUBMISSION; ; Changes:; WDplayer: Major Memory Leek fixed related to fclose(); Game_Manager: Tuned Scene Order and Improved Random number generator.; SceneFuctions: Added a Randomly changing Object to Scene 4.

Who changed what in which revision?

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