USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Fri Nov 11 16:12:21 2011 +0000
Revision:
3:0ffb2eee9e06
good: we can use the sd card as mass storage device

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 3:0ffb2eee9e06 1 /* mbed Microcontroller Library - SDFileSystem
samux 3:0ffb2eee9e06 2 * Copyright (c) 2008-2009, sford
samux 3:0ffb2eee9e06 3 *
samux 3:0ffb2eee9e06 4 * Introduction
samux 3:0ffb2eee9e06 5 * ------------
samux 3:0ffb2eee9e06 6 * SD and MMC cards support a number of interfaces, but common to them all
samux 3:0ffb2eee9e06 7 * is one based on SPI. This is the one I'm implmenting because it means
samux 3:0ffb2eee9e06 8 * it is much more portable even though not so performant, and we already
samux 3:0ffb2eee9e06 9 * have the mbed SPI Interface!
samux 3:0ffb2eee9e06 10 *
samux 3:0ffb2eee9e06 11 * The main reference I'm using is Chapter 7, "SPI Mode" of:
samux 3:0ffb2eee9e06 12 * http://www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
samux 3:0ffb2eee9e06 13 *
samux 3:0ffb2eee9e06 14 * SPI Startup
samux 3:0ffb2eee9e06 15 * -----------
samux 3:0ffb2eee9e06 16 * The SD card powers up in SD mode. The SPI interface mode is selected by
samux 3:0ffb2eee9e06 17 * asserting CS low and sending the reset command (CMD0). The card will
samux 3:0ffb2eee9e06 18 * respond with a (R1) response.
samux 3:0ffb2eee9e06 19 *
samux 3:0ffb2eee9e06 20 * CMD8 is optionally sent to determine the voltage range supported, and
samux 3:0ffb2eee9e06 21 * indirectly determine whether it is a version 1.x SD/non-SD card or
samux 3:0ffb2eee9e06 22 * version 2.x. I'll just ignore this for now.
samux 3:0ffb2eee9e06 23 *
samux 3:0ffb2eee9e06 24 * ACMD41 is repeatedly issued to initialise the card, until "in idle"
samux 3:0ffb2eee9e06 25 * (bit 0) of the R1 response goes to '0', indicating it is initialised.
samux 3:0ffb2eee9e06 26 *
samux 3:0ffb2eee9e06 27 * You should also indicate whether the host supports High Capicity cards,
samux 3:0ffb2eee9e06 28 * and check whether the card is high capacity - i'll also ignore this
samux 3:0ffb2eee9e06 29 *
samux 3:0ffb2eee9e06 30 * SPI Protocol
samux 3:0ffb2eee9e06 31 * ------------
samux 3:0ffb2eee9e06 32 * The SD SPI protocol is based on transactions made up of 8-bit words, with
samux 3:0ffb2eee9e06 33 * the host starting every bus transaction by asserting the CS signal low. The
samux 3:0ffb2eee9e06 34 * card always responds to commands, data blocks and errors.
samux 3:0ffb2eee9e06 35 *
samux 3:0ffb2eee9e06 36 * The protocol supports a CRC, but by default it is off (except for the
samux 3:0ffb2eee9e06 37 * first reset CMD0, where the CRC can just be pre-calculated, and CMD8)
samux 3:0ffb2eee9e06 38 * I'll leave the CRC off I think!
samux 3:0ffb2eee9e06 39 *
samux 3:0ffb2eee9e06 40 * Standard capacity cards have variable data block sizes, whereas High
samux 3:0ffb2eee9e06 41 * Capacity cards fix the size of data block to 512 bytes. I'll therefore
samux 3:0ffb2eee9e06 42 * just always use the Standard Capacity cards with a block size of 512 bytes.
samux 3:0ffb2eee9e06 43 * This is set with CMD16.
samux 3:0ffb2eee9e06 44 *
samux 3:0ffb2eee9e06 45 * You can read and write single blocks (CMD17, CMD25) or multiple blocks
samux 3:0ffb2eee9e06 46 * (CMD18, CMD25). For simplicity, I'll just use single block accesses. When
samux 3:0ffb2eee9e06 47 * the card gets a read command, it responds with a response token, and then
samux 3:0ffb2eee9e06 48 * a data token or an error.
samux 3:0ffb2eee9e06 49 *
samux 3:0ffb2eee9e06 50 * SPI Command Format
samux 3:0ffb2eee9e06 51 * ------------------
samux 3:0ffb2eee9e06 52 * Commands are 6-bytes long, containing the command, 32-bit argument, and CRC.
samux 3:0ffb2eee9e06 53 *
samux 3:0ffb2eee9e06 54 * +---------------+------------+------------+-----------+----------+--------------+
samux 3:0ffb2eee9e06 55 * | 01 | cmd[5:0] | arg[31:24] | arg[23:16] | arg[15:8] | arg[7:0] | crc[6:0] | 1 |
samux 3:0ffb2eee9e06 56 * +---------------+------------+------------+-----------+----------+--------------+
samux 3:0ffb2eee9e06 57 *
samux 3:0ffb2eee9e06 58 * As I'm not using CRC, I can fix that byte to what is needed for CMD0 (0x95)
samux 3:0ffb2eee9e06 59 *
samux 3:0ffb2eee9e06 60 * All Application Specific commands shall be preceded with APP_CMD (CMD55).
samux 3:0ffb2eee9e06 61 *
samux 3:0ffb2eee9e06 62 * SPI Response Format
samux 3:0ffb2eee9e06 63 * -------------------
samux 3:0ffb2eee9e06 64 * The main response format (R1) is a status byte (normally zero). Key flags:
samux 3:0ffb2eee9e06 65 * idle - 1 if the card is in an idle state/initialising
samux 3:0ffb2eee9e06 66 * cmd - 1 if an illegal command code was detected
samux 3:0ffb2eee9e06 67 *
samux 3:0ffb2eee9e06 68 * +-------------------------------------------------+
samux 3:0ffb2eee9e06 69 * R1 | 0 | arg | addr | seq | crc | cmd | erase | idle |
samux 3:0ffb2eee9e06 70 * +-------------------------------------------------+
samux 3:0ffb2eee9e06 71 *
samux 3:0ffb2eee9e06 72 * R1b is the same, except it is followed by a busy signal (zeros) until
samux 3:0ffb2eee9e06 73 * the first non-zero byte when it is ready again.
samux 3:0ffb2eee9e06 74 *
samux 3:0ffb2eee9e06 75 * Data Response Token
samux 3:0ffb2eee9e06 76 * -------------------
samux 3:0ffb2eee9e06 77 * Every data block written to the card is acknowledged by a byte
samux 3:0ffb2eee9e06 78 * response token
samux 3:0ffb2eee9e06 79 *
samux 3:0ffb2eee9e06 80 * +----------------------+
samux 3:0ffb2eee9e06 81 * | xxx | 0 | status | 1 |
samux 3:0ffb2eee9e06 82 * +----------------------+
samux 3:0ffb2eee9e06 83 * 010 - OK!
samux 3:0ffb2eee9e06 84 * 101 - CRC Error
samux 3:0ffb2eee9e06 85 * 110 - Write Error
samux 3:0ffb2eee9e06 86 *
samux 3:0ffb2eee9e06 87 * Single Block Read and Write
samux 3:0ffb2eee9e06 88 * ---------------------------
samux 3:0ffb2eee9e06 89 *
samux 3:0ffb2eee9e06 90 * Block transfers have a byte header, followed by the data, followed
samux 3:0ffb2eee9e06 91 * by a 16-bit CRC. In our case, the data will always be 512 bytes.
samux 3:0ffb2eee9e06 92 *
samux 3:0ffb2eee9e06 93 * +------+---------+---------+- - - -+---------+-----------+----------+
samux 3:0ffb2eee9e06 94 * | 0xFE | data[0] | data[1] | | data[n] | crc[15:8] | crc[7:0] |
samux 3:0ffb2eee9e06 95 * +------+---------+---------+- - - -+---------+-----------+----------+
samux 3:0ffb2eee9e06 96 */
samux 3:0ffb2eee9e06 97
samux 3:0ffb2eee9e06 98 #include "USB_SDcard.h"
samux 3:0ffb2eee9e06 99
samux 3:0ffb2eee9e06 100 #define SD_COMMAND_TIMEOUT 5000
samux 3:0ffb2eee9e06 101
samux 3:0ffb2eee9e06 102 USB_SDcard::USB_SDcard(PinName mosi, PinName miso, PinName sclk, PinName cs) :
samux 3:0ffb2eee9e06 103 _spi(mosi, miso, sclk), _cs(cs) {
samux 3:0ffb2eee9e06 104 _cs = 1;
samux 3:0ffb2eee9e06 105 }
samux 3:0ffb2eee9e06 106
samux 3:0ffb2eee9e06 107 int USB_SDcard::blockWrite(uint8_t * buffer, uint16_t block_number) {
samux 3:0ffb2eee9e06 108 // set write address for single block (CMD24)
samux 3:0ffb2eee9e06 109 if(_cmd(24, block_number * 512) != 0) {
samux 3:0ffb2eee9e06 110 return 1;
samux 3:0ffb2eee9e06 111 }
samux 3:0ffb2eee9e06 112
samux 3:0ffb2eee9e06 113 // send the data block
samux 3:0ffb2eee9e06 114 _write(buffer, 512);
samux 3:0ffb2eee9e06 115 return 0;
samux 3:0ffb2eee9e06 116 }
samux 3:0ffb2eee9e06 117
samux 3:0ffb2eee9e06 118 int USB_SDcard::blockRead(uint8_t * buffer, uint16_t block_number) {
samux 3:0ffb2eee9e06 119 // set read address for single block (CMD17)
samux 3:0ffb2eee9e06 120 if(_cmd(17, block_number * 512) != 0) {
samux 3:0ffb2eee9e06 121 return 1;
samux 3:0ffb2eee9e06 122 }
samux 3:0ffb2eee9e06 123
samux 3:0ffb2eee9e06 124 // receive the data
samux 3:0ffb2eee9e06 125 _read(buffer, 512);
samux 3:0ffb2eee9e06 126 return 0;
samux 3:0ffb2eee9e06 127 }
samux 3:0ffb2eee9e06 128
samux 3:0ffb2eee9e06 129 // PRIVATE FUNCTIONS
samux 3:0ffb2eee9e06 130
samux 3:0ffb2eee9e06 131 int USB_SDcard::_cmd(int cmd, int arg) {
samux 3:0ffb2eee9e06 132 _cs = 0;
samux 3:0ffb2eee9e06 133
samux 3:0ffb2eee9e06 134 // send a command
samux 3:0ffb2eee9e06 135 _spi.write(0x40 | cmd);
samux 3:0ffb2eee9e06 136 _spi.write(arg >> 24);
samux 3:0ffb2eee9e06 137 _spi.write(arg >> 16);
samux 3:0ffb2eee9e06 138 _spi.write(arg >> 8);
samux 3:0ffb2eee9e06 139 _spi.write(arg >> 0);
samux 3:0ffb2eee9e06 140 _spi.write(0x95);
samux 3:0ffb2eee9e06 141
samux 3:0ffb2eee9e06 142 // wait for the repsonse (response[7] == 0)
samux 3:0ffb2eee9e06 143 for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
samux 3:0ffb2eee9e06 144 int response = _spi.write(0xFF);
samux 3:0ffb2eee9e06 145 if(!(response & 0x80)) {
samux 3:0ffb2eee9e06 146 _cs = 1;
samux 3:0ffb2eee9e06 147 return response;
samux 3:0ffb2eee9e06 148 }
samux 3:0ffb2eee9e06 149 }
samux 3:0ffb2eee9e06 150 _cs = 1;
samux 3:0ffb2eee9e06 151 return -1; // timeout
samux 3:0ffb2eee9e06 152 }
samux 3:0ffb2eee9e06 153
samux 3:0ffb2eee9e06 154 int USB_SDcard::_read(uint8_t * buffer, uint16_t length) {
samux 3:0ffb2eee9e06 155 _cs = 0;
samux 3:0ffb2eee9e06 156
samux 3:0ffb2eee9e06 157 // read until start byte (0xFF)
samux 3:0ffb2eee9e06 158 while(_spi.write(0xFF) != 0xFE);
samux 3:0ffb2eee9e06 159
samux 3:0ffb2eee9e06 160 // read data
samux 3:0ffb2eee9e06 161 for(int i=0; i<length; i++) {
samux 3:0ffb2eee9e06 162 buffer[i] = _spi.write(0xFF);
samux 3:0ffb2eee9e06 163 }
samux 3:0ffb2eee9e06 164 _spi.write(0xFF); // checksum
samux 3:0ffb2eee9e06 165 _spi.write(0xFF);
samux 3:0ffb2eee9e06 166
samux 3:0ffb2eee9e06 167 _cs = 1;
samux 3:0ffb2eee9e06 168 return 0;
samux 3:0ffb2eee9e06 169 }
samux 3:0ffb2eee9e06 170
samux 3:0ffb2eee9e06 171 int USB_SDcard::_write(uint8_t * buffer, uint16_t length) {
samux 3:0ffb2eee9e06 172 _cs = 0;
samux 3:0ffb2eee9e06 173
samux 3:0ffb2eee9e06 174 // indicate start of block
samux 3:0ffb2eee9e06 175 _spi.write(0xFE);
samux 3:0ffb2eee9e06 176
samux 3:0ffb2eee9e06 177 // write the data
samux 3:0ffb2eee9e06 178 for(int i=0; i<length; i++) {
samux 3:0ffb2eee9e06 179 _spi.write(buffer[i]);
samux 3:0ffb2eee9e06 180 }
samux 3:0ffb2eee9e06 181
samux 3:0ffb2eee9e06 182 // write the checksum
samux 3:0ffb2eee9e06 183 _spi.write(0xFF);
samux 3:0ffb2eee9e06 184 _spi.write(0xFF);
samux 3:0ffb2eee9e06 185
samux 3:0ffb2eee9e06 186 // check the repsonse token
samux 3:0ffb2eee9e06 187 if((_spi.write(0xFF) & 0x1F) != 0x05) {
samux 3:0ffb2eee9e06 188 _cs = 1;
samux 3:0ffb2eee9e06 189 return 1;
samux 3:0ffb2eee9e06 190 }
samux 3:0ffb2eee9e06 191
samux 3:0ffb2eee9e06 192 // wait for write to finish
samux 3:0ffb2eee9e06 193 while(_spi.write(0xFF) == 0);
samux 3:0ffb2eee9e06 194
samux 3:0ffb2eee9e06 195 _cs = 1;
samux 3:0ffb2eee9e06 196 return 0;
samux 3:0ffb2eee9e06 197 }