Xbee CountUp

Dependencies:   mbed

Fork of HeptaXbee_CountUp by 智也 大野

Committer:
tomoya123
Date:
Tue Dec 13 07:55:03 2016 +0000
Revision:
1:715b80d2a02b
Parent:
0:0a7fa0911e6c
Xbee CountUp

Who changed what in which revision?

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