Copy of SDFileSystem library

Dependents:   IoTGateway_Basic y_CameraC1098_ES_01 y_XBeeTest_5_read MOVIE_MUSIC_TRIVIA_GAME ... more

Committer:
SomeRandomBloke
Date:
Sat Mar 31 06:58:28 2012 +0000
Revision:
0:9c5f0c655284
Copy of SDFileSystem library

Who changed what in which revision?

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