Latest FM Code with EEPROM

Dependencies:   FreescaleIAP SimpleDMA eeprom mbed-rtos mbed

Fork of CDMS_QM_03MAR2017_Flash_with_obsrs by Team Fox

Revision:
257:7d404e9dc9e2
Parent:
254:22d97475ca79
Child:
259:066a7abd2a5f
diff -r 22d97475ca79 -r 7d404e9dc9e2 COM_MNG_TMTC.h
--- a/COM_MNG_TMTC.h	Tue Jul 12 11:10:48 2016 +0000
+++ b/COM_MNG_TMTC.h	Tue Jul 12 17:07:17 2016 +0000
@@ -16,6 +16,25 @@
 //DigitalIn tm_status_4m_pl(PIN61); //I2C interrupt to CDMS from PL
 #define RESET_CDMS NVIC_SystemReset()
 
+#define BAE_TC{\
+    tc_ptr->next_TC = NULL;\
+    PUTshort_or_long(tc_ptr,SHORT_TC_CODE);\
+    PUTcrc_pass(tc_ptr,0x1);\
+    PUTexec_status(tc_ptr,0);\
+    tc_ptr->TC_string[0] = 0x01;\
+    tc_ptr->TC_string[1] = 0x60;\
+    tc_ptr->TC_string[2] = 0x81;\
+    tc_ptr->TC_string[3] = 0x21;\
+    tc_ptr->TC_string[4] = 0x00;\
+    tc_ptr->TC_string[5] = 0;\
+    tc_ptr->TC_string[6] = 0;\
+    tc_ptr->TC_string[7] = 0;\
+    tc_ptr->TC_string[8] = 0;\
+    uint16_t crc16 = crc16_gen(tc_ptr->TC_string, 9);\
+    tc_ptr->TC_string[9]  = (crc16 & 0xFF00)>>8;\
+    tc_ptr->TC_string[10] = (crc16 & 0x00FF);\
+}
+
 
 uint8_t received = 0;
 uint16_t mid1;
@@ -31,7 +50,7 @@
 uint8_t ACK_CODE;
 
 void TM_PMS_PL_SCHEDULE(Base_tm* tm_pointer1,uint8_t psc);
