Hi Shubham I am just using this to test PYLD code
Dependencies: FreescaleIAP SimpleDMA mbed-rtos mbed
Fork of COM_MNG_TMTC_SIMPLE by
cdms_sd.h
00001 00002 00003 00004 //SPI spi(PTE1, PTE3, PTE2); // MOSI,MISO, CLOCK microcontroller(in order) 00005 //DigitalOut cs_sd(PTE22); 00006 00007 //Serial sd1(USBTX,USBRX); 00008 00009 00010 #define SD_COMMAND_TIMEOUT 5000 00011 00012 #define SD_DBG 0 00013 00014 #define R1_IDLE_STATE (1 << 0) 00015 #define R1_ERASE_RESET (1 << 1) 00016 #define R1_ILLEGAL_COMMAND (1 << 2) 00017 #define R1_COM_CRC_ERROR (1 << 3) 00018 #define R1_ERASE_SEQUENCE_ERROR (1 << 4) 00019 #define R1_ADDRESS_ERROR (1 << 5) 00020 #define R1_PARAMETER_ERROR (1 << 6) 00021 00022 00023 00024 #define SD_MAX_CYCLES 10000 00025 00026 extern uint8_t SD_INIT_FLAGS; 00027 00028 00029 00030 int initialise_card(); 00031 int initialise_card_v1(); 00032 int initialise_card_v2(); 00033 int disk_write(const uint8_t *, uint64_t); 00034 int disk_read(uint8_t *, uint64_t); 00035 int disk_initialize(); 00036 int disk_erase(int,int); 00037 00038 //void FCTN_CDMS_INIT_SD(); 00039 //int FCTN_CDMS_WR_SD(const uint8_t *, uint64_t); 00040 //int FCTN_CDMS_RD_SD(uint8_t *, uint64_t); 00041 uint32_t FCTN_SD_MNGR(uint8_t); 00042 00043 00044 int cmd(int, int); 00045 int cmd58(); 00046 int cmdx(int, int); 00047 int cmd8(); 00048 int read(uint8_t*, uint32_t ); 00049 int write(const uint8_t*, uint32_t ); 00050 static uint32_t ext_bits(unsigned char *, int , int ); 00051 int SD_WRITE(uint8_t*,uint32_t,uint8_t); 00052 void FCTN_CDMS_SD_INIT(); 00053 int SD_READ(uint8_t*,uint32_t,uint8_t); 00054 #define SDCARD_FAIL 0 00055 #define SDCARD_V1 1 00056 #define SDCARD_V2 2 00057 #define SDCARD_V2HC 3 00058 00059 00060 00061 00062 00063 00064 00065 00066 00067 00068 00069 int cdv; 00070 uint64_t sd_sectors(); 00071 uint64_t sectors; 00072 00073 void FCTN_CDMS_SD_INIT() 00074 { 00075 00076 initialise_card(); 00077 00078 disk_initialize(); 00079 00080 } 00081 00082 00083 00084 uint32_t FCTN_SD_MNGR(uint8_t sid) 00085 00086 { 00087 00088 uint32_t SD_MNG_SECT=7000; 00089 00090 uint32_t fsc; 00091 uint8_t buffer[512]; 00092 int b=disk_read(buffer, SD_MNG_SECT); 00093 if(sid==0x0) 00094 { 00095 00096 fsc=(uint32_t)(buffer[0]<<24)+(uint32_t)(buffer[1]<<16)+(uint32_t)(buffer[2]<<8)+(uint32_t)buffer[3]; 00097 uint32_t next_fsc=fsc+1; 00098 buffer[0]=(uint8_t) (next_fsc>>24 & 0xFF); 00099 buffer[1]=(uint8_t) (next_fsc>>16 & 0xFF); 00100 buffer[2]=(uint8_t) (next_fsc>>8 & 0xFF); 00101 buffer[3]=(uint8_t) (next_fsc & 0xFF); 00102 buffer[511]+=2; 00103 disk_write(buffer,SD_MNG_SECT); 00104 } 00105 if(sid==0x1) 00106 { 00107 fsc=(uint32_t)(buffer[4]<<24)+(uint32_t)(buffer[5]<<16)+(uint32_t)(buffer[6]<<8)+(uint32_t)buffer[7]; 00108 uint32_t next_fsc=fsc+1; 00109 buffer[4]=(uint8_t) (next_fsc>>24 & 0xFF); 00110 buffer[5]=(uint8_t) (next_fsc>>16 & 0xFF); 00111 buffer[6]=(uint8_t) (next_fsc>>8 & 0xFF); 00112 buffer[7]=(uint8_t) (next_fsc & 0xFF); 00113 buffer[511]+=2; 00114 disk_write(buffer,SD_MNG_SECT); 00115 } 00116 if(sid==0x2) 00117 { 00118 fsc=(uint32_t)(buffer[8]<<24)+(uint32_t)(buffer[9]<<16)+(uint32_t)(buffer[10]<<8)+(uint32_t)buffer[11]; 00119 uint32_t next_fsc=fsc+1; 00120 buffer[8]=(uint8_t) (next_fsc>>24 & 0xFF); 00121 buffer[9]=(uint8_t) (next_fsc>>16 & 0xFF); 00122 buffer[10]=(uint8_t) (next_fsc>>8 & 0xFF); 00123 buffer[11]=(uint8_t) (next_fsc & 0xFF); 00124 buffer[511]+=2; 00125 disk_write(buffer,SD_MNG_SECT); 00126 } 00127 if(sid==0x3) 00128 { 00129 fsc=(uint32_t)(buffer[12]<<24)+(uint32_t)(buffer[13]<<16)+(uint32_t)(buffer[14]<<8)+(uint32_t)buffer[15]; 00130 uint32_t next_fsc=fsc+1; 00131 buffer[12]=(uint8_t) (next_fsc>>24 & 0xFF); 00132 buffer[13]=(uint8_t) (next_fsc>>16 & 0xFF); 00133 buffer[14]=(uint8_t) (next_fsc>>8 & 0xFF); 00134 buffer[15]=(uint8_t) (next_fsc & 0xFF); 00135 buffer[511]+=2; 00136 disk_write(buffer,SD_MNG_SECT); 00137 } 00138 if(sid==0x4) 00139 { 00140 fsc=(uint32_t)(buffer[16]<<24)+(uint32_t)(buffer[17]<<16)+(uint32_t)(buffer[18]<<8)+(uint32_t)buffer[19]; 00141 uint32_t next_fsc=fsc+1; 00142 buffer[16]=(uint8_t) (next_fsc>>24 & 0xFF); 00143 buffer[17]=(uint8_t) (next_fsc>>16 & 0xFF); 00144 buffer[18]=(uint8_t) (next_fsc>>8 & 0xFF); 00145 buffer[19]=(uint8_t) (next_fsc & 0xFF); 00146 buffer[511]+=2; 00147 disk_write(buffer,SD_MNG_SECT); 00148 } 00149 return fsc; 00150 } 00151 00152 00153 int SD_WRITE(uint8_t* buffer,uint32_t fsc,uint8_t sid) 00154 { 00155 00156 uint32_t SD_SCP_FIRST=1001; 00157 uint32_t SD_SCP_LAST=2000; 00158 uint32_t SD_SFF_AT_FIRST=2001; 00159 uint32_t SD_SFF_AT_LAST = 3000; 00160 uint32_t SD_SFF_BT_FIRST =3001; 00161 uint32_t SD_SFF_BT_LAST=4000; 00162 uint32_t SD_HK_ARCH_FIRST=4001; 00163 uint32_t SD_HK_ARCH_LAST= 5000; 00164 uint32_t LOG_FIRST =5001; 00165 uint32_t LOG_LAST=6000; 00166 uint32_t SD_MNG_SECT=7000; 00167 uint32_t block_number; 00168 int result; 00169 if(sid==0x0) 00170 { 00171 block_number=SD_SCP_FIRST+fsc; 00172 // printf("write_block_number=%d\r\n",block_number); 00173 result= disk_write(buffer,block_number); 00174 return result; 00175 } 00176 if(sid==0x1) 00177 { 00178 block_number=SD_SFF_AT_FIRST + fsc; 00179 result= disk_write(buffer,block_number); 00180 return result; 00181 } 00182 if(sid==0x2) 00183 { 00184 block_number=SD_SFF_BT_FIRST + fsc; 00185 result= disk_write(buffer,block_number); 00186 return result; 00187 } 00188 if(sid==0x3) 00189 { 00190 block_number=SD_HK_ARCH_FIRST+fsc; 00191 // sd1.printf("Block number is %d \r\n",block_number); 00192 result= disk_write(buffer,block_number); 00193 return result; 00194 } 00195 if(sid==0x4) 00196 { 00197 block_number=LOG_FIRST +fsc; 00198 result= disk_write(buffer,block_number); 00199 return result; 00200 } 00201 return 1; 00202 } 00203 00204 int SD_READ(uint8_t* buffer,uint32_t fsc,uint8_t sid) 00205 { 00206 00207 uint32_t SD_SCP_FIRST=1001; 00208 uint32_t SD_SCP_LAST=2000; 00209 uint32_t SD_SFF_AT_FIRST=2001; 00210 uint32_t SD_SFF_AT_LAST = 3000; 00211 uint32_t SD_SFF_BT_FIRST =3001; 00212 uint32_t SD_SFF_BT_LAST=4000; 00213 uint32_t SD_HK_ARCH_FIRST=4001; 00214 uint32_t SD_HK_ARCH_LAST= 5000; 00215 uint32_t LOG_FIRST =5001; 00216 uint32_t LOG_LAST=6000; 00217 uint32_t SD_MNG_SECT=7000; 00218 uint32_t block_number; 00219 int result; 00220 if(sid==0x0) 00221 { 00222 block_number=SD_SCP_FIRST + fsc; 00223 // sd1.printf("read_block_number=%d\r\n",block_number); 00224 result= disk_read(buffer,block_number); 00225 } 00226 else if(sid==0x1) 00227 { 00228 block_number=SD_SFF_AT_FIRST + fsc; 00229 result= disk_read(buffer,block_number); 00230 } 00231 else if(sid==0x2) 00232 { 00233 block_number=SD_SFF_BT_FIRST + fsc; 00234 result= disk_read(buffer,block_number); 00235 } 00236 else if(sid==0x3) 00237 { 00238 block_number=SD_HK_ARCH_FIRST + fsc; 00239 result= disk_read(buffer,block_number); 00240 } 00241 else if(sid==0x4) 00242 { 00243 block_number=LOG_FIRST +fsc; 00244 result= disk_read(buffer,block_number); 00245 } 00246 else 00247 { 00248 return 1; 00249 } 00250 return result; 00251 } 00252 00253 00254 int initialise_card() 00255 { 00256 // Set to 100kHz for initialisation, and clock card with cs_sd = 1 00257 spi.frequency(100000); // changed on 31 12 2015 to 1 MHz 00258 cs_sd = 1; 00259 for (int i = 0; i < 16; i++) { 00260 spi.write(0xFF); 00261 } 00262 00263 // send CMD0, should return with all zeros except IDLE STATE set (bit 0) 00264 if (cmd(0, 0) != R1_IDLE_STATE) { 00265 debug("No disk, or could not put SD card in to spi idle state\r\n"); 00266 return SDCARD_FAIL; 00267 } 00268 00269 // send CMD8 to determine whther it is ver 2.x 00270 int r = cmd8(); 00271 if (r == R1_IDLE_STATE) { 00272 // printf("\rEntering v2\r\n"); 00273 return initialise_card_v2(); 00274 00275 } else if (r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) { 00276 // printf("\rEntering v1\r\n"); 00277 return initialise_card_v1(); 00278 00279 } else { 00280 debug("\rNot in idle state after sending CMD8 (not an SD card?)\r\n"); 00281 return SDCARD_FAIL; 00282 } 00283 } 00284 00285 int initialise_card_v1() 00286 { 00287 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { 00288 cmd(55, 0); 00289 if (cmd(41, 0) == 0) { 00290 gPC.puts("\rv1 initialization successfull\r\n"); 00291 cdv = 512; 00292 debug_if(SD_DBG, "\n\rInit: SEDCARD_V1\n\r"); 00293 00294 return SDCARD_V1; 00295 } 00296 } 00297 00298 debug("\rTimeout waiting for v1.x card\r\n"); 00299 return SDCARD_FAIL; 00300 } 00301 00302 00303 int initialise_card_v2() 00304 { 00305 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { 00306 wait_ms(50); 00307 cmd58(); 00308 cmd(55, 0); 00309 if (cmd(41, 0x40000000) == 0) { 00310 if (DEBUG) 00311 gPC.puts("\rv2 initialization successfull\r\n"); 00312 cmd58(); 00313 debug_if(SD_DBG, "\n\rInit: SDCARD_V2\n\r"); 00314 cdv = 1; 00315 00316 return SDCARD_V2; 00317 } 00318 } 00319 00320 debug("\rTimeout waiting for v2.x card\r\n"); 00321 return SDCARD_FAIL; 00322 } 00323 00324 int cmd(int cmd, int arg) 00325 { 00326 cs_sd = 0; 00327 00328 // send a command 00329 spi.write(0x40 | cmd); 00330 spi.write(arg >> 24); 00331 spi.write(arg >> 16); 00332 spi.write(arg >> 8); 00333 spi.write(arg >> 0); 00334 spi.write(0x95); 00335 00336 // wait for the repsonse (response[7] == 0) 00337 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { 00338 int response = spi.write(0xFF); 00339 if (!(response & 0x80)) { 00340 cs_sd = 1; 00341 spi.write(0xFF); 00342 return response; 00343 } 00344 } 00345 cs_sd = 1; 00346 spi.write(0xFF); 00347 return -1; // timeout 00348 } 00349 00350 00351 int cmd58() 00352 { 00353 cs_sd = 0; 00354 int arg = 0; 00355 00356 // send a command 00357 spi.write(0x40 | 58); 00358 spi.write(arg >> 24); 00359 spi.write(arg >> 16); 00360 spi.write(arg >> 8); 00361 spi.write(arg >> 0); 00362 spi.write(0x95); 00363 00364 // wait for the repsonse (response[7] == 0) 00365 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { 00366 int response = spi.write(0xFF); 00367 if (!(response & 0x80)) { 00368 int ocr = spi.write(0xFF) << 24; 00369 ocr |= spi.write(0xFF) << 16; 00370 ocr |= spi.write(0xFF) << 8; 00371 ocr |= spi.write(0xFF) << 0; 00372 cs_sd = 1; 00373 spi.write(0xFF); 00374 return response; 00375 } 00376 } 00377 cs_sd = 1; 00378 spi.write(0xFF); 00379 return -1; // timeout 00380 } 00381 00382 00383 int cmd8() 00384 { 00385 cs_sd = 0; 00386 00387 // send a command 00388 spi.write(0x40 | 8); // CMD8 00389 spi.write(0x00); // reserved 00390 spi.write(0x00); // reserved 00391 spi.write(0x01); // 3.3v 00392 spi.write(0xAA); // check pattern 00393 spi.write(0x87); // crc 00394 00395 // wait for the repsonse (response[7] == 0) 00396 for (int i = 0; i < SD_COMMAND_TIMEOUT * 1000; i++) { 00397 char response[5]; 00398 response[0] = spi.write(0xFF); 00399 if (!(response[0] & 0x80)) { 00400 for (int j = 1; j < 5; j++) { 00401 response[i] = spi.write(0xFF); 00402 } 00403 cs_sd = 1; 00404 spi.write(0xFF); 00405 return response[0]; 00406 } 00407 } 00408 cs_sd = 1; 00409 spi.write(0xFF); 00410 return -1; // timeout 00411 } 00412 00413 uint64_t sd_sectors() 00414 { 00415 uint32_t c_size, c_size_mult, read_bl_len; 00416 uint32_t block_len, mult, blocknr, capacity; 00417 uint32_t hc_c_size; 00418 uint64_t blocks; 00419 00420 // CMD9, Response R2 (R1 byte + 16-byte block read) 00421 if (cmdx(9, 0) != 0) { 00422 debug("\rDidn't get a response from the disk\n"); 00423 return 0; 00424 } 00425 00426 uint8_t cs_sdd[16]; 00427 if (read(cs_sdd, 16) != 0) { 00428 debug("\rCouldn't read cs_sdd response from disk\n"); 00429 return 0; 00430 } 00431 00432 // cs_sdd_structure : cs_sdd[127:126] 00433 // c_size : cs_sdd[73:62] 00434 // c_size_mult : cs_sdd[49:47] 00435 // read_bl_len : cs_sdd[83:80] - the *maximum* read block length 00436 00437 int cs_sdd_structure = ext_bits(cs_sdd, 127, 126); 00438 00439 switch (cs_sdd_structure) { 00440 case 0: 00441 cdv = 512; 00442 c_size = ext_bits(cs_sdd, 73, 62); 00443 c_size_mult = ext_bits(cs_sdd, 49, 47); 00444 read_bl_len = ext_bits(cs_sdd, 83, 80); 00445 00446 block_len = 1 << read_bl_len; 00447 mult = 1 << (c_size_mult + 2); 00448 blocknr = (c_size + 1) * mult; 00449 capacity = blocknr * block_len; 00450 blocks = capacity / 512; 00451 debug_if(SD_DBG, "\n\rSDCard\n\rc_size: %d \n\rcapacity: %ld \n\rsectors: %lld\n\r", c_size, capacity, blocks); 00452 break; 00453 00454 case 1: 00455 cdv = 1; 00456 hc_c_size = ext_bits(cs_sdd, 63, 48); 00457 blocks = (hc_c_size+1)*1024; 00458 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); 00459 break; 00460 00461 default: 00462 debug("cs_sdD struct unsupported\r\n"); 00463 return 0; 00464 }; 00465 return blocks; 00466 } 00467 00468 int cmdx(int cmd, int arg) 00469 { 00470 cs_sd = 0; 00471 00472 // send a command 00473 spi.write(0x40 | cmd); 00474 spi.write(arg >> 24); 00475 spi.write(arg >> 16); 00476 spi.write(arg >> 8); 00477 spi.write(arg >> 0); 00478 spi.write(0x95); 00479 00480 // wait for the repsonse (response[7] == 0) 00481 for (int i = 0; i < SD_COMMAND_TIMEOUT; i++) { 00482 int response = spi.write(0xFF); 00483 if (!(response & 0x80)) { 00484 return response; 00485 } 00486 } 00487 cs_sd = 1; 00488 spi.write(0xFF); 00489 return -1; // timeout 00490 } 00491 00492 static uint32_t ext_bits(unsigned char *data, int msb, int lsb) 00493 { 00494 uint32_t bits = 0; 00495 uint32_t size = 1 + msb - lsb; 00496 for (int i = 0; i < size; i++) { 00497 uint32_t position = lsb + i; 00498 uint32_t byte = 15 - (position >> 3); 00499 uint32_t bit = position & 0x7; 00500 uint32_t value = (data[byte] >> bit) & 1; 00501 bits |= value << i; 00502 } 00503 return bits; 00504 } 00505 00506 int disk_initialize() 00507 { 00508 int i = initialise_card(); 00509 debug_if(SD_DBG, "init card = %d\n", i); 00510 sectors = sd_sectors(); 00511 00512 // Set block length to 512 (CMD16) 00513 if (cmd(16, 512) != 0) { 00514 debug("\rSet 512-byte block timed out\r\n"); 00515 return 1; 00516 } else { 00517 // printf("\rDisk initialization successfull\r\n"); 00518 } 00519 00520 spi.frequency(1000000); // Set to 1MHz for data transfer 00521 return 0; 00522 } 00523 00524 int disk_write(const uint8_t *buffer, uint64_t block_number) 00525 00526 { 00527 // set write address for single block (CMD24) 00528 if (cmd(24, block_number * cdv) != 0) { 00529 return 1; 00530 } 00531 00532 // send the data block 00533 write(buffer, 512); 00534 //printf("Written Successfully bro \n"); 00535 return 0; 00536 } 00537 00538 int write(const uint8_t*buffer, uint32_t length) 00539 { 00540 cs_sd = 0; 00541 00542 // indicate start of block 00543 spi.write(0xFE); 00544 00545 // write the data 00546 for (int i = 0; i < length; i++) { 00547 spi.write(buffer[i]); 00548 } 00549 00550 // write the checksum 00551 spi.write(0xFF); 00552 spi.write(0xFF); 00553 00554 // check the response token 00555 if ((spi.write(0xFF) & 0x1F) != 0x05) { 00556 cs_sd = 1; 00557 spi.write(0xFF); 00558 return 1; 00559 } 00560 00561 // wait for write to finish 00562 while (spi.write(0xFF) == 0); 00563 00564 cs_sd = 1; 00565 spi.write(0xFF); 00566 return 0; 00567 } 00568 00569 int disk_read(uint8_t *buffer, uint64_t block_number) 00570 { 00571 // set read address for single block (CMD17) 00572 if (cmd(17, block_number * cdv) != 0) { 00573 return 1; 00574 } 00575 00576 // receive the data 00577 read(buffer, 512); 00578 return 0; 00579 } 00580 00581 int read(uint8_t *buffer, uint32_t length) 00582 { 00583 cs_sd = 0; 00584 00585 // read until start byte (0xFF) 00586 while (spi.write(0xFF) != 0xFE); 00587 00588 // read data 00589 for (int i = 0; i < length; i++) { 00590 buffer[i] = spi.write(0xFF); 00591 } 00592 spi.write(0xFF); // checksum 00593 spi.write(0xFF); 00594 00595 cs_sd = 1; 00596 spi.write(0xFF); 00597 return 0; 00598 } 00599 00600 int disk_erase(int startBlock, int totalBlocks) 00601 { 00602 if(cmd(32, startBlock * cdv) != 0) { 00603 return 1; 00604 } 00605 if (cmd(33, (startBlock+totalBlocks-1) * cdv) != 0) { 00606 return 1; 00607 } 00608 if (cmd(38,0) != 0) { 00609 return 1; 00610 } 00611 00612 return 0; //normal return 00613 }
Generated on Wed Jul 13 2022 02:12:45 by 1.7.2