vr1.1

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of CDMS_RTOS_v1_1 by Team Fox

Committer:
cholletisaik777
Date:
Fri Jul 17 06:35:40 2015 +0000
Revision:
16:7428828a5da2
Parent:
15:2c8629da6ec9
COM INTEGRATION

Who changed what in which revision?

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