This program is for Star Board Orange. Wave file into SD card is read, and its DAC signal output.

Dependencies:   EthernetNetIf mbed HTTPServer

Committer:
y_notsu
Date:
Thu Nov 25 14:59:16 2010 +0000
Revision:
0:383bff20e783

        

Who changed what in which revision?

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