Test of Embedded Artists LPCXpresso baseboard SD card and ethernet facilities. This program downloads files from a website to the SD card. This program now uses the HTTPClient from: http://mbed.org/users/donatien/programs/HTTPClient/latest which downloads the files without errors. The previous version of this program was used to demonstrate that an earlier HTTPClient downloaded files with errors.

Dependencies:   EthernetNetIf mbed

Committer:
tom_coxon
Date:
Fri Aug 13 14:45:50 2010 +0000
Revision:
1:0734a7b0fd5e

        

Who changed what in which revision?

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