This is code is part of a Technion course project in advanced IoT, implementing a device to receive and present sensors data from a Formula racing car built by students at Technion - Israel Institute of Technology.

Dependencies:   mbed Buffer

Fork of DISCO-L072CZ-LRWAN1_LoRa_PingPong by ST

This is code is part of a Technion course project in advanced IoT, implementing a device to receive sensors data from another L072CZ-LRWAN1 installed on a Formula racing car (built by students at Technion - Israel Institute of Technology), and sends it to a GUI presenting the data (GUI project: github.com/ward-mattar/TechnionFormulaGUI).

How to install

  • Create an account on Mbed: https://os.mbed.com/account/signup/
  • Import project into Compiler
  • In the Program Workspace select "Formula_Nucleo_Receiver"
  • Select a Platform like so:
  1. Click button at top-left
  2. Add Board
  3. Search "NUCLEO F103RB" and then "Add to your Mbed Compiler"
  • Finally click "Compile", if the build was successful, the binary would download automatically
  • To install it on device simply plug it in to a PC, open device drive and drag then drop binary file in it
Committer:
wardm
Date:
Sat May 19 15:42:38 2018 +0000
Revision:
12:046346a16ff4
V1.0.0

Who changed what in which revision?

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