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