SD_hepta

Dependencies:   FatFileSystem

Dependents:   HEPTA2_assembly_0720 HEPTA2_ALL HEPTA2_ALL_ver0803_02 HEPTA

Committer:
hepta2ume
Date:
Mon Jul 24 05:52:49 2017 +0000
Revision:
0:ee6c3a0bbbbb
Hepta SD

Who changed what in which revision?

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