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