Just a \"collage\" of some existing examples to test the web capabilities of the mBed

Dependencies:   EthernetNetIf NTPClient_NetServices mbed HTTPServer

Committer:
guiott
Date:
Thu Feb 17 13:13:13 2011 +0000
Revision:
0:bf8b2e1459d0

        

Who changed what in which revision?

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