This is a lib to use ChaNFS lib with SD cards. Long filenames should work

Dependencies:  

Dependents:   USB_MSC USB_CDC_MSD_Hello TFTPServerTest ENVLogger ... more

Committer:
NeoBelerophon
Date:
Tue Feb 01 22:32:48 2011 +0000
Revision:
0:7532f4c4163c
Initial commit

Who changed what in which revision?

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