USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Sat Nov 12 17:53:04 2011 +0000
Revision:
4:980e6470dcce
Child:
6:126c4d980196
will try to find a solution to init before connecting devices

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 4:980e6470dcce 1 /* mbed USB_SDcard Library, for accessing SD cards
samux 4:980e6470dcce 2 * Copyright (c) 2008-2010, sford
samux 4:980e6470dcce 3 *
samux 4:980e6470dcce 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
samux 4:980e6470dcce 5 * of this software and associated documentation files (the "Software"), to deal
samux 4:980e6470dcce 6 * in the Software without restriction, including without limitation the rights
samux 4:980e6470dcce 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
samux 4:980e6470dcce 8 * copies of the Software, and to permit persons to whom the Software is
samux 4:980e6470dcce 9 * furnished to do so, subject to the following conditions:
samux 4:980e6470dcce 10 *
samux 4:980e6470dcce 11 * The above copyright notice and this permission notice shall be included in
samux 4:980e6470dcce 12 * all copies or substantial portions of the Software.
samux 4:980e6470dcce 13 *
samux 4:980e6470dcce 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
samux 4:980e6470dcce 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
samux 4:980e6470dcce 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
samux 4:980e6470dcce 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
samux 4:980e6470dcce 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 4:980e6470dcce 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
samux 4:980e6470dcce 20 * THE SOFTWARE.
samux 4:980e6470dcce 21 */
samux 4:980e6470dcce 22
samux 4:980e6470dcce 23 #include "USB_SDcard.h"
samux 4:980e6470dcce 24
samux 4:980e6470dcce 25 #define SD_COMMAND_TIMEOUT 5000
samux 4:980e6470dcce 26
samux 4:980e6470dcce 27 USB_SDcard::USB_SDcard(PinName mosi, PinName miso, PinName sclk, PinName cs):
samux 4:980e6470dcce 28 _spi(mosi, miso, sclk), _cs(cs) {
samux 4:980e6470dcce 29 _cs = 1;
samux 4:980e6470dcce 30 init();
samux 4:980e6470dcce 31 }
samux 4:980e6470dcce 32
samux 4:980e6470dcce 33 #define R1_IDLE_STATE (1 << 0)
samux 4:980e6470dcce 34 #define R1_ERASE_RESET (1 << 1)
samux 4:980e6470dcce 35 #define R1_ILLEGAL_COMMAND (1 << 2)
samux 4:980e6470dcce 36 #define R1_COM_CRC_ERROR (1 << 3)
samux 4:980e6470dcce 37 #define R1_ERASE_SEQUENCE_ERROR (1 << 4)
samux 4:980e6470dcce 38 #define R1_ADDRESS_ERROR (1 << 5)
samux 4:980e6470dcce 39 #define R1_PARAMETER_ERROR (1 << 6)
samux 4:980e6470dcce 40
samux 4:980e6470dcce 41 // Types
samux 4:980e6470dcce 42 // - v1.x Standard Capacity
samux 4:980e6470dcce 43 // - v2.x Standard Capacity
samux 4:980e6470dcce 44 // - v2.x High Capacity
samux 4:980e6470dcce 45 // - Not recognised as an SD Card
samux 4:980e6470dcce 46
samux 4:980e6470dcce 47 #define SDCARD_FAIL 0
samux 4:980e6470dcce 48 #define SDCARD_V1 1
samux 4:980e6470dcce 49 #define SDCARD_V2 2
samux 4:980e6470dcce 50 #define SDCARD_V2HC 3
samux 4:980e6470dcce 51
samux 4:980e6470dcce 52 int USB_SDcard::initialise_card() {
samux 4:980e6470dcce 53 // Set to 100kHz for initialisation, and clock card with cs = 1
samux 4:980e6470dcce 54 _spi.frequency(100000);
samux 4:980e6470dcce 55 _cs = 1;
samux 4:980e6470dcce 56 for(int i=0; i<16; i++) {
samux 4:980e6470dcce 57 _spi.write(0xFF);
samux 4:980e6470dcce 58 }
samux 4:980e6470dcce 59
samux 4:980e6470dcce 60 // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
samux 4:980e6470dcce 61 if(_cmd(0, 0) != R1_IDLE_STATE) {
samux 4:980e6470dcce 62 fprintf(stderr, "No disk, or could not put SD card in to SPI idle state\n");
samux 4:980e6470dcce 63 return SDCARD_FAIL;
samux 4:980e6470dcce 64 }
samux 4:980e6470dcce 65
samux 4:980e6470dcce 66 // send CMD8 to determine whther it is ver 2.x
samux 4:980e6470dcce 67 int r = _cmd8();
samux 4:980e6470dcce 68 if(r == R1_IDLE_STATE) {
samux 4:980e6470dcce 69 return initialise_card_v2();
samux 4:980e6470dcce 70 } else if(r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
samux 4:980e6470dcce 71 return initialise_card_v1();
samux 4:980e6470dcce 72 } else {
samux 4:980e6470dcce 73 fprintf(stderr, "Not in idle state after sending CMD8 (not an SD card?)\n");
samux 4:980e6470dcce 74 return SDCARD_FAIL;
samux 4:980e6470dcce 75 }
samux 4:980e6470dcce 76 }
samux 4:980e6470dcce 77
samux 4:980e6470dcce 78 int USB_SDcard::initialise_card_v1() {
samux 4:980e6470dcce 79 for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
samux 4:980e6470dcce 80 _cmd(55, 0);
samux 4:980e6470dcce 81 if(_cmd(41, 0) == 0) {
samux 4:980e6470dcce 82 return SDCARD_V1;
samux 4:980e6470dcce 83 }
samux 4:980e6470dcce 84 }
samux 4:980e6470dcce 85
samux 4:980e6470dcce 86 fprintf(stderr, "Timeout waiting for v1.x card\n");
samux 4:980e6470dcce 87 return SDCARD_FAIL;
samux 4:980e6470dcce 88 }
samux 4:980e6470dcce 89
samux 4:980e6470dcce 90 int USB_SDcard::initialise_card_v2() {
samux 4:980e6470dcce 91
samux 4:980e6470dcce 92 for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
samux 4:980e6470dcce 93 _cmd(55, 0);
samux 4:980e6470dcce 94 if(_cmd(41, 0) == 0) {
samux 4:980e6470dcce 95 _cmd58();
samux 4:980e6470dcce 96 return SDCARD_V2;
samux 4:980e6470dcce 97 }
samux 4:980e6470dcce 98 }
samux 4:980e6470dcce 99
samux 4:980e6470dcce 100 fprintf(stderr, "Timeout waiting for v2.x card\n");
samux 4:980e6470dcce 101 return SDCARD_FAIL;
samux 4:980e6470dcce 102 }
samux 4:980e6470dcce 103
samux 4:980e6470dcce 104 int USB_SDcard::init() {
samux 4:980e6470dcce 105
samux 4:980e6470dcce 106 int i = initialise_card();
samux 4:980e6470dcce 107 // printf("init card = %d\n", i);
samux 4:980e6470dcce 108 // printf("OK\n");
samux 4:980e6470dcce 109
samux 4:980e6470dcce 110 _sectors = _sd_sectors();
samux 4:980e6470dcce 111
samux 4:980e6470dcce 112 // Set block length to 512 (CMD16)
samux 4:980e6470dcce 113 if(_cmd(16, 512) != 0) {
samux 4:980e6470dcce 114 fprintf(stderr, "Set 512-byte block timed out\n");
samux 4:980e6470dcce 115 return 1;
samux 4:980e6470dcce 116 }
samux 4:980e6470dcce 117
samux 4:980e6470dcce 118 _spi.frequency(1000000); // Set to 1MHz for data transfer
samux 4:980e6470dcce 119 return 0;
samux 4:980e6470dcce 120 }
samux 4:980e6470dcce 121
samux 4:980e6470dcce 122 int USB_SDcard::blockWrite(uint8_t * buffer, uint16_t block_number) {
samux 4:980e6470dcce 123 // set write address for single block (CMD24)
samux 4:980e6470dcce 124 if(_cmd(24, block_number * 512) != 0) {
samux 4:980e6470dcce 125 return 1;
samux 4:980e6470dcce 126 }
samux 4:980e6470dcce 127
samux 4:980e6470dcce 128 // send the data block
samux 4:980e6470dcce 129 _write((const char *)buffer, 512);
samux 4:980e6470dcce 130 return 0;
samux 4:980e6470dcce 131 }
samux 4:980e6470dcce 132
samux 4:980e6470dcce 133 int USB_SDcard::blockRead(uint8_t * buffer, uint16_t block_number) {
samux 4:980e6470dcce 134 // set read address for single block (CMD17)
samux 4:980e6470dcce 135 if(_cmd(17, block_number * 512) != 0) {
samux 4:980e6470dcce 136 return 1;
samux 4:980e6470dcce 137 }
samux 4:980e6470dcce 138
samux 4:980e6470dcce 139 // receive the data
samux 4:980e6470dcce 140 _read((char *)buffer, 512);
samux 4:980e6470dcce 141 return 0;
samux 4:980e6470dcce 142 }
samux 4:980e6470dcce 143
samux 4:980e6470dcce 144 int USB_SDcard::disk_status() { return 0; }
samux 4:980e6470dcce 145 int USB_SDcard::disk_sync() { return 0; }
samux 4:980e6470dcce 146 int USB_SDcard::disk_sectors() { return _sectors; }
samux 4:980e6470dcce 147
samux 4:980e6470dcce 148 // PRIVATE FUNCTIONS
samux 4:980e6470dcce 149
samux 4:980e6470dcce 150 int USB_SDcard::_cmd(int cmd, int arg) {
samux 4:980e6470dcce 151 _cs = 0;
samux 4:980e6470dcce 152
samux 4:980e6470dcce 153 // send a command
samux 4:980e6470dcce 154 _spi.write(0x40 | cmd);
samux 4:980e6470dcce 155 _spi.write(arg >> 24);
samux 4:980e6470dcce 156 _spi.write(arg >> 16);
samux 4:980e6470dcce 157 _spi.write(arg >> 8);
samux 4:980e6470dcce 158 _spi.write(arg >> 0);
samux 4:980e6470dcce 159 _spi.write(0x95);
samux 4:980e6470dcce 160
samux 4:980e6470dcce 161 // wait for the repsonse (response[7] == 0)
samux 4:980e6470dcce 162 for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
samux 4:980e6470dcce 163 int response = _spi.write(0xFF);
samux 4:980e6470dcce 164 if(!(response & 0x80)) {
samux 4:980e6470dcce 165 _cs = 1;
samux 4:980e6470dcce 166 _spi.write(0xFF);
samux 4:980e6470dcce 167 return response;
samux 4:980e6470dcce 168 }
samux 4:980e6470dcce 169 }
samux 4:980e6470dcce 170 _cs = 1;
samux 4:980e6470dcce 171 _spi.write(0xFF);
samux 4:980e6470dcce 172 return -1; // timeout
samux 4:980e6470dcce 173 }
samux 4:980e6470dcce 174 int USB_SDcard::_cmdx(int cmd, int arg) {
samux 4:980e6470dcce 175 _cs = 0;
samux 4:980e6470dcce 176
samux 4:980e6470dcce 177 // send a command
samux 4:980e6470dcce 178 _spi.write(0x40 | cmd);
samux 4:980e6470dcce 179 _spi.write(arg >> 24);
samux 4:980e6470dcce 180 _spi.write(arg >> 16);
samux 4:980e6470dcce 181 _spi.write(arg >> 8);
samux 4:980e6470dcce 182 _spi.write(arg >> 0);
samux 4:980e6470dcce 183 _spi.write(0x95);
samux 4:980e6470dcce 184
samux 4:980e6470dcce 185 // wait for the repsonse (response[7] == 0)
samux 4:980e6470dcce 186 for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
samux 4:980e6470dcce 187 int response = _spi.write(0xFF);
samux 4:980e6470dcce 188 if(!(response & 0x80)) {
samux 4:980e6470dcce 189 return response;
samux 4:980e6470dcce 190 }
samux 4:980e6470dcce 191 }
samux 4:980e6470dcce 192 _cs = 1;
samux 4:980e6470dcce 193 _spi.write(0xFF);
samux 4:980e6470dcce 194 return -1; // timeout
samux 4:980e6470dcce 195 }
samux 4:980e6470dcce 196
samux 4:980e6470dcce 197
samux 4:980e6470dcce 198 int USB_SDcard::_cmd58() {
samux 4:980e6470dcce 199 _cs = 0;
samux 4:980e6470dcce 200 int arg = 0;
samux 4:980e6470dcce 201
samux 4:980e6470dcce 202 // send a command
samux 4:980e6470dcce 203 _spi.write(0x40 | 58);
samux 4:980e6470dcce 204 _spi.write(arg >> 24);
samux 4:980e6470dcce 205 _spi.write(arg >> 16);
samux 4:980e6470dcce 206 _spi.write(arg >> 8);
samux 4:980e6470dcce 207 _spi.write(arg >> 0);
samux 4:980e6470dcce 208 _spi.write(0x95);
samux 4:980e6470dcce 209
samux 4:980e6470dcce 210 // wait for the repsonse (response[7] == 0)
samux 4:980e6470dcce 211 for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
samux 4:980e6470dcce 212 int response = _spi.write(0xFF);
samux 4:980e6470dcce 213 if(!(response & 0x80)) {
samux 4:980e6470dcce 214 int ocr = _spi.write(0xFF) << 24;
samux 4:980e6470dcce 215 ocr |= _spi.write(0xFF) << 16;
samux 4:980e6470dcce 216 ocr |= _spi.write(0xFF) << 8;
samux 4:980e6470dcce 217 ocr |= _spi.write(0xFF) << 0;
samux 4:980e6470dcce 218 // printf("OCR = 0x%08X\n", ocr);
samux 4:980e6470dcce 219 _cs = 1;
samux 4:980e6470dcce 220 _spi.write(0xFF);
samux 4:980e6470dcce 221 return response;
samux 4:980e6470dcce 222 }
samux 4:980e6470dcce 223 }
samux 4:980e6470dcce 224 _cs = 1;
samux 4:980e6470dcce 225 _spi.write(0xFF);
samux 4:980e6470dcce 226 return -1; // timeout
samux 4:980e6470dcce 227 }
samux 4:980e6470dcce 228
samux 4:980e6470dcce 229 int USB_SDcard::_cmd8() {
samux 4:980e6470dcce 230 _cs = 0;
samux 4:980e6470dcce 231
samux 4:980e6470dcce 232 // send a command
samux 4:980e6470dcce 233 _spi.write(0x40 | 8); // CMD8
samux 4:980e6470dcce 234 _spi.write(0x00); // reserved
samux 4:980e6470dcce 235 _spi.write(0x00); // reserved
samux 4:980e6470dcce 236 _spi.write(0x01); // 3.3v
samux 4:980e6470dcce 237 _spi.write(0xAA); // check pattern
samux 4:980e6470dcce 238 _spi.write(0x87); // crc
samux 4:980e6470dcce 239
samux 4:980e6470dcce 240 // wait for the repsonse (response[7] == 0)
samux 4:980e6470dcce 241 for(int i=0; i<SD_COMMAND_TIMEOUT * 1000; i++) {
samux 4:980e6470dcce 242 char response[5];
samux 4:980e6470dcce 243 response[0] = _spi.write(0xFF);
samux 4:980e6470dcce 244 if(!(response[0] & 0x80)) {
samux 4:980e6470dcce 245 for(int j=1; j<5; j++) {
samux 4:980e6470dcce 246 response[i] = _spi.write(0xFF);
samux 4:980e6470dcce 247 }
samux 4:980e6470dcce 248 _cs = 1;
samux 4:980e6470dcce 249 _spi.write(0xFF);
samux 4:980e6470dcce 250 return response[0];
samux 4:980e6470dcce 251 }
samux 4:980e6470dcce 252 }
samux 4:980e6470dcce 253 _cs = 1;
samux 4:980e6470dcce 254 _spi.write(0xFF);
samux 4:980e6470dcce 255 return -1; // timeout
samux 4:980e6470dcce 256 }
samux 4:980e6470dcce 257
samux 4:980e6470dcce 258 int USB_SDcard::_read(char *buffer, int length) {
samux 4:980e6470dcce 259 _cs = 0;
samux 4:980e6470dcce 260
samux 4:980e6470dcce 261 // read until start byte (0xFF)
samux 4:980e6470dcce 262 while(_spi.write(0xFF) != 0xFE);
samux 4:980e6470dcce 263
samux 4:980e6470dcce 264 // read data
samux 4:980e6470dcce 265 for(int i=0; i<length; i++) {
samux 4:980e6470dcce 266 buffer[i] = _spi.write(0xFF);
samux 4:980e6470dcce 267 }
samux 4:980e6470dcce 268 _spi.write(0xFF); // checksum
samux 4:980e6470dcce 269 _spi.write(0xFF);
samux 4:980e6470dcce 270
samux 4:980e6470dcce 271 _cs = 1;
samux 4:980e6470dcce 272 _spi.write(0xFF);
samux 4:980e6470dcce 273 return 0;
samux 4:980e6470dcce 274 }
samux 4:980e6470dcce 275
samux 4:980e6470dcce 276 int USB_SDcard::_write(const char *buffer, int length) {
samux 4:980e6470dcce 277 _cs = 0;
samux 4:980e6470dcce 278
samux 4:980e6470dcce 279 // indicate start of block
samux 4:980e6470dcce 280 _spi.write(0xFE);
samux 4:980e6470dcce 281
samux 4:980e6470dcce 282 // write the data
samux 4:980e6470dcce 283 for(int i=0; i<length; i++) {
samux 4:980e6470dcce 284 _spi.write(buffer[i]);
samux 4:980e6470dcce 285 }
samux 4:980e6470dcce 286
samux 4:980e6470dcce 287 // write the checksum
samux 4:980e6470dcce 288 _spi.write(0xFF);
samux 4:980e6470dcce 289 _spi.write(0xFF);
samux 4:980e6470dcce 290
samux 4:980e6470dcce 291 // check the repsonse token
samux 4:980e6470dcce 292 if((_spi.write(0xFF) & 0x1F) != 0x05) {
samux 4:980e6470dcce 293 _cs = 1;
samux 4:980e6470dcce 294 _spi.write(0xFF);
samux 4:980e6470dcce 295 return 1;
samux 4:980e6470dcce 296 }
samux 4:980e6470dcce 297
samux 4:980e6470dcce 298 // wait for write to finish
samux 4:980e6470dcce 299 while(_spi.write(0xFF) == 0);
samux 4:980e6470dcce 300
samux 4:980e6470dcce 301 _cs = 1;
samux 4:980e6470dcce 302 _spi.write(0xFF);
samux 4:980e6470dcce 303 return 0;
samux 4:980e6470dcce 304 }
samux 4:980e6470dcce 305
samux 4:980e6470dcce 306 static int ext_bits(char *data, int msb, int lsb) {
samux 4:980e6470dcce 307 int bits = 0;
samux 4:980e6470dcce 308 int size = 1 + msb - lsb;
samux 4:980e6470dcce 309 for(int i=0; i<size; i++) {
samux 4:980e6470dcce 310 int position = lsb + i;
samux 4:980e6470dcce 311 int byte = 15 - (position >> 3);
samux 4:980e6470dcce 312 int bit = position & 0x7;
samux 4:980e6470dcce 313 int value = (data[byte] >> bit) & 1;
samux 4:980e6470dcce 314 bits |= value << i;
samux 4:980e6470dcce 315 }
samux 4:980e6470dcce 316 return bits;
samux 4:980e6470dcce 317 }
samux 4:980e6470dcce 318
samux 4:980e6470dcce 319 int USB_SDcard::_sd_sectors() {
samux 4:980e6470dcce 320
samux 4:980e6470dcce 321 // CMD9, Response R2 (R1 byte + 16-byte block read)
samux 4:980e6470dcce 322 if(_cmdx(9, 0) != 0) {
samux 4:980e6470dcce 323 fprintf(stderr, "Didn't get a response from the disk\n");
samux 4:980e6470dcce 324 return 0;
samux 4:980e6470dcce 325 }
samux 4:980e6470dcce 326
samux 4:980e6470dcce 327 char csd[16];
samux 4:980e6470dcce 328 if(_read(csd, 16) != 0) {
samux 4:980e6470dcce 329 fprintf(stderr, "Couldn't read csd response from disk\n");
samux 4:980e6470dcce 330 return 0;
samux 4:980e6470dcce 331 }
samux 4:980e6470dcce 332
samux 4:980e6470dcce 333 // csd_structure : csd[127:126]
samux 4:980e6470dcce 334 // c_size : csd[73:62]
samux 4:980e6470dcce 335 // c_size_mult : csd[49:47]
samux 4:980e6470dcce 336 // read_bl_len : csd[83:80] - the *maximum* read block length
samux 4:980e6470dcce 337
samux 4:980e6470dcce 338 int csd_structure = ext_bits(csd, 127, 126);
samux 4:980e6470dcce 339 int c_size = ext_bits(csd, 73, 62);
samux 4:980e6470dcce 340 int c_size_mult = ext_bits(csd, 49, 47);
samux 4:980e6470dcce 341 int read_bl_len = ext_bits(csd, 83, 80);
samux 4:980e6470dcce 342
samux 4:980e6470dcce 343 // printf("CSD_STRUCT = %d\n", csd_structure);
samux 4:980e6470dcce 344
samux 4:980e6470dcce 345 if(csd_structure != 0) {
samux 4:980e6470dcce 346 fprintf(stderr, "This disk tastes funny! I only know about type 0 CSD structures\n");
samux 4:980e6470dcce 347 return 0;
samux 4:980e6470dcce 348 }
samux 4:980e6470dcce 349
samux 4:980e6470dcce 350 // memory capacity = BLOCKNR * BLOCK_LEN
samux 4:980e6470dcce 351 // where
samux 4:980e6470dcce 352 // BLOCKNR = (C_SIZE+1) * MULT
samux 4:980e6470dcce 353 // MULT = 2^(C_SIZE_MULT+2) (C_SIZE_MULT < 8)
samux 4:980e6470dcce 354 // BLOCK_LEN = 2^READ_BL_LEN, (READ_BL_LEN < 12)
samux 4:980e6470dcce 355
samux 4:980e6470dcce 356 int block_len = 1 << read_bl_len;
samux 4:980e6470dcce 357 int mult = 1 << (c_size_mult + 2);
samux 4:980e6470dcce 358 int blocknr = (c_size + 1) * mult;
samux 4:980e6470dcce 359 int capacity = blocknr * block_len;
samux 4:980e6470dcce 360
samux 4:980e6470dcce 361 int blocks = capacity / 512;
samux 4:980e6470dcce 362
samux 4:980e6470dcce 363 return blocks;
samux 4:980e6470dcce 364 }