To fix the hang problem

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

Committer:
chaithanyarss
Date:
Tue Dec 20 08:36:41 2016 +0000
Revision:
328:2242ebc71be8
Parent:
327:5a967a66c10f
Child:
339:5c4bda268e08
Child:
341:599aa20d0279
Child:
343:204497974293
changed ADF temperature calibration; testing OBSRS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chaithanyarss 328:2242ebc71be8 1 #ifndef CDMS_SD_INLCUDED
chaithanyarss 328:2242ebc71be8 2 #define CDMS_SD_INLCUDED
chaithanyarss 328:2242ebc71be8 3
chaithanyarss 327:5a967a66c10f 4 //SPI spi(PTE1, PTE3, PTE2); // MOSI,MISO, CLOCK microcontroller(in order)
ee12b079 86:a26f5f22631d 5 //DigitalOut cs_sd(PTE22);
ee12b079 86:a26f5f22631d 6
ee12b079 86:a26f5f22631d 7 //Serial sd1(USBTX,USBRX);
ee12b079 86:a26f5f22631d 8
ee12b079 86:a26f5f22631d 9
chaithanyarss 261:1e54415b34d3 10 #define SD_COMMAND_TIMEOUT 325
ee12b079 86:a26f5f22631d 11
chaithanyarss 328:2242ebc71be8 12 #define SD_DBG 1
ee12b079 86:a26f5f22631d 13
ee12b079 86:a26f5f22631d 14 #define R1_IDLE_STATE (1 << 0)
ee12b079 86:a26f5f22631d 15 #define R1_ERASE_RESET (1 << 1)
ee12b079 86:a26f5f22631d 16 #define R1_ILLEGAL_COMMAND (1 << 2)
ee12b079 86:a26f5f22631d 17 #define R1_COM_CRC_ERROR (1 << 3)
ee12b079 86:a26f5f22631d 18 #define R1_ERASE_SEQUENCE_ERROR (1 << 4)
ee12b079 86:a26f5f22631d 19 #define R1_ADDRESS_ERROR (1 << 5)
ee12b079 86:a26f5f22631d 20 #define R1_PARAMETER_ERROR (1 << 6)
ee12b079 86:a26f5f22631d 21
chaithanyarss 327:5a967a66c10f 22 uint32_t SD_DATABLOCK_START[] = {0, 1001, 11001, 21001, 31001, 41001};
chaithanyarss 327:5a967a66c10f 23 uint32_t SD_DATABLOCK_END[] = {0, 11000, 21000, 31000, 41000, 51000};
chaithanyarss 327:5a967a66c10f 24 uint32_t SD_MNG_SECT=80000;
ee12b079 86:a26f5f22631d 25
ee12b079 194:ab528911780d 26 extern uint8_t SD_INIT_FLAGS;
ee12b079 86:a26f5f22631d 27
ee12b079 86:a26f5f22631d 28 int initialise_card();
ee12b079 86:a26f5f22631d 29 int initialise_card_v1();
ee12b079 86:a26f5f22631d 30 int initialise_card_v2();
ee12b079 86:a26f5f22631d 31 int disk_write(const uint8_t *, uint64_t);
ee12b079 86:a26f5f22631d 32 int disk_read(uint8_t *, uint64_t);
ee12b079 86:a26f5f22631d 33 int disk_erase(int,int);
ee12b079 209:63e9c8f8b5d2 34 int disk_read_statusbits(uint8_t *);
ee12b079 86:a26f5f22631d 35
chaithanyarss 326:e424f70b86c0 36 void FCTN_SD_MNGR();
ee12b079 194:ab528911780d 37 int INCREMENT_SD_LIB(uint8_t);
ee12b079 86:a26f5f22631d 38
ee12b079 86:a26f5f22631d 39
ee12b079 86:a26f5f22631d 40 int cmd(int, int);
ee12b079 86:a26f5f22631d 41 int cmd58();
ee12b079 86:a26f5f22631d 42 int cmdx(int, int);
ee12b079 86:a26f5f22631d 43 int cmd8();
ee12b079 86:a26f5f22631d 44 int read(uint8_t*, uint32_t );
ee12b079 86:a26f5f22631d 45 int write(const uint8_t*, uint32_t );
ee12b079 86:a26f5f22631d 46 static uint32_t ext_bits(unsigned char *, int , int );
ee12b079 92:bce22b38c440 47 int SD_WRITE(uint8_t*,uint32_t,uint8_t);
ee12b079 194:ab528911780d 48 int FCTN_CDMS_SD_INIT();
ee12b079 195:6a549c0e9287 49 uint8_t SD_READ(uint8_t*,uint32_t,uint8_t);
chaithanyarss 261:1e54415b34d3 50 #define SDCARD_FAIL 4
ee12b079 86:a26f5f22631d 51 #define SDCARD_V1 1
ee12b079 86:a26f5f22631d 52 #define SDCARD_V2 2
ee12b079 86:a26f5f22631d 53 #define SDCARD_V2HC 3
ee12b079 86:a26f5f22631d 54
ee12b079 86:a26f5f22631d 55 int cdv;
ee12b079 86:a26f5f22631d 56 uint64_t sd_sectors();
ee12b079 86:a26f5f22631d 57 uint64_t sectors;
ee12b079 86:a26f5f22631d 58
ee12b079 194:ab528911780d 59 int FCTN_CDMS_SD_INIT()
chaithanyarss 327:5a967a66c10f 60 {
ee12b079 194:ab528911780d 61 int i = initialise_card();
chaithanyarss 261:1e54415b34d3 62 if( i == 4)
chaithanyarss 327:5a967a66c10f 63 return 4;
ee12b079 194:ab528911780d 64 debug_if(SD_DBG, "init card = %d\n", i);
ee12b079 194:ab528911780d 65 sectors = sd_sectors();
ee12b079 194:ab528911780d 66
ee12b079 194:ab528911780d 67 // Set block length to 512 (CMD16)
ee12b079 194:ab528911780d 68 if (cmd(16, 512) != 0) {
ee12b079 194:ab528911780d 69 debug("\rSet 512-byte block timed out\r\n");
ee12b079 194:ab528911780d 70 return 1;
ee12b079 194:ab528911780d 71 } else {
chaithanyarss 327:5a967a66c10f 72 //printf("\rDisk initialization successfull\r\n");
ee12b079 194:ab528911780d 73 }
chaithanyarss 261:1e54415b34d3 74 SD_STATUS = DEVICE_POWERED;
ee12b079 194:ab528911780d 75 spi.frequency(1000000); // Set to 1MHz for data transfer
ee12b079 194:ab528911780d 76 return 0;
ee12b079 86:a26f5f22631d 77 }
ee12b079 86:a26f5f22631d 78
ee12b079 209:63e9c8f8b5d2 79 void FCTN_SD_MNGR()
ee12b079 86:a26f5f22631d 80 {
ee12b079 92:bce22b38c440 81 uint32_t fsc;
chaithanyarss 327:5a967a66c10f 82 uint32_t fsc_old;
ee12b079 92:bce22b38c440 83 uint8_t buffer[512];
chaithanyarss 261:1e54415b34d3 84 int b;
chaithanyarss 327:5a967a66c10f 85 if(SD_STATUS == DEVICE_POWERED) {
chaithanyarss 327:5a967a66c10f 86 b=disk_read(buffer, SD_MNG_SECT);
ee12b079 195:6a549c0e9287 87
chaithanyarss 327:5a967a66c10f 88 for(int i = 0; i < 5; i++ ) {
chaithanyarss 327:5a967a66c10f 89 fsc= (uint32_t)(buffer[0 + i*8]<<24)+(uint32_t)(buffer[1 + i*8]<<16)+(uint32_t)(buffer[2 + i*8]<<8)+(uint32_t)buffer[3 + i*8];
chaithanyarss 327:5a967a66c10f 90 fsc_old= (uint32_t)(buffer[4 + i*8]<<24)+(uint32_t)(buffer[5 + i*8]<<16)+(uint32_t)(buffer[6 + i*8]<<8)+(uint32_t)buffer[7 + i*8];
chaithanyarss 327:5a967a66c10f 91 FSC_CURRENT[1 + i] = fsc;
chaithanyarss 327:5a967a66c10f 92 FSC_OLD[1 + i] = fsc_old;
chaithanyarss 327:5a967a66c10f 93 }
chaithanyarss 261:1e54415b34d3 94 }
ee12b079 194:ab528911780d 95 }
ee12b079 194:ab528911780d 96
ee12b079 194:ab528911780d 97 int INCREMENT_SD_LIB(uint8_t sid)
ee12b079 194:ab528911780d 98 {
ee12b079 194:ab528911780d 99 uint32_t fsc;
chaithanyarss 327:5a967a66c10f 100 uint32_t fsc_old;
ee12b079 209:63e9c8f8b5d2 101 int i;
ee12b079 194:ab528911780d 102 uint8_t buffer[512];
chaithanyarss 327:5a967a66c10f 103
spacelab 308:7c3872365d58 104 disk_read(buffer,SD_MNG_SECT);
ee12b079 209:63e9c8f8b5d2 105 SD_MNG_SECT += SD_LIB_WRITES/(int)0xFFFF;
chaithanyarss 327:5a967a66c10f 106 if(SD_MNG_SECT != SD_LIB_BLK_CURRENT) {
chaithanyarss 324:c0a5228cc666 107 SD_LIB_BLK_CURRENT = SD_MNG_SECT;
chaithanyarss 327:5a967a66c10f 108 FCTN_CDMS_WR_FLASH(16,SD_LIB_BLK_CURRENT);
chaithanyarss 324:c0a5228cc666 109 }
ee12b079 209:63e9c8f8b5d2 110 SD_LIB_WRITES = SD_LIB_WRITES%(int)0xFFFF;
spacelab 308:7c3872365d58 111 disk_write(buffer,SD_MNG_SECT);
chaithanyarss 327:5a967a66c10f 112
ee12b079 194:ab528911780d 113 disk_read(buffer,SD_MNG_SECT);
chaithanyarss 328:2242ebc71be8 114 if(sid>=0x01 && sid <=0x05)
chaithanyarss 328:2242ebc71be8 115 {
chaithanyarss 328:2242ebc71be8 116 fsc=(uint32_t)(buffer[0 + ((sid-1)*8)]<<24)+(uint32_t)(buffer[1 + ((sid-1)*8)]<<16)+(uint32_t)(buffer[2 + ((sid-1)*8)]<<8)+(uint32_t)buffer[3 + ((sid-1)*8)];
chaithanyarss 328:2242ebc71be8 117 fsc_old=(uint32_t)(buffer[4 + ((sid-1)*8)]<<24)+(uint32_t)(buffer[5 + ((sid-1)*8)]<<16)+(uint32_t)(buffer[6 + ((sid-1)*8)]<<8)+(uint32_t)buffer[7 + ((sid-1)*8)];
ee12b079 203:424308159a56 118 fsc++;
chaithanyarss 328:2242ebc71be8 119 buffer[0 + ((sid-1)*8)]=(uint8_t) (fsc>>24 & 0xFF);
chaithanyarss 328:2242ebc71be8 120 buffer[1 + ((sid-1)*8)]=(uint8_t) (fsc>>16 & 0xFF);
chaithanyarss 328:2242ebc71be8 121 buffer[2 + ((sid-1)*8)]=(uint8_t) (fsc>>8 & 0xFF);
chaithanyarss 328:2242ebc71be8 122 buffer[3 + ((sid-1)*8)]=(uint8_t) (fsc & 0xFF);
chaithanyarss 328:2242ebc71be8 123 if(fsc > SD_DATABLOCK_END[sid]-SD_DATABLOCK_START[sid]+1) {
chaithanyarss 327:5a967a66c10f 124 fsc_old = fsc_old+1;
chaithanyarss 328:2242ebc71be8 125 buffer[4 + ((sid-1)*8)]=(uint8_t) (fsc_old>>24 & 0xFF);
chaithanyarss 328:2242ebc71be8 126 buffer[5 + ((sid-1)*8)]=(uint8_t) (fsc_old>>16 & 0xFF);
chaithanyarss 328:2242ebc71be8 127 buffer[6 + ((sid-1)*8)]=(uint8_t) (fsc_old>>8 & 0xFF);
chaithanyarss 328:2242ebc71be8 128 buffer[7 + ((sid-1)*8)]=(uint8_t) (fsc_old & 0xFF);
ee12b079 194:ab528911780d 129 }
chaithanyarss 327:5a967a66c10f 130
ee12b079 203:424308159a56 131 i = disk_write(buffer,SD_MNG_SECT);
chaithanyarss 327:5a967a66c10f 132 if(i == 0) {
chaithanyarss 328:2242ebc71be8 133 FSC_CURRENT[sid] = fsc;
chaithanyarss 328:2242ebc71be8 134 FSC_OLD[sid] = fsc_old;
ee12b079 209:63e9c8f8b5d2 135 return i;
ee12b079 209:63e9c8f8b5d2 136 }
ee12b079 194:ab528911780d 137 }
ee12b079 194:ab528911780d 138 return -1;
ee12b079 86:a26f5f22631d 139 }
ee12b079 86:a26f5f22631d 140
ee12b079 86:a26f5f22631d 141
ee12b079 92:bce22b38c440 142 int SD_WRITE(uint8_t* buffer,uint32_t fsc,uint8_t sid)
ee12b079 86:a26f5f22631d 143 {
spacelab 308:7c3872365d58 144 uint32_t block_number;
ee12b079 194:ab528911780d 145 int result = 10;
chaithanyarss 327:5a967a66c10f 146 if(SD_STATUS == DEVICE_POWERED) {
chaithanyarss 327:5a967a66c10f 147 if(sid==0x01) {
chaithanyarss 327:5a967a66c10f 148 block_number=SD_DATABLOCK_START[1]+(fsc%(SD_DATABLOCK_END[1]-SD_DATABLOCK_START[1]+1));
chaithanyarss 327:5a967a66c10f 149 //block_number=SD_DATABLOCK_START[1]+fsc;
chaithanyarss 327:5a967a66c10f 150 result= disk_write(buffer,block_number);
chaithanyarss 327:5a967a66c10f 151 if(result == 0) {
chaithanyarss 327:5a967a66c10f 152 if(INCREMENT_SD_LIB(sid) == 0)
chaithanyarss 327:5a967a66c10f 153 SD_LIB_WRITES++;
chaithanyarss 327:5a967a66c10f 154 }
chaithanyarss 327:5a967a66c10f 155 return result;
chaithanyarss 327:5a967a66c10f 156 }
chaithanyarss 327:5a967a66c10f 157 if(sid==0x02) {
chaithanyarss 327:5a967a66c10f 158 block_number= SD_DATABLOCK_START[2]+(fsc%(SD_DATABLOCK_END[2] - SD_DATABLOCK_START[2]+1));
chaithanyarss 327:5a967a66c10f 159 //block_number= SD_DATABLOCK_START[2]+fsc;
chaithanyarss 327:5a967a66c10f 160 result= disk_write(buffer,block_number);
chaithanyarss 327:5a967a66c10f 161 if(result == 0) {
chaithanyarss 327:5a967a66c10f 162 if(INCREMENT_SD_LIB(sid) == 0)
chaithanyarss 327:5a967a66c10f 163 SD_LIB_WRITES++;
chaithanyarss 327:5a967a66c10f 164 }
chaithanyarss 327:5a967a66c10f 165 return result;
ee12b079 209:63e9c8f8b5d2 166 }
chaithanyarss 327:5a967a66c10f 167 if(sid==0x03) {
chaithanyarss 327:5a967a66c10f 168 block_number= SD_DATABLOCK_START[3] +(fsc%(SD_DATABLOCK_END[3] - SD_DATABLOCK_START[3] +1));
chaithanyarss 327:5a967a66c10f 169 //block_number= SD_DATABLOCK_START[3] +fsc;
chaithanyarss 327:5a967a66c10f 170 result= disk_write(buffer,block_number);
chaithanyarss 327:5a967a66c10f 171 if(result == 0) {
chaithanyarss 327:5a967a66c10f 172 if(INCREMENT_SD_LIB(sid) == 0)
chaithanyarss 327:5a967a66c10f 173 SD_LIB_WRITES++;
chaithanyarss 327:5a967a66c10f 174 }
chaithanyarss 327:5a967a66c10f 175 return result;
ee12b079 209:63e9c8f8b5d2 176 }
chaithanyarss 327:5a967a66c10f 177 if(sid==0x04) {
chaithanyarss 327:5a967a66c10f 178 block_number=SD_DATABLOCK_START[4] +(fsc%(SD_DATABLOCK_END[4] - SD_DATABLOCK_START[4] +1));
chaithanyarss 327:5a967a66c10f 179 //block_number=SD_DATABLOCK_START[4] +fsc;
chaithanyarss 327:5a967a66c10f 180 result= disk_write(buffer,block_number);
chaithanyarss 327:5a967a66c10f 181 if(result == 0) {
chaithanyarss 327:5a967a66c10f 182 if(INCREMENT_SD_LIB(sid) == 0)
chaithanyarss 327:5a967a66c10f 183 SD_LIB_WRITES++;
chaithanyarss 327:5a967a66c10f 184 }
chaithanyarss 327:5a967a66c10f 185 return result;
chaithanyarss 327:5a967a66c10f 186 }
chaithanyarss 327:5a967a66c10f 187 if(sid==0x05) {
chaithanyarss 327:5a967a66c10f 188 block_number= SD_DATABLOCK_START[5] +(fsc%(SD_DATABLOCK_START[5] - SD_DATABLOCK_START[5] +1));
chaithanyarss 327:5a967a66c10f 189 //block_number= SD_DATABLOCK_START[5] +fsc;
chaithanyarss 327:5a967a66c10f 190 result= disk_write(buffer,block_number);
chaithanyarss 327:5a967a66c10f 191 if(result == 0) {
chaithanyarss 327:5a967a66c10f 192 if(INCREMENT_SD_LIB(sid) == 0)
chaithanyarss 327:5a967a66c10f 193 SD_LIB_WRITES++;
chaithanyarss 327:5a967a66c10f 194 }
chaithanyarss 327:5a967a66c10f 195 return result;
chaithanyarss 327:5a967a66c10f 196 }
ee12b079 86:a26f5f22631d 197 }
chaithanyarss 327:5a967a66c10f 198 // return 1;
ee12b079 86:a26f5f22631d 199 }
ee12b079 86:a26f5f22631d 200
ee12b079 195:6a549c0e9287 201 uint8_t SD_READ(uint8_t* buffer,uint32_t fsc,uint8_t sid)
ee12b079 86:a26f5f22631d 202 {
spacelab 308:7c3872365d58 203 FCTN_SD_MNGR();
ee12b079 209:63e9c8f8b5d2 204 uint32_t block_number;
ee12b079 209:63e9c8f8b5d2 205 int result;
ee12b079 295:699801854b71 206 //if(SD_SW_EN_DS == 1)
ee12b079 295:699801854b71 207 // return 0x89;
chaithanyarss 327:5a967a66c10f 208 if(sid==0x01) {
chaithanyarss 327:5a967a66c10f 209 if(!(FSC_OLD[1]<=fsc && fsc<=FSC_CURRENT[1])) {
ee12b079 195:6a549c0e9287 210 return 0x86;
ee12b079 195:6a549c0e9287 211 }
chaithanyarss 327:5a967a66c10f 212 block_number=SD_DATABLOCK_START[1]+(fsc%(SD_DATABLOCK_END[1]-SD_DATABLOCK_START[1]+1));
ee12b079 86:a26f5f22631d 213 result= disk_read(buffer,block_number);
chaithanyarss 327:5a967a66c10f 214 } else if(sid==0x02) {
chaithanyarss 327:5a967a66c10f 215 if(!(FSC_OLD[2]<=fsc && fsc<=FSC_CURRENT[2])) {
ee12b079 195:6a549c0e9287 216 return 0x86;
ee12b079 195:6a549c0e9287 217 }
chaithanyarss 327:5a967a66c10f 218 block_number= SD_DATABLOCK_START[2]+(fsc%(SD_DATABLOCK_END[2] - SD_DATABLOCK_START[2]+1));
ee12b079 86:a26f5f22631d 219 result= disk_read(buffer,block_number);
chaithanyarss 327:5a967a66c10f 220 } else if(sid==0x03) {
chaithanyarss 327:5a967a66c10f 221 if(!(FSC_OLD[3]<=fsc && fsc<=FSC_CURRENT[3])) {
ee12b079 195:6a549c0e9287 222 return 0x86;
ee12b079 195:6a549c0e9287 223 }
chaithanyarss 327:5a967a66c10f 224 block_number= SD_DATABLOCK_START[3] +(fsc%(SD_DATABLOCK_END[3] - SD_DATABLOCK_START[3] +1));
ee12b079 86:a26f5f22631d 225 result= disk_read(buffer,block_number);
chaithanyarss 327:5a967a66c10f 226 } else if(sid==0x04) {
chaithanyarss 327:5a967a66c10f 227 if(!(FSC_OLD[4]<=fsc && fsc<=FSC_CURRENT[4])) {
ee12b079 195:6a549c0e9287 228 return 0x86;
ee12b079 195:6a549c0e9287 229 }
chaithanyarss 327:5a967a66c10f 230 block_number=SD_DATABLOCK_START[4] +(fsc%(SD_DATABLOCK_END[4] - SD_DATABLOCK_START[4] +1));
ee12b079 86:a26f5f22631d 231 result= disk_read(buffer,block_number);
chaithanyarss 327:5a967a66c10f 232 } else if(sid==0x05) {
chaithanyarss 327:5a967a66c10f 233 if(!(FSC_OLD[5]<=fsc && fsc<=FSC_CURRENT[5])) {
ee12b079 195:6a549c0e9287 234 return 0x86;
ee12b079 195:6a549c0e9287 235 }
chaithanyarss 327:5a967a66c10f 236 block_number= SD_DATABLOCK_START[5] +(fsc%(SD_DATABLOCK_START[5] - SD_DATABLOCK_START[5] +1));
ee12b079 86:a26f5f22631d 237 result= disk_read(buffer,block_number);
chaithanyarss 327:5a967a66c10f 238 } else {
ee12b079 195:6a549c0e9287 239 return 0x02;
ee12b079 86:a26f5f22631d 240 }
ee12b079 195:6a549c0e9287 241 if(result == 0)
ee12b079 195:6a549c0e9287 242 return 0xA0;
ee12b079 195:6a549c0e9287 243 else
spacelab 308:7c3872365d58 244 return 0x88;
chaithanyarss 304:7cc4fe191a54 245 return 0xA0;
ee12b079 86:a26f5f22631d 246 }
ee12b079 86:a26f5f22631d 247
ee12b079 86:a26f5f22631d 248
ee12b079 86:a26f5f22631d 249 int initialise_card()
ee12b079 86:a26f5f22631d 250 {
ee12b079 86:a26f5f22631d 251 // Set to 100kHz for initialisation, and clock card with cs_sd = 1
chaithanyarss 327:5a967a66c10f 252 spi.frequency(100000); // changed on 31 12 2015 to 1 MHz
ee12b079 86:a26f5f22631d 253 cs_sd = 1;
ee12b079 86:a26f5f22631d 254 for (int i = 0; i < 16; i++) {
ee12b079 86:a26f5f22631d 255 spi.write(0xFF);
ee12b079 86:a26f5f22631d 256 }
ee12b079 194:ab528911780d 257 uint8_t R1_response = cmd(0,0);
ee12b079 194:ab528911780d 258 gPC.printf("0x%02X",R1_response);
ee12b079 86:a26f5f22631d 259 // send CMD0, should return with all zeros except IDLE STATE set (bit 0)
ee12b079 194:ab528911780d 260 if (R1_response != R1_IDLE_STATE) {
ee12b079 86:a26f5f22631d 261 debug("No disk, or could not put SD card in to spi idle state\r\n");
ee12b079 86:a26f5f22631d 262 return SDCARD_FAIL;
chaithanyarss 327:5a967a66c10f 263 } else
chaithanyarss 327:5a967a66c10f 264 gPC.puts("SD Card is in IDLE state\n\r");
ee12b079 86:a26f5f22631d 265
ee12b079 194:ab528911780d 266 // send CMD8 to determine whther it is ver 2.x
ee12b079 86:a26f5f22631d 267 int r = cmd8();
ee12b079 86:a26f5f22631d 268 if (r == R1_IDLE_STATE) {
ee12b079 194:ab528911780d 269 gPC.puts("Entering V2\r");
ee12b079 194:ab528911780d 270 int q = initialise_card_v2();
ee12b079 194:ab528911780d 271 return q;
ee12b079 86:a26f5f22631d 272
ee12b079 86:a26f5f22631d 273 } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
ee12b079 194:ab528911780d 274 gPC.puts("Entering V1");
ee12b079 86:a26f5f22631d 275 return initialise_card_v1();
ee12b079 86:a26f5f22631d 276
ee12b079 86:a26f5f22631d 277 } else {
ee12b079 86:a26f5f22631d 278 debug("\rNot in idle state after sending CMD8 (not an SD card?)\r\n");
ee12b079 86:a26f5f22631d 279 return SDCARD_FAIL;
ee12b079 86:a26f5f22631d 280 }
ee12b079 86:a26f5f22631d 281 }
ee12b079 86:a26f5f22631d 282
ee12b079 86:a26f5f22631d 283 int initialise_card_v1()
ee12b079 86:a26f5f22631d 284 {
ee12b079 86:a26f5f22631d 285 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
ee12b079 86:a26f5f22631d 286 cmd(55, 0);
ee12b079 86:a26f5f22631d 287 if (cmd(41, 0) == 0) {
ee12b079 96:4ca92f9775e0 288 gPC.puts("\rv1 initialization successfull\r\n");
ee12b079 86:a26f5f22631d 289 cdv = 512;
ee12b079 86:a26f5f22631d 290 debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r");
ee12b079 86:a26f5f22631d 291 return SDCARD_V1;
ee12b079 86:a26f5f22631d 292 }
ee12b079 86:a26f5f22631d 293 }
ee12b079 86:a26f5f22631d 294
ee12b079 86:a26f5f22631d 295 debug("\rTimeout waiting for v1.x card\r\n");
ee12b079 86:a26f5f22631d 296 return SDCARD_FAIL;
ee12b079 86:a26f5f22631d 297 }
ee12b079 86:a26f5f22631d 298
ee12b079 86:a26f5f22631d 299
ee12b079 86:a26f5f22631d 300 int initialise_card_v2()
ee12b079 86:a26f5f22631d 301 {
ee12b079 86:a26f5f22631d 302 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
ee12b079 86:a26f5f22631d 303 wait_ms(50);
ee12b079 86:a26f5f22631d 304 cmd58();
ee12b079 86:a26f5f22631d 305 cmd(55, 0);
ee12b079 86:a26f5f22631d 306 if (cmd(41, 0x40000000) == 0) {
ee12b079 194:ab528911780d 307 if (DEBUG)
ee12b079 194:ab528911780d 308 gPC.puts("\rv2 initialization successfull\r\n");
ee12b079 86:a26f5f22631d 309 cmd58();
ee12b079 86:a26f5f22631d 310 debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r");
ee12b079 86:a26f5f22631d 311 cdv = 1;
ee12b079 322:7d906d34aaff 312 FCTN_SD_MNGR();
ee12b079 86:a26f5f22631d 313 return SDCARD_V2;
ee12b079 86:a26f5f22631d 314 }
ee12b079 86:a26f5f22631d 315 }
ee12b079 86:a26f5f22631d 316
ee12b079 86:a26f5f22631d 317 debug("\rTimeout waiting for v2.x card\r\n");
ee12b079 86:a26f5f22631d 318 return SDCARD_FAIL;
ee12b079 86:a26f5f22631d 319 }
ee12b079 86:a26f5f22631d 320
ee12b079 86:a26f5f22631d 321 int cmd(int cmd, int arg)
ee12b079 86:a26f5f22631d 322 {
ee12b079 86:a26f5f22631d 323 cs_sd = 0;
ee12b079 86:a26f5f22631d 324
ee12b079 86:a26f5f22631d 325 // send a command
ee12b079 86:a26f5f22631d 326 spi.write(0x40 | cmd);
ee12b079 86:a26f5f22631d 327 spi.write(arg >> 24);
ee12b079 86:a26f5f22631d 328 spi.write(arg >> 16);
ee12b079 86:a26f5f22631d 329 spi.write(arg >> 8);
ee12b079 86:a26f5f22631d 330 spi.write(arg >> 0);
ee12b079 86:a26f5f22631d 331 spi.write(0x95);
ee12b079 86:a26f5f22631d 332
ee12b079 86:a26f5f22631d 333 // wait for the repsonse (response[7] == 0)
ee12b079 86:a26f5f22631d 334 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
ee12b079 86:a26f5f22631d 335 int response = spi.write(0xFF);
ee12b079 86:a26f5f22631d 336 if (!(response & 0x80)) {
ee12b079 86:a26f5f22631d 337 cs_sd = 1;
ee12b079 86:a26f5f22631d 338 spi.write(0xFF);
ee12b079 86:a26f5f22631d 339 return response;
ee12b079 86:a26f5f22631d 340 }
ee12b079 86:a26f5f22631d 341 }
ee12b079 86:a26f5f22631d 342 cs_sd = 1;
ee12b079 86:a26f5f22631d 343 spi.write(0xFF);
ee12b079 86:a26f5f22631d 344 return -1; // timeout
ee12b079 86:a26f5f22631d 345 }
ee12b079 86:a26f5f22631d 346
ee12b079 86:a26f5f22631d 347
ee12b079 86:a26f5f22631d 348 int cmd58()
ee12b079 86:a26f5f22631d 349 {
ee12b079 86:a26f5f22631d 350 cs_sd = 0;
ee12b079 86:a26f5f22631d 351 int arg = 0;
ee12b079 86:a26f5f22631d 352
ee12b079 86:a26f5f22631d 353 // send a command
ee12b079 86:a26f5f22631d 354 spi.write(0x40 | 58);
ee12b079 86:a26f5f22631d 355 spi.write(arg >> 24);
ee12b079 86:a26f5f22631d 356 spi.write(arg >> 16);
ee12b079 86:a26f5f22631d 357 spi.write(arg >> 8);
ee12b079 86:a26f5f22631d 358 spi.write(arg >> 0);
ee12b079 86:a26f5f22631d 359 spi.write(0x95);
ee12b079 86:a26f5f22631d 360
ee12b079 86:a26f5f22631d 361 // wait for the repsonse (response[7] == 0)
ee12b079 86:a26f5f22631d 362 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
ee12b079 86:a26f5f22631d 363 int response = spi.write(0xFF);
ee12b079 86:a26f5f22631d 364 if (!(response & 0x80)) {
ee12b079 86:a26f5f22631d 365 int ocr = spi.write(0xFF) << 24;
ee12b079 86:a26f5f22631d 366 ocr |= spi.write(0xFF) << 16;
ee12b079 86:a26f5f22631d 367 ocr |= spi.write(0xFF) << 8;
ee12b079 86:a26f5f22631d 368 ocr |= spi.write(0xFF) << 0;
ee12b079 86:a26f5f22631d 369 cs_sd = 1;
ee12b079 86:a26f5f22631d 370 spi.write(0xFF);
ee12b079 86:a26f5f22631d 371 return response;
ee12b079 86:a26f5f22631d 372 }
ee12b079 86:a26f5f22631d 373 }
ee12b079 86:a26f5f22631d 374 cs_sd = 1;
ee12b079 86:a26f5f22631d 375 spi.write(0xFF);
ee12b079 86:a26f5f22631d 376 return -1; // timeout
ee12b079 86:a26f5f22631d 377 }
ee12b079 86:a26f5f22631d 378
ee12b079 86:a26f5f22631d 379
ee12b079 86:a26f5f22631d 380 int cmd8()
ee12b079 86:a26f5f22631d 381 {
ee12b079 86:a26f5f22631d 382 cs_sd = 0;
ee12b079 86:a26f5f22631d 383
ee12b079 86:a26f5f22631d 384 // send a command
ee12b079 86:a26f5f22631d 385 spi.write(0x40 | 8); // CMD8
ee12b079 86:a26f5f22631d 386 spi.write(0x00); // reserved
ee12b079 86:a26f5f22631d 387 spi.write(0x00); // reserved
ee12b079 86:a26f5f22631d 388 spi.write(0x01); // 3.3v
ee12b079 86:a26f5f22631d 389 spi.write(0xAA); // check pattern
ee12b079 86:a26f5f22631d 390 spi.write(0x87); // crc
ee12b079 86:a26f5f22631d 391
ee12b079 86:a26f5f22631d 392 // wait for the repsonse (response[7] == 0)
ee12b079 86:a26f5f22631d 393 for (int i = 0; i < SD_COMMAND_TIMEOUT * 1000; i++) {
ee12b079 86:a26f5f22631d 394 char response[5];
ee12b079 86:a26f5f22631d 395 response[0] = spi.write(0xFF);
ee12b079 86:a26f5f22631d 396 if (!(response[0] & 0x80)) {
ee12b079 86:a26f5f22631d 397 for (int j = 1; j < 5; j++) {
ee12b079 86:a26f5f22631d 398 response[i] = spi.write(0xFF);
ee12b079 86:a26f5f22631d 399 }
ee12b079 86:a26f5f22631d 400 cs_sd = 1;
ee12b079 86:a26f5f22631d 401 spi.write(0xFF);
ee12b079 86:a26f5f22631d 402 return response[0];
ee12b079 86:a26f5f22631d 403 }
ee12b079 86:a26f5f22631d 404 }
ee12b079 86:a26f5f22631d 405 cs_sd = 1;
ee12b079 86:a26f5f22631d 406 spi.write(0xFF);
ee12b079 86:a26f5f22631d 407 return -1; // timeout
ee12b079 86:a26f5f22631d 408 }
ee12b079 86:a26f5f22631d 409
ee12b079 86:a26f5f22631d 410 uint64_t sd_sectors()
ee12b079 86:a26f5f22631d 411 {
ee12b079 86:a26f5f22631d 412 uint32_t c_size, c_size_mult, read_bl_len;
ee12b079 86:a26f5f22631d 413 uint32_t block_len, mult, blocknr, capacity;
ee12b079 86:a26f5f22631d 414 uint32_t hc_c_size;
ee12b079 86:a26f5f22631d 415 uint64_t blocks;
ee12b079 86:a26f5f22631d 416
ee12b079 86:a26f5f22631d 417 // CMD9, Response R2 (R1 byte + 16-byte block read)
ee12b079 86:a26f5f22631d 418 if (cmdx(9, 0) != 0) {
ee12b079 86:a26f5f22631d 419 debug("\rDidn't get a response from the disk\n");
ee12b079 86:a26f5f22631d 420 return 0;
ee12b079 86:a26f5f22631d 421 }
ee12b079 86:a26f5f22631d 422
ee12b079 86:a26f5f22631d 423 uint8_t cs_sdd[16];
ee12b079 86:a26f5f22631d 424 if (read(cs_sdd, 16) != 0) {
ee12b079 86:a26f5f22631d 425 debug("\rCouldn't read cs_sdd response from disk\n");
ee12b079 86:a26f5f22631d 426 return 0;
ee12b079 86:a26f5f22631d 427 }
ee12b079 86:a26f5f22631d 428
ee12b079 86:a26f5f22631d 429 // cs_sdd_structure : cs_sdd[127:126]
ee12b079 86:a26f5f22631d 430 // c_size : cs_sdd[73:62]
ee12b079 86:a26f5f22631d 431 // c_size_mult : cs_sdd[49:47]
ee12b079 86:a26f5f22631d 432 // read_bl_len : cs_sdd[83:80] - the *maximum* read block length
ee12b079 86:a26f5f22631d 433
ee12b079 86:a26f5f22631d 434 int cs_sdd_structure = ext_bits(cs_sdd, 127, 126);
ee12b079 86:a26f5f22631d 435
ee12b079 86:a26f5f22631d 436 switch (cs_sdd_structure) {
ee12b079 86:a26f5f22631d 437 case 0:
ee12b079 86:a26f5f22631d 438 cdv = 512;
ee12b079 86:a26f5f22631d 439 c_size = ext_bits(cs_sdd, 73, 62);
ee12b079 86:a26f5f22631d 440 c_size_mult = ext_bits(cs_sdd, 49, 47);
ee12b079 86:a26f5f22631d 441 read_bl_len = ext_bits(cs_sdd, 83, 80);
ee12b079 86:a26f5f22631d 442
ee12b079 86:a26f5f22631d 443 block_len = 1 << read_bl_len;
ee12b079 86:a26f5f22631d 444 mult = 1 << (c_size_mult + 2);
ee12b079 86:a26f5f22631d 445 blocknr = (c_size + 1) * mult;
ee12b079 86:a26f5f22631d 446 capacity = blocknr * block_len;
ee12b079 86:a26f5f22631d 447 blocks = capacity / 512;
ee12b079 86:a26f5f22631d 448 debug_if(SD_DBG, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks);
ee12b079 86:a26f5f22631d 449 break;
ee12b079 86:a26f5f22631d 450
ee12b079 86:a26f5f22631d 451 case 1:
ee12b079 86:a26f5f22631d 452 cdv = 1;
ee12b079 86:a26f5f22631d 453 hc_c_size = ext_bits(cs_sdd, 63, 48);
ee12b079 86:a26f5f22631d 454 blocks = (hc_c_size+1)*1024;
ee12b079 86:a26f5f22631d 455 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);
ee12b079 86:a26f5f22631d 456 break;
ee12b079 86:a26f5f22631d 457
ee12b079 86:a26f5f22631d 458 default:
ee12b079 86:a26f5f22631d 459 debug("cs_sdD struct unsupported\r\n");
ee12b079 86:a26f5f22631d 460 return 0;
ee12b079 86:a26f5f22631d 461 };
ee12b079 86:a26f5f22631d 462 return blocks;
ee12b079 86:a26f5f22631d 463 }
ee12b079 86:a26f5f22631d 464
ee12b079 86:a26f5f22631d 465 int cmdx(int cmd, int arg)
ee12b079 86:a26f5f22631d 466 {
ee12b079 86:a26f5f22631d 467 cs_sd = 0;
ee12b079 86:a26f5f22631d 468
ee12b079 86:a26f5f22631d 469 // send a command
ee12b079 86:a26f5f22631d 470 spi.write(0x40 | cmd);
ee12b079 86:a26f5f22631d 471 spi.write(arg >> 24);
ee12b079 86:a26f5f22631d 472 spi.write(arg >> 16);
ee12b079 86:a26f5f22631d 473 spi.write(arg >> 8);
ee12b079 86:a26f5f22631d 474 spi.write(arg >> 0);
ee12b079 86:a26f5f22631d 475 spi.write(0x95);
ee12b079 86:a26f5f22631d 476
ee12b079 86:a26f5f22631d 477 // wait for the repsonse (response[7] == 0)
ee12b079 86:a26f5f22631d 478 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) {
ee12b079 86:a26f5f22631d 479 int response = spi.write(0xFF);
ee12b079 86:a26f5f22631d 480 if (!(response & 0x80)) {
ee12b079 86:a26f5f22631d 481 return response;
ee12b079 86:a26f5f22631d 482 }
ee12b079 86:a26f5f22631d 483 }
ee12b079 86:a26f5f22631d 484 cs_sd = 1;
ee12b079 86:a26f5f22631d 485 spi.write(0xFF);
ee12b079 194:ab528911780d 486 return 1; // timeout
ee12b079 86:a26f5f22631d 487 }
ee12b079 86:a26f5f22631d 488
ee12b079 86:a26f5f22631d 489 static uint32_t ext_bits(unsigned char *data, int msb, int lsb)
ee12b079 86:a26f5f22631d 490 {
ee12b079 86:a26f5f22631d 491 uint32_t bits = 0;
ee12b079 86:a26f5f22631d 492 uint32_t size = 1 + msb - lsb;
ee12b079 86:a26f5f22631d 493 for (int i = 0; i < size; i++) {
ee12b079 86:a26f5f22631d 494 uint32_t position = lsb + i;
ee12b079 86:a26f5f22631d 495 uint32_t byte = 15 - (position >> 3);
ee12b079 86:a26f5f22631d 496 uint32_t bit = position & 0x7;
ee12b079 86:a26f5f22631d 497 uint32_t value = (data[byte] >> bit) & 1;
ee12b079 86:a26f5f22631d 498 bits |= value << i;
ee12b079 86:a26f5f22631d 499 }
ee12b079 86:a26f5f22631d 500 return bits;
ee12b079 86:a26f5f22631d 501 }
ee12b079 86:a26f5f22631d 502
ee12b079 86:a26f5f22631d 503 int disk_write(const uint8_t *buffer, uint64_t block_number)
ee12b079 86:a26f5f22631d 504
ee12b079 86:a26f5f22631d 505 {
ee12b079 86:a26f5f22631d 506 // set write address for single block (CMD24)
ee12b079 86:a26f5f22631d 507 if (cmd(24, block_number * cdv) != 0) {
chaithanyarss 303:b49b486a7107 508 CDMS_WR_SD_FAULT_COUNTER++;
ee12b079 86:a26f5f22631d 509 return 1;
ee12b079 86:a26f5f22631d 510 }
chaithanyarss 327:5a967a66c10f 511
ee12b079 209:63e9c8f8b5d2 512 uint64_t temp;
ee12b079 209:63e9c8f8b5d2 513 int r = write(buffer, 512);
chaithanyarss 327:5a967a66c10f 514 if(r == 0 ) {
ee12b079 209:63e9c8f8b5d2 515 temp = FCTN_CDMS_RD_RTC();
spacelab 313:5d06a08baf0e 516 TIME_LATEST_SD_WR = temp >> 7; //corrected by samp:TIME_LATEST_SD_WRD = temp >> 7;
ee12b079 209:63e9c8f8b5d2 517 }
ee12b079 209:63e9c8f8b5d2 518 return r;
ee12b079 86:a26f5f22631d 519 }
ee12b079 86:a26f5f22631d 520
ee12b079 86:a26f5f22631d 521 int write(const uint8_t*buffer, uint32_t length)
ee12b079 86:a26f5f22631d 522 {
ee12b079 86:a26f5f22631d 523 cs_sd = 0;
ee12b079 86:a26f5f22631d 524
ee12b079 86:a26f5f22631d 525 // indicate start of block
ee12b079 86:a26f5f22631d 526 spi.write(0xFE);
ee12b079 86:a26f5f22631d 527
ee12b079 86:a26f5f22631d 528 // write the data
ee12b079 86:a26f5f22631d 529 for (int i = 0; i < length; i++) {
ee12b079 86:a26f5f22631d 530 spi.write(buffer[i]);
ee12b079 86:a26f5f22631d 531 }
ee12b079 86:a26f5f22631d 532
ee12b079 86:a26f5f22631d 533 // write the checksum
ee12b079 86:a26f5f22631d 534 spi.write(0xFF);
ee12b079 86:a26f5f22631d 535 spi.write(0xFF);
ee12b079 86:a26f5f22631d 536
ee12b079 86:a26f5f22631d 537 // check the response token
ee12b079 86:a26f5f22631d 538 if ((spi.write(0xFF) & 0x1F) != 0x05) {
ee12b079 86:a26f5f22631d 539 cs_sd = 1;
ee12b079 86:a26f5f22631d 540 spi.write(0xFF);
chaithanyarss 303:b49b486a7107 541 CDMS_WR_SD_FAULT_COUNTER++;
ee12b079 86:a26f5f22631d 542 return 1;
ee12b079 86:a26f5f22631d 543 }
ee12b079 86:a26f5f22631d 544
ee12b079 86:a26f5f22631d 545 // wait for write to finish
ee12b079 86:a26f5f22631d 546 while (spi.write(0xFF) == 0);
ee12b079 86:a26f5f22631d 547
ee12b079 86:a26f5f22631d 548 cs_sd = 1;
ee12b079 86:a26f5f22631d 549 spi.write(0xFF);
ee12b079 86:a26f5f22631d 550 return 0;
ee12b079 86:a26f5f22631d 551 }
ee12b079 86:a26f5f22631d 552
ee12b079 86:a26f5f22631d 553 int disk_read(uint8_t *buffer, uint64_t block_number)
ee12b079 86:a26f5f22631d 554 {
ee12b079 86:a26f5f22631d 555 // set read address for single block (CMD17)
ee12b079 86:a26f5f22631d 556 if (cmd(17, block_number * cdv) != 0) {
ee12b079 209:63e9c8f8b5d2 557 SD_RD_ERROR = 1;
ee12b079 86:a26f5f22631d 558 return 1;
ee12b079 86:a26f5f22631d 559 }
ee12b079 86:a26f5f22631d 560
ee12b079 86:a26f5f22631d 561 // receive the data
ee12b079 86:a26f5f22631d 562 read(buffer, 512);
ee12b079 209:63e9c8f8b5d2 563 uint64_t temp = FCTN_CDMS_RD_RTC();
ee12b079 209:63e9c8f8b5d2 564 TIME_LATEST_SD_RD = temp >> 7;
ee12b079 86:a26f5f22631d 565 return 0;
ee12b079 86:a26f5f22631d 566 }
ee12b079 86:a26f5f22631d 567
ee12b079 86:a26f5f22631d 568 int read(uint8_t *buffer, uint32_t length)
ee12b079 86:a26f5f22631d 569 {
ee12b079 86:a26f5f22631d 570 cs_sd = 0;
ee12b079 86:a26f5f22631d 571
ee12b079 86:a26f5f22631d 572 // read until start byte (0xFF)
ee12b079 86:a26f5f22631d 573 while (spi.write(0xFF) != 0xFE);
ee12b079 86:a26f5f22631d 574
ee12b079 86:a26f5f22631d 575 // read data
ee12b079 86:a26f5f22631d 576 for (int i = 0; i < length; i++) {
ee12b079 86:a26f5f22631d 577 buffer[i] = spi.write(0xFF);
ee12b079 86:a26f5f22631d 578 }
ee12b079 86:a26f5f22631d 579 spi.write(0xFF); // checksum
ee12b079 86:a26f5f22631d 580 spi.write(0xFF);
ee12b079 86:a26f5f22631d 581
ee12b079 86:a26f5f22631d 582 cs_sd = 1;
ee12b079 86:a26f5f22631d 583 spi.write(0xFF);
ee12b079 86:a26f5f22631d 584 return 0;
ee12b079 86:a26f5f22631d 585 }
ee12b079 86:a26f5f22631d 586
ee12b079 86:a26f5f22631d 587 int disk_erase(int startBlock, int totalBlocks)
ee12b079 86:a26f5f22631d 588 {
ee12b079 86:a26f5f22631d 589 if(cmd(32, startBlock * cdv) != 0) {
ee12b079 86:a26f5f22631d 590 return 1;
ee12b079 86:a26f5f22631d 591 }
ee12b079 86:a26f5f22631d 592 if (cmd(33, (startBlock+totalBlocks-1) * cdv) != 0) {
ee12b079 86:a26f5f22631d 593 return 1;
ee12b079 86:a26f5f22631d 594 }
ee12b079 86:a26f5f22631d 595 if (cmd(38,0) != 0) {
ee12b079 86:a26f5f22631d 596 return 1;
ee12b079 86:a26f5f22631d 597 }
chaithanyarss 327:5a967a66c10f 598
ee12b079 86:a26f5f22631d 599 return 0; //normal return
ee12b079 209:63e9c8f8b5d2 600 }
ee12b079 209:63e9c8f8b5d2 601
ee12b079 209:63e9c8f8b5d2 602 int disk_read_statusbits(uint8_t *buffer)
ee12b079 209:63e9c8f8b5d2 603 {
ee12b079 209:63e9c8f8b5d2 604 if (cmd(17, 0) != 0) {
chaithanyarss 327:5a967a66c10f 605 SD_RD_ERROR = 1;
ee12b079 209:63e9c8f8b5d2 606 return -1;
ee12b079 209:63e9c8f8b5d2 607 }
ee12b079 209:63e9c8f8b5d2 608
ee12b079 209:63e9c8f8b5d2 609 // receive the data
ee12b079 209:63e9c8f8b5d2 610 return read(buffer,64);
chaithanyarss 328:2242ebc71be8 611 }
chaithanyarss 328:2242ebc71be8 612
chaithanyarss 328:2242ebc71be8 613 #endif