-void ACK_L234(Base_tm* tm_pointer_short1,uint8_t ackCode,uint8_t psc);
+void ACK_L234(Base_tm* tm_pointer1,uint8_t ackCode,uint8_t psc);
 
 #define delete_TC(tc_ptr) {\
     if(tc_ptr == gHEAD_NODE_TCL){\
@@ -201,19 +220,19 @@
 /*tm_ptr is the next_TM of a linked list, and should have the value NULL, i.e. tm_ptr should be the next_TM pointer of thte last node */
 
 // CDMS TEAM CODE START
-void ACK_L234(Base_tm* tm_pointer_short1,uint8_t ackCode,uint8_t psc)
+void ACK_L234(Base_tm* tm_pointer1,uint8_t ackCode,uint8_t psc)
 {
-    tm_pointer_short1->TM_string[0] = 0xB0; 
-    tm_pointer_short1->TM_string[1] = psc;
-    tm_pointer_short1->TM_string[2] = ackCode;
-    tm_pointer_short1->TM_string[3] = 0x00;
-    tm_pointer_short1->TM_string[4] = 0x00;
+    tm_pointer1->TM_string[0] = 0xB0; 
+    tm_pointer1->TM_string[1] = psc;
+    tm_pointer1->TM_string[2] = ackCode;
+    tm_pointer1->TM_string[3] = 0x00;
+    tm_pointer1->TM_string[4] = 0x00;
     for(uint8_t i=0;i<6;i++)
-        tm_pointer_short1->TM_string[i+5] = 0;
-    crc16 = crc16_gen(tm_pointer_short1->TM_string,11);
-    tm_pointer_short1->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-    tm_pointer_short1->TM_string[12] = (uint8_t)(crc16&0x00FF);
-    tm_pointer_short1->next_TM = NULL;                                                 
+        tm_pointer1->TM_string[i+5] = 0;
+    crc16 = crc16_gen(tm_pointer1->TM_string,11);
+    tm_pointer1->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
+    tm_pointer1->TM_string[12] = (uint8_t)(crc16&0x00FF);
+    tm_pointer1->next_TM = NULL;                                                 
 }
 void TM_PMS_PL_SCHEDULE(Base_tm *tm_pointer1,uint8_t psc)
 {
@@ -348,26 +367,23 @@
     //uint8_t ACKCODE = 0xB0;
     bool y;
     //gPC.printf("\rTC execution in progress\r\n");
-    Base_tm *tm_pointer = new Long_tm;
+    Base_tm *tm_pointer = new Base_tm;
     Base_tm *tm_ptr = tm_pointer;
-    //Base_tm *tm_ptr = new Long_tm;
-    Base_tm *tm_pointer_short = new Short_tm;
-    Base_tm *tm_ptr_short = tm_pointer_short;
     received = 0;
     switch(GETapid(tc_ptr))             //checking APID's
     {
         case 1: //apid=01 implies it corresponds to bae
         {
             BAE_I2C_mutex.lock();
-            //BAE_TC;
+            BAE_TC;
             gPC.printf("Telecommand is for BAE\r\n");
             gPC.printf("Sending TC to BAE...\r\n"); //interrupt to be sent to the bae
             y = FCTN_I2C_WRITE((char*)tc_ptr->TC_string,TC_LONG_SIZE);                         // Check for SHORT and LONG TC and then send
                 
             if(y==1)                                                            
             {                                                                               
-                ACK_L234(tm_ptr_short,0x03,GETpacket_seq_count(tc_ptr));
-                return tm_ptr_short;
+                ACK_L234(tm_pointer,0x03,GETpacket_seq_count(tc_ptr));
+                return tm_ptr;
             }                                  
             else if(y==0)
             {
@@ -390,17 +406,17 @@
                     }
                     else if(y==1)
                     {
-                        ACK_L234(tm_ptr_short,0x85,GETpacket_seq_count(tc_ptr));                
+                        ACK_L234(tm_pointer,0x85,GETpacket_seq_count(tc_ptr));                
                         //gPC.printf("\n\rPTE->PDIR = 0x%08X",PTE->PDIR);
-                        return tm_ptr_short;
+                        return tm_ptr;
                         break;  
                     }
                 }
                 else if(BAE_I2C_GPIO == 0)                                       //name to be changed later
                 {
                     gPC.printf("BAE_I2C_GPIO is not high\r\n");                    
-                    ACK_L234(tm_ptr_short,0x84,GETpacket_seq_count(tc_ptr));
-                    return tm_ptr_short;
+                    ACK_L234(tm_pointer,0x84,GETpacket_seq_count(tc_ptr));
+                    return tm_ptr;
                 }
             }
             BAE_I2C_mutex.unlock();
@@ -411,7 +427,7 @@
             //gPC.printf("Telecommand is for CDMS\r\n"); //apid = 10 corresponds to cdms
             switch(GETservice_type(tc_ptr))
             {
-                /*case 0x60:  //service type = 0x6
+                case 0x60:  //service type = 0x6
                 {
                     gPC.printf("service:MMS\r\n");
                     switch(GETservice_subtype(tc_ptr))      
@@ -422,19 +438,20 @@
                             mid=((uint16_t)(tc_ptr->TC_string[3]<<4))+((uint16_t)(tc_ptr->TC_string[4]));                                                       
                             if(mid!=0x0000 && mid!=0x0001)
                             {
-                                ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
-                                return tm_ptr_short;
+                                ACK_L234(tm_pointer,0x02,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr;
                             }
-                            FCTN_CDMS_RD_L_RAM(tm_pointer);
 
                             tm_pointer->TM_string[0] = 0x30;
                             tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);
                             tm_pointer->TM_string[2] = 0xA0;
                             tm_pointer->TM_string[3] = 0x00;                            //spare bits
                             
+                            FCTN_CDMS_RD_L_RAM(tm_pointer);
+                            
                             crc16 = crc16_gen(tm_ptr->TM_string,132);
-                            tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                            tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
+                            tm_pointer->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
+                            tm_pointer->TM_string[133] = (uint8_t)(crc16&0x00FF);
                             tm_pointer->next_TM = NULL;
                             return tm_ptr;
                         }                           
@@ -444,17 +461,17 @@
                             mid=(uint16_t)(tc_ptr->TC_string[3]<<4)+(uint16_t)(tc_ptr->TC_string[4]);
                             //32 bits at a time           
 
-                            if(mid!= && mid!=)                                  //allowable MID values?
+                            if(mid<0 || mid>4)                                  //allowable MID values?
                             {
-                                ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
-                                return tm_ptr_short;
+                                ACK_L234(tm_pointer,0x02,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr;
                             }                                                        
+                            
+                            FCTN_CDMS_WR_S_FLASH(mid,(uint32_t)(tc_ptr->TC_string[5]<<24)+(uint32_t)(tc_ptr->TC_string[6]<<16)+(uint32_t)(tc_ptr->TC_string[7]<<8)+(uint32_t)(tc_ptr->TC_string[8]));                          
 
-                            FCTN_CDMS_WR_S_FLASH(tc_ptr);                            
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));                     //ackCode = ? for success
 
-                            ACK_L234(tm_ptr_short,???,GETpacket_seq_count(tc_ptr));                     //ackCode = ? for success
-
-                            return tm_ptr_short;
+                            return tm_ptr;
                         }
                         case 0x6:
                         {
@@ -464,20 +481,20 @@
 
                             if(mid<0x0002 || mid>0x000A)                                  //allowable MID values?
                             {
-                                ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));                     //ackCode = ? for invalid mid
-                                return tm_ptr_short;
+                                ACK_L234(tm_pointer,0x02,GETpacket_seq_count(tc_ptr));                     //ackCode = ? for invalid mid
+                                return tm_ptr;
                             }    
 
-                            ///Send TM for success after writing onto flash
+                            //Send TM for success after writing onto flash
                             FCTN_CDMS_WR_L_FLASH(mid);
 
-                            return tm_ptr_short;
+                            return tm_ptr;
                         }                                                    
                         default:
                         {
                             gPC.printf("INVALID SERVICE SUBTYPE\r\n");
-                            ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
-                            return tm_ptr_short;
+                            ACK_L234(tm_pointer,0x02,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;
                         }
                     } 
                 }  */                                                                 
@@ -494,13 +511,13 @@
                                 
                                 P_PL_INIT();
 
-                                ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                                return tm_ptr_short;
+                                ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr;
                             }                
                             else if(CDMS_STANDBY==0)
                             {
-                                ACK_L234(tm_ptr_short,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
-                                return tm_ptr_short;
+                                ACK_L234(tm_pointer,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
+                                return tm_ptr;
                             }
                         }                                
                         else if(GETpid(tc_ptr)==0x02)
@@ -511,13 +528,13 @@
                                 
                                 P_PL_MAIN();
 
-                                ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                                return tm_ptr_short;
+                                ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr;
                             }                
                             else if(CDMS_STANDBY==0)
                             {
-                                ACK_L234(tm_ptr_short,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
-                                return tm_ptr_short;
+                                ACK_L234(tm_pointer,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
+                                return tm_ptr;
                             }
                         }
                         else if(GETpid(tc_ptr)==0x03)
@@ -528,13 +545,13 @@
                                 
                                 P_COM_INIT();
 
-                                ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                                return tm_ptr_short;
+                                ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr;
                             }                
                             else if(CDMS_STANDBY==0)
                             {
-                                ACK_L234(tm_ptr_short,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
-                                return tm_ptr_short;
+                                ACK_L234(tm_pointer,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
+                                return tm_ptr;
                             }
                         }
                         else if(GETpid(tc_ptr)==0x04)
@@ -545,13 +562,13 @@
                                 
                                 P_CDMS_HK_MAIN();
 
-                                ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                                return tm_ptr_short;
+                                ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr;
                             }                
                             else if(CDMS_STANDBY==0)
                             {
-                                ACK_L234(tm_ptr_short,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
-                                return tm_ptr_short;
+                                ACK_L234(tm_pointer,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
+                                return tm_ptr;
                             }
                         }
                         else if(GETpid(tc_ptr)==0x05)
@@ -562,13 +579,13 @@
                                 
                                 P_PL_RCV_SC_DATA();
 
-                                ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                                return tm_ptr_short;
+                                ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr;
                             }                
                             else if(CDMS_STANDBY==0)
                             {
-                                ACK_L234(tm_ptr_short,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
-                                return tm_ptr_short;
+                                ACK_L234(tm_pointer,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
+                                return tm_ptr;
                             }
                         }
                         else if(GETpid(tc_ptr)==0x06)
@@ -579,13 +596,13 @@
                                 
                                 P_CDMS_INIT_SD();
 
-                                ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                                return tm_ptr_short;
+                                ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr;
                             }                
                             else if(CDMS_STANDBY==0)
                             {
-                                ACK_L234(tm_ptr_short,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
-                                return tm_ptr_short;
+                                ACK_L234(tm_pointer,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
+                                return tm_ptr;
                             }    
                         }
                         else if(GETpid(tc_ptr)==0x11)
@@ -594,8 +611,8 @@
                             
                             CDMS_SD_SW_ON();
 
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                            return tm_ptr_short;                                                       
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;                                                       
                         }
                         else if(GETpid(tc_ptr)==0x21)
                         {       
@@ -603,8 +620,26 @@
                             
                             CDMS_SD_SW_OFF();
 
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                            return tm_ptr_short;     
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;     
+                        }
+                        else if(GETpid(tc_ptr)==0x12)
+                        {       
+                            //gPC.printf("TC_SW_ON_SD\r\n"); // call PWR_SWCH_ON function
+                            
+                            CDMS_RTC_ON();
+
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;                                                       
+                        }
+                        else if(GETpid(tc_ptr)==0x22)
+                        {       
+                            //gPC.printf("TC_SW_ON_SD\r\n"); // call PWR_SWCH_ON function
+                            
+                            CDMS_RTC_OFF();
+
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;                                                       
                         }
                         else if(GETpid(tc_ptr)==0x13)
                         {                           
@@ -612,8 +647,8 @@
                             
                             SW_ON_BAE();
 
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                            return tm_ptr_short;                            
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;                            
                         }
                         else if(GETpid(tc_ptr)==0x23)
                         {                           
@@ -621,8 +656,8 @@
                             
                             SW_OFF_BAE();
 
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                            return tm_ptr_short;                                                        
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;                                                        
                         }
                         else if(GETpid(tc_ptr)==0x14)
                         {                            
@@ -630,8 +665,8 @@
                             
                             SW_ON_PL_BEE();
 
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                            return tm_ptr_short;                                                        
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;                                                        
                         }
                         else if(GETpid(tc_ptr)==0x24)
                         {                   
@@ -639,8 +674,8 @@
                             
                             SW_OFF_PL_BEE();
 
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                            return tm_ptr_short;    
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;    
                         }
                         else if(GETpid(tc_ptr)==0x15)
                         {                            
@@ -648,8 +683,8 @@
                             
                             SW_ON_PL_EPS();
 
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));     
-                            return tm_ptr_short;                            
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));     
+                            return tm_ptr;                            
                         }
                         else if(GETpid(tc_ptr)==0x25)
                         {                            
@@ -657,8 +692,8 @@
                             
                             SW_OFF_PL_EPS();
 
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                            return tm_ptr_short;                            
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;                            
                         }
                         else if(GETpid(tc_ptr)==0x16)
                         {                            
@@ -667,8 +702,8 @@
                             
                             SW_ON_V_A_EN();
 
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                            return tm_ptr_short;                        
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;                        
                         }
                         else if(GETpid(tc_ptr)==0x26)
                         {                        
@@ -676,8 +711,8 @@
                             
                             SW_OFF_V_A_EN();
 
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                            return tm_ptr_short;                                                    
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;                                                    
                         }
                         else if(GETpid(tc_ptr)==0x31)
                         {                            
@@ -685,8 +720,8 @@
                             
                             RST_SD();
 
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                            return tm_ptr_short;                                                        
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;                                                        
                         }
                         else if(GETpid(tc_ptr)==0x33)
                         {                            
@@ -694,8 +729,8 @@
                             
                             RST_BAE();
 
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                            return tm_ptr_short;                            
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;                            
                         }
                         else if(GETpid(tc_ptr)==0x34)
                         {                            
@@ -703,24 +738,25 @@
                             
                             RST_PL_BEE();
 
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                            return tm_ptr_short;                                                        
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;                                                        
                         }
                         else if(GETpid(tc_ptr)==0x38)
                         {
                             //gPC.printf()
                             
                             CDMS_INTERNAL_RESET();
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;
                         }
                         else if(GETpid(tc_ptr)==0x40)
                         {                         
                             //gPC.printf("RESET_HK_COUNTER\r\n"); // call PWR_SWCH_ON function
                             
-                            RST_HK_COUNTER();
+                            STANDBY_PRCS();         //////// Need to Complete this function
 
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                            return tm_ptr_short;                                                    
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;                                                    
                         }
                         else if(GETpid(tc_ptr)==0x41)
                         {                            
@@ -728,22 +764,22 @@
                             
                             RST_HK_COUNTER();
 
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
-                            return tm_ptr_short;    
+                            ACK_L234(tm_pointer,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr;    
                         }
                         else if(GETpid(tc_ptr)==0xF1)
                         {          
                             //gPC.printf("TC_CDMS_RD_RTC\r\n"); // call PWR_SWCH_ON function
                             
-                            /*uint32_t time_temp = FCTN_CDMS_RD_RTC();                        //RTC_RD_EROR has to incorporated
+                            uint32_t time_temp = FCTN_CDMS_RD_RTC();                        //RTC_RD_EROR has to incorporated
                             tm_pointer->TM_string[0] = 0x78;     
                             tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                            tm_pointer->TM_string[2] = 0xA0;
+                            tm_pointer->TM_string[2] = 0xA0;    // ACK CODE
                             tm_pointer->TM_string[3] = 0x00;
-                            tm_pointer->TM_string[4] = (uint8_t)((time & 0xFF000000)>>24);
-                            tm_pointer->TM_string[5] = (uint8_t)((time & 0x00FF0000)>>16);
-                            tm_pointer->TM_string[6] = (uint8_t)((time & 0x0000FF00)>>8);
-                            tm_pointer->TM_string[7] = (uint8_t)((time & 0x000000FF));
+                            tm_pointer->TM_string[4] = (uint8_t)((time_temp & 0xFF000000)>>24);
+                            tm_pointer->TM_string[5] = (uint8_t)((time_temp & 0x00FF0000)>>16);
+                            tm_pointer->TM_string[6] = (uint8_t)((time_temp & 0x0000FF00)>>8);
+                            tm_pointer->TM_string[7] = (uint8_t)((time_temp & 0x000000FF));
                             for(uint8_t i=0;i<124;i++)
                             {
                                 tm_pointer->TM_string[i+8] = 0x00;
@@ -751,27 +787,50 @@
                             crc16 = crc16_gen(tm_ptr->TM_string,132);
                             tm_pointer->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
                             tm_pointer->TM_string[133] = (uint8_t)(crc16&0x00FF);
-                            tm_pointer->next_TM = NULL;                 
-                            return tm_ptr; */                                                    //incomplete
+                            tm_pointer->next_TM = NULL;                                                                     //incomplete
+                            return tm_ptr;
                         }
                         else if(GETpid(tc_ptr)==0xF0)
                         {
                             uint8_t statusbits[64];
-                            CDMS_RD_SD_HK(statusbits);
-                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            if( CDMS_RD_SD_HK(statusbits) != 0){
+                                ACK_L234(tm_pointer,0xC0,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr;
+                            }
+                            else
+                            {
+                                uint32_t time_temp = FCTN_CDMS_RD_RTC();                        //RTC_RD_EROR has to incorporated
+                                tm_pointer->TM_string[0] = 0x78;     
+                                tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
+                                tm_pointer->TM_string[2] = 0xA0;    // ACK CODE
+                                tm_pointer->TM_string[3] = 0x00;
+                                for(int i=4;i<64;i++)
+                                {
+                                    tm_pointer->TM_string[i+4] = (i<68)?statusbits[i]:0x00;
+                                }
+                                for(uint8_t i=68;i<132;i++)
+                                {
+                                    tm_pointer->TM_string[i] = 0x00;
+                                }
+                                crc16 = crc16_gen(tm_ptr->TM_string,132);
+                                tm_pointer->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
+                                tm_pointer->TM_string[133] = (uint8_t)(crc16&0x00FF);
+                                tm_pointer->next_TM = NULL;                                                                     //incomplete
+                                return tm_ptr;
+                            }
                         }                               
                         else 
                         {
                             gPC.printf("INVALID FID\r\n");
-                            ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));  
-                            return tm_ptr_short;
+                            ACK_L234(tm_pointer,0x02,GETpacket_seq_count(tc_ptr));  
+                            return tm_ptr;
                         }
                     }
                     else 
                     {
                         gPC.printf("INVALID SERVICE SUBTYPE\r\n");
-                        ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));  
-                        return tm_ptr_short;
+                        ACK_L234(tm_pointer,0x02,GETpacket_seq_count(tc_ptr));  
+                        return tm_ptr;
                     }                                    
                 }
                 case 0x70:
