CDMS code for testing sbc

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

Committer:
chaithanyarss
Date:
Thu Jun 30 14:00:33 2016 +0000
Revision:
206:fba4aeebf004
Parent:
194:5f75962ae7ea
Child:
207:28a07943dded
For integrating PL_Main with rest of CDMS code

Who changed what in which revision?

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