This is the firmware for the LaOS - Laser Open Source project. You can use it to drive a laser cutter. For hardware and more information, look at our wiki: http://wiki.laoslaser.org

Dependencies:   EthernetNetIf mbed

Committer:
fablabtruck
Date:
Fri Jun 08 09:26:40 2012 +0000
Revision:
0:3852426a5068
svn revision 379

Who changed what in which revision?

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