This package contains a simple test of tests for various elements of the SmartBoard hardware, which is a simple baseboard designed for easy embedding. It is able to run both a semi-automatic test suite as well as allow interactive testing.

Dependencies:   EthernetNetIf NTPClient_NetServices mbed

This program is most of what you need to test your SmartBoard baseboard hardware. It provides a means to test the following:

  • Two channels of CAN (with a loopback cable)
  • RS-232 Ports
  • Analog inputs
  • PWM outputs
  • Ethernet port
  • Real time clock
  • micro SD
  • USB Host port
Committer:
WiredHome
Date:
Sun Jan 16 18:30:14 2011 +0000
Revision:
0:5db287f0060b
First release

Who changed what in which revision?

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