パラメータを適応変化させる事により圧縮率を向上させた動的ライス・ゴロム符号を利用した可逆圧縮方式。圧縮ソフト、圧縮率のMATLABシミュレーションは詳細はInterface誌2011年8月号に掲載されるRX62Nマイコン連動特集にて掲載予定。

Dependencies:   mbed

Committer:
lynxeyed_atsu
Date:
Wed Mar 30 06:05:24 2011 +0000
Revision:
0:d920d64db582
alpha

Who changed what in which revision?

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