pl ack in tmtc

Dependencies:   FreescaleIAP SimpleDMA mbed-rtos mbed

Fork of COM_MNG_TMTC_SIMPLE_pl123 by shubham c

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cdms_sd.h Source File

cdms_sd.h

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