A fork of the original SDFileSystem, added only stat() for getting file information.

Dependents:   FRDM_K64F_IOT

Added code to the original SDFileSystem to export the stat() command. It would now be possible to get the FILEINFO struct of a directory entry to get information such as file size, etc.

SDFileSystem2 usage

#include "SDFileSystem.h"
SDFileSystem sd(p5,p6,p7,p8,"sd"); // mosi, miso, sck, cs 

static void cmd_ls(Stream * chp, int argc, char * argv[])
{
   DIR * dp;
   struct dirent * dirp;
   FILINFO fileInfo;
   char dirroot[256];
   
   if (argc >= 1)
       sprintf(dirroot, "/sd/%s", argv[0]);
   else
       sprintf(dirroot, "/sd");
   
   chp->printf("Listing directory [%s]\r\n", dirroot);
   
   dp = opendir(dirroot);			
   while((dirp = readdir(dp)) != NULL)
   {
       if (sd.stat(dirp->d_name, &fileInfo) == 0)
       {
           if (fileInfo.fattrib & AM_DIR )
                   chp->printf("<DIR>\t\t");
           else
                   chp->printf("%ld\t\t", fileInfo.fsize);
       }
       chp->printf("%s\r\n", dirp->d_name);
   }
   closedir(dp);
}
Committer:
vpcola
Date:
Tue Apr 28 16:55:30 2015 +0000
Revision:
0:572d27f56fcd
Added stat() to get file information

Who changed what in which revision?

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