CDMS_CODE_samp_23SEP_DMA_flag

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE_samp_23SEP_DMA by iitm sat

Committer:
chaithanyarss
Date:
Sun Jul 03 09:33:33 2016 +0000
Revision:
214:6848a51af734
Parent:
209:63e9c8f8b5d2
Child:
223:7c802e1ccd30
Reset CDMS added;

Who changed what in which revision?

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