CDMS code for testing sbc

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE by shubham c

Committer:
ee12b079
Date:
Wed Jun 29 14:24:12 2016 +0000
Revision:
193:ab528911780d
Parent:
191:f76b475d7807
Child:
195:6a549c0e9287
Sdcard function updated

Who changed what in which revision?

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