3.3[V]で動作するJpegシリアルカメラ(C1098-SS( http://www.silentsystem.jp/c1098.htm ))のサンプルプログラムです。

Dependencies:   mbed

Committer:
sunifu
Date:
Tue May 08 13:52:50 2012 +0000
Revision:
0:0482779cd78e
v 0.9

Who changed what in which revision?

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