The Big Mouth Billy Bass program

Dependencies:   mbed

Committer:
sravet
Date:
Mon Jan 17 16:53:37 2011 +0000
Revision:
0:ef6fc1737022
Initial checkin

Who changed what in which revision?

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