USBMSD SD card Hello World for Mbed platforms

Dependencies:   mbed USBMSD_SD USBDevice

Committer:
samux
Date:
Mon Nov 14 10:00:07 2011 +0000
Revision:
6:126c4d980196
Parent:
4:980e6470dcce
now we have a connect inside USBMSD to get all capacities from storage chip

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 6:126c4d980196 30 USBMSD::connect();
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 6:126c4d980196 104 uint32_t USB_SDcard::memorySize()
samux 6:126c4d980196 105 {
samux 6:126c4d980196 106 return (uint32_t)capacity;
samux 6:126c4d980196 107 }
samux 6:126c4d980196 108
samux 6:126c4d980196 109 uint16_t USB_SDcard::blockSize()
samux 6:126c4d980196 110 {
samux 6:126c4d980196 111 return (uint16_t)block_len;
samux 6:126c4d980196 112 }
samux 6:126c4d980196 113
samux 6:126c4d980196 114 int USB_SDcard::diskInit() {
samux 4:980e6470dcce 115
samux 4:980e6470dcce 116 int i = initialise_card();
samux 4:980e6470dcce 117 // printf("init card = %d\n", i);
samux 4:980e6470dcce 118 // printf("OK\n");
samux 4:980e6470dcce 119
samux 4:980e6470dcce 120 _sectors = _sd_sectors();
samux 4:980e6470dcce 121
samux 4:980e6470dcce 122 // Set block length to 512 (CMD16)
samux 4:980e6470dcce 123 if(_cmd(16, 512) != 0) {
samux 4:980e6470dcce 124 fprintf(stderr, "Set 512-byte block timed out\n");
samux 4:980e6470dcce 125 return 1;
samux 4:980e6470dcce 126 }
samux 4:980e6470dcce 127
samux 4:980e6470dcce 128 _spi.frequency(1000000); // Set to 1MHz for data transfer
samux 4:980e6470dcce 129 return 0;
samux 4:980e6470dcce 130 }
samux 4:980e6470dcce 131
samux 4:980e6470dcce 132 int USB_SDcard::blockWrite(uint8_t * buffer, uint16_t block_number) {
samux 4:980e6470dcce 133 // set write address for single block (CMD24)
samux 4:980e6470dcce 134 if(_cmd(24, block_number * 512) != 0) {
samux 4:980e6470dcce 135 return 1;
samux 4:980e6470dcce 136 }
samux 4:980e6470dcce 137
samux 4:980e6470dcce 138 // send the data block
samux 4:980e6470dcce 139 _write((const char *)buffer, 512);
samux 4:980e6470dcce 140 return 0;
samux 4:980e6470dcce 141 }
samux 4:980e6470dcce 142
samux 4:980e6470dcce 143 int USB_SDcard::blockRead(uint8_t * buffer, uint16_t block_number) {
samux 4:980e6470dcce 144 // set read address for single block (CMD17)
samux 4:980e6470dcce 145 if(_cmd(17, block_number * 512) != 0) {
samux 4:980e6470dcce 146 return 1;
samux 4:980e6470dcce 147 }
samux 4:980e6470dcce 148
samux 4:980e6470dcce 149 // receive the data
samux 4:980e6470dcce 150 _read((char *)buffer, 512);
samux 4:980e6470dcce 151 return 0;
samux 4:980e6470dcce 152 }
samux 4:980e6470dcce 153
samux 4:980e6470dcce 154 int USB_SDcard::disk_status() { return 0; }
samux 4:980e6470dcce 155 int USB_SDcard::disk_sync() { return 0; }
samux 4:980e6470dcce 156 int USB_SDcard::disk_sectors() { return _sectors; }
samux 4:980e6470dcce 157
samux 4:980e6470dcce 158 // PRIVATE FUNCTIONS
samux 4:980e6470dcce 159
samux 4:980e6470dcce 160 int USB_SDcard::_cmd(int cmd, int arg) {
samux 4:980e6470dcce 161 _cs = 0;
samux 4:980e6470dcce 162
samux 4:980e6470dcce 163 // send a command
samux 4:980e6470dcce 164 _spi.write(0x40 | cmd);
samux 4:980e6470dcce 165 _spi.write(arg >> 24);
samux 4:980e6470dcce 166 _spi.write(arg >> 16);
samux 4:980e6470dcce 167 _spi.write(arg >> 8);
samux 4:980e6470dcce 168 _spi.write(arg >> 0);
samux 4:980e6470dcce 169 _spi.write(0x95);
samux 4:980e6470dcce 170
samux 4:980e6470dcce 171 // wait for the repsonse (response[7] == 0)
samux 4:980e6470dcce 172 for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
samux 4:980e6470dcce 173 int response = _spi.write(0xFF);
samux 4:980e6470dcce 174 if(!(response & 0x80)) {
samux 4:980e6470dcce 175 _cs = 1;
samux 4:980e6470dcce 176 _spi.write(0xFF);
samux 4:980e6470dcce 177 return response;
samux 4:980e6470dcce 178 }
samux 4:980e6470dcce 179 }
samux 4:980e6470dcce 180 _cs = 1;
samux 4:980e6470dcce 181 _spi.write(0xFF);
samux 4:980e6470dcce 182 return -1; // timeout
samux 4:980e6470dcce 183 }
samux 4:980e6470dcce 184 int USB_SDcard::_cmdx(int cmd, int arg) {
samux 4:980e6470dcce 185 _cs = 0;
samux 4:980e6470dcce 186
samux 4:980e6470dcce 187 // send a command
samux 4:980e6470dcce 188 _spi.write(0x40 | cmd);
samux 4:980e6470dcce 189 _spi.write(arg >> 24);
samux 4:980e6470dcce 190 _spi.write(arg >> 16);
samux 4:980e6470dcce 191 _spi.write(arg >> 8);
samux 4:980e6470dcce 192 _spi.write(arg >> 0);
samux 4:980e6470dcce 193 _spi.write(0x95);
samux 4:980e6470dcce 194
samux 4:980e6470dcce 195 // wait for the repsonse (response[7] == 0)
samux 4:980e6470dcce 196 for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
samux 4:980e6470dcce 197 int response = _spi.write(0xFF);
samux 4:980e6470dcce 198 if(!(response & 0x80)) {
samux 4:980e6470dcce 199 return response;
samux 4:980e6470dcce 200 }
samux 4:980e6470dcce 201 }
samux 4:980e6470dcce 202 _cs = 1;
samux 4:980e6470dcce 203 _spi.write(0xFF);
samux 4:980e6470dcce 204 return -1; // timeout
samux 4:980e6470dcce 205 }
samux 4:980e6470dcce 206
samux 4:980e6470dcce 207
samux 4:980e6470dcce 208 int USB_SDcard::_cmd58() {
samux 4:980e6470dcce 209 _cs = 0;
samux 4:980e6470dcce 210 int arg = 0;
samux 4:980e6470dcce 211
samux 4:980e6470dcce 212 // send a command
samux 4:980e6470dcce 213 _spi.write(0x40 | 58);
samux 4:980e6470dcce 214 _spi.write(arg >> 24);
samux 4:980e6470dcce 215 _spi.write(arg >> 16);
samux 4:980e6470dcce 216 _spi.write(arg >> 8);
samux 4:980e6470dcce 217 _spi.write(arg >> 0);
samux 4:980e6470dcce 218 _spi.write(0x95);
samux 4:980e6470dcce 219
samux 4:980e6470dcce 220 // wait for the repsonse (response[7] == 0)
samux 4:980e6470dcce 221 for(int i=0; i<SD_COMMAND_TIMEOUT; i++) {
samux 4:980e6470dcce 222 int response = _spi.write(0xFF);
samux 4:980e6470dcce 223 if(!(response & 0x80)) {
samux 4:980e6470dcce 224 int ocr = _spi.write(0xFF) << 24;
samux 4:980e6470dcce 225 ocr |= _spi.write(0xFF) << 16;
samux 4:980e6470dcce 226 ocr |= _spi.write(0xFF) << 8;
samux 4:980e6470dcce 227 ocr |= _spi.write(0xFF) << 0;
samux 4:980e6470dcce 228 // printf("OCR = 0x%08X\n", ocr);
samux 4:980e6470dcce 229 _cs = 1;
samux 4:980e6470dcce 230 _spi.write(0xFF);
samux 4:980e6470dcce 231 return response;
samux 4:980e6470dcce 232 }
samux 4:980e6470dcce 233 }
samux 4:980e6470dcce 234 _cs = 1;
samux 4:980e6470dcce 235 _spi.write(0xFF);
samux 4:980e6470dcce 236 return -1; // timeout
samux 4:980e6470dcce 237 }
samux 4:980e6470dcce 238
samux 4:980e6470dcce 239 int USB_SDcard::_cmd8() {
samux 4:980e6470dcce 240 _cs = 0;
samux 4:980e6470dcce 241
samux 4:980e6470dcce 242 // send a command
samux 4:980e6470dcce 243 _spi.write(0x40 | 8); // CMD8
samux 4:980e6470dcce 244 _spi.write(0x00); // reserved
samux 4:980e6470dcce 245 _spi.write(0x00); // reserved
samux 4:980e6470dcce 246 _spi.write(0x01); // 3.3v
samux 4:980e6470dcce 247 _spi.write(0xAA); // check pattern
samux 4:980e6470dcce 248 _spi.write(0x87); // crc
samux 4:980e6470dcce 249
samux 4:980e6470dcce 250 // wait for the repsonse (response[7] == 0)
samux 4:980e6470dcce 251 for(int i=0; i<SD_COMMAND_TIMEOUT * 1000; i++) {
samux 4:980e6470dcce 252 char response[5];
samux 4:980e6470dcce 253 response[0] = _spi.write(0xFF);
samux 4:980e6470dcce 254 if(!(response[0] & 0x80)) {
samux 4:980e6470dcce 255 for(int j=1; j<5; j++) {
samux 4:980e6470dcce 256 response[i] = _spi.write(0xFF);
samux 4:980e6470dcce 257 }
samux 4:980e6470dcce 258 _cs = 1;
samux 4:980e6470dcce 259 _spi.write(0xFF);
samux 4:980e6470dcce 260 return response[0];
samux 4:980e6470dcce 261 }
samux 4:980e6470dcce 262 }
samux 4:980e6470dcce 263 _cs = 1;
samux 4:980e6470dcce 264 _spi.write(0xFF);
samux 4:980e6470dcce 265 return -1; // timeout
samux 4:980e6470dcce 266 }
samux 4:980e6470dcce 267
samux 4:980e6470dcce 268 int USB_SDcard::_read(char *buffer, int length) {
samux 4:980e6470dcce 269 _cs = 0;
samux 4:980e6470dcce 270
samux 4:980e6470dcce 271 // read until start byte (0xFF)
samux 4:980e6470dcce 272 while(_spi.write(0xFF) != 0xFE);
samux 4:980e6470dcce 273
samux 4:980e6470dcce 274 // read data
samux 4:980e6470dcce 275 for(int i=0; i<length; i++) {
samux 4:980e6470dcce 276 buffer[i] = _spi.write(0xFF);
samux 4:980e6470dcce 277 }
samux 4:980e6470dcce 278 _spi.write(0xFF); // checksum
samux 4:980e6470dcce 279 _spi.write(0xFF);
samux 4:980e6470dcce 280
samux 4:980e6470dcce 281 _cs = 1;
samux 4:980e6470dcce 282 _spi.write(0xFF);
samux 4:980e6470dcce 283 return 0;
samux 4:980e6470dcce 284 }
samux 4:980e6470dcce 285
samux 4:980e6470dcce 286 int USB_SDcard::_write(const char *buffer, int length) {
samux 4:980e6470dcce 287 _cs = 0;
samux 4:980e6470dcce 288
samux 4:980e6470dcce 289 // indicate start of block
samux 4:980e6470dcce 290 _spi.write(0xFE);
samux 4:980e6470dcce 291
samux 4:980e6470dcce 292 // write the data
samux 4:980e6470dcce 293 for(int i=0; i<length; i++) {
samux 4:980e6470dcce 294 _spi.write(buffer[i]);
samux 4:980e6470dcce 295 }
samux 4:980e6470dcce 296
samux 4:980e6470dcce 297 // write the checksum
samux 4:980e6470dcce 298 _spi.write(0xFF);
samux 4:980e6470dcce 299 _spi.write(0xFF);
samux 4:980e6470dcce 300
samux 4:980e6470dcce 301 // check the repsonse token
samux 4:980e6470dcce 302 if((_spi.write(0xFF) & 0x1F) != 0x05) {
samux 4:980e6470dcce 303 _cs = 1;
samux 4:980e6470dcce 304 _spi.write(0xFF);
samux 4:980e6470dcce 305 return 1;
samux 4:980e6470dcce 306 }
samux 4:980e6470dcce 307
samux 4:980e6470dcce 308 // wait for write to finish
samux 4:980e6470dcce 309 while(_spi.write(0xFF) == 0);
samux 4:980e6470dcce 310
samux 4:980e6470dcce 311 _cs = 1;
samux 4:980e6470dcce 312 _spi.write(0xFF);
samux 4:980e6470dcce 313 return 0;
samux 4:980e6470dcce 314 }
samux 4:980e6470dcce 315
samux 4:980e6470dcce 316 static int ext_bits(char *data, int msb, int lsb) {
samux 4:980e6470dcce 317 int bits = 0;
samux 4:980e6470dcce 318 int size = 1 + msb - lsb;
samux 4:980e6470dcce 319 for(int i=0; i<size; i++) {
samux 4:980e6470dcce 320 int position = lsb + i;
samux 4:980e6470dcce 321 int byte = 15 - (position >> 3);
samux 4:980e6470dcce 322 int bit = position & 0x7;
samux 4:980e6470dcce 323 int value = (data[byte] >> bit) & 1;
samux 4:980e6470dcce 324 bits |= value << i;
samux 4:980e6470dcce 325 }
samux 4:980e6470dcce 326 return bits;
samux 4:980e6470dcce 327 }
samux 4:980e6470dcce 328
samux 4:980e6470dcce 329 int USB_SDcard::_sd_sectors() {
samux 4:980e6470dcce 330
samux 4:980e6470dcce 331 // CMD9, Response R2 (R1 byte + 16-byte block read)
samux 4:980e6470dcce 332 if(_cmdx(9, 0) != 0) {
samux 4:980e6470dcce 333 fprintf(stderr, "Didn't get a response from the disk\n");
samux 4:980e6470dcce 334 return 0;
samux 4:980e6470dcce 335 }
samux 4:980e6470dcce 336
samux 4:980e6470dcce 337 char csd[16];
samux 4:980e6470dcce 338 if(_read(csd, 16) != 0) {
samux 4:980e6470dcce 339 fprintf(stderr, "Couldn't read csd response from disk\n");
samux 4:980e6470dcce 340 return 0;
samux 4:980e6470dcce 341 }
samux 4:980e6470dcce 342
samux 4:980e6470dcce 343 // csd_structure : csd[127:126]
samux 4:980e6470dcce 344 // c_size : csd[73:62]
samux 4:980e6470dcce 345 // c_size_mult : csd[49:47]
samux 4:980e6470dcce 346 // read_bl_len : csd[83:80] - the *maximum* read block length
samux 4:980e6470dcce 347
samux 4:980e6470dcce 348 int csd_structure = ext_bits(csd, 127, 126);
samux 4:980e6470dcce 349 int c_size = ext_bits(csd, 73, 62);
samux 4:980e6470dcce 350 int c_size_mult = ext_bits(csd, 49, 47);
samux 4:980e6470dcce 351 int read_bl_len = ext_bits(csd, 83, 80);
samux 4:980e6470dcce 352
samux 4:980e6470dcce 353 // printf("CSD_STRUCT = %d\n", csd_structure);
samux 4:980e6470dcce 354
samux 4:980e6470dcce 355 if(csd_structure != 0) {
samux 4:980e6470dcce 356 fprintf(stderr, "This disk tastes funny! I only know about type 0 CSD structures\n");
samux 4:980e6470dcce 357 return 0;
samux 4:980e6470dcce 358 }
samux 4:980e6470dcce 359
samux 4:980e6470dcce 360 // memory capacity = BLOCKNR * BLOCK_LEN
samux 4:980e6470dcce 361 // where
samux 4:980e6470dcce 362 // BLOCKNR = (C_SIZE+1) * MULT
samux 4:980e6470dcce 363 // MULT = 2^(C_SIZE_MULT+2) (C_SIZE_MULT < 8)
samux 4:980e6470dcce 364 // BLOCK_LEN = 2^READ_BL_LEN, (READ_BL_LEN < 12)
samux 4:980e6470dcce 365
samux 6:126c4d980196 366 block_len = 1 << read_bl_len;
samux 4:980e6470dcce 367 int mult = 1 << (c_size_mult + 2);
samux 4:980e6470dcce 368 int blocknr = (c_size + 1) * mult;
samux 6:126c4d980196 369 capacity = blocknr * block_len;
samux 4:980e6470dcce 370
samux 4:980e6470dcce 371 int blocks = capacity / 512;
samux 4:980e6470dcce 372
samux 4:980e6470dcce 373 return blocks;
samux 4:980e6470dcce 374 }