dotHR_URG

Dependencies:   FatFileSystem TextLCD mbed

Committer:
higedura
Date:
Tue Sep 29 02:00:59 2015 +0000
Revision:
2:22b200c374cd
Parent:
0:a1accf06b614
150929_URG

Who changed what in which revision?

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