For BU9480F D-A Conv.

Dependencies:   mbed

Committer:
lynxeyed_atsu
Date:
Sat Sep 11 11:23:26 2010 +0000
Revision:
3:64c3ed0fc4b4
Parent:
0:805cffac956b

        

Who changed what in which revision?

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