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