FM

Dependencies:   SimpleDMA eeprom mbed-rtos mbed FreescaleIAP

Fork of CDMS_CODE by shubham c

Committer:
chaithanyarss
Date:
Sat Jan 07 00:40:47 2017 +0000
Revision:
339:5c4bda268e08
Parent:
328:2242ebc71be8
reverted sd card changes, removed tc print

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