A simple digital lock with attempted audio guidance

Dependencies:   FATFileSystem TextLCD mbed

Fork of Digital_Lock_with_audio by NITH ece

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SDFileSystem.cpp Source File

SDFileSystem.cpp

00001 /* mbed Microcontroller Library - SDFileSystem
00002  * Copyright (c) 2008-2009, sford
00003  *
00004  * Introduction
00005  * ------------
00006  * SD and MMC cards support a number of interfaces, but common to them all
00007  * is one based on SPI. This is the one I'm implmenting because it means
00008  * it is much more portable even though not so performant, and we already 
00009  * have the mbed SPI Interface!
00010  *
00011  * The main reference I'm using is Chapter 7, "SPI Mode" of: 
00012  *  http://www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
00013  *
00014  * SPI Startup
00015  * -----------
00016  * The SD card powers up in SD mode. The SPI interface mode is selected by
00017  * asserting CS low and sending the reset command (CMD0). The card will 
00018  * respond with a (R1) response.
00019  *
00020  * CMD8 is optionally sent to determine the voltage range supported, and 
00021  * indirectly determine whether it is a version 1.x SD/non-SD card or 
00022  * version 2.x. I'll just ignore this for now.
00023  *
00024  * ACMD41 is repeatedly issued to initialise the card, until "in idle"
00025  * (bit 0) of the R1 response goes to '0', indicating it is initialised.
00026  *
00027  * You should also indicate whether the host supports High Capicity cards,
00028  * and check whether the card is high capacity - i'll also ignore this
00029  *
00030  * SPI Protocol
00031  * ------------
00032  * The SD SPI protocol is based on transactions made up of 8-bit words, with
00033  * the host starting every bus transaction by asserting the CS signal low. The
00034  * card always responds to commands, data blocks and errors.
00035  * 
00036  * The protocol supports a CRC, but by default it is off (except for the 
00037  * first reset CMD0, where the CRC can just be pre-calculated, and CMD8)
00038  * I'll leave the CRC off I think! 
00039  * 
00040  * Standard capacity cards have variable data block sizes, whereas High 
00041  * Capacity cards fix the size of data block to 512 bytes. I'll therefore
00042  * just always use the Standard Capacity cards with a block size of 512 bytes.
00043  * This is set with CMD16.
00044  *
00045  * You can read and write single blocks (CMD17, CMD25) or multiple blocks 
00046  * (CMD18, CMD25). For simplicity, I'll just use single block accesses. When
00047  * the card gets a read command, it responds with a response token, and then 
00048  * a data token or an error.
00049  * 
00050  * SPI Command Format
00051  * ------------------
00052  * Commands are 6-bytes long, containing the command, 32-bit argument, and CRC.
00053  *
00054  * +---------------+------------+------------+-----------+----------+--------------+
00055  * | 01 | cmd[5:0] | arg[31:24] | arg[23:16] | arg[15:8] | arg[7:0] | crc[6:0] | 1 |
00056  * +---------------+------------+------------+-----------+----------+--------------+
00057  *
00058  * As I'm not using CRC, I can fix that byte to what is needed for CMD0 (0x95)
00059  *
00060  * All Application Specific commands shall be preceded with APP_CMD (CMD55).
00061  *
00062  * SPI Response Format
00063  * -------------------
00064  * The main response format (R1) is a status byte (normally zero). Key flags:
00065  *  idle - 1 if the card is in an idle state/initialising 
00066  *  cmd  - 1 if an illegal command code was detected
00067  *
00068  *    +-------------------------------------------------+
00069  * R1 | 0 | arg | addr | seq | crc | cmd | erase | idle |
00070  *    +-------------------------------------------------+
00071  *
00072  * R1b is the same, except it is followed by a busy signal (zeros) until
00073  * the first non-zero byte when it is ready again.
00074  *
00075  * Data Response Token
00076  * -------------------
00077  * Every data block written to the card is acknowledged by a byte 
00078  * response token
00079  *
00080  * +----------------------+
00081  * | xxx | 0 | status | 1 |
00082  * +----------------------+
00083  *              010 - OK!
00084  *              101 - CRC Error
00085  *              110 - Write Error
00086  *
00087  * Single Block Read and Write
00088  * ---------------------------
00089  *
00090  * Block transfers have a byte header, followed by the data, followed
00091  * by a 16-bit CRC. In our case, the data will always be 512 bytes.
00092  *  
00093  * +------+---------+---------+- -  - -+---------+-----------+----------+
00094  * | 0xFE | data[0] | data[1] |        | data[n] | crc[15:8] | crc[7:0] | 
00095  * +------+---------+---------+- -  - -+---------+-----------+----------+
00096  */
00097  
00098 #include "SDFileSystem.h"
00099 
00100 #define SD_COMMAND_TIMEOUT 5000
00101 
00102 SDFileSystem::SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name) :
00103   FATFileSystem(name), _spi(mosi, miso, sclk), _cs(cs) {
00104       _cs = 1; 
00105 }
00106 
00107 int SDFileSystem::disk_initialize() {
00108 
00109     _spi.frequency(100000); // Set to 100kHz for initialisation
00110     
00111     // Initialise the card by clocking it with cs = 1
00112     _cs = 1;
00113     for(int i=0; i<16; i++) {   
00114         _spi.write(0xFF);
00115     }
00116 
00117     // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
00118     if(_cmd(0, 0) != 0x01) { 
00119         fprintf(stderr, "Not in idle state\n");
00120         return 1;
00121     }
00122     
00123     // ACMD41 to give host capacity support (repeat until not busy)
00124     // ACMD41 is application specific command, so we send APP_CMD (CMD55) beforehand
00125     for(int i=0;; i++) {
00126         _cmd(55, 0); 
00127         int response = _cmd(41, 0);
00128         if(response == 0) { 
00129             break;
00130         } else if(i > SD_COMMAND_TIMEOUT) {
00131             fprintf(stderr, "Timeout waiting for card\n");
00132             return 1;
00133         }    
00134     }
00135 
00136     _sectors = _sd_sectors();
00137 
00138     // Set block length to 512 (CMD16)
00139     if(_cmd(16, 512) != 0) {
00140         fprintf(stderr, "Set block timeout\n");
00141         return 1;
00142     }
00143         
00144     _spi.frequency(1000000); // Set to 1MHz for data transfer
00145     return 0;
00146 }
00147 
00148 int SDFileSystem::disk_write(const char *buffer, int block_number) {
00149     // set write address for single block (CMD24)
00150     if(_cmd(24, block_number * 512) != 0) {
00151         return 1;
00152     }
00153 
00154     // send the data block
00155     _write(buffer, 512);    
00156     return 0;    
00157 }
00158 
00159 int SDFileSystem::disk_read(char *buffer, int block_number) {        
00160     // set read address for single block (CMD17)
00161     if(_cmd(17, block_number * 512) != 0) {
00162         return 1;
00163     }
00164     
00165     // receive the data
00166     _read(buffer, 512);
00167     return 0;
00168 }
00169 
00170 int SDFileSystem::disk_status() { return 0; }
00171 int SDFileSystem::disk_sync() { return 0; }
00172 int SDFileSystem::disk_sectors() { return _sectors; }
00173 
00174 // PRIVATE FUNCTIONS
00175 
00176 int SDFileSystem::_cmd(int cmd, int arg) {
00177     _cs = 0; 
00178 
00179     // send a command
00180     _spi.write(0x40 | cmd);
00181     _spi.write(arg >> 24);
00182     _spi.write(arg >> 16);
00183     _spi.write(arg >> 8);
00184     _spi.write(arg >> 0);
00185     _spi.write(0x95);
00186 
00187     // wait for the repsonse (response[7] == 0)
00188     for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
00189         int response = _spi.write(0xFF);
00190         if(!(response & 0x80)) {
00191             _cs = 1;
00192             _spi.write(0xFF);
00193             return response;
00194         }
00195     }
00196     _cs = 1;
00197     _spi.write(0xFF);
00198     return -1; // timeout
00199 }
00200 
00201 int SDFileSystem::_read(char *buffer, int length) {
00202     _cs = 0;
00203 
00204     // read until start byte (0xFF)
00205     while(_spi.write(0xFF) != 0xFE);
00206 
00207     // read data
00208     for(int i=0; i<length; i++) {
00209         buffer[i] = _spi.write(0xFF);
00210     }
00211     _spi.write(0xFF); // checksum
00212     _spi.write(0xFF);
00213 
00214     _cs = 1;    
00215     _spi.write(0xFF);
00216     return 0;
00217 }
00218 
00219 int SDFileSystem::_write(const char *buffer, int length) {
00220     _cs = 0;
00221     
00222     // indicate start of block
00223     _spi.write(0xFE);
00224     
00225     // write the data
00226     for(int i=0; i<length; i++) {
00227         _spi.write(buffer[i]);
00228     }
00229     
00230     // write the checksum
00231     _spi.write(0xFF); 
00232     _spi.write(0xFF);
00233 
00234     // check the repsonse token
00235     if((_spi.write(0xFF) & 0x1F) != 0x05) {
00236         _cs = 1;
00237         _spi.write(0xFF);        
00238         return 1;
00239     }
00240 
00241     // wait for write to finish
00242     while(_spi.write(0xFF) == 0);
00243 
00244     _cs = 1; 
00245     _spi.write(0xFF);
00246     return 0;
00247 }
00248 
00249 static int ext_bits(char *data, int msb, int lsb) {
00250     int bits = 0;
00251     int size = 1 + msb - lsb; 
00252     for(int i=0; i<size; i++) {
00253         int position = lsb + i;
00254         int byte = 15 - (position >> 3);
00255         int bit = position & 0x7;
00256         int value = (data[byte] >> bit) & 1;
00257         bits |= value << i;
00258     }
00259     return bits;
00260 }
00261 
00262 int SDFileSystem::_sd_sectors() {
00263 
00264     // CMD9, Response R2 (R1 byte + 16-byte block read)
00265     if(_cmd(9, 0) != 0) {
00266         fprintf(stderr, "Didn't get a response from the disk\n");
00267         return 0;
00268     }
00269     
00270     char csd[16];    
00271     if(_read(csd, 16) != 0) {
00272         fprintf(stderr, "Couldn't read csd response from disk\n");
00273         return 0;
00274     }
00275 
00276     // csd_structure : csd[127:126]
00277     // c_size        : csd[73:62]
00278     // c_size_mult   : csd[49:47]
00279     // read_bl_len   : csd[83:80] 
00280 
00281     int csd_structure = ext_bits(csd, 127, 126);
00282     int c_size = ext_bits(csd, 73, 62);
00283     int c_size_mult = ext_bits(csd, 49, 47);
00284     int read_bl_len = ext_bits(csd, 83, 80);
00285     
00286     if(csd_structure != 0) {
00287         fprintf(stderr, "This disk tastes funny! I only know about type 0 CSD structures");
00288         return 0;
00289     }
00290                             
00291     int blocks = (c_size + 1) * (1 << (c_size_mult + 2));
00292     int block_size = 1 << read_bl_len;
00293 
00294     if(block_size != 512) {
00295         fprintf(stderr, "This disk tastes funny! I only like 512-byte blocks");
00296         return 0;
00297     }
00298     
00299     return blocks;
00300 }