Wave-Player with TLV320

Dependencies:   FatFileSystemCpp I2SSlave TLV320 mbed

Committer:
HB9GAA
Date:
Wed Dec 09 20:58:55 2015 +0000
Revision:
0:3087f1924901
Wave-Player with TLV320

Who changed what in which revision?

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