teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Committer:
brunofgc
Date:
Fri Jun 08 22:14:21 2018 +0000
Revision:
38:07d3907b74e5
Parent:
0:1c0a769988ee
teste de publish para compilar no mbed-cli

Who changed what in which revision?

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