A simple digital lock with attempted audio guidance

Dependencies:   FATFileSystem TextLCD mbed

Fork of Digital_Lock_with_audio by NITH ece

Committer:
kit2
Date:
Fri May 17 17:17:24 2013 +0000
Revision:
5:d45608f231d3
Parent:
1:85eb1c94154a
Audio guidance not working

Who changed what in which revision?

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