White Wizard micro SD Card module test program.You can use White Wizard SPI system easily! Details : http://wizard.nestegg.jp/sd.html

Dependencies:   mbed

Committer:
halfpitch
Date:
Wed Jul 06 16:22:44 2011 +0000
Revision:
0:b8ab9a03a28d
Rev.A

Who changed what in which revision?

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