@@ -871,15 +930,15 @@
                     else
                     {
                         gPC.printf("INVALID SERVICE SUBTYPE\r\n");
-                        ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));  
-                        return tm_ptr_short;
+                        ACK_L234(tm_pointer,0x02,GETpacket_seq_count(tc_ptr));  
+                        return tm_ptr;
                     }
                 }
                 default:
                 {
                     gPC.printf("ILLEGAL TC"); 
-                    ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
-                    return tm_ptr_short;                    
+                    ACK_L234(tm_pointer,0x02,GETpacket_seq_count(tc_ptr));
+                    return tm_ptr;                    
                 }
             }
         }
@@ -895,8 +954,8 @@
                 
             if(y==1)                                                            
             {                                                                               
-                ACK_L234(tm_ptr_short,0x03,GETpacket_seq_count(tc_ptr));
-                return tm_ptr_short;
+                ACK_L234(tm_pointer,0x03,GETpacket_seq_count(tc_ptr));
+                return tm_ptr;
                 break;
             }                                  
             else if(y==0)
@@ -921,25 +980,25 @@
                     }
                     else if(y==1)
                     {
-                        ACK_L234(tm_ptr_short,0x85,GETpacket_seq_count(tc_ptr));                
+                        ACK_L234(tm_pointer,0x85,GETpacket_seq_count(tc_ptr));                
                         //gPC.printf("\n\rPTE->PDIR = 0x%08X",PTE->PDIR);
-                        return tm_ptr_short;
+                        return tm_ptr;
                         break;  
                     }
                 }
                 else if(PL_I2C_Intr == 0)                                       //name to be changed later
                 {
                     gPC.printf("PL_I2C_Intr is not high\r\n");                    
-                    ACK_L234(tm_ptr_short,0x84,GETpacket_seq_count(tc_ptr));
-                    return tm_ptr_short;
+                    ACK_L234(tm_pointer,0x84,GETpacket_seq_count(tc_ptr));
+                    return tm_ptr;
                 }
             }                             
         }
         default:    //invalid TC
         {
             gPC.printf("INVALID TC\r\n");                 
-            ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
-            return tm_ptr_short;
+            ACK_L234(tm_pointer,0x02,GETpacket_seq_count(tc_ptr));
+            return tm_ptr;
         }
     }
     return;