SDFileSystem, included for backwards compatibility.
SDFileSystem.cpp@0:6afe57e15f1a, 2012-05-08 (annotated)
- Committer:
- root@mbed.org
- Date:
- Tue May 08 15:20:54 2012 +0100
- Revision:
- 0:6afe57e15f1a
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
root@mbed.org | 0:6afe57e15f1a | 1 | /* mbed Microcontroller Library - SDFileSystem |
root@mbed.org | 0:6afe57e15f1a | 2 | * Copyright (c) 2008-2009, sford |
root@mbed.org | 0:6afe57e15f1a | 3 | * |
root@mbed.org | 0:6afe57e15f1a | 4 | * Introduction |
root@mbed.org | 0:6afe57e15f1a | 5 | * ------------ |
root@mbed.org | 0:6afe57e15f1a | 6 | * SD and MMC cards support a number of interfaces, but common to them all |
root@mbed.org | 0:6afe57e15f1a | 7 | * is one based on SPI. This is the one I'm implmenting because it means |
root@mbed.org | 0:6afe57e15f1a | 8 | * it is much more portable even though not so performant, and we already |
root@mbed.org | 0:6afe57e15f1a | 9 | * have the mbed SPI Interface! |
root@mbed.org | 0:6afe57e15f1a | 10 | * |
root@mbed.org | 0:6afe57e15f1a | 11 | * The main reference I'm using is Chapter 7, "SPI Mode" of: |
root@mbed.org | 0:6afe57e15f1a | 12 | * http://www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf |
root@mbed.org | 0:6afe57e15f1a | 13 | * |
root@mbed.org | 0:6afe57e15f1a | 14 | * SPI Startup |
root@mbed.org | 0:6afe57e15f1a | 15 | * ----------- |
root@mbed.org | 0:6afe57e15f1a | 16 | * The SD card powers up in SD mode. The SPI interface mode is selected by |
root@mbed.org | 0:6afe57e15f1a | 17 | * asserting CS low and sending the reset command (CMD0). The card will |
root@mbed.org | 0:6afe57e15f1a | 18 | * respond with a (R1) response. |
root@mbed.org | 0:6afe57e15f1a | 19 | * |
root@mbed.org | 0:6afe57e15f1a | 20 | * CMD8 is optionally sent to determine the voltage range supported, and |
root@mbed.org | 0:6afe57e15f1a | 21 | * indirectly determine whether it is a version 1.x SD/non-SD card or |
root@mbed.org | 0:6afe57e15f1a | 22 | * version 2.x. I'll just ignore this for now. |
root@mbed.org | 0:6afe57e15f1a | 23 | * |
root@mbed.org | 0:6afe57e15f1a | 24 | * ACMD41 is repeatedly issued to initialise the card, until "in idle" |
root@mbed.org | 0:6afe57e15f1a | 25 | * (bit 0) of the R1 response goes to '0', indicating it is initialised. |
root@mbed.org | 0:6afe57e15f1a | 26 | * |
root@mbed.org | 0:6afe57e15f1a | 27 | * You should also indicate whether the host supports High Capicity cards, |
root@mbed.org | 0:6afe57e15f1a | 28 | * and check whether the card is high capacity - i'll also ignore this |
root@mbed.org | 0:6afe57e15f1a | 29 | * |
root@mbed.org | 0:6afe57e15f1a | 30 | * SPI Protocol |
root@mbed.org | 0:6afe57e15f1a | 31 | * ------------ |
root@mbed.org | 0:6afe57e15f1a | 32 | * The SD SPI protocol is based on transactions made up of 8-bit words, with |
root@mbed.org | 0:6afe57e15f1a | 33 | * the host starting every bus transaction by asserting the CS signal low. The |
root@mbed.org | 0:6afe57e15f1a | 34 | * card always responds to commands, data blocks and errors. |
root@mbed.org | 0:6afe57e15f1a | 35 | * |
root@mbed.org | 0:6afe57e15f1a | 36 | * The protocol supports a CRC, but by default it is off (except for the |
root@mbed.org | 0:6afe57e15f1a | 37 | * first reset CMD0, where the CRC can just be pre-calculated, and CMD8) |
root@mbed.org | 0:6afe57e15f1a | 38 | * I'll leave the CRC off I think! |
root@mbed.org | 0:6afe57e15f1a | 39 | * |
root@mbed.org | 0:6afe57e15f1a | 40 | * Standard capacity cards have variable data block sizes, whereas High |
root@mbed.org | 0:6afe57e15f1a | 41 | * Capacity cards fix the size of data block to 512 bytes. I'll therefore |
root@mbed.org | 0:6afe57e15f1a | 42 | * just always use the Standard Capacity cards with a block size of 512 bytes. |
root@mbed.org | 0:6afe57e15f1a | 43 | * This is set with CMD16. |
root@mbed.org | 0:6afe57e15f1a | 44 | * |
root@mbed.org | 0:6afe57e15f1a | 45 | * You can read and write single blocks (CMD17, CMD25) or multiple blocks |
root@mbed.org | 0:6afe57e15f1a | 46 | * (CMD18, CMD25). For simplicity, I'll just use single block accesses. When |
root@mbed.org | 0:6afe57e15f1a | 47 | * the card gets a read command, it responds with a response token, and then |
root@mbed.org | 0:6afe57e15f1a | 48 | * a data token or an error. |
root@mbed.org | 0:6afe57e15f1a | 49 | * |
root@mbed.org | 0:6afe57e15f1a | 50 | * SPI Command Format |
root@mbed.org | 0:6afe57e15f1a | 51 | * ------------------ |
root@mbed.org | 0:6afe57e15f1a | 52 | * Commands are 6-bytes long, containing the command, 32-bit argument, and CRC. |
root@mbed.org | 0:6afe57e15f1a | 53 | * |
root@mbed.org | 0:6afe57e15f1a | 54 | * +---------------+------------+------------+-----------+----------+--------------+ |
root@mbed.org | 0:6afe57e15f1a | 55 | * | 01 | cmd[5:0] | arg[31:24] | arg[23:16] | arg[15:8] | arg[7:0] | crc[6:0] | 1 | |
root@mbed.org | 0:6afe57e15f1a | 56 | * +---------------+------------+------------+-----------+----------+--------------+ |
root@mbed.org | 0:6afe57e15f1a | 57 | * |
root@mbed.org | 0:6afe57e15f1a | 58 | * As I'm not using CRC, I can fix that byte to what is needed for CMD0 (0x95) |
root@mbed.org | 0:6afe57e15f1a | 59 | * |
root@mbed.org | 0:6afe57e15f1a | 60 | * All Application Specific commands shall be preceded with APP_CMD (CMD55). |
root@mbed.org | 0:6afe57e15f1a | 61 | * |
root@mbed.org | 0:6afe57e15f1a | 62 | * SPI Response Format |
root@mbed.org | 0:6afe57e15f1a | 63 | * ------------------- |
root@mbed.org | 0:6afe57e15f1a | 64 | * The main response format (R1) is a status byte (normally zero). Key flags: |
root@mbed.org | 0:6afe57e15f1a | 65 | * idle - 1 if the card is in an idle state/initialising |
root@mbed.org | 0:6afe57e15f1a | 66 | * cmd - 1 if an illegal command code was detected |
root@mbed.org | 0:6afe57e15f1a | 67 | * |
root@mbed.org | 0:6afe57e15f1a | 68 | * +-------------------------------------------------+ |
root@mbed.org | 0:6afe57e15f1a | 69 | * R1 | 0 | arg | addr | seq | crc | cmd | erase | idle | |
root@mbed.org | 0:6afe57e15f1a | 70 | * +-------------------------------------------------+ |
root@mbed.org | 0:6afe57e15f1a | 71 | * |
root@mbed.org | 0:6afe57e15f1a | 72 | * R1b is the same, except it is followed by a busy signal (zeros) until |
root@mbed.org | 0:6afe57e15f1a | 73 | * the first non-zero byte when it is ready again. |
root@mbed.org | 0:6afe57e15f1a | 74 | * |
root@mbed.org | 0:6afe57e15f1a | 75 | * Data Response Token |
root@mbed.org | 0:6afe57e15f1a | 76 | * ------------------- |
root@mbed.org | 0:6afe57e15f1a | 77 | * Every data block written to the card is acknowledged by a byte |
root@mbed.org | 0:6afe57e15f1a | 78 | * response token |
root@mbed.org | 0:6afe57e15f1a | 79 | * |
root@mbed.org | 0:6afe57e15f1a | 80 | * +----------------------+ |
root@mbed.org | 0:6afe57e15f1a | 81 | * | xxx | 0 | status | 1 | |
root@mbed.org | 0:6afe57e15f1a | 82 | * +----------------------+ |
root@mbed.org | 0:6afe57e15f1a | 83 | * 010 - OK! |
root@mbed.org | 0:6afe57e15f1a | 84 | * 101 - CRC Error |
root@mbed.org | 0:6afe57e15f1a | 85 | * 110 - Write Error |
root@mbed.org | 0:6afe57e15f1a | 86 | * |
root@mbed.org | 0:6afe57e15f1a | 87 | * Single Block Read and Write |
root@mbed.org | 0:6afe57e15f1a | 88 | * --------------------------- |
root@mbed.org | 0:6afe57e15f1a | 89 | * |
root@mbed.org | 0:6afe57e15f1a | 90 | * Block transfers have a byte header, followed by the data, followed |
root@mbed.org | 0:6afe57e15f1a | 91 | * by a 16-bit CRC. In our case, the data will always be 512 bytes. |
root@mbed.org | 0:6afe57e15f1a | 92 | * |
root@mbed.org | 0:6afe57e15f1a | 93 | * +------+---------+---------+- - - -+---------+-----------+----------+ |
root@mbed.org | 0:6afe57e15f1a | 94 | * | 0xFE | data[0] | data[1] | | data[n] | crc[15:8] | crc[7:0] | |
root@mbed.org | 0:6afe57e15f1a | 95 | * +------+---------+---------+- - - -+---------+-----------+----------+ |
root@mbed.org | 0:6afe57e15f1a | 96 | */ |
root@mbed.org | 0:6afe57e15f1a | 97 | |
root@mbed.org | 0:6afe57e15f1a | 98 | #include "SDFileSystem.h" |
root@mbed.org | 0:6afe57e15f1a | 99 | |
root@mbed.org | 0:6afe57e15f1a | 100 | #define SD_COMMAND_TIMEOUT 5000 |
root@mbed.org | 0:6afe57e15f1a | 101 | |
root@mbed.org | 0:6afe57e15f1a | 102 | SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) : |
root@mbed.org | 0:6afe57e15f1a | 103 | FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs) { |
root@mbed.org | 0:6afe57e15f1a | 104 | _cs = 1; |
root@mbed.org | 0:6afe57e15f1a | 105 | } |
root@mbed.org | 0:6afe57e15f1a | 106 | |
root@mbed.org | 0:6afe57e15f1a | 107 | int SDFileSystem::disk_initialize() { |
root@mbed.org | 0:6afe57e15f1a | 108 | |
root@mbed.org | 0:6afe57e15f1a | 109 | _spi.frequency(100000); // Set to 100kHz for initialisation |
root@mbed.org | 0:6afe57e15f1a | 110 | |
root@mbed.org | 0:6afe57e15f1a | 111 | // Initialise the card by clocking it a bit (cs = 1) |
root@mbed.org | 0:6afe57e15f1a | 112 | for(int i=0; i<16; i++) { |
root@mbed.org | 0:6afe57e15f1a | 113 | _spi.write(0xFF); |
root@mbed.org | 0:6afe57e15f1a | 114 | } |
root@mbed.org | 0:6afe57e15f1a | 115 | |
root@mbed.org | 0:6afe57e15f1a | 116 | // send CMD0, should return with all zeros except IDLE STATE set (bit 0) |
root@mbed.org | 0:6afe57e15f1a | 117 | if(_cmd(0, 0) != 0x01) { |
root@mbed.org | 0:6afe57e15f1a | 118 | fprintf(stderr, "Not in idle state\n"); |
root@mbed.org | 0:6afe57e15f1a | 119 | return 1; |
root@mbed.org | 0:6afe57e15f1a | 120 | } |
root@mbed.org | 0:6afe57e15f1a | 121 | |
root@mbed.org | 0:6afe57e15f1a | 122 | // ACMD41 to give host capacity support (repeat until not busy) |
root@mbed.org | 0:6afe57e15f1a | 123 | // ACMD41 is application specific command, so we send APP_CMD (CMD55) beforehand |
root@mbed.org | 0:6afe57e15f1a | 124 | for(int i=0;; i++) { |
root@mbed.org | 0:6afe57e15f1a | 125 | _cmd(55, 0); |
root@mbed.org | 0:6afe57e15f1a | 126 | int response = _cmd(41, 0); |
root@mbed.org | 0:6afe57e15f1a | 127 | if(response == 0) { |
root@mbed.org | 0:6afe57e15f1a | 128 | break; |
root@mbed.org | 0:6afe57e15f1a | 129 | } else if(i > SD_COMMAND_TIMEOUT) { |
root@mbed.org | 0:6afe57e15f1a | 130 | fprintf(stderr, "Timeout waiting for card\n"); |
root@mbed.org | 0:6afe57e15f1a | 131 | return 1; |
root@mbed.org | 0:6afe57e15f1a | 132 | } |
root@mbed.org | 0:6afe57e15f1a | 133 | } |
root@mbed.org | 0:6afe57e15f1a | 134 | |
root@mbed.org | 0:6afe57e15f1a | 135 | _sectors = _sd_sectors(); |
root@mbed.org | 0:6afe57e15f1a | 136 | |
root@mbed.org | 0:6afe57e15f1a | 137 | // Set block length to 512 (CMD16) |
root@mbed.org | 0:6afe57e15f1a | 138 | if(_cmd(16, 512) != 0) { |
root@mbed.org | 0:6afe57e15f1a | 139 | fprintf(stderr, "Set block timeout\n"); |
root@mbed.org | 0:6afe57e15f1a | 140 | return 1; |
root@mbed.org | 0:6afe57e15f1a | 141 | } |
root@mbed.org | 0:6afe57e15f1a | 142 | |
root@mbed.org | 0:6afe57e15f1a | 143 | _spi.frequency(1000000); // Set to 1MHz for data transfer |
root@mbed.org | 0:6afe57e15f1a | 144 | return 0; |
root@mbed.org | 0:6afe57e15f1a | 145 | } |
root@mbed.org | 0:6afe57e15f1a | 146 | |
root@mbed.org | 0:6afe57e15f1a | 147 | int SDFileSystem::disk_write(const char *buffer, int block_number) { |
root@mbed.org | 0:6afe57e15f1a | 148 | // set write address for single block (CMD24) |
root@mbed.org | 0:6afe57e15f1a | 149 | if(_cmd(24, block_number * 512) != 0) { |
root@mbed.org | 0:6afe57e15f1a | 150 | return 1; |
root@mbed.org | 0:6afe57e15f1a | 151 | } |
root@mbed.org | 0:6afe57e15f1a | 152 | |
root@mbed.org | 0:6afe57e15f1a | 153 | // send the data block |
root@mbed.org | 0:6afe57e15f1a | 154 | _write(buffer, 512); |
root@mbed.org | 0:6afe57e15f1a | 155 | return 0; |
root@mbed.org | 0:6afe57e15f1a | 156 | } |
root@mbed.org | 0:6afe57e15f1a | 157 | |
root@mbed.org | 0:6afe57e15f1a | 158 | int SDFileSystem::disk_read(char *buffer, int block_number) { |
root@mbed.org | 0:6afe57e15f1a | 159 | // set read address for single block (CMD17) |
root@mbed.org | 0:6afe57e15f1a | 160 | if(_cmd(17, block_number * 512) != 0) { |
root@mbed.org | 0:6afe57e15f1a | 161 | return 1; |
root@mbed.org | 0:6afe57e15f1a | 162 | } |
root@mbed.org | 0:6afe57e15f1a | 163 | |
root@mbed.org | 0:6afe57e15f1a | 164 | // receive the data |
root@mbed.org | 0:6afe57e15f1a | 165 | _read(buffer, 512); |
root@mbed.org | 0:6afe57e15f1a | 166 | return 0; |
root@mbed.org | 0:6afe57e15f1a | 167 | } |
root@mbed.org | 0:6afe57e15f1a | 168 | |
root@mbed.org | 0:6afe57e15f1a | 169 | int SDFileSystem::disk_status() { return 0; } |
root@mbed.org | 0:6afe57e15f1a | 170 | int SDFileSystem::disk_sync() { return 0; } |
root@mbed.org | 0:6afe57e15f1a | 171 | int SDFileSystem::disk_sectors() { return _sectors; } |
root@mbed.org | 0:6afe57e15f1a | 172 | |
root@mbed.org | 0:6afe57e15f1a | 173 | // PRIVATE FUNCTIONS |
root@mbed.org | 0:6afe57e15f1a | 174 | |
root@mbed.org | 0:6afe57e15f1a | 175 | int SDFileSystem::_cmd(int cmd, int arg) { |
root@mbed.org | 0:6afe57e15f1a | 176 | _cs = 0; |
root@mbed.org | 0:6afe57e15f1a | 177 | |
root@mbed.org | 0:6afe57e15f1a | 178 | // send a command |
root@mbed.org | 0:6afe57e15f1a | 179 | _spi.write(0x40 | cmd); |
root@mbed.org | 0:6afe57e15f1a | 180 | _spi.write(arg >> 24); |
root@mbed.org | 0:6afe57e15f1a | 181 | _spi.write(arg >> 16); |
root@mbed.org | 0:6afe57e15f1a | 182 | _spi.write(arg >> 8); |
root@mbed.org | 0:6afe57e15f1a | 183 | _spi.write(arg >> 0); |
root@mbed.org | 0:6afe57e15f1a | 184 | _spi.write(0x95); |
root@mbed.org | 0:6afe57e15f1a | 185 | |
root@mbed.org | 0:6afe57e15f1a | 186 | // wait for the repsonse (response[7] == 0) |
root@mbed.org | 0:6afe57e15f1a | 187 | for(int i=0; i<SD_COMMAND_TIMEOUT; i++) { |
root@mbed.org | 0:6afe57e15f1a | 188 | int response = _spi.write(0xFF); |
root@mbed.org | 0:6afe57e15f1a | 189 | if(!(response & 0x80)) { |
root@mbed.org | 0:6afe57e15f1a | 190 | _cs = 1; |
root@mbed.org | 0:6afe57e15f1a | 191 | return response; |
root@mbed.org | 0:6afe57e15f1a | 192 | } |
root@mbed.org | 0:6afe57e15f1a | 193 | } |
root@mbed.org | 0:6afe57e15f1a | 194 | _cs = 1; |
root@mbed.org | 0:6afe57e15f1a | 195 | return -1; // timeout |
root@mbed.org | 0:6afe57e15f1a | 196 | } |
root@mbed.org | 0:6afe57e15f1a | 197 | |
root@mbed.org | 0:6afe57e15f1a | 198 | int SDFileSystem::_read(char *buffer, int length) { |
root@mbed.org | 0:6afe57e15f1a | 199 | _cs = 0; |
root@mbed.org | 0:6afe57e15f1a | 200 | |
root@mbed.org | 0:6afe57e15f1a | 201 | // read until start byte (0xFF) |
root@mbed.org | 0:6afe57e15f1a | 202 | while(_spi.write(0xFF) != 0xFE); |
root@mbed.org | 0:6afe57e15f1a | 203 | |
root@mbed.org | 0:6afe57e15f1a | 204 | // read data |
root@mbed.org | 0:6afe57e15f1a | 205 | for(int i=0; i<length; i++) { |
root@mbed.org | 0:6afe57e15f1a | 206 | buffer[i] = _spi.write(0xFF); |
root@mbed.org | 0:6afe57e15f1a | 207 | } |
root@mbed.org | 0:6afe57e15f1a | 208 | _spi.write(0xFF); // checksum |
root@mbed.org | 0:6afe57e15f1a | 209 | _spi.write(0xFF); |
root@mbed.org | 0:6afe57e15f1a | 210 | |
root@mbed.org | 0:6afe57e15f1a | 211 | _cs = 1; |
root@mbed.org | 0:6afe57e15f1a | 212 | return 0; |
root@mbed.org | 0:6afe57e15f1a | 213 | } |
root@mbed.org | 0:6afe57e15f1a | 214 | |
root@mbed.org | 0:6afe57e15f1a | 215 | int SDFileSystem::_write(const char *buffer, int length) { |
root@mbed.org | 0:6afe57e15f1a | 216 | _cs = 0; |
root@mbed.org | 0:6afe57e15f1a | 217 | |
root@mbed.org | 0:6afe57e15f1a | 218 | // indicate start of block |
root@mbed.org | 0:6afe57e15f1a | 219 | _spi.write(0xFE); |
root@mbed.org | 0:6afe57e15f1a | 220 | |
root@mbed.org | 0:6afe57e15f1a | 221 | // write the data |
root@mbed.org | 0:6afe57e15f1a | 222 | for(int i=0; i<length; i++) { |
root@mbed.org | 0:6afe57e15f1a | 223 | _spi.write(buffer[i]); |
root@mbed.org | 0:6afe57e15f1a | 224 | } |
root@mbed.org | 0:6afe57e15f1a | 225 | |
root@mbed.org | 0:6afe57e15f1a | 226 | // write the checksum |
root@mbed.org | 0:6afe57e15f1a | 227 | _spi.write(0xFF); |
root@mbed.org | 0:6afe57e15f1a | 228 | _spi.write(0xFF); |
root@mbed.org | 0:6afe57e15f1a | 229 | |
root@mbed.org | 0:6afe57e15f1a | 230 | // check the repsonse token |
root@mbed.org | 0:6afe57e15f1a | 231 | if((_spi.write(0xFF) & 0x1F) != 0x05) { |
root@mbed.org | 0:6afe57e15f1a | 232 | _cs = 1; |
root@mbed.org | 0:6afe57e15f1a | 233 | return 1; |
root@mbed.org | 0:6afe57e15f1a | 234 | } |
root@mbed.org | 0:6afe57e15f1a | 235 | |
root@mbed.org | 0:6afe57e15f1a | 236 | // wait for write to finish |
root@mbed.org | 0:6afe57e15f1a | 237 | while(_spi.write(0xFF) == 0); |
root@mbed.org | 0:6afe57e15f1a | 238 | |
root@mbed.org | 0:6afe57e15f1a | 239 | _cs = 1; |
root@mbed.org | 0:6afe57e15f1a | 240 | return 0; |
root@mbed.org | 0:6afe57e15f1a | 241 | } |
root@mbed.org | 0:6afe57e15f1a | 242 | |
root@mbed.org | 0:6afe57e15f1a | 243 | static int ext_bits(char *data, int msb, int lsb) { |
root@mbed.org | 0:6afe57e15f1a | 244 | int bits = 0; |
root@mbed.org | 0:6afe57e15f1a | 245 | int size = 1 + msb - lsb; |
root@mbed.org | 0:6afe57e15f1a | 246 | for(int i=0; i<size; i++) { |
root@mbed.org | 0:6afe57e15f1a | 247 | int position = lsb + i; |
root@mbed.org | 0:6afe57e15f1a | 248 | int byte = 15 - (position >> 3); |
root@mbed.org | 0:6afe57e15f1a | 249 | int bit = position & 0x7; |
root@mbed.org | 0:6afe57e15f1a | 250 | int value = (data[byte] >> bit) & 1; |
root@mbed.org | 0:6afe57e15f1a | 251 | bits |= value << i; |
root@mbed.org | 0:6afe57e15f1a | 252 | } |
root@mbed.org | 0:6afe57e15f1a | 253 | return bits; |
root@mbed.org | 0:6afe57e15f1a | 254 | } |
root@mbed.org | 0:6afe57e15f1a | 255 | |
root@mbed.org | 0:6afe57e15f1a | 256 | int SDFileSystem::_sd_sectors() { |
root@mbed.org | 0:6afe57e15f1a | 257 | |
root@mbed.org | 0:6afe57e15f1a | 258 | // CMD9, Response R2 (R1 byte + 16-byte block read) |
root@mbed.org | 0:6afe57e15f1a | 259 | if(_cmd(9, 0) != 0) { |
root@mbed.org | 0:6afe57e15f1a | 260 | fprintf(stderr, "Didn't get a response from the disk\n"); |
root@mbed.org | 0:6afe57e15f1a | 261 | return 0; |
root@mbed.org | 0:6afe57e15f1a | 262 | } |
root@mbed.org | 0:6afe57e15f1a | 263 | |
root@mbed.org | 0:6afe57e15f1a | 264 | char csd[16]; |
root@mbed.org | 0:6afe57e15f1a | 265 | if(_read(csd, 16) != 0) { |
root@mbed.org | 0:6afe57e15f1a | 266 | fprintf(stderr, "Couldn't read csd response from disk\n"); |
root@mbed.org | 0:6afe57e15f1a | 267 | return 0; |
root@mbed.org | 0:6afe57e15f1a | 268 | } |
root@mbed.org | 0:6afe57e15f1a | 269 | |
root@mbed.org | 0:6afe57e15f1a | 270 | // csd_structure : csd[127:126] |
root@mbed.org | 0:6afe57e15f1a | 271 | // c_size : csd[73:62] |
root@mbed.org | 0:6afe57e15f1a | 272 | // c_size_mult : csd[49:47] |
root@mbed.org | 0:6afe57e15f1a | 273 | // read_bl_len : csd[83:80] |
root@mbed.org | 0:6afe57e15f1a | 274 | |
root@mbed.org | 0:6afe57e15f1a | 275 | int csd_structure = ext_bits(csd, 127, 126); |
root@mbed.org | 0:6afe57e15f1a | 276 | int c_size = ext_bits(csd, 73, 62); |
root@mbed.org | 0:6afe57e15f1a | 277 | int c_size_mult = ext_bits(csd, 49, 47); |
root@mbed.org | 0:6afe57e15f1a | 278 | int read_bl_len = ext_bits(csd, 83, 80); |
root@mbed.org | 0:6afe57e15f1a | 279 | |
root@mbed.org | 0:6afe57e15f1a | 280 | if(csd_structure != 0) { |
root@mbed.org | 0:6afe57e15f1a | 281 | fprintf(stderr, "This disk tastes funny! I only know about type 0 CSD structures"); |
root@mbed.org | 0:6afe57e15f1a | 282 | return 0; |
root@mbed.org | 0:6afe57e15f1a | 283 | } |
root@mbed.org | 0:6afe57e15f1a | 284 | |
root@mbed.org | 0:6afe57e15f1a | 285 | int blocks = (c_size + 1) * (1 << (c_size_mult + 2)); |
root@mbed.org | 0:6afe57e15f1a | 286 | int block_size = 1 << read_bl_len; |
root@mbed.org | 0:6afe57e15f1a | 287 | |
root@mbed.org | 0:6afe57e15f1a | 288 | if(block_size != 512) { |
root@mbed.org | 0:6afe57e15f1a | 289 | fprintf(stderr, "This disk tastes funny! I only like 512-byte blocks"); |
root@mbed.org | 0:6afe57e15f1a | 290 | return 0; |
root@mbed.org | 0:6afe57e15f1a | 291 | } |
root@mbed.org | 0:6afe57e15f1a | 292 | |
root@mbed.org | 0:6afe57e15f1a | 293 | return blocks; |
root@mbed.org | 0:6afe57e15f1a | 294 | } |