test

Dependencies:   FATFileSystem

Committer:
MuroMoe
Date:
Wed Nov 06 07:15:58 2019 +0000
Revision:
0:36610d84d988
ex

Who changed what in which revision?

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