This is the final version of Mini Gateway for Automation and Security desgined for Renesas GR Peach Design Contest

Dependencies:   GR-PEACH_video GraphicsFramework HTTPServer R_BSP mbed-rpc mbed-rtos Socket lwip-eth lwip-sys lwip FATFileSystem

Fork of mbed-os-example-mbed5-blinky by mbed-os-examples

Committer:
vipinranka
Date:
Wed Jan 11 11:41:30 2017 +0000
Revision:
12:9a20164dcc47
This is the final version MGAS Project for Renesas GR Peach Design Contest

Who changed what in which revision?

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