wefwe

Dependencies:   mbed C12832 DogM163 FatFileSystem

Committer:
JostBaus
Date:
Wed May 08 13:48:54 2019 +0000
Revision:
28:19aac2daf669
Wavspiller using RiceGulumb

Who changed what in which revision?

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