Dependencies:   mbed

Committer:
joosttromp
Date:
Fri Jun 17 11:30:43 2011 +0000
Revision:
0:aa9931e79e3f

        

Who changed what in which revision?

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