Pradeep Kotipalli
/
raw_sd_card_write_raw
efe
Fork of raw_sd_card_disc_init by
main.cpp@1:1843a53b51a8, 2015-06-06 (annotated)
- Committer:
- pradeepvk2208
- Date:
- Sat Jun 06 09:58:13 2015 +0000
- Revision:
- 1:1843a53b51a8
- Parent:
- 0:d4197e4552ea
- Child:
- 2:aea18e9b89d8
qbfkjehfiwgh
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pradeepvk2208 | 0:d4197e4552ea | 1 | #include "mbed.h" |
pradeepvk2208 | 0:d4197e4552ea | 2 | #include "mbed_debug.h" |
pradeepvk2208 | 0:d4197e4552ea | 3 | |
pradeepvk2208 | 0:d4197e4552ea | 4 | #define SD_COMMAND_TIMEOUT 5000 |
pradeepvk2208 | 0:d4197e4552ea | 5 | |
pradeepvk2208 | 0:d4197e4552ea | 6 | #define SD_DBG 0 |
pradeepvk2208 | 0:d4197e4552ea | 7 | |
pradeepvk2208 | 0:d4197e4552ea | 8 | #define R1_IDLE_STATE (1 << 0) |
pradeepvk2208 | 0:d4197e4552ea | 9 | #define R1_ERASE_RESET (1 << 1) |
pradeepvk2208 | 0:d4197e4552ea | 10 | #define R1_ILLEGAL_COMMAND (1 << 2) |
pradeepvk2208 | 0:d4197e4552ea | 11 | #define R1_COM_CRC_ERROR (1 << 3) |
pradeepvk2208 | 0:d4197e4552ea | 12 | #define R1_ERASE_SEQUENCE_ERROR (1 << 4) |
pradeepvk2208 | 0:d4197e4552ea | 13 | #define R1_ADDRESS_ERROR (1 << 5) |
pradeepvk2208 | 0:d4197e4552ea | 14 | #define R1_PARAMETER_ERROR (1 << 6) |
pradeepvk2208 | 0:d4197e4552ea | 15 | |
pradeepvk2208 | 0:d4197e4552ea | 16 | int initialise_card(); |
pradeepvk2208 | 0:d4197e4552ea | 17 | int initialise_card_v1(); |
pradeepvk2208 | 0:d4197e4552ea | 18 | int initialise_card_v2(); |
pradeepvk2208 | 0:d4197e4552ea | 19 | int cmd(int, int); |
pradeepvk2208 | 0:d4197e4552ea | 20 | int cmd58(); |
pradeepvk2208 | 0:d4197e4552ea | 21 | int cmd8(); |
pradeepvk2208 | 0:d4197e4552ea | 22 | |
pradeepvk2208 | 0:d4197e4552ea | 23 | int cdv; |
pradeepvk2208 | 0:d4197e4552ea | 24 | |
pradeepvk2208 | 0:d4197e4552ea | 25 | #define SDCARD_FAIL 0 |
pradeepvk2208 | 0:d4197e4552ea | 26 | #define SDCARD_V1 1 |
pradeepvk2208 | 0:d4197e4552ea | 27 | #define SDCARD_V2 2 |
pradeepvk2208 | 0:d4197e4552ea | 28 | #define SDCARD_V2HC 3 |
pradeepvk2208 | 0:d4197e4552ea | 29 | |
pradeepvk2208 | 0:d4197e4552ea | 30 | |
pradeepvk2208 | 0:d4197e4552ea | 31 | |
pradeepvk2208 | 0:d4197e4552ea | 32 | SPI spi(PTD6, PTD7, PTD5); // mosi, miso, sclk |
pradeepvk2208 | 0:d4197e4552ea | 33 | DigitalOut cs(PTD2); |
pradeepvk2208 | 0:d4197e4552ea | 34 | |
pradeepvk2208 | 0:d4197e4552ea | 35 | |
pradeepvk2208 | 0:d4197e4552ea | 36 | |
pradeepvk2208 | 0:d4197e4552ea | 37 | int main() |
pradeepvk2208 | 0:d4197e4552ea | 38 | { |
pradeepvk2208 | 0:d4197e4552ea | 39 | while(1) |
pradeepvk2208 | 0:d4197e4552ea | 40 | { initialise_card(); |
pradeepvk2208 | 0:d4197e4552ea | 41 | int result= initialise_card(); |
pradeepvk2208 | 0:d4197e4552ea | 42 | printf("%d\n",result); |
pradeepvk2208 | 1:1843a53b51a8 | 43 | wait(5); |
pradeepvk2208 | 0:d4197e4552ea | 44 | } |
pradeepvk2208 | 0:d4197e4552ea | 45 | } |
pradeepvk2208 | 0:d4197e4552ea | 46 | |
pradeepvk2208 | 0:d4197e4552ea | 47 | |
pradeepvk2208 | 0:d4197e4552ea | 48 | |
pradeepvk2208 | 0:d4197e4552ea | 49 | |
pradeepvk2208 | 0:d4197e4552ea | 50 | int initialise_card() { |
pradeepvk2208 | 0:d4197e4552ea | 51 | // Set to 100kHz for initialisation, and clock card with cs = 1 |
pradeepvk2208 | 0:d4197e4552ea | 52 | spi.frequency(100000); |
pradeepvk2208 | 0:d4197e4552ea | 53 | cs = 1; |
pradeepvk2208 | 0:d4197e4552ea | 54 | for (int i = 0; i < 16; i++) { |
pradeepvk2208 | 0:d4197e4552ea | 55 | spi.write(0xFF); |
pradeepvk2208 | 0:d4197e4552ea | 56 | } |
pradeepvk2208 | 0:d4197e4552ea | 57 | |
pradeepvk2208 | 0:d4197e4552ea | 58 | // send CMD0, should return with all zeros except IDLE STATE set (bit 0) |
pradeepvk2208 | 0:d4197e4552ea | 59 | if (cmd(0, 0) != R1_IDLE_STATE) { |
pradeepvk2208 | 0:d4197e4552ea | 60 | debug("No disk, or could not put SD card in to SPI idle state\n"); |
pradeepvk2208 | 0:d4197e4552ea | 61 | return SDCARD_FAIL; |
pradeepvk2208 | 0:d4197e4552ea | 62 | } |
pradeepvk2208 | 0:d4197e4552ea | 63 | |
pradeepvk2208 | 0:d4197e4552ea | 64 | // send CMD8 to determine whther it is ver 2.x |
pradeepvk2208 | 0:d4197e4552ea | 65 | int r = cmd8(); |
pradeepvk2208 | 0:d4197e4552ea | 66 | if (r == R1_IDLE_STATE) { |
pradeepvk2208 | 0:d4197e4552ea | 67 | return initialise_card_v2(); |
pradeepvk2208 | 1:1843a53b51a8 | 68 | printf("Entering v2 bro"); |
pradeepvk2208 | 0:d4197e4552ea | 69 | } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) { |
pradeepvk2208 | 0:d4197e4552ea | 70 | return initialise_card_v1(); |
pradeepvk2208 | 1:1843a53b51a8 | 71 | printf("Entering v1 bro"); |
pradeepvk2208 | 0:d4197e4552ea | 72 | } else { |
pradeepvk2208 | 0:d4197e4552ea | 73 | debug("Not in idle state after sending CMD8 (not an SD card?)\n"); |
pradeepvk2208 | 0:d4197e4552ea | 74 | return SDCARD_FAIL; |
pradeepvk2208 | 0:d4197e4552ea | 75 | } |
pradeepvk2208 | 0:d4197e4552ea | 76 | } |
pradeepvk2208 | 0:d4197e4552ea | 77 | |
pradeepvk2208 | 0:d4197e4552ea | 78 | int initialise_card_v1() { |
pradeepvk2208 | 0:d4197e4552ea | 79 | for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { |
pradeepvk2208 | 0:d4197e4552ea | 80 | cmd(55, 0); |
pradeepvk2208 | 0:d4197e4552ea | 81 | if (cmd(41, 0) == 0) { |
pradeepvk2208 | 1:1843a53b51a8 | 82 | printf("Yuppie v1 successful"); |
pradeepvk2208 | 0:d4197e4552ea | 83 | cdv = 512; |
pradeepvk2208 | 0:d4197e4552ea | 84 | debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r"); |
pradeepvk2208 | 1:1843a53b51a8 | 85 | |
pradeepvk2208 | 0:d4197e4552ea | 86 | return SDCARD_V1; |
pradeepvk2208 | 0:d4197e4552ea | 87 | } |
pradeepvk2208 | 0:d4197e4552ea | 88 | } |
pradeepvk2208 | 0:d4197e4552ea | 89 | |
pradeepvk2208 | 0:d4197e4552ea | 90 | debug("Timeout waiting for v1.x card\n"); |
pradeepvk2208 | 0:d4197e4552ea | 91 | return SDCARD_FAIL; |
pradeepvk2208 | 0:d4197e4552ea | 92 | } |
pradeepvk2208 | 0:d4197e4552ea | 93 | |
pradeepvk2208 | 0:d4197e4552ea | 94 | |
pradeepvk2208 | 0:d4197e4552ea | 95 | int initialise_card_v2() { |
pradeepvk2208 | 0:d4197e4552ea | 96 | for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { |
pradeepvk2208 | 0:d4197e4552ea | 97 | wait_ms(50); |
pradeepvk2208 | 0:d4197e4552ea | 98 | cmd58(); |
pradeepvk2208 | 0:d4197e4552ea | 99 | cmd(55, 0); |
pradeepvk2208 | 0:d4197e4552ea | 100 | if (cmd(41, 0x40000000) == 0) { |
pradeepvk2208 | 1:1843a53b51a8 | 101 | printf("Yuppie,v2 successful"); |
pradeepvk2208 | 0:d4197e4552ea | 102 | cmd58(); |
pradeepvk2208 | 0:d4197e4552ea | 103 | debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r"); |
pradeepvk2208 | 0:d4197e4552ea | 104 | cdv = 1; |
pradeepvk2208 | 1:1843a53b51a8 | 105 | |
pradeepvk2208 | 0:d4197e4552ea | 106 | return SDCARD_V2; |
pradeepvk2208 | 0:d4197e4552ea | 107 | } |
pradeepvk2208 | 0:d4197e4552ea | 108 | } |
pradeepvk2208 | 0:d4197e4552ea | 109 | |
pradeepvk2208 | 0:d4197e4552ea | 110 | debug("Timeout waiting for v2.x card\n"); |
pradeepvk2208 | 0:d4197e4552ea | 111 | return SDCARD_FAIL; |
pradeepvk2208 | 0:d4197e4552ea | 112 | } |
pradeepvk2208 | 0:d4197e4552ea | 113 | |
pradeepvk2208 | 0:d4197e4552ea | 114 | int cmd(int cmd, int arg) { |
pradeepvk2208 | 0:d4197e4552ea | 115 | cs = 0; |
pradeepvk2208 | 0:d4197e4552ea | 116 | |
pradeepvk2208 | 0:d4197e4552ea | 117 | // send a command |
pradeepvk2208 | 0:d4197e4552ea | 118 | spi.write(0x40 | cmd); |
pradeepvk2208 | 0:d4197e4552ea | 119 | spi.write(arg >> 24); |
pradeepvk2208 | 0:d4197e4552ea | 120 | spi.write(arg >> 16); |
pradeepvk2208 | 0:d4197e4552ea | 121 | spi.write(arg >> 8); |
pradeepvk2208 | 0:d4197e4552ea | 122 | spi.write(arg >> 0); |
pradeepvk2208 | 0:d4197e4552ea | 123 | spi.write(0x95); |
pradeepvk2208 | 0:d4197e4552ea | 124 | |
pradeepvk2208 | 0:d4197e4552ea | 125 | // wait for the repsonse (response[7] == 0) |
pradeepvk2208 | 0:d4197e4552ea | 126 | for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { |
pradeepvk2208 | 0:d4197e4552ea | 127 | int response = spi.write(0xFF); |
pradeepvk2208 | 0:d4197e4552ea | 128 | if (!(response & 0x80)) { |
pradeepvk2208 | 0:d4197e4552ea | 129 | cs = 1; |
pradeepvk2208 | 0:d4197e4552ea | 130 | spi.write(0xFF); |
pradeepvk2208 | 0:d4197e4552ea | 131 | return response; |
pradeepvk2208 | 0:d4197e4552ea | 132 | } |
pradeepvk2208 | 0:d4197e4552ea | 133 | } |
pradeepvk2208 | 0:d4197e4552ea | 134 | cs = 1; |
pradeepvk2208 | 0:d4197e4552ea | 135 | spi.write(0xFF); |
pradeepvk2208 | 0:d4197e4552ea | 136 | return -1; // timeout |
pradeepvk2208 | 0:d4197e4552ea | 137 | } |
pradeepvk2208 | 0:d4197e4552ea | 138 | |
pradeepvk2208 | 0:d4197e4552ea | 139 | |
pradeepvk2208 | 0:d4197e4552ea | 140 | int cmd58() { |
pradeepvk2208 | 0:d4197e4552ea | 141 | cs = 0; |
pradeepvk2208 | 0:d4197e4552ea | 142 | int arg = 0; |
pradeepvk2208 | 0:d4197e4552ea | 143 | |
pradeepvk2208 | 0:d4197e4552ea | 144 | // send a command |
pradeepvk2208 | 0:d4197e4552ea | 145 | spi.write(0x40 | 58); |
pradeepvk2208 | 0:d4197e4552ea | 146 | spi.write(arg >> 24); |
pradeepvk2208 | 0:d4197e4552ea | 147 | spi.write(arg >> 16); |
pradeepvk2208 | 0:d4197e4552ea | 148 | spi.write(arg >> 8); |
pradeepvk2208 | 0:d4197e4552ea | 149 | spi.write(arg >> 0); |
pradeepvk2208 | 0:d4197e4552ea | 150 | spi.write(0x95); |
pradeepvk2208 | 0:d4197e4552ea | 151 | |
pradeepvk2208 | 0:d4197e4552ea | 152 | // wait for the repsonse (response[7] == 0) |
pradeepvk2208 | 0:d4197e4552ea | 153 | for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { |
pradeepvk2208 | 0:d4197e4552ea | 154 | int response = spi.write(0xFF); |
pradeepvk2208 | 0:d4197e4552ea | 155 | if (!(response & 0x80)) { |
pradeepvk2208 | 0:d4197e4552ea | 156 | int ocr = spi.write(0xFF) << 24; |
pradeepvk2208 | 0:d4197e4552ea | 157 | ocr |= spi.write(0xFF) << 16; |
pradeepvk2208 | 0:d4197e4552ea | 158 | ocr |= spi.write(0xFF) << 8; |
pradeepvk2208 | 0:d4197e4552ea | 159 | ocr |= spi.write(0xFF) << 0; |
pradeepvk2208 | 0:d4197e4552ea | 160 | cs = 1; |
pradeepvk2208 | 0:d4197e4552ea | 161 | spi.write(0xFF); |
pradeepvk2208 | 0:d4197e4552ea | 162 | return response; |
pradeepvk2208 | 0:d4197e4552ea | 163 | } |
pradeepvk2208 | 0:d4197e4552ea | 164 | } |
pradeepvk2208 | 0:d4197e4552ea | 165 | cs = 1; |
pradeepvk2208 | 0:d4197e4552ea | 166 | spi.write(0xFF); |
pradeepvk2208 | 0:d4197e4552ea | 167 | return -1; // timeout |
pradeepvk2208 | 0:d4197e4552ea | 168 | } |
pradeepvk2208 | 0:d4197e4552ea | 169 | |
pradeepvk2208 | 0:d4197e4552ea | 170 | |
pradeepvk2208 | 0:d4197e4552ea | 171 | int cmd8() { |
pradeepvk2208 | 0:d4197e4552ea | 172 | cs = 0; |
pradeepvk2208 | 0:d4197e4552ea | 173 | |
pradeepvk2208 | 0:d4197e4552ea | 174 | // send a command |
pradeepvk2208 | 0:d4197e4552ea | 175 | spi.write(0x40 | 8); // CMD8 |
pradeepvk2208 | 0:d4197e4552ea | 176 | spi.write(0x00); // reserved |
pradeepvk2208 | 0:d4197e4552ea | 177 | spi.write(0x00); // reserved |
pradeepvk2208 | 0:d4197e4552ea | 178 | spi.write(0x01); // 3.3v |
pradeepvk2208 | 0:d4197e4552ea | 179 | spi.write(0xAA); // check pattern |
pradeepvk2208 | 0:d4197e4552ea | 180 | spi.write(0x87); // crc |
pradeepvk2208 | 0:d4197e4552ea | 181 | |
pradeepvk2208 | 0:d4197e4552ea | 182 | // wait for the repsonse (response[7] == 0) |
pradeepvk2208 | 0:d4197e4552ea | 183 | for (int i = 0; i < SD_COMMAND_TIMEOUT * 1000; i++) { |
pradeepvk2208 | 0:d4197e4552ea | 184 | char response[5]; |
pradeepvk2208 | 0:d4197e4552ea | 185 | response[0] = spi.write(0xFF); |
pradeepvk2208 | 0:d4197e4552ea | 186 | if (!(response[0] & 0x80)) { |
pradeepvk2208 | 0:d4197e4552ea | 187 | for (int j = 1; j < 5; j++) { |
pradeepvk2208 | 0:d4197e4552ea | 188 | response[i] = spi.write(0xFF); |
pradeepvk2208 | 0:d4197e4552ea | 189 | } |
pradeepvk2208 | 0:d4197e4552ea | 190 | cs = 1; |
pradeepvk2208 | 0:d4197e4552ea | 191 | spi.write(0xFF); |
pradeepvk2208 | 0:d4197e4552ea | 192 | return response[0]; |
pradeepvk2208 | 0:d4197e4552ea | 193 | } |
pradeepvk2208 | 0:d4197e4552ea | 194 | } |
pradeepvk2208 | 0:d4197e4552ea | 195 | cs = 1; |
pradeepvk2208 | 0:d4197e4552ea | 196 | spi.write(0xFF); |
pradeepvk2208 | 0:d4197e4552ea | 197 | return -1; // timeout |
pradeepvk2208 | 0:d4197e4552ea | 198 | } |