testing TextLCD and SD card

Dependencies:   mbed

Committer:
funax
Date:
Thu Jul 01 18:04:37 2010 +0000
Revision:
1:60d8589343b6
Parent:
0:cc883de36463

        

Who changed what in which revision?

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