![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
sd test code
Fork of SDCardTest by
main.cpp@0:cd61eb321578, 2015-07-15 (annotated)
- Committer:
- cholletisaik777
- Date:
- Wed Jul 15 14:46:33 2015 +0000
- Revision:
- 0:cd61eb321578
- Child:
- 1:ab8b18769cf3
sd card test code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cholletisaik777 | 0:cd61eb321578 | 1 | #include "mbed.h" |
cholletisaik777 | 0:cd61eb321578 | 2 | #include "mbed_debug.h" |
cholletisaik777 | 0:cd61eb321578 | 3 | |
cholletisaik777 | 0:cd61eb321578 | 4 | #define SD_COMMAND_TIMEOUT 5000 |
cholletisaik777 | 0:cd61eb321578 | 5 | |
cholletisaik777 | 0:cd61eb321578 | 6 | #define SD_DBG 0 |
cholletisaik777 | 0:cd61eb321578 | 7 | |
cholletisaik777 | 0:cd61eb321578 | 8 | #define R1_IDLE_STATE (1 << 0) |
cholletisaik777 | 0:cd61eb321578 | 9 | #define R1_ERASE_RESET (1 << 1) |
cholletisaik777 | 0:cd61eb321578 | 10 | #define R1_ILLEGAL_COMMAND (1 << 2) |
cholletisaik777 | 0:cd61eb321578 | 11 | #define R1_COM_CRC_ERROR (1 << 3) |
cholletisaik777 | 0:cd61eb321578 | 12 | #define R1_ERASE_SEQUENCE_ERROR (1 << 4) |
cholletisaik777 | 0:cd61eb321578 | 13 | #define R1_ADDRESS_ERROR (1 << 5) |
cholletisaik777 | 0:cd61eb321578 | 14 | #define R1_PARAMETER_ERROR (1 << 6) |
cholletisaik777 | 0:cd61eb321578 | 15 | |
cholletisaik777 | 0:cd61eb321578 | 16 | int initialise_card(); |
cholletisaik777 | 0:cd61eb321578 | 17 | int initialise_card_v1(); |
cholletisaik777 | 0:cd61eb321578 | 18 | int initialise_card_v2(); |
cholletisaik777 | 0:cd61eb321578 | 19 | int disk_initialize(); |
cholletisaik777 | 0:cd61eb321578 | 20 | int disk_write(const uint8_t *, uint64_t); |
cholletisaik777 | 0:cd61eb321578 | 21 | int disk_read(uint8_t *, uint64_t); |
cholletisaik777 | 0:cd61eb321578 | 22 | int disk_erase(int,int); |
cholletisaik777 | 0:cd61eb321578 | 23 | |
cholletisaik777 | 0:cd61eb321578 | 24 | uint64_t sd_sectors(); |
cholletisaik777 | 0:cd61eb321578 | 25 | uint64_t sectors; |
cholletisaik777 | 0:cd61eb321578 | 26 | |
cholletisaik777 | 0:cd61eb321578 | 27 | int cmd(int, int); |
cholletisaik777 | 0:cd61eb321578 | 28 | int cmd58(); |
cholletisaik777 | 0:cd61eb321578 | 29 | int cmdx(int, int); |
cholletisaik777 | 0:cd61eb321578 | 30 | int cmd8(); |
cholletisaik777 | 0:cd61eb321578 | 31 | int read(uint8_t*, uint32_t ); |
cholletisaik777 | 0:cd61eb321578 | 32 | int write(const uint8_t*, uint32_t ); |
cholletisaik777 | 0:cd61eb321578 | 33 | static uint32_t ext_bits(unsigned char *, int , int ); |
cholletisaik777 | 0:cd61eb321578 | 34 | |
cholletisaik777 | 0:cd61eb321578 | 35 | int cdv; |
cholletisaik777 | 0:cd61eb321578 | 36 | |
cholletisaik777 | 0:cd61eb321578 | 37 | #define SDCARD_FAIL 0 |
cholletisaik777 | 0:cd61eb321578 | 38 | #define SDCARD_V1 1 |
cholletisaik777 | 0:cd61eb321578 | 39 | #define SDCARD_V2 2 |
cholletisaik777 | 0:cd61eb321578 | 40 | #define SDCARD_V2HC 3 |
cholletisaik777 | 0:cd61eb321578 | 41 | |
cholletisaik777 | 0:cd61eb321578 | 42 | Serial pc(USBTX,USBRX); |
cholletisaik777 | 0:cd61eb321578 | 43 | |
cholletisaik777 | 0:cd61eb321578 | 44 | SPI spi(PTD6, PTD7, PTD5); // mosi, miso, sclk |
cholletisaik777 | 0:cd61eb321578 | 45 | DigitalOut cs(D2); |
cholletisaik777 | 0:cd61eb321578 | 46 | int count; |
cholletisaik777 | 0:cd61eb321578 | 47 | int i; |
cholletisaik777 | 0:cd61eb321578 | 48 | int random[1000]; |
cholletisaik777 | 0:cd61eb321578 | 49 | |
cholletisaik777 | 0:cd61eb321578 | 50 | |
cholletisaik777 | 0:cd61eb321578 | 51 | int main() |
cholletisaik777 | 0:cd61eb321578 | 52 | { |
cholletisaik777 | 0:cd61eb321578 | 53 | |
cholletisaik777 | 0:cd61eb321578 | 54 | initialise_card(); |
cholletisaik777 | 0:cd61eb321578 | 55 | int result= initialise_card(); |
cholletisaik777 | 0:cd61eb321578 | 56 | disk_initialize(); |
cholletisaik777 | 0:cd61eb321578 | 57 | uint8_t buffer[512]; |
cholletisaik777 | 0:cd61eb321578 | 58 | uint8_t buffer2[512]; |
cholletisaik777 | 0:cd61eb321578 | 59 | for(count=0; count<512; count++) |
cholletisaik777 | 0:cd61eb321578 | 60 | { |
cholletisaik777 | 0:cd61eb321578 | 61 | buffer[count]= count; |
cholletisaik777 | 0:cd61eb321578 | 62 | } |
cholletisaik777 | 0:cd61eb321578 | 63 | pc.printf("\n\rData written to SDCard is \n"); |
cholletisaik777 | 0:cd61eb321578 | 64 | for(count=0; count<512; count++) |
cholletisaik777 | 0:cd61eb321578 | 65 | { |
cholletisaik777 | 0:cd61eb321578 | 66 | pc.printf("%d ",buffer[count]); |
cholletisaik777 | 0:cd61eb321578 | 67 | } |
cholletisaik777 | 0:cd61eb321578 | 68 | disk_write(buffer,2); |
cholletisaik777 | 0:cd61eb321578 | 69 | disk_read(buffer2,2); |
cholletisaik777 | 0:cd61eb321578 | 70 | pc.printf("\n\rData Read from the SDCard is\r\n"); |
cholletisaik777 | 0:cd61eb321578 | 71 | for(count=0; count<512; count++) |
cholletisaik777 | 0:cd61eb321578 | 72 | { |
cholletisaik777 | 0:cd61eb321578 | 73 | pc.printf("%d ",buffer2[count]); |
cholletisaik777 | 0:cd61eb321578 | 74 | } |
cholletisaik777 | 0:cd61eb321578 | 75 | } |
cholletisaik777 | 0:cd61eb321578 | 76 | |
cholletisaik777 | 0:cd61eb321578 | 77 | int initialise_card() |
cholletisaik777 | 0:cd61eb321578 | 78 | { |
cholletisaik777 | 0:cd61eb321578 | 79 | // Set to 100kHz for initialisation, and clock card with cs = 1 |
cholletisaik777 | 0:cd61eb321578 | 80 | spi.frequency(100000); |
cholletisaik777 | 0:cd61eb321578 | 81 | cs = 1; |
cholletisaik777 | 0:cd61eb321578 | 82 | for (int i = 0; i < 16; i++) { |
cholletisaik777 | 0:cd61eb321578 | 83 | spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 84 | } |
cholletisaik777 | 0:cd61eb321578 | 85 | |
cholletisaik777 | 0:cd61eb321578 | 86 | // send CMD0, should return with all zeros except IDLE STATE set (bit 0) |
cholletisaik777 | 0:cd61eb321578 | 87 | if (cmd(0, 0) != R1_IDLE_STATE) { |
cholletisaik777 | 0:cd61eb321578 | 88 | debug("No disk, or could not put SD card in to SPI idle state\n"); |
cholletisaik777 | 0:cd61eb321578 | 89 | return SDCARD_FAIL; |
cholletisaik777 | 0:cd61eb321578 | 90 | } |
cholletisaik777 | 0:cd61eb321578 | 91 | |
cholletisaik777 | 0:cd61eb321578 | 92 | // send CMD8 to determine whther it is ver 2.x |
cholletisaik777 | 0:cd61eb321578 | 93 | int r = cmd8(); |
cholletisaik777 | 0:cd61eb321578 | 94 | if (r == R1_IDLE_STATE) { |
cholletisaik777 | 0:cd61eb321578 | 95 | printf("Entering v2 bro\n"); |
cholletisaik777 | 0:cd61eb321578 | 96 | return initialise_card_v2(); |
cholletisaik777 | 0:cd61eb321578 | 97 | |
cholletisaik777 | 0:cd61eb321578 | 98 | } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) { |
cholletisaik777 | 0:cd61eb321578 | 99 | printf("Entering v1 bro\n"); |
cholletisaik777 | 0:cd61eb321578 | 100 | return initialise_card_v1(); |
cholletisaik777 | 0:cd61eb321578 | 101 | |
cholletisaik777 | 0:cd61eb321578 | 102 | } else { |
cholletisaik777 | 0:cd61eb321578 | 103 | debug("Not in idle state after sending CMD8 (not an SD card?)\n"); |
cholletisaik777 | 0:cd61eb321578 | 104 | return SDCARD_FAIL; |
cholletisaik777 | 0:cd61eb321578 | 105 | } |
cholletisaik777 | 0:cd61eb321578 | 106 | } |
cholletisaik777 | 0:cd61eb321578 | 107 | |
cholletisaik777 | 0:cd61eb321578 | 108 | int initialise_card_v1() |
cholletisaik777 | 0:cd61eb321578 | 109 | { |
cholletisaik777 | 0:cd61eb321578 | 110 | for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { |
cholletisaik777 | 0:cd61eb321578 | 111 | cmd(55, 0); |
cholletisaik777 | 0:cd61eb321578 | 112 | if (cmd(41, 0) == 0) { |
cholletisaik777 | 0:cd61eb321578 | 113 | printf("Yuppie v1 successful\n"); |
cholletisaik777 | 0:cd61eb321578 | 114 | cdv = 512; |
cholletisaik777 | 0:cd61eb321578 | 115 | debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r"); |
cholletisaik777 | 0:cd61eb321578 | 116 | |
cholletisaik777 | 0:cd61eb321578 | 117 | return SDCARD_V1; |
cholletisaik777 | 0:cd61eb321578 | 118 | } |
cholletisaik777 | 0:cd61eb321578 | 119 | } |
cholletisaik777 | 0:cd61eb321578 | 120 | |
cholletisaik777 | 0:cd61eb321578 | 121 | debug("Timeout waiting for v1.x card\n"); |
cholletisaik777 | 0:cd61eb321578 | 122 | return SDCARD_FAIL; |
cholletisaik777 | 0:cd61eb321578 | 123 | } |
cholletisaik777 | 0:cd61eb321578 | 124 | |
cholletisaik777 | 0:cd61eb321578 | 125 | |
cholletisaik777 | 0:cd61eb321578 | 126 | int initialise_card_v2() |
cholletisaik777 | 0:cd61eb321578 | 127 | { |
cholletisaik777 | 0:cd61eb321578 | 128 | for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { |
cholletisaik777 | 0:cd61eb321578 | 129 | wait_ms(50); |
cholletisaik777 | 0:cd61eb321578 | 130 | cmd58(); |
cholletisaik777 | 0:cd61eb321578 | 131 | cmd(55, 0); |
cholletisaik777 | 0:cd61eb321578 | 132 | if (cmd(41, 0x40000000) == 0) { |
cholletisaik777 | 0:cd61eb321578 | 133 | printf("Yuppie,v2 successful\n"); |
cholletisaik777 | 0:cd61eb321578 | 134 | cmd58(); |
cholletisaik777 | 0:cd61eb321578 | 135 | debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r"); |
cholletisaik777 | 0:cd61eb321578 | 136 | cdv = 1; |
cholletisaik777 | 0:cd61eb321578 | 137 | |
cholletisaik777 | 0:cd61eb321578 | 138 | return SDCARD_V2; |
cholletisaik777 | 0:cd61eb321578 | 139 | } |
cholletisaik777 | 0:cd61eb321578 | 140 | } |
cholletisaik777 | 0:cd61eb321578 | 141 | |
cholletisaik777 | 0:cd61eb321578 | 142 | debug("Timeout waiting for v2.x card\n"); |
cholletisaik777 | 0:cd61eb321578 | 143 | return SDCARD_FAIL; |
cholletisaik777 | 0:cd61eb321578 | 144 | } |
cholletisaik777 | 0:cd61eb321578 | 145 | |
cholletisaik777 | 0:cd61eb321578 | 146 | int cmd(int cmd, int arg) |
cholletisaik777 | 0:cd61eb321578 | 147 | { |
cholletisaik777 | 0:cd61eb321578 | 148 | cs = 0; |
cholletisaik777 | 0:cd61eb321578 | 149 | |
cholletisaik777 | 0:cd61eb321578 | 150 | // send a command |
cholletisaik777 | 0:cd61eb321578 | 151 | spi.write(0x40 | cmd); |
cholletisaik777 | 0:cd61eb321578 | 152 | spi.write(arg >> 24); |
cholletisaik777 | 0:cd61eb321578 | 153 | spi.write(arg >> 16); |
cholletisaik777 | 0:cd61eb321578 | 154 | spi.write(arg >> 8); |
cholletisaik777 | 0:cd61eb321578 | 155 | spi.write(arg >> 0); |
cholletisaik777 | 0:cd61eb321578 | 156 | spi.write(0x95); |
cholletisaik777 | 0:cd61eb321578 | 157 | |
cholletisaik777 | 0:cd61eb321578 | 158 | // wait for the repsonse (response[7] == 0) |
cholletisaik777 | 0:cd61eb321578 | 159 | for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { |
cholletisaik777 | 0:cd61eb321578 | 160 | int response = spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 161 | if (!(response & 0x80)) { |
cholletisaik777 | 0:cd61eb321578 | 162 | cs = 1; |
cholletisaik777 | 0:cd61eb321578 | 163 | spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 164 | return response; |
cholletisaik777 | 0:cd61eb321578 | 165 | } |
cholletisaik777 | 0:cd61eb321578 | 166 | } |
cholletisaik777 | 0:cd61eb321578 | 167 | cs = 1; |
cholletisaik777 | 0:cd61eb321578 | 168 | spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 169 | return -1; // timeout |
cholletisaik777 | 0:cd61eb321578 | 170 | } |
cholletisaik777 | 0:cd61eb321578 | 171 | |
cholletisaik777 | 0:cd61eb321578 | 172 | |
cholletisaik777 | 0:cd61eb321578 | 173 | int cmd58() |
cholletisaik777 | 0:cd61eb321578 | 174 | { |
cholletisaik777 | 0:cd61eb321578 | 175 | cs = 0; |
cholletisaik777 | 0:cd61eb321578 | 176 | int arg = 0; |
cholletisaik777 | 0:cd61eb321578 | 177 | |
cholletisaik777 | 0:cd61eb321578 | 178 | // send a command |
cholletisaik777 | 0:cd61eb321578 | 179 | spi.write(0x40 | 58); |
cholletisaik777 | 0:cd61eb321578 | 180 | spi.write(arg >> 24); |
cholletisaik777 | 0:cd61eb321578 | 181 | spi.write(arg >> 16); |
cholletisaik777 | 0:cd61eb321578 | 182 | spi.write(arg >> 8); |
cholletisaik777 | 0:cd61eb321578 | 183 | spi.write(arg >> 0); |
cholletisaik777 | 0:cd61eb321578 | 184 | spi.write(0x95); |
cholletisaik777 | 0:cd61eb321578 | 185 | |
cholletisaik777 | 0:cd61eb321578 | 186 | // wait for the repsonse (response[7] == 0) |
cholletisaik777 | 0:cd61eb321578 | 187 | for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { |
cholletisaik777 | 0:cd61eb321578 | 188 | int response = spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 189 | if (!(response & 0x80)) { |
cholletisaik777 | 0:cd61eb321578 | 190 | int ocr = spi.write(0xFF) << 24; |
cholletisaik777 | 0:cd61eb321578 | 191 | ocr |= spi.write(0xFF) << 16; |
cholletisaik777 | 0:cd61eb321578 | 192 | ocr |= spi.write(0xFF) << 8; |
cholletisaik777 | 0:cd61eb321578 | 193 | ocr |= spi.write(0xFF) << 0; |
cholletisaik777 | 0:cd61eb321578 | 194 | cs = 1; |
cholletisaik777 | 0:cd61eb321578 | 195 | spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 196 | return response; |
cholletisaik777 | 0:cd61eb321578 | 197 | } |
cholletisaik777 | 0:cd61eb321578 | 198 | } |
cholletisaik777 | 0:cd61eb321578 | 199 | cs = 1; |
cholletisaik777 | 0:cd61eb321578 | 200 | spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 201 | return -1; // timeout |
cholletisaik777 | 0:cd61eb321578 | 202 | } |
cholletisaik777 | 0:cd61eb321578 | 203 | |
cholletisaik777 | 0:cd61eb321578 | 204 | |
cholletisaik777 | 0:cd61eb321578 | 205 | int cmd8() |
cholletisaik777 | 0:cd61eb321578 | 206 | { |
cholletisaik777 | 0:cd61eb321578 | 207 | cs = 0; |
cholletisaik777 | 0:cd61eb321578 | 208 | |
cholletisaik777 | 0:cd61eb321578 | 209 | // send a command |
cholletisaik777 | 0:cd61eb321578 | 210 | spi.write(0x40 | 8); // CMD8 |
cholletisaik777 | 0:cd61eb321578 | 211 | spi.write(0x00); // reserved |
cholletisaik777 | 0:cd61eb321578 | 212 | spi.write(0x00); // reserved |
cholletisaik777 | 0:cd61eb321578 | 213 | spi.write(0x01); // 3.3v |
cholletisaik777 | 0:cd61eb321578 | 214 | spi.write(0xAA); // check pattern |
cholletisaik777 | 0:cd61eb321578 | 215 | spi.write(0x87); // crc |
cholletisaik777 | 0:cd61eb321578 | 216 | |
cholletisaik777 | 0:cd61eb321578 | 217 | // wait for the repsonse (response[7] == 0) |
cholletisaik777 | 0:cd61eb321578 | 218 | for (int i = 0; i < SD_COMMAND_TIMEOUT * 1000; i++) { |
cholletisaik777 | 0:cd61eb321578 | 219 | char response[5]; |
cholletisaik777 | 0:cd61eb321578 | 220 | response[0] = spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 221 | if (!(response[0] & 0x80)) { |
cholletisaik777 | 0:cd61eb321578 | 222 | for (int j = 1; j < 5; j++) { |
cholletisaik777 | 0:cd61eb321578 | 223 | response[i] = spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 224 | } |
cholletisaik777 | 0:cd61eb321578 | 225 | cs = 1; |
cholletisaik777 | 0:cd61eb321578 | 226 | spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 227 | return response[0]; |
cholletisaik777 | 0:cd61eb321578 | 228 | } |
cholletisaik777 | 0:cd61eb321578 | 229 | } |
cholletisaik777 | 0:cd61eb321578 | 230 | cs = 1; |
cholletisaik777 | 0:cd61eb321578 | 231 | spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 232 | return -1; // timeout |
cholletisaik777 | 0:cd61eb321578 | 233 | } |
cholletisaik777 | 0:cd61eb321578 | 234 | |
cholletisaik777 | 0:cd61eb321578 | 235 | uint64_t sd_sectors() |
cholletisaik777 | 0:cd61eb321578 | 236 | { |
cholletisaik777 | 0:cd61eb321578 | 237 | uint32_t c_size, c_size_mult, read_bl_len; |
cholletisaik777 | 0:cd61eb321578 | 238 | uint32_t block_len, mult, blocknr, capacity; |
cholletisaik777 | 0:cd61eb321578 | 239 | uint32_t hc_c_size; |
cholletisaik777 | 0:cd61eb321578 | 240 | uint64_t blocks; |
cholletisaik777 | 0:cd61eb321578 | 241 | |
cholletisaik777 | 0:cd61eb321578 | 242 | // CMD9, Response R2 (R1 byte + 16-byte block read) |
cholletisaik777 | 0:cd61eb321578 | 243 | if (cmdx(9, 0) != 0) { |
cholletisaik777 | 0:cd61eb321578 | 244 | debug("Didn't get a response from the disk\n"); |
cholletisaik777 | 0:cd61eb321578 | 245 | return 0; |
cholletisaik777 | 0:cd61eb321578 | 246 | } |
cholletisaik777 | 0:cd61eb321578 | 247 | |
cholletisaik777 | 0:cd61eb321578 | 248 | uint8_t csd[16]; |
cholletisaik777 | 0:cd61eb321578 | 249 | if (read(csd, 16) != 0) { |
cholletisaik777 | 0:cd61eb321578 | 250 | debug("Couldn't read csd response from disk\n"); |
cholletisaik777 | 0:cd61eb321578 | 251 | return 0; |
cholletisaik777 | 0:cd61eb321578 | 252 | } |
cholletisaik777 | 0:cd61eb321578 | 253 | |
cholletisaik777 | 0:cd61eb321578 | 254 | // csd_structure : csd[127:126] |
cholletisaik777 | 0:cd61eb321578 | 255 | // c_size : csd[73:62] |
cholletisaik777 | 0:cd61eb321578 | 256 | // c_size_mult : csd[49:47] |
cholletisaik777 | 0:cd61eb321578 | 257 | // read_bl_len : csd[83:80] - the *maximum* read block length |
cholletisaik777 | 0:cd61eb321578 | 258 | |
cholletisaik777 | 0:cd61eb321578 | 259 | int csd_structure = ext_bits(csd, 127, 126); |
cholletisaik777 | 0:cd61eb321578 | 260 | |
cholletisaik777 | 0:cd61eb321578 | 261 | switch (csd_structure) { |
cholletisaik777 | 0:cd61eb321578 | 262 | case 0: |
cholletisaik777 | 0:cd61eb321578 | 263 | cdv = 512; |
cholletisaik777 | 0:cd61eb321578 | 264 | c_size = ext_bits(csd, 73, 62); |
cholletisaik777 | 0:cd61eb321578 | 265 | c_size_mult = ext_bits(csd, 49, 47); |
cholletisaik777 | 0:cd61eb321578 | 266 | read_bl_len = ext_bits(csd, 83, 80); |
cholletisaik777 | 0:cd61eb321578 | 267 | |
cholletisaik777 | 0:cd61eb321578 | 268 | block_len = 1 << read_bl_len; |
cholletisaik777 | 0:cd61eb321578 | 269 | mult = 1 << (c_size_mult + 2); |
cholletisaik777 | 0:cd61eb321578 | 270 | blocknr = (c_size + 1) * mult; |
cholletisaik777 | 0:cd61eb321578 | 271 | capacity = blocknr * block_len; |
cholletisaik777 | 0:cd61eb321578 | 272 | blocks = capacity / 512; |
cholletisaik777 | 0:cd61eb321578 | 273 | debug_if(SD_DBG, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks); |
cholletisaik777 | 0:cd61eb321578 | 274 | break; |
cholletisaik777 | 0:cd61eb321578 | 275 | |
cholletisaik777 | 0:cd61eb321578 | 276 | case 1: |
cholletisaik777 | 0:cd61eb321578 | 277 | cdv = 1; |
cholletisaik777 | 0:cd61eb321578 | 278 | hc_c_size = ext_bits(csd, 63, 48); |
cholletisaik777 | 0:cd61eb321578 | 279 | blocks = (hc_c_size+1)*1024; |
cholletisaik777 | 0:cd61eb321578 | 280 | debug_if(SD_DBG, "\n\rSDHC Card \n\rhc_c_size: %d\n\rcapacity: %lld \n\rsectors: %lld\n\r", hc_c_size, blocks*512, blocks); |
cholletisaik777 | 0:cd61eb321578 | 281 | break; |
cholletisaik777 | 0:cd61eb321578 | 282 | |
cholletisaik777 | 0:cd61eb321578 | 283 | default: |
cholletisaik777 | 0:cd61eb321578 | 284 | debug("CSD struct unsupported\r\n"); |
cholletisaik777 | 0:cd61eb321578 | 285 | return 0; |
cholletisaik777 | 0:cd61eb321578 | 286 | }; |
cholletisaik777 | 0:cd61eb321578 | 287 | return blocks; |
cholletisaik777 | 0:cd61eb321578 | 288 | } |
cholletisaik777 | 0:cd61eb321578 | 289 | |
cholletisaik777 | 0:cd61eb321578 | 290 | int cmdx(int cmd, int arg) |
cholletisaik777 | 0:cd61eb321578 | 291 | { |
cholletisaik777 | 0:cd61eb321578 | 292 | cs = 0; |
cholletisaik777 | 0:cd61eb321578 | 293 | |
cholletisaik777 | 0:cd61eb321578 | 294 | // send a command |
cholletisaik777 | 0:cd61eb321578 | 295 | spi.write(0x40 | cmd); |
cholletisaik777 | 0:cd61eb321578 | 296 | spi.write(arg >> 24); |
cholletisaik777 | 0:cd61eb321578 | 297 | spi.write(arg >> 16); |
cholletisaik777 | 0:cd61eb321578 | 298 | spi.write(arg >> 8); |
cholletisaik777 | 0:cd61eb321578 | 299 | spi.write(arg >> 0); |
cholletisaik777 | 0:cd61eb321578 | 300 | spi.write(0x95); |
cholletisaik777 | 0:cd61eb321578 | 301 | |
cholletisaik777 | 0:cd61eb321578 | 302 | // wait for the repsonse (response[7] == 0) |
cholletisaik777 | 0:cd61eb321578 | 303 | for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { |
cholletisaik777 | 0:cd61eb321578 | 304 | int response = spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 305 | if (!(response & 0x80)) { |
cholletisaik777 | 0:cd61eb321578 | 306 | return response; |
cholletisaik777 | 0:cd61eb321578 | 307 | } |
cholletisaik777 | 0:cd61eb321578 | 308 | } |
cholletisaik777 | 0:cd61eb321578 | 309 | cs = 1; |
cholletisaik777 | 0:cd61eb321578 | 310 | spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 311 | return -1; // timeout |
cholletisaik777 | 0:cd61eb321578 | 312 | } |
cholletisaik777 | 0:cd61eb321578 | 313 | |
cholletisaik777 | 0:cd61eb321578 | 314 | static uint32_t ext_bits(unsigned char *data, int msb, int lsb) |
cholletisaik777 | 0:cd61eb321578 | 315 | { |
cholletisaik777 | 0:cd61eb321578 | 316 | uint32_t bits = 0; |
cholletisaik777 | 0:cd61eb321578 | 317 | uint32_t size = 1 + msb - lsb; |
cholletisaik777 | 0:cd61eb321578 | 318 | for (int i = 0; i < size; i++) { |
cholletisaik777 | 0:cd61eb321578 | 319 | uint32_t position = lsb + i; |
cholletisaik777 | 0:cd61eb321578 | 320 | uint32_t byte = 15 - (position >> 3); |
cholletisaik777 | 0:cd61eb321578 | 321 | uint32_t bit = position & 0x7; |
cholletisaik777 | 0:cd61eb321578 | 322 | uint32_t value = (data[byte] >> bit) & 1; |
cholletisaik777 | 0:cd61eb321578 | 323 | bits |= value << i; |
cholletisaik777 | 0:cd61eb321578 | 324 | } |
cholletisaik777 | 0:cd61eb321578 | 325 | return bits; |
cholletisaik777 | 0:cd61eb321578 | 326 | } |
cholletisaik777 | 0:cd61eb321578 | 327 | |
cholletisaik777 | 0:cd61eb321578 | 328 | int disk_initialize() |
cholletisaik777 | 0:cd61eb321578 | 329 | { |
cholletisaik777 | 0:cd61eb321578 | 330 | int i = initialise_card(); |
cholletisaik777 | 0:cd61eb321578 | 331 | debug_if(SD_DBG, "init card = %d\n", i); |
cholletisaik777 | 0:cd61eb321578 | 332 | sectors = sd_sectors(); |
cholletisaik777 | 0:cd61eb321578 | 333 | |
cholletisaik777 | 0:cd61eb321578 | 334 | // Set block length to 512 (CMD16) |
cholletisaik777 | 0:cd61eb321578 | 335 | if (cmd(16, 512) != 0) { |
cholletisaik777 | 0:cd61eb321578 | 336 | debug("Set 512-byte block timed out\n"); |
cholletisaik777 | 0:cd61eb321578 | 337 | return 1; |
cholletisaik777 | 0:cd61eb321578 | 338 | } else { |
cholletisaik777 | 0:cd61eb321578 | 339 | printf("Hey,block init succesful\n"); |
cholletisaik777 | 0:cd61eb321578 | 340 | } |
cholletisaik777 | 0:cd61eb321578 | 341 | |
cholletisaik777 | 0:cd61eb321578 | 342 | spi.frequency(1000000); // Set to 1MHz for data transfer |
cholletisaik777 | 0:cd61eb321578 | 343 | return 0; |
cholletisaik777 | 0:cd61eb321578 | 344 | } |
cholletisaik777 | 0:cd61eb321578 | 345 | |
cholletisaik777 | 0:cd61eb321578 | 346 | int disk_write(const uint8_t *buffer, uint64_t block_number) |
cholletisaik777 | 0:cd61eb321578 | 347 | |
cholletisaik777 | 0:cd61eb321578 | 348 | { |
cholletisaik777 | 0:cd61eb321578 | 349 | // set write address for single block (CMD24) |
cholletisaik777 | 0:cd61eb321578 | 350 | if (cmd(24, block_number * cdv) != 0) { |
cholletisaik777 | 0:cd61eb321578 | 351 | return 1; |
cholletisaik777 | 0:cd61eb321578 | 352 | } |
cholletisaik777 | 0:cd61eb321578 | 353 | |
cholletisaik777 | 0:cd61eb321578 | 354 | // send the data block |
cholletisaik777 | 0:cd61eb321578 | 355 | write(buffer, 512); |
cholletisaik777 | 0:cd61eb321578 | 356 | //printf("Written Successfully bro \n"); |
cholletisaik777 | 0:cd61eb321578 | 357 | return 0; |
cholletisaik777 | 0:cd61eb321578 | 358 | } |
cholletisaik777 | 0:cd61eb321578 | 359 | |
cholletisaik777 | 0:cd61eb321578 | 360 | int write(const uint8_t*buffer, uint32_t length) |
cholletisaik777 | 0:cd61eb321578 | 361 | { |
cholletisaik777 | 0:cd61eb321578 | 362 | cs = 0; |
cholletisaik777 | 0:cd61eb321578 | 363 | |
cholletisaik777 | 0:cd61eb321578 | 364 | // indicate start of block |
cholletisaik777 | 0:cd61eb321578 | 365 | spi.write(0xFE); |
cholletisaik777 | 0:cd61eb321578 | 366 | |
cholletisaik777 | 0:cd61eb321578 | 367 | // write the data |
cholletisaik777 | 0:cd61eb321578 | 368 | for (int i = 0; i < length; i++) { |
cholletisaik777 | 0:cd61eb321578 | 369 | spi.write(buffer[i]); |
cholletisaik777 | 0:cd61eb321578 | 370 | } |
cholletisaik777 | 0:cd61eb321578 | 371 | |
cholletisaik777 | 0:cd61eb321578 | 372 | // write the checksum |
cholletisaik777 | 0:cd61eb321578 | 373 | spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 374 | spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 375 | |
cholletisaik777 | 0:cd61eb321578 | 376 | // check the response token |
cholletisaik777 | 0:cd61eb321578 | 377 | if ((spi.write(0xFF) & 0x1F) != 0x05) { |
cholletisaik777 | 0:cd61eb321578 | 378 | cs = 1; |
cholletisaik777 | 0:cd61eb321578 | 379 | spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 380 | return 1; |
cholletisaik777 | 0:cd61eb321578 | 381 | } |
cholletisaik777 | 0:cd61eb321578 | 382 | |
cholletisaik777 | 0:cd61eb321578 | 383 | // wait for write to finish |
cholletisaik777 | 0:cd61eb321578 | 384 | while (spi.write(0xFF) == 0); |
cholletisaik777 | 0:cd61eb321578 | 385 | |
cholletisaik777 | 0:cd61eb321578 | 386 | cs = 1; |
cholletisaik777 | 0:cd61eb321578 | 387 | spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 388 | return 0; |
cholletisaik777 | 0:cd61eb321578 | 389 | } |
cholletisaik777 | 0:cd61eb321578 | 390 | |
cholletisaik777 | 0:cd61eb321578 | 391 | int disk_read(uint8_t *buffer, uint64_t block_number) |
cholletisaik777 | 0:cd61eb321578 | 392 | { |
cholletisaik777 | 0:cd61eb321578 | 393 | // set read address for single block (CMD17) |
cholletisaik777 | 0:cd61eb321578 | 394 | if (cmd(17, block_number * cdv) != 0) { |
cholletisaik777 | 0:cd61eb321578 | 395 | return 1; |
cholletisaik777 | 0:cd61eb321578 | 396 | } |
cholletisaik777 | 0:cd61eb321578 | 397 | |
cholletisaik777 | 0:cd61eb321578 | 398 | // receive the data |
cholletisaik777 | 0:cd61eb321578 | 399 | read(buffer, 512); |
cholletisaik777 | 0:cd61eb321578 | 400 | return 0; |
cholletisaik777 | 0:cd61eb321578 | 401 | } |
cholletisaik777 | 0:cd61eb321578 | 402 | |
cholletisaik777 | 0:cd61eb321578 | 403 | int read(uint8_t *buffer, uint32_t length) |
cholletisaik777 | 0:cd61eb321578 | 404 | { |
cholletisaik777 | 0:cd61eb321578 | 405 | cs = 0; |
cholletisaik777 | 0:cd61eb321578 | 406 | |
cholletisaik777 | 0:cd61eb321578 | 407 | // read until start byte (0xFF) |
cholletisaik777 | 0:cd61eb321578 | 408 | while (spi.write(0xFF) != 0xFE); |
cholletisaik777 | 0:cd61eb321578 | 409 | |
cholletisaik777 | 0:cd61eb321578 | 410 | // read data |
cholletisaik777 | 0:cd61eb321578 | 411 | for (int i = 0; i < length; i++) { |
cholletisaik777 | 0:cd61eb321578 | 412 | buffer[i] = spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 413 | } |
cholletisaik777 | 0:cd61eb321578 | 414 | spi.write(0xFF); // checksum |
cholletisaik777 | 0:cd61eb321578 | 415 | spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 416 | |
cholletisaik777 | 0:cd61eb321578 | 417 | cs = 1; |
cholletisaik777 | 0:cd61eb321578 | 418 | spi.write(0xFF); |
cholletisaik777 | 0:cd61eb321578 | 419 | return 0; |
cholletisaik777 | 0:cd61eb321578 | 420 | } |
cholletisaik777 | 0:cd61eb321578 | 421 | |
cholletisaik777 | 0:cd61eb321578 | 422 | int disk_erase(int startBlock, int totalBlocks) |
cholletisaik777 | 0:cd61eb321578 | 423 | { |
cholletisaik777 | 0:cd61eb321578 | 424 | if(cmd(32, startBlock * cdv) != 0) { |
cholletisaik777 | 0:cd61eb321578 | 425 | return 1; |
cholletisaik777 | 0:cd61eb321578 | 426 | } |
cholletisaik777 | 0:cd61eb321578 | 427 | if (cmd(33, (startBlock+totalBlocks-1) * cdv) != 0) { |
cholletisaik777 | 0:cd61eb321578 | 428 | return 1; |
cholletisaik777 | 0:cd61eb321578 | 429 | } |
cholletisaik777 | 0:cd61eb321578 | 430 | if (cmd(38,0) != 0) { |
cholletisaik777 | 0:cd61eb321578 | 431 | return 1; |
cholletisaik777 | 0:cd61eb321578 | 432 | } |
cholletisaik777 | 0:cd61eb321578 | 433 | |
cholletisaik777 | 0:cd61eb321578 | 434 | return 0; //normal return |
cholletisaik777 | 0:cd61eb321578 | 435 | } |