Alex Allen / Mbed 2 deprecated Balloon

Dependencies:   UM12 mbed

Committer:
AlexAllen
Date:
Wed Mar 07 20:02:35 2012 +0000
Revision:
0:feaa05d35ccf

        

Who changed what in which revision?

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