efe

Dependencies:   mbed

Fork of raw_sd_card_disc_init by Pradeep Kotipalli

Committer:
pradeepvk2208
Date:
Sat Jun 06 11:07:06 2015 +0000
Revision:
2:aea18e9b89d8
Parent:
1:1843a53b51a8
Child:
3:d4bb1e13a897
disc init

Who changed what in which revision?

UserRevisionLine numberNew 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 2:aea18e9b89d8 19 int disk_initialize();
pradeepvk2208 2:aea18e9b89d8 20 uint64_t sd_sectors();
pradeepvk2208 2:aea18e9b89d8 21 uint64_t sectors;
pradeepvk2208 2:aea18e9b89d8 22
pradeepvk2208 0:d4197e4552ea 23 int cmd(int, int);
pradeepvk2208 0:d4197e4552ea 24 int cmd58();
pradeepvk2208 2:aea18e9b89d8 25 int cmdx(int, int);
pradeepvk2208 0:d4197e4552ea 26 int cmd8();
pradeepvk2208 2:aea18e9b89d8 27 int read(uint8_t*, uint32_t );
pradeepvk2208 2:aea18e9b89d8 28 static uint32_t ext_bits(unsigned char *, int , int );
pradeepvk2208 0:d4197e4552ea 29
pradeepvk2208 0:d4197e4552ea 30 int cdv;
pradeepvk2208 0:d4197e4552ea 31
pradeepvk2208 0:d4197e4552ea 32 #define SDCARD_FAIL 0
pradeepvk2208 0:d4197e4552ea 33 #define SDCARD_V1 1
pradeepvk2208 0:d4197e4552ea 34 #define SDCARD_V2 2
pradeepvk2208 0:d4197e4552ea 35 #define SDCARD_V2HC 3
pradeepvk2208 0:d4197e4552ea 36
pradeepvk2208 0:d4197e4552ea 37
pradeepvk2208 0:d4197e4552ea 38
pradeepvk2208 0:d4197e4552ea 39 SPI spi(PTD6, PTD7, PTD5); // mosi, miso, sclk
pradeepvk2208 0:d4197e4552ea 40 DigitalOut cs(PTD2);
pradeepvk2208 0:d4197e4552ea 41
pradeepvk2208 0:d4197e4552ea 42
pradeepvk2208 0:d4197e4552ea 43
pradeepvk2208 0:d4197e4552ea 44 int main()
pradeepvk2208 0:d4197e4552ea 45 {
pradeepvk2208 0:d4197e4552ea 46 while(1)
pradeepvk2208 0:d4197e4552ea 47 { initialise_card();
pradeepvk2208 0:d4197e4552ea 48 int result= initialise_card();
pradeepvk2208 2:aea18e9b89d8 49 printf("initialise card result=%d\n",result);
pradeepvk2208 2:aea18e9b89d8 50 disk_initialize();
pradeepvk2208 1:1843a53b51a8 51 wait(5);
pradeepvk2208 0:d4197e4552ea 52 }
pradeepvk2208 0:d4197e4552ea 53 }
pradeepvk2208 0:d4197e4552ea 54
pradeepvk2208 0:d4197e4552ea 55
pradeepvk2208 0:d4197e4552ea 56
pradeepvk2208 0:d4197e4552ea 57
pradeepvk2208 0:d4197e4552ea 58 int initialise_card() {
pradeepvk2208 0:d4197e4552ea 59 // Set to 100kHz for initialisation, and clock card with cs = 1
pradeepvk2208 0:d4197e4552ea 60 spi.frequency(100000);
pradeepvk2208 0:d4197e4552ea 61 cs = 1;
pradeepvk2208 0:d4197e4552ea 62 for (int i = 0; i < 16; i++) {
pradeepvk2208 0:d4197e4552ea 63 spi.write(0xFF);
pradeepvk2208 0:d4197e4552ea 64 }
pradeepvk2208 0:d4197e4552ea 65
pradeepvk2208 0:d4197e4552ea 66 // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
pradeepvk2208 0:d4197e4552ea 67 if (cmd(0, 0) != R1_IDLE_STATE) {
pradeepvk2208 0:d4197e4552ea 68 debug("No disk, or could not put SD card in to SPI idle state\n");
pradeepvk2208 0:d4197e4552ea 69 return SDCARD_FAIL;
pradeepvk2208 0:d4197e4552ea 70 }
pradeepvk2208 0:d4197e4552ea 71
pradeepvk2208 0:d4197e4552ea 72 // send CMD8 to determine whther it is ver 2.x
pradeepvk2208 0:d4197e4552ea 73 int r = cmd8();
pradeepvk2208 0:d4197e4552ea 74 if (r == R1_IDLE_STATE) {
pradeepvk2208 2:aea18e9b89d8 75 printf("Entering v2 bro\n");
pradeepvk2208 0:d4197e4552ea 76 return initialise_card_v2();
pradeepvk2208 2:aea18e9b89d8 77
pradeepvk2208 0:d4197e4552ea 78 } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
pradeepvk2208 2:aea18e9b89d8 79 printf("Entering v1 bro\n");
pradeepvk2208 0:d4197e4552ea 80 return initialise_card_v1();
pradeepvk2208 2:aea18e9b89d8 81
pradeepvk2208 0:d4197e4552ea 82 } else {
pradeepvk2208 0:d4197e4552ea 83 debug("Not in idle state after sending CMD8 (not an SD card?)\n");
pradeepvk2208 0:d4197e4552ea 84 return SDCARD_FAIL;
pradeepvk2208 0:d4197e4552ea 85 }
pradeepvk2208 0:d4197e4552ea 86 }
pradeepvk2208 0:d4197e4552ea 87
pradeepvk2208 0:d4197e4552ea 88 int initialise_card_v1() {
pradeepvk2208 0:d4197e4552ea 89 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
pradeepvk2208 0:d4197e4552ea 90 cmd(55, 0);
pradeepvk2208 0:d4197e4552ea 91 if (cmd(41, 0) == 0) {
pradeepvk2208 1:1843a53b51a8 92 printf("Yuppie v1 successful");
pradeepvk2208 0:d4197e4552ea 93 cdv = 512;
pradeepvk2208 0:d4197e4552ea 94 debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r");
pradeepvk2208 1:1843a53b51a8 95
pradeepvk2208 0:d4197e4552ea 96 return SDCARD_V1;
pradeepvk2208 0:d4197e4552ea 97 }
pradeepvk2208 0:d4197e4552ea 98 }
pradeepvk2208 0:d4197e4552ea 99
pradeepvk2208 0:d4197e4552ea 100 debug("Timeout waiting for v1.x card\n");
pradeepvk2208 0:d4197e4552ea 101 return SDCARD_FAIL;
pradeepvk2208 0:d4197e4552ea 102 }
pradeepvk2208 0:d4197e4552ea 103
pradeepvk2208 0:d4197e4552ea 104
pradeepvk2208 0:d4197e4552ea 105 int initialise_card_v2() {
pradeepvk2208 0:d4197e4552ea 106 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
pradeepvk2208 0:d4197e4552ea 107 wait_ms(50);
pradeepvk2208 0:d4197e4552ea 108 cmd58();
pradeepvk2208 0:d4197e4552ea 109 cmd(55, 0);
pradeepvk2208 0:d4197e4552ea 110 if (cmd(41, 0x40000000) == 0) {
pradeepvk2208 1:1843a53b51a8 111 printf("Yuppie,v2 successful");
pradeepvk2208 0:d4197e4552ea 112 cmd58();
pradeepvk2208 0:d4197e4552ea 113 debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r");
pradeepvk2208 0:d4197e4552ea 114 cdv = 1;
pradeepvk2208 1:1843a53b51a8 115
pradeepvk2208 0:d4197e4552ea 116 return SDCARD_V2;
pradeepvk2208 0:d4197e4552ea 117 }
pradeepvk2208 0:d4197e4552ea 118 }
pradeepvk2208 0:d4197e4552ea 119
pradeepvk2208 0:d4197e4552ea 120 debug("Timeout waiting for v2.x card\n");
pradeepvk2208 0:d4197e4552ea 121 return SDCARD_FAIL;
pradeepvk2208 0:d4197e4552ea 122 }
pradeepvk2208 0:d4197e4552ea 123
pradeepvk2208 0:d4197e4552ea 124 int cmd(int cmd, int arg) {
pradeepvk2208 0:d4197e4552ea 125 cs = 0;
pradeepvk2208 0:d4197e4552ea 126
pradeepvk2208 0:d4197e4552ea 127 // send a command
pradeepvk2208 0:d4197e4552ea 128 spi.write(0x40 | cmd);
pradeepvk2208 0:d4197e4552ea 129 spi.write(arg >> 24);
pradeepvk2208 0:d4197e4552ea 130 spi.write(arg >> 16);
pradeepvk2208 0:d4197e4552ea 131 spi.write(arg >> 8);
pradeepvk2208 0:d4197e4552ea 132 spi.write(arg >> 0);
pradeepvk2208 0:d4197e4552ea 133 spi.write(0x95);
pradeepvk2208 0:d4197e4552ea 134
pradeepvk2208 0:d4197e4552ea 135 // wait for the repsonse (response[7] == 0)
pradeepvk2208 0:d4197e4552ea 136 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
pradeepvk2208 0:d4197e4552ea 137 int response = spi.write(0xFF);
pradeepvk2208 0:d4197e4552ea 138 if (!(response & 0x80)) {
pradeepvk2208 0:d4197e4552ea 139 cs = 1;
pradeepvk2208 0:d4197e4552ea 140 spi.write(0xFF);
pradeepvk2208 0:d4197e4552ea 141 return response;
pradeepvk2208 0:d4197e4552ea 142 }
pradeepvk2208 0:d4197e4552ea 143 }
pradeepvk2208 0:d4197e4552ea 144 cs = 1;
pradeepvk2208 0:d4197e4552ea 145 spi.write(0xFF);
pradeepvk2208 0:d4197e4552ea 146 return -1; // timeout
pradeepvk2208 0:d4197e4552ea 147 }
pradeepvk2208 0:d4197e4552ea 148
pradeepvk2208 0:d4197e4552ea 149
pradeepvk2208 0:d4197e4552ea 150 int cmd58() {
pradeepvk2208 0:d4197e4552ea 151 cs = 0;
pradeepvk2208 0:d4197e4552ea 152 int arg = 0;
pradeepvk2208 0:d4197e4552ea 153
pradeepvk2208 0:d4197e4552ea 154 // send a command
pradeepvk2208 0:d4197e4552ea 155 spi.write(0x40 | 58);
pradeepvk2208 0:d4197e4552ea 156 spi.write(arg >> 24);
pradeepvk2208 0:d4197e4552ea 157 spi.write(arg >> 16);
pradeepvk2208 0:d4197e4552ea 158 spi.write(arg >> 8);
pradeepvk2208 0:d4197e4552ea 159 spi.write(arg >> 0);
pradeepvk2208 0:d4197e4552ea 160 spi.write(0x95);
pradeepvk2208 0:d4197e4552ea 161
pradeepvk2208 0:d4197e4552ea 162 // wait for the repsonse (response[7] == 0)
pradeepvk2208 0:d4197e4552ea 163 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
pradeepvk2208 0:d4197e4552ea 164 int response = spi.write(0xFF);
pradeepvk2208 0:d4197e4552ea 165 if (!(response & 0x80)) {
pradeepvk2208 0:d4197e4552ea 166 int ocr = spi.write(0xFF) << 24;
pradeepvk2208 0:d4197e4552ea 167 ocr |= spi.write(0xFF) << 16;
pradeepvk2208 0:d4197e4552ea 168 ocr |= spi.write(0xFF) << 8;
pradeepvk2208 0:d4197e4552ea 169 ocr |= spi.write(0xFF) << 0;
pradeepvk2208 0:d4197e4552ea 170 cs = 1;
pradeepvk2208 0:d4197e4552ea 171 spi.write(0xFF);
pradeepvk2208 0:d4197e4552ea 172 return response;
pradeepvk2208 0:d4197e4552ea 173 }
pradeepvk2208 0:d4197e4552ea 174 }
pradeepvk2208 0:d4197e4552ea 175 cs = 1;
pradeepvk2208 0:d4197e4552ea 176 spi.write(0xFF);
pradeepvk2208 0:d4197e4552ea 177 return -1; // timeout
pradeepvk2208 0:d4197e4552ea 178 }
pradeepvk2208 0:d4197e4552ea 179
pradeepvk2208 0:d4197e4552ea 180
pradeepvk2208 0:d4197e4552ea 181 int cmd8() {
pradeepvk2208 0:d4197e4552ea 182 cs = 0;
pradeepvk2208 0:d4197e4552ea 183
pradeepvk2208 0:d4197e4552ea 184 // send a command
pradeepvk2208 0:d4197e4552ea 185 spi.write(0x40 | 8); // CMD8
pradeepvk2208 0:d4197e4552ea 186 spi.write(0x00); // reserved
pradeepvk2208 0:d4197e4552ea 187 spi.write(0x00); // reserved
pradeepvk2208 0:d4197e4552ea 188 spi.write(0x01); // 3.3v
pradeepvk2208 0:d4197e4552ea 189 spi.write(0xAA); // check pattern
pradeepvk2208 0:d4197e4552ea 190 spi.write(0x87); // crc
pradeepvk2208 0:d4197e4552ea 191
pradeepvk2208 0:d4197e4552ea 192 // wait for the repsonse (response[7] == 0)
pradeepvk2208 0:d4197e4552ea 193 for (int i = 0; i < SD_COMMAND_TIMEOUT * 1000; i++) {
pradeepvk2208 0:d4197e4552ea 194 char response[5];
pradeepvk2208 0:d4197e4552ea 195 response[0] = spi.write(0xFF);
pradeepvk2208 0:d4197e4552ea 196 if (!(response[0] & 0x80)) {
pradeepvk2208 0:d4197e4552ea 197 for (int j = 1; j < 5; j++) {
pradeepvk2208 0:d4197e4552ea 198 response[i] = spi.write(0xFF);
pradeepvk2208 0:d4197e4552ea 199 }
pradeepvk2208 0:d4197e4552ea 200 cs = 1;
pradeepvk2208 0:d4197e4552ea 201 spi.write(0xFF);
pradeepvk2208 0:d4197e4552ea 202 return response[0];
pradeepvk2208 0:d4197e4552ea 203 }
pradeepvk2208 0:d4197e4552ea 204 }
pradeepvk2208 0:d4197e4552ea 205 cs = 1;
pradeepvk2208 0:d4197e4552ea 206 spi.write(0xFF);
pradeepvk2208 0:d4197e4552ea 207 return -1; // timeout
pradeepvk2208 2:aea18e9b89d8 208 }
pradeepvk2208 2:aea18e9b89d8 209
pradeepvk2208 2:aea18e9b89d8 210 uint64_t sd_sectors() {
pradeepvk2208 2:aea18e9b89d8 211 uint32_t c_size, c_size_mult, read_bl_len;
pradeepvk2208 2:aea18e9b89d8 212 uint32_t block_len, mult, blocknr, capacity;
pradeepvk2208 2:aea18e9b89d8 213 uint32_t hc_c_size;
pradeepvk2208 2:aea18e9b89d8 214 uint64_t blocks;
pradeepvk2208 2:aea18e9b89d8 215
pradeepvk2208 2:aea18e9b89d8 216 // CMD9, Response R2 (R1 byte + 16-byte block read)
pradeepvk2208 2:aea18e9b89d8 217 if (cmdx(9, 0) != 0) {
pradeepvk2208 2:aea18e9b89d8 218 debug("Didn't get a response from the disk\n");
pradeepvk2208 2:aea18e9b89d8 219 return 0;
pradeepvk2208 2:aea18e9b89d8 220 }
pradeepvk2208 2:aea18e9b89d8 221
pradeepvk2208 2:aea18e9b89d8 222 uint8_t csd[16];
pradeepvk2208 2:aea18e9b89d8 223 if (read(csd, 16) != 0) {
pradeepvk2208 2:aea18e9b89d8 224 debug("Couldn't read csd response from disk\n");
pradeepvk2208 2:aea18e9b89d8 225 return 0;
pradeepvk2208 2:aea18e9b89d8 226 }
pradeepvk2208 2:aea18e9b89d8 227
pradeepvk2208 2:aea18e9b89d8 228 // csd_structure : csd[127:126]
pradeepvk2208 2:aea18e9b89d8 229 // c_size : csd[73:62]
pradeepvk2208 2:aea18e9b89d8 230 // c_size_mult : csd[49:47]
pradeepvk2208 2:aea18e9b89d8 231 // read_bl_len : csd[83:80] - the *maximum* read block length
pradeepvk2208 2:aea18e9b89d8 232
pradeepvk2208 2:aea18e9b89d8 233 int csd_structure = ext_bits(csd, 127, 126);
pradeepvk2208 2:aea18e9b89d8 234
pradeepvk2208 2:aea18e9b89d8 235 switch (csd_structure) {
pradeepvk2208 2:aea18e9b89d8 236 case 0:
pradeepvk2208 2:aea18e9b89d8 237 cdv = 512;
pradeepvk2208 2:aea18e9b89d8 238 c_size = ext_bits(csd, 73, 62);
pradeepvk2208 2:aea18e9b89d8 239 c_size_mult = ext_bits(csd, 49, 47);
pradeepvk2208 2:aea18e9b89d8 240 read_bl_len = ext_bits(csd, 83, 80);
pradeepvk2208 2:aea18e9b89d8 241
pradeepvk2208 2:aea18e9b89d8 242 block_len = 1 << read_bl_len;
pradeepvk2208 2:aea18e9b89d8 243 mult = 1 << (c_size_mult + 2);
pradeepvk2208 2:aea18e9b89d8 244 blocknr = (c_size + 1) * mult;
pradeepvk2208 2:aea18e9b89d8 245 capacity = blocknr * block_len;
pradeepvk2208 2:aea18e9b89d8 246 blocks = capacity / 512;
pradeepvk2208 2:aea18e9b89d8 247 debug_if(SD_DBG, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks);
pradeepvk2208 2:aea18e9b89d8 248 break;
pradeepvk2208 2:aea18e9b89d8 249
pradeepvk2208 2:aea18e9b89d8 250 case 1:
pradeepvk2208 2:aea18e9b89d8 251 cdv = 1;
pradeepvk2208 2:aea18e9b89d8 252 hc_c_size = ext_bits(csd, 63, 48);
pradeepvk2208 2:aea18e9b89d8 253 blocks = (hc_c_size+1)*1024;
pradeepvk2208 2:aea18e9b89d8 254 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);
pradeepvk2208 2:aea18e9b89d8 255 break;
pradeepvk2208 2:aea18e9b89d8 256
pradeepvk2208 2:aea18e9b89d8 257 default:
pradeepvk2208 2:aea18e9b89d8 258 debug("CSD struct unsupported\r\n");
pradeepvk2208 2:aea18e9b89d8 259 return 0;
pradeepvk2208 2:aea18e9b89d8 260 };
pradeepvk2208 2:aea18e9b89d8 261 return blocks;
pradeepvk2208 2:aea18e9b89d8 262 }
pradeepvk2208 2:aea18e9b89d8 263
pradeepvk2208 2:aea18e9b89d8 264 int cmdx(int cmd, int arg) {
pradeepvk2208 2:aea18e9b89d8 265 cs = 0;
pradeepvk2208 2:aea18e9b89d8 266
pradeepvk2208 2:aea18e9b89d8 267 // send a command
pradeepvk2208 2:aea18e9b89d8 268 spi.write(0x40 | cmd);
pradeepvk2208 2:aea18e9b89d8 269 spi.write(arg >> 24);
pradeepvk2208 2:aea18e9b89d8 270 spi.write(arg >> 16);
pradeepvk2208 2:aea18e9b89d8 271 spi.write(arg >> 8);
pradeepvk2208 2:aea18e9b89d8 272 spi.write(arg >> 0);
pradeepvk2208 2:aea18e9b89d8 273 spi.write(0x95);
pradeepvk2208 2:aea18e9b89d8 274
pradeepvk2208 2:aea18e9b89d8 275 // wait for the repsonse (response[7] == 0)
pradeepvk2208 2:aea18e9b89d8 276 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
pradeepvk2208 2:aea18e9b89d8 277 int response = spi.write(0xFF);
pradeepvk2208 2:aea18e9b89d8 278 if (!(response & 0x80)) {
pradeepvk2208 2:aea18e9b89d8 279 return response;
pradeepvk2208 2:aea18e9b89d8 280 }
pradeepvk2208 2:aea18e9b89d8 281 }
pradeepvk2208 2:aea18e9b89d8 282 cs = 1;
pradeepvk2208 2:aea18e9b89d8 283 spi.write(0xFF);
pradeepvk2208 2:aea18e9b89d8 284 return -1; // timeout
pradeepvk2208 2:aea18e9b89d8 285 }
pradeepvk2208 2:aea18e9b89d8 286
pradeepvk2208 2:aea18e9b89d8 287
pradeepvk2208 2:aea18e9b89d8 288 int read(uint8_t *buffer, uint32_t length) {
pradeepvk2208 2:aea18e9b89d8 289 cs = 0;
pradeepvk2208 2:aea18e9b89d8 290
pradeepvk2208 2:aea18e9b89d8 291 // read until start byte (0xFF)
pradeepvk2208 2:aea18e9b89d8 292 while (spi.write(0xFF) != 0xFE);
pradeepvk2208 2:aea18e9b89d8 293
pradeepvk2208 2:aea18e9b89d8 294 // read data
pradeepvk2208 2:aea18e9b89d8 295 for (int i = 0; i < length; i++) {
pradeepvk2208 2:aea18e9b89d8 296 buffer[i] = spi.write(0xFF);
pradeepvk2208 2:aea18e9b89d8 297 }
pradeepvk2208 2:aea18e9b89d8 298 spi.write(0xFF); // checksum
pradeepvk2208 2:aea18e9b89d8 299 spi.write(0xFF);
pradeepvk2208 2:aea18e9b89d8 300
pradeepvk2208 2:aea18e9b89d8 301 cs = 1;
pradeepvk2208 2:aea18e9b89d8 302 spi.write(0xFF);
pradeepvk2208 2:aea18e9b89d8 303 return 0;
pradeepvk2208 2:aea18e9b89d8 304 }
pradeepvk2208 2:aea18e9b89d8 305
pradeepvk2208 2:aea18e9b89d8 306 static uint32_t ext_bits(unsigned char *data, int msb, int lsb) {
pradeepvk2208 2:aea18e9b89d8 307 uint32_t bits = 0;
pradeepvk2208 2:aea18e9b89d8 308 uint32_t size = 1 + msb - lsb;
pradeepvk2208 2:aea18e9b89d8 309 for (int i = 0; i < size; i++) {
pradeepvk2208 2:aea18e9b89d8 310 uint32_t position = lsb + i;
pradeepvk2208 2:aea18e9b89d8 311 uint32_t byte = 15 - (position >> 3);
pradeepvk2208 2:aea18e9b89d8 312 uint32_t bit = position & 0x7;
pradeepvk2208 2:aea18e9b89d8 313 uint32_t value = (data[byte] >> bit) & 1;
pradeepvk2208 2:aea18e9b89d8 314 bits |= value << i;
pradeepvk2208 2:aea18e9b89d8 315 }
pradeepvk2208 2:aea18e9b89d8 316 return bits;
pradeepvk2208 2:aea18e9b89d8 317 }
pradeepvk2208 2:aea18e9b89d8 318
pradeepvk2208 2:aea18e9b89d8 319 int disk_initialize() {
pradeepvk2208 2:aea18e9b89d8 320 int i = initialise_card();
pradeepvk2208 2:aea18e9b89d8 321 debug_if(SD_DBG, "init card = %d\n", i);
pradeepvk2208 2:aea18e9b89d8 322 sectors = sd_sectors();
pradeepvk2208 2:aea18e9b89d8 323
pradeepvk2208 2:aea18e9b89d8 324 // Set block length to 512 (CMD16)
pradeepvk2208 2:aea18e9b89d8 325 if (cmd(16, 512) != 0) {
pradeepvk2208 2:aea18e9b89d8 326 debug("Set 512-byte block timed out\n");
pradeepvk2208 2:aea18e9b89d8 327 return 1;
pradeepvk2208 2:aea18e9b89d8 328 }
pradeepvk2208 2:aea18e9b89d8 329 else
pradeepvk2208 2:aea18e9b89d8 330 {
pradeepvk2208 2:aea18e9b89d8 331 printf("Hey,block init succesful");
pradeepvk2208 2:aea18e9b89d8 332 }
pradeepvk2208 2:aea18e9b89d8 333
pradeepvk2208 2:aea18e9b89d8 334 spi.frequency(1000000); // Set to 1MHz for data transfer
pradeepvk2208 2:aea18e9b89d8 335 return 0;
pradeepvk2208 2:aea18e9b89d8 336 }
pradeepvk2208 2:aea18e9b89d8 337