A program to read out the FTSE100 in real time using a TLV320 CODEC and Ethernet connexion

Dependencies:   EthernetNetIf I2SSlave mbed TLV320

Committer:
d_worrall
Date:
Fri Aug 05 13:05:52 2011 +0000
Revision:
0:e1324d2fc338
version 2.0

Who changed what in which revision?

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