Use the TLV320 with in-built I2S object to stream audio data from an SD Card and send it to the TLV320 CODEC for audio playback

Dependencies:   I2SSlave mbed TLV320

Committer:
d_worrall
Date:
Fri Aug 05 10:07:47 2011 +0000
Revision:
0:3d6892f6384f
version 2.0

Who changed what in which revision?

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