This is a simple benchmark of the SDFileSystem module. This will attempt to write bytes to the SD card, and will time the operation and display the bits/sec.

Dependencies:   mbed

Committer:
theterg
Date:
Mon Feb 14 22:12:09 2011 +0000
Revision:
0:5e7c5b7321c9
1/14/10

Who changed what in which revision?

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