To fix the hang problem

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

Committer:
chaithanyarss
Date:
Fri Dec 16 08:53:12 2016 +0000
Revision:
327:5a967a66c10f
Parent:
326:e424f70b86c0
Child:
328:2242ebc71be8
Working on optimizing SD card code

Who changed what in which revision?

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