CDMS_CODE_samp_23SEP_DMA_flag

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of CDMS_CODE_samp_23SEP_DMA by iitm sat

Committer:
ee12b079
Date:
Fri Jul 01 17:41:48 2016 +0000
Revision:
203:424308159a56
Parent:
197:1369ef45b49e
Child:
209:63e9c8f8b5d2
obsrs algo checked for two blocks

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