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