Mbed_Text_Reader using an SDcard reader, 12-key touch pad and uLCD.

Dependencies:   FATFileSystem mbed-rtos mbed

Committer:
ndureja3
Date:
Mon Mar 24 06:48:12 2014 +0000
Revision:
1:806d2fa3e155
Parent:
0:6bd0fe190ecc
24 Mar 2014

Who changed what in which revision?

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