mbed Dev board test program

Dependencies:   EthernetNetIf mbed HTTPServer SerialLCD

Committer:
pangsk
Date:
Mon Jul 11 15:02:04 2011 +0000
Revision:
0:0f36b9fac4c5

        

Who changed what in which revision?

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