Send IR data from SD card. I changed Chip Select signal timing.

Dependencies:   mbed

Committer:
halfpitch
Date:
Thu Sep 01 09:45:40 2011 +0000
Revision:
1:372b09a39fa4
Parent:
0:da2f4475a464
Rev.B

Who changed what in which revision?

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