Talk Watch system using NTP timer for JBB.

Dependencies:   EthernetNetIf FatFileSystem HTTPClient_ToBeRemoved HTTPServer NTPClient_NetServices TextLCD mbed

Fork of StarBoardOrangeTest3 by Yuji Notsu

Committer:
y_notsu
Date:
Sun Jun 22 05:12:34 2014 +0000
Revision:
1:8816ea8be54b
Parent:
0:ae31fe6f181c
Talk Watch using NTP client for JBB

Who changed what in which revision?

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