Moon Light / Mbed 2 deprecated CANUSB30

Dependencies:   mbed

Committer:
moon_light
Date:
Thu Feb 10 12:21:50 2011 +0000
Revision:
0:050fdb1efb7a
ver 0.1

Who changed what in which revision?

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