MI0283QT Library Program demostration

Dependencies:   mbed MI0283QTlib

Committer:
clemente
Date:
Sat May 26 21:03:21 2012 +0000
Revision:
0:4c87cf4af8b5

        

Who changed what in which revision?

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