Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

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