ロケット用プログラム

Dependencies:   mbed

Committer:
ishiyamayuto
Date:
Wed Sep 11 06:37:20 2019 +0000
Revision:
0:d58274531d38
BMP180,MPU6050

Who changed what in which revision?

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