Repository for CDMS code

Dependencies:   SimpleDMA mbed-rtos mbed eeprom

Fork of COM_MNG_TMTC_SIMPLE by Shreesha S

Files at this revision

API Documentation at this revision

Comitter:
chaithanyarss
Date:
Thu Jun 30 14:00:33 2016 +0000
Parent:
188:5f75962ae7ea
Child:
207:28a07943dded
Commit message:
For integrating PL_Main with rest of CDMS code

Changed in this revision

CDMS_HK.h Show annotated file Show diff for this revision Revisions of this file
CDMS_PL.h Show annotated file Show diff for this revision Revisions of this file
COM_MNG_TMTC.h Show annotated file Show diff for this revision Revisions of this file
Compression.h Show annotated file Show diff for this revision Revisions of this file
DefinitionsAndGlobals.h Show annotated file Show diff for this revision Revisions of this file
FMS_all.h Show annotated file Show diff for this revision Revisions of this file
FMS_functions.h Show annotated file Show diff for this revision Revisions of this file
Reset_functions.h Show annotated file Show diff for this revision Revisions of this file
TEST_PL.h Show annotated file Show diff for this revision Revisions of this file
cdms_rtc.h Show annotated file Show diff for this revision Revisions of this file
cdms_sd.h Show annotated file Show diff for this revision Revisions of this file
crc.h Show annotated file Show diff for this revision Revisions of this file
i2c.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/CDMS_HK.h	Wed Jun 29 14:17:34 2016 +0000
+++ b/CDMS_HK.h	Thu Jun 30 14:00:33 2016 +0000
@@ -3,6 +3,7 @@
 #include "reset_functions.h"
 #include "DefinitionsAndGlobals.h"
 #include "COM_SND_TM_functions.h"
+#include "COM_SND_TM_functions.h"
 
 void FCTN_CDMS_HK_MAIN();
 void FCTN_CDMS_HK();
@@ -49,6 +50,18 @@
 
 void FCTN_CDMS_HK_MAIN(void const *args)
 {
+
+  uint8_t CDMS_HEALTH_DATA[128] = {0};
+  unsigned char CDMS_HK_FRAME[134] = {0};
+  char BAE_HK[128] = {0};
+  uint8_t convoluted_CDMS_HK[270];
+  uint8_t interleave_CDMS_HK[288];
+  uint8_t CDMS_HEALTH_FINAL[512] = {0};
+  uint8_t convoluted_BAE_HK[270];
+  uint8_t interleave_BAE_HK[288];
+  uint8_t BAE_HEALTH_FINAL[512] = {0};
+  unsigned char BAE_HK_FRAME[134] = {0};
+
   CDMS_HK_MAIN_STATUS = 0x01;
   CDMS_HK_MAIN_COUNTER++;
 
@@ -58,7 +71,6 @@
   VERIFY_RTC();
   HANDLE_HW_FAULTS();
   FUNC_CDMS_GPIO_STATUS(); //yet to be done
-  COLLECT_CDMS_RAM();
 
   uint8_t CDMS_quant[19];
   for(int i=0;i<16;i++)
@@ -68,40 +80,79 @@
   CDMS_quant[16]= (uint8_t)RSSI_volatge;
   CDMS_quant[17]= (uint8_t)quant_data.CDMS_temp_quant;
   
-
-  char BAE_HK[74];
-  BAE_HK_I2C = FCTN_I2C_READ(BAE_HK,74);
-  if(BAE_HK_I2C == 0)
-    hk_cdms.printf("Bae hk data received");
-  else
-  {
-    hk_cdms.printf("Bae hk data NOT received");
-    for(int i = 0;i<74;i++)
-    BAE_HK = 0;
-  }
   
-  for(int i=0;i<512;i++)
-  {
-      CDMS_HEALTH_DATA[i]=0x00;
-  }
-  
-  uint64_t time = FCTN_CDMS_RD_RTC();
+  uint64_t time = FCTN_CDMS_RD_RTC();             //Reading Time from RTC 
   time = time>>7;
   uint32_t HK_time = (uint32_t)time;
   for(int i = 0;i<4;i++)
   CDMS_HEALTH_DATA[i] = HK_time[i];
+
   // Here: Have to FIT flash data.
-  for(int i = 0;i<19;i++)
-  CDMS_HEALTH_DATA[i+24] = CDMS_quant[i];
+  for(int i = 0;i<19;i++)                         //Collecting Data from Temp sensors
+    CDMS_HEALTH_DATA[i+24] = CDMS_quant[i];
+  
+  COLLECT_CDMS_RAM();                             //Reading RAM parameters
   
+  CDMS_HEALTH_DATA[126] = GPIO_STATUS;            //Reading GPIO Pins
+  CDMS_HEALTH_DATA[127] = GPIO_STATUS >> 8;
+ 
+  FCTN_SD_MNGR();                                 //Adding FSC & TMID to TM frame
+  CDMS_HK_FRAME[0] = 0x20;
+  CDMS_HK_FRAME[1] = FSC_CURRENT[4]+1;
+  CDMS_HK_FRAME[2] = (FSC_CURRENT[4]+1) >> 8;
+  CDMS_HK_FRAME[3] = (FSC_CURRENT[4]+1) >> 16;
+  
+  for(int i = 0;i<128;i++)                         /*Adding actual CDMS Health data to TM frame*/
+    CDMS_HK_FRAME[4+i] = CDMS_HEALTH_DATA[i];
   
-  for(int i=0;i<512;i++)
-  {
-      printf("%d",CDMS_HEALTH_DATA[i]);
+  uint16_t crc = crc16_gen(CDMS_HK_FRAME,132);      /*Adding CRC to TM frame*/
+  CDMS_HK_FRAME[133] = crc;
+  CDMS_HK_FRAME[132] = crc >> 8;
+
+  Convolution CDMS_HEALTH;
+  Convolution BAE_HEALTH;
+  CDMS_HEALTH.convolutionEncode(CDMS_HK_FRAME , convoluted_CDMS_HK);
+  CDMS_HEALTH.convolutionEncode(CDMS_HK_FRAME + 67, convoluted_CDMS_HK + 135);
+  interleave(convoluted_CDMS_HK ,  interleave_CDMS_HK);
+  interleave(convoluted_CDMS_HK +135, interleave_CDMS_HK + 144);
+  for(int i=0;i<288;i++)
+    CDMS_HEALTH_FINAL[i] = interleave_CDMS_HK[i];
+
+  SD_WRITE(CDMS_HEALTH_FINAL,FSC_CURRENT[4]+1,4);
+  hk_cdms.printf("CDMS hk succesfully completed\r\n");
+
+
+/*---------------------------------- BAE HK --------------------------------------------*/
+  
+  BAE_HK_I2C = FCTN_I2C_READ(BAE_HK,134);
+  if(BAE_HK_I2C == 0){
+    TIME_LATEST_I2C_SPEED = FCTN_CDMS_RD_RTC() >> 7;
+    hk_cdms.printf("Bae hk data received");
   }
-  CDMS_HK_FSC = FCTN_SD_MNGR(0x04);
-  BAE_HK_FSC = FCTN_SD_MNGR(0x05);
-  hk_cdms.printf("CDMS executed succesfully\r\n");
+  else
+  {
+    for(int i = 0;i<128;i++)
+      BAE_HK[i] = 0;
+  }
+  for(int i = 0;i<4;i++)
+  BAE_HK[i] = HK_time[i];
+  BAE_HK_FRAME[0] = 0x28;
+  BAE_HK_FRAME[1] = FSC_CURRENT[5]+1;
+  BAE_HK_FRAME[2] = (FSC_CURRENT[5]+1) >> 8;
+  BAE_HK_FRAME[3] = (FSC_CURRENT[5]+1) >> 16;
+  for(int i = 0;i<128;i++)                         /*Adding actual CDMS Health data to TM frame*/
+    BAE_HK_FRAME[4+i] = BAE_HK[i];
+  crc = crc16_gen(BAE_HK_FRAME,132);               /*Adding CRC to TM frame*/
+  BAE_HK_FRAME[133] = crc;
+  BAE_HK_FRAME[132] = crc >> 8;
+  BAE_HEALTH.convolutionEncode(BAE_HK_FRAME , convoluted_BAE_HK);
+  BAE_HEALTH.convolutionEncode(BAE_HK_FRAME + 67, convoluted_BAE_HK + 135);
+  interleave(convoluted_BAE_HK ,  interleave_BAE_HK);
+  interleave(convoluted_BAE_HK +135, interleave_BAE_HK + 144);
+  for(int i=0;i<288;i++)
+  BAE_HEALTH_FINAL[i] = interleave_BAE_HK[i];
+  SD_WRITE(BAE_HEALTH_FINAL,FSC_CURRENT[5]+1,5);
+  hk_cdms.printf("BAE hk succesfully completed\r\n");
 }
 
 int quantiz(float start,float step,float x)
@@ -305,11 +356,11 @@
   if(BAE_STATUS != DEVICE_DISABLED)
   {
     if(BAE_STATUS = DEVICE_OC_FAULT)
-        BAE_SW_EN_DS = 1; //Power ON BAE
+        BAE_SW_EN_DS = 0; //Power ON BAE
 
     if(BAE_OC_FAULT == 0)  // If OC Fault
     {
-      BAE_SW_EN_DS = 0; //Switch OFF BAE
+      BAE_SW_EN_DS = 1; //Switch OFF BAE
       BAE_FAULTCOUNT++;
       BAE_STATUS = (BAE_FAULTCOUNT == 3)?DEVICE_DISABLED:DEVICE_OC_FAULT;
     }
@@ -326,11 +377,11 @@
   if(PL_STATUS != DEVICE_DISABLED)
   {
     if(PL_STATUS = DEVICE_OC_FAULT)
-      PL_SW_EN_DS = 1; //Power ON PL
+      PL_SW_EN_DS = 0; //Power ON PL
 
-    if(PL_BEE_SW_OC_FAULT == 1)  // if OC Fault
+    if(PL_BEE_SW_OC_FAULT == 0)  // if OC Fault
     {
-      PL_SW_EN_DS = 0;  // switching OFF PL
+      PL_SW_EN_DS = 1;  // switching OFF PL
       PL_FAULTCOUNT++;
       PL_STATUS = (PL_FAULTCOUNT == 3)?DEVICE_DISABLED:DEVICE_OC_FAULT;
     }
@@ -347,61 +398,70 @@
 { 
   /*--------------------Current FSC's---------------------*/\
   for(int i=0;i<4;i++)
-    CDMS_HEALTH_DATA[i+43] = CURRENT_FSC_HK_ARCH_BAE >> (i*8);
+    CDMS_HEALTH_DATA[i+43] = FSC_LAST[5] >> (i*8);
+  for(int i=0;i<4;i++)
+    CDMS_HEALTH_DATA[i+47] = FSC_CURRENT[5] >> (i*8);
   for(int i=0;i<4;i++)
-    CDMS_HEALTH_DATA[i+47] = CURRENT_FSC_HK_ARCH_CDMS >> (i*8);
+    CDMS_HEALTH_DATA[i+51] = FSC_LAST[4] >> (i*8);
+  for(int i=0;i<4;i++)
+    CDMS_HEALTH_DATA[i+55] = FSC_CURRENT[4] >> (i*8);
+  for(int i=0;i<4;i++)
+    CDMS_HEALTH_DATA[i+59] = FSC_LAST[3] >> (i*8);
   for(int i=0;i<4;i++)
-    CDMS_HEALTH_DATA[i+51] = CURRENT_FSC_SFF_BT >> (i*8);
+    CDMS_HEALTH_DATA[i+63] = FSC_CURRENT[3] >> (i*8);
+  for(int i=0;i<4;i++)
+    CDMS_HEALTH_DATA[i+67] = FSC_LAST[2] >> (i*8);
   for(int i=0;i<4;i++)
-    CDMS_HEALTH_DATA[i+55] = CURRENT_FSC_SFF_AT >> (i*8);
+    CDMS_HEALTH_DATA[i+71] = FSC_CURRENT[2] >> (i*8);
   for(int i=0;i<4;i++)
-    CDMS_HEALTH_DATA[i+59] = CURRENT_FSC_SCF >> (i*8);
-
+    CDMS_HEALTH_DATA[i+75] = FSC_LAST[1] >> (i*8);
+  for(int i=0;i<4;i++)
+    CDMS_HEALTH_DATA[i+79] = FSC_CURRENT[1] >> (i*8);
   /*---------------------Latest Time----------------------*/
   for(int i=0;i<4;i++)
-    CDMS_HEALTH_DATA[i+63] = TIME_LATEST_SPI_SPEED >> (i*8);
+    CDMS_HEALTH_DATA[i+83] = TIME_LATEST_SPI_SPEED >> (i*8);
   for(int i=0;i<4;i++)
-    CDMS_HEALTH_DATA[i+67] = TIME_LATEST_SD_RD >> (i*8);
+    CDMS_HEALTH_DATA[i+87] = TIME_LATEST_SD_RD >> (i*8);
   for(int i=0;i<4;i++)
-    CDMS_HEALTH_DATA[i+71] = TIME_LATEST_SD_WR >> (i*8);
+    CDMS_HEALTH_DATA[i+91] = TIME_LATEST_SD_WR >> (i*8);
   for(int i=0;i<4;i++)
-    CDMS_HEALTH_DATA[i+75] = TIME_LATEST_I2C_SPEED >> (i*8);
+    CDMS_HEALTH_DATA[i+95] = TIME_LATEST_I2C_SPEED >> (i*8);
   for(int i=0;i<4;i++)
-    CDMS_HEALTH_DATA[i+79] = TIME_LATEST_I2C_BAE >> (i*8);
+    CDMS_HEALTH_DATA[i+99] = TIME_LATEST_I2C_BAE >> (i*8);
   for(int i=0;i<4;i++)
-    CDMS_HEALTH_DATA[i+83] = TIME_LATEST_RTC >> (i*8);
+    CDMS_HEALTH_DATA[i+103] = TIME_LATEST_RTC >> (i*8);
   for(int i=0;i<2;i++)
-    CDMS_HEALTH_DATA[i+87] = COMRX_RESET_COUNTER >> (i*8);
+    CDMS_HEALTH_DATA[i+107] = COMRX_RESET_COUNTER >> (i*8);
   for(int i=0;i<2;i++)
-    CDMS_HEALTH_DATA[i+89] = PL_RCV_SC_DATA_COUNTER >> (i*8);
+    CDMS_HEALTH_DATA[i+107] = PL_RCV_SC_DATA_COUNTER >> (i*8);
   for(int i=0;i<2;i++)
-    CDMS_HEALTH_DATA[i+91] = PL_MAIN_COUNTER >> (i*8);
+    CDMS_HEALTH_DATA[i+111] = PL_MAIN_COUNTER >> (i*8);
   for(int i=0;i<2;i++)
-    CDMS_HEALTH_DATA[i+93] = CDMS_HK_MAIN_COUNTER >> (i*8);
+    CDMS_HEALTH_DATA[i+113] = CDMS_HK_MAIN_COUNTER >> (i*8);
   for(int i=0;i<2;i++)
-    CDMS_HEALTH_DATA[i+95] = CDMS_I2C_ERR_BAE_COUNTER >> (i*8);
+    CDMS_HEALTH_DATA[i+115] = CDMS_I2C_ERR_BAE_COUNTER >> (i*8);
   for(int i=0;i<2;i++)
-    CDMS_HEALTH_DATA[i+97] = CDMS_I2C_ERR_SPEED_COUNTER >> (i*8);
-  CDMS_HEALTH_DATA[100] = CDMS_HEALTH_DATA[100] | CDMS_STANDBY_PL << 7;
-  CDMS_HEALTH_DATA[100] = CDMS_HEALTH_DATA[100] | ((CDMS_INIT_STATUS << 6) & 0x40);
-  CDMS_HEALTH_DATA[100] = CDMS_HEALTH_DATA[100] | ((CDMS_HK_MAIN_STATUS << 5) & 0x20);
-  CDMS_HEALTH_DATA[100] = CDMS_HEALTH_DATA[100] | ((CDMS_HK_STATUS << 3) & 0x18);
-  CDMS_HEALTH_DATA[100] = CDMS_HEALTH_DATA[100] | ((COMRX_STATUS << 2) & 0x04);
-  CDMS_HEALTH_DATA[100] = CDMS_HEALTH_DATA[100] | ((CDMS_RTC_BL << 1) & 0x02);
-  CDMS_HEALTH_DATA[100] = CDMS_HEALTH_DATA[100] & 0xFE;
+    CDMS_HEALTH_DATA[i+117] = CDMS_I2C_ERR_SPEED_COUNTER >> (i*8);
+  CDMS_HEALTH_DATA[120] = CDMS_HEALTH_DATA[120] | CDMS_STANDBY_PL << 7;
+  CDMS_HEALTH_DATA[120] = CDMS_HEALTH_DATA[120] | ((CDMS_INIT_STATUS << 6) & 0x40);
+  CDMS_HEALTH_DATA[120] = CDMS_HEALTH_DATA[120] | ((CDMS_HK_MAIN_STATUS << 5) & 0x20);
+  CDMS_HEALTH_DATA[120] = CDMS_HEALTH_DATA[120] | ((CDMS_HK_STATUS << 3) & 0x18);
+  CDMS_HEALTH_DATA[120] = CDMS_HEALTH_DATA[120] | ((COMRX_STATUS << 2) & 0x04);
+  CDMS_HEALTH_DATA[120] = CDMS_HEALTH_DATA[120] | ((CDMS_RTC_BL << 1) & 0x02);
+  CDMS_HEALTH_DATA[120] = CDMS_HEALTH_DATA[120] & 0xFE;
   
-  CDMS_HEALTH_DATA[101] = CDMS_HEALTH_DATA[101] | PL_RCV_SC_DATA_STATUS << 7;
-  CDMS_HEALTH_DATA[101] = CDMS_HEALTH_DATA[101] | ((COM_SESSION << 6) & 0x40);
-  CDMS_HEALTH_DATA[101] = CDMS_HEALTH_DATA[101] | ((COM_RX << 5) & 0x20);
-  CDMS_HEALTH_DATA[101] = CDMS_HEALTH_DATA[101] | ((RF_SW_STATUS << 4) & 0x10);
-  CDMS_HEALTH_DATA[101] = CDMS_HEALTH_DATA[101] | ((COM_TX << 3) & 0x08);
-  CDMS_HEALTH_DATA[101] = CDMS_HEALTH_DATA[101] | ((COM_TX_STATUS << 2) & 0x04);
-  CDMS_HEALTH_DATA[101] = CDMS_HEALTH_DATA[101] | ((COM_MNG_TMTC << 1) & 0x02);
-  CDMS_HEALTH_DATA[101] = CDMS_HEALTH_DATA[101] | (CDMS_STANDBY_HK & 0x01);
+  CDMS_HEALTH_DATA[121] = CDMS_HEALTH_DATA[121] | PL_RCV_SC_DATA_STATUS << 7;
+  CDMS_HEALTH_DATA[121] = CDMS_HEALTH_DATA[121] | ((COM_SESSION << 6) & 0x40);
+  CDMS_HEALTH_DATA[121] = CDMS_HEALTH_DATA[121] | ((COM_RX << 5) & 0x20);
+  CDMS_HEALTH_DATA[121] = CDMS_HEALTH_DATA[121] | ((RF_SW_STATUS << 4) & 0x10);
+  CDMS_HEALTH_DATA[121] = CDMS_HEALTH_DATA[121] | ((COM_TX << 3) & 0x08);
+  CDMS_HEALTH_DATA[121] = CDMS_HEALTH_DATA[121] | ((COM_TX_STATUS << 2) & 0x04);
+  CDMS_HEALTH_DATA[121] = CDMS_HEALTH_DATA[121] | ((COM_MNG_TMTC << 1) & 0x02);
+  CDMS_HEALTH_DATA[121] = CDMS_HEALTH_DATA[121] | (CDMS_STANDBY_HK & 0x01);
 
-  CDMS_HEALTH_DATA[102] = CDMS_HEALTH_DATA[102] | PL_INIT_STATUS << 7;
-  CDMS_HEALTH_DATA[102] = CDMS_HEALTH_DATA[102] | ((PL_MAIN_STATUS << 6) & 0x40);
-  CDMS_HEALTH_DATA[102] = CDMS_HEALTH_DATA[102] | ((PL_LOW_POWER << 5) & 0x20);
-  CDMS_HEALTH_DATA[102] = CDMS_HEALTH_DATA[102] | ((PL_STATE << 3) & 0x18);
-  CDMS_HEALTH_DATA[102] = CDMS_HEALTH_DATA[102] | (PL_STATUS & 0x07);
+  CDMS_HEALTH_DATA[122] = CDMS_HEALTH_DATA[122] | PL_INIT_STATUS << 7;
+  CDMS_HEALTH_DATA[122] = CDMS_HEALTH_DATA[122] | ((PL_MAIN_STATUS << 6) & 0x40);
+  CDMS_HEALTH_DATA[122] = CDMS_HEALTH_DATA[122] | ((PL_LOW_POWER << 5) & 0x20);
+  CDMS_HEALTH_DATA[122] = CDMS_HEALTH_DATA[122] | ((PL_STATE << 3) & 0x18);
+  CDMS_HEALTH_DATA[122] = CDMS_HEALTH_DATA[122] | (PL_STATUS & 0x07);
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CDMS_PL.h	Thu Jun 30 14:00:33 2016 +0000
@@ -0,0 +1,580 @@
+int pl_next_index=-1;   
+uint8_t pl_main_flag=0;
+uint8_t PL_PREV_STATE=0x00;
+uint8_t POWER_LEVEL = 3;                                //Would be present in HK data extracted 
+extern uint32_t pl_block[192]={0};
+uint32_t pl_time;
+uint16_t PL_MAIN_COUNTER=0;
+uint32_t TIME_LATEST_PL=0;
+uint8_t i;
+uint8_t PL_BEE_SW_STATUS=0;
+
+//Serial pc(USBTX,USBRX);
+
+#define PL_MAIN_STATUS              0x01
+#define PL_LOW_POWER                0x02
+#define STATE_OFF                   0x00
+#define STATE_STANDBY               0x04
+#define STATE_HIBERNATE             0x08
+#define STATE_SCIENCE               0x0C            //also used as mask for PL_STATE
+#define PL_OFF                      0x00
+#define PL_STANDBY                  0x10
+#define PL_HIBERNATE                0x20
+#define PL_SCIENCE                  0x30
+#define PL_SUCCESS_I2C              0x40
+#define PL_ERR_I2C                  0x50
+#define PL_INVALID_STATE            0x60
+#define PL_DISABLED                 0x70            //also used as mask for PL_STATUS
+#define EXECUTED                    0x00000001
+#define RETRY                       0x00000002
+#define UNEXECUTED                  0x00000003      //also used as mask for EXEC_STATUS
+
+
+DigitalOut PYLD_DFF(PIN73);
+DigitalOut PYLD_DFF_CLK(PIN65);
+DigitalOut PYLD_EPS_DFF_PWR(PIN64);
+DigitalOut PYLD_EPS_DFF_PWR_CLK(PIN40);
+Base_tm* FCTN_CDMS_RLY_TMTC(Base_tc *tc_ptr);
+
+// waiting 1us for setup time,hold time and propagation delay
+#define SET_PL_BEE_OFF {\
+    PYLD_DFF_CLK = 0;\
+    PYLD_DFF = 0;\
+    wait_us(1);\
+    PYLD_DFF_CLK = 1;\
+    wait_us(1);\
+    PYLD_DFF_CLK = 0;\
+    wait_us(1);\
+}
+#define SET_PL_BEE_ON {\
+    PYLD_DFF_CLK = 0;\
+    PYLD_DFF = 1;\
+    wait_us(1);\
+    PYLD_DFF_CLK = 1;\
+    wait_us(1);\
+    PYLD_DFF_CLK = 0;\
+    wait_us(1);\
+}
+
+//TC_string[0] should not be 0x00
+
+#define SET_PL_BEE_STANDBY(tm_ptr_standby) {\
+    Base_tc *pl_tc_standby = new Short_tc;\
+    pl_tc_standby->next_TC = NULL;\
+    PUTshort_or_long(pl_tc_standby,0);\
+    PUTcrc_pass(pl_tc_standby,0x1);\
+    PUTexec_status(pl_tc_standby,0);\
+    pl_tc_standby->TC_string[0] = 0x01;\
+    pl_tc_standby->TC_string[1] = 0xE1;\
+    pl_tc_standby->TC_string[2] = 0x81;\
+    pl_tc_standby->TC_string[3] = 0x02;\
+    pl_tc_standby->TC_string[4] = 0;\
+    pl_tc_standby->TC_string[5] = 0;\
+    pl_tc_standby->TC_string[6] = 0;\
+    pl_tc_standby->TC_string[7] = 0;\
+    pl_tc_standby->TC_string[8] = 0;\    
+    uint16_t crc16 = crc16_gen(pl_tc_standby->TC_string, 9);\
+    pl_tc_standby->TC_string[9]  = (uint8_t)(crc16 & 0xFF00)>>8;\
+    pl_tc_standby->TC_string[10] = (uint8_t)(crc16 & 0x00FF);\
+    tm_ptr_standby =  FCTN_CDMS_RLY_TMTC(pl_tc_standby);\
+    VERIFY_TM(tm_ptr_standby);\
+}
+
+//TC_string[0] should not be 0x00
+
+#define SET_PL_BEE_HIBERNATE(tm_ptr_hibernate) {\
+    Base_tc *pl_tc_hibernate = new Short_tc;\
+    pl_tc_hibernate->next_TC = NULL;\
+    PUTshort_or_long(pl_tc_hibernate,0);\
+    PUTcrc_pass(pl_tc_hibernate,0x1);\
+    PUTexec_status(pl_tc_hibernate,0);\
+    pl_tc_hibernate->TC_string[0] = 0x01;\
+    pl_tc_hibernate->TC_string[1] = 0xE1;\
+    pl_tc_hibernate->TC_string[2] = 0x81;\
+    pl_tc_hibernate->TC_string[3] = 0x03;\
+    pl_tc_hibernate->TC_string[4] = 0;\
+    pl_tc_hibernate->TC_string[5] = 0;\
+    pl_tc_hibernate->TC_string[6] = 0;\
+    pl_tc_hibernate->TC_string[7] = 0;\
+    pl_tc_hibernate->TC_string[8] = 0;\    
+    uint16_t crc16 = crc16_gen(pl_tc_hibernate->TC_string, 9);\
+    pl_tc_hibernate->TC_string[9]  = (uint8_t)(crc16 & 0xFF00)>>8;\
+    pl_tc_hibernate->TC_string[10] = (uint8_t)(crc16 & 0x00FF);\
+    tm_ptr_hibernate =  FCTN_CDMS_RLY_TMTC(pl_tc_hibernate);\
+    VERIFY_TM(tm_ptr_hibernate);\
+}
+
+//TC_string[0] should not be 0x00
+/*
+PUTshort_or_long(pl_tc_science,0);\
+    PUTcrc_pass(pl_tc_science,0x1);\
+    PUTexec_status(pl_tc_science,0);\*/
+    
+#define SET_PL_BEE_SCIENCE(tm_ptr_science) {\
+    Base_tc *pl_tc_science = new Short_tc;\
+    pl_tc_science->next_TC = NULL;\    
+    pl_tc_science->TC_string[0] = 0x01;\
+    pl_tc_science->TC_string[1] = 0xE1;\
+    pl_tc_science->TC_string[2] = 0x81;\
+    pl_tc_science->TC_string[3] = 0x04;\
+    pl_tc_science->TC_string[4] = 0;\
+    pl_tc_science->TC_string[5] = 0;\
+    pl_tc_science->TC_string[6] = 0;\
+    pl_tc_science->TC_string[7] = 0;\
+    pl_tc_science->TC_string[8] = 0;\
+    uint16_t crc16 = crc16_gen(pl_tc_science->TC_string, 9);\
+    pl_tc_science->TC_string[9]  = (uint8_t)(crc16 & 0xFF00)>>8;\
+    pl_tc_science->TC_string[10] = (uint8_t)(crc16 & 0x00FF);\
+    tm_ptr_science =  FCTN_CDMS_RLY_TMTC(pl_tc_science);\
+    VERIFY_TM(tm_ptr_science);\
+}
+void print_processed_block(uint8_t index)
+{
+    gPC.printf("\n\n\rBlock after processing:");
+    gPC.printf("\n\rTime of block:");
+    gPC.printf("\n\rYear :%d",((((pl_block[index]&0xFFFFFFF0)>>4) & 0x0C000000)>>26)+2016);
+    gPC.printf("\tMonth  :%d",((((pl_block[index]&0xFFFFFFF0)>>4) & 0x03C00000)>>22));
+    gPC.printf("\tDay    :%d",((((pl_block[index]&0xFFFFFFF0)>>4) & 0x003E0000)>>17));
+    gPC.printf("\n\rHours  :%d",((((pl_block[index]&0xFFFFFFF0)>>4) & 0x0001F000)>>12));
+    gPC.printf("\tMin    :%d",((((pl_block[index]&0xFFFFFFF0)>>4) & 0x00000FC0)>>6));
+    gPC.printf("\tSec    :%d",(((pl_block[index]&0xFFFFFFF0)>>4) & 0x0000003F));
+    gPC.printf("\n\rSID    :%d",(pl_block[index] & 0x0000000C)>>2);
+    gPC.printf("\tExecution Status :%d",pl_block[index] & UNEXECUTED);
+}
+void print_exit(uint8_t* temp)
+{    
+    uint8_t temp2[3];
+    temp2[0] = (pl_main_flag&STATE_SCIENCE)>>2;
+    temp2[1] = (pl_main_flag&PL_DISABLED)>>4;
+    temp2[2] = (PL_PREV_STATE & STATE_SCIENCE)>>2;
+    gPC.printf("\n\rAt exit");
+    char state[][17] = {"STATE_OFF","STATE_STANDBY","STATE_HIBERNATE","STATE_SCIENCE"};
+    char status[][17] = {"PL_OFF","PL_STANDBY","PL_HIBERNATE","PL_SCIENCE","PL_SUCCESS_I2C","PL_ERR_I2C","PL_INVALID_STATE","PL_DISABLED"};
+    gPC.printf("\n\rPL_state:%s -> %s",state[(uint8_t)temp[0]],state[(uint8_t)temp2[0]]);
+    gPC.printf("\n\rPL_status:%s -> %s",status[(uint8_t)temp[1]],status[(uint8_t)temp2[1]]);
+    gPC.printf("\n\rPL_PREV_state:%s -> %s",state[(uint8_t)temp[2]],state[(uint8_t)temp2[2]]);
+}
+void VERIFY_TM(Base_tm *tm_ptr)
+{
+    uint8_t temp = tm_ptr->TM_string[2];            //Obatining ACK_CODE
+    if(GETshort_or_long_tm(tm_ptr)==1)              //short TM
+    {
+        gPC.printf("\n\rSHORT TM received");
+        uint16_t crc16 = crc16_gen(tm_ptr->TM_string, 11);        
+        if(tm_ptr->TM_string[12]==((uint8_t)(crc16 & 0x00FF)) && tm_ptr->TM_string[11]==((uint8_t)((crc16 & 0xFF00)>>8)))        
+        {            
+            if(temp!=0x00 && temp!=0x01 && temp!=0x02 && temp!=0x03 && temp!=0x84 && temp!=0x85)
+            {
+                gPC.printf("\n\rTime_Latest_PL Updated");
+                TIME_LATEST_PL = pl_time;                  //update latest time when I2C communication was successful
+            }
+            if(temp==0xA0 || temp==0xC0)
+            {
+                gPC.printf("\n\rACK_CODE Success");
+                if(temp==0xA0)
+                    gPC.printf("\n\rACK_CODE = 0x%02X",temp);
+                else
+                    gPC.printf("\n\rACK_CODE = 0x%02X",temp);
+                pl_main_flag  = pl_main_flag & (~PL_DISABLED);
+                pl_main_flag |= PL_SUCCESS_I2C;
+                pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
+                pl_block[pl_next_index-1] |= EXECUTED;
+            }
+            else
+            {
+                gPC.printf("\n\rACK_CODE failure (0x%02X)",temp);
+                //gPC.printf("\n\rACK_CODE = 0x%02X",temp);
+                pl_main_flag  = pl_main_flag & (~PL_DISABLED);
+                pl_main_flag |= PL_ERR_I2C;
+                pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
+                pl_block[pl_next_index-1] |= RETRY;   
+            }
+        }
+        else
+        {
+            gPC.printf("\n\rShort_TM CRC failed");
+            pl_main_flag  = pl_main_flag & (~PL_DISABLED);
+            pl_main_flag |= PL_ERR_I2C;
+            pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
+            pl_block[pl_next_index-1] |= RETRY;
+        }
+    }
+    else if(GETshort_or_long_tm(tm_ptr)==0)                  //LONG TM
+    {
+        gPC.printf("\n\rLONG TM received");
+        uint16_t crc16 = crc16_gen(tm_ptr->TM_string,132);        
+        if(tm_ptr->TM_string[133]==((uint8_t)(crc16 & 0x00FF)) && tm_ptr->TM_string[132]==((uint8_t)((crc16 & 0xFF00)>>8)))        
+        {
+            if(temp!=0x00 && temp!=0x01 && temp!=0x02 && temp!=0x03 && temp!=0x84 && temp!=0x85)
+            {
+                gPC.printf("\n\rTime_Latest_PL Updated");
+                TIME_LATEST_PL = pl_time;                  //update latest time when I2C communication was successful
+            }
+            if(temp==0xA0 || temp==0xC0)
+            {
+                gPC.printf("\n\rACK_CODE Success");
+                if(temp==0xA0)
+                    gPC.printf("\n\rACK_CODE = 0x%02X",temp);
+                else
+                    gPC.printf("\n\rACK_CODE = 0x%02X",temp);
+                pl_main_flag  = pl_main_flag & (~PL_DISABLED);
+                pl_main_flag |= PL_SUCCESS_I2C;
+                pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
+                pl_block[pl_next_index-1] |= EXECUTED;
+            }
+            else
+            {
+                gPC.printf("\n\rACK_CODE failure (0x%02X)",temp);
+                pl_main_flag  = pl_main_flag & (~PL_DISABLED);
+                pl_main_flag |= PL_ERR_I2C;
+                pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
+                pl_block[pl_next_index-1] |= RETRY;   
+            }   
+        }
+        else
+        {
+            gPC.printf("\n\rLong_TM CRC failed");
+            pl_main_flag  = pl_main_flag & (~PL_DISABLED);
+            pl_main_flag |= PL_ERR_I2C;        
+            pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
+            pl_block[pl_next_index-1] |= RETRY;   
+        }
+    }    
+}
+void test1(uint8_t t)
+{
+    //Output should be PL_STATUS = PL_DISABLED
+    if(t!=0)
+    {
+        gPC.printf("\n\rTesting OC protection");
+        PL_BEE_SW_STATUS = 2;   //OC protection
+    }
+}
+void test2(uint8_t t)
+{
+    //output should be same as test1()
+    if(t!=0)    
+    {
+        gPC.printf("\n\rTesting Deviced Disabled case");
+        PL_BEE_SW_STATUS = 3;   //Device DISABLED 
+    }
+}
+void test3(uint8_t t)
+{
+    if(t!=0)
+    {
+        gPC.printf("\n\rTesting RTC failed case");
+        pl_time = 0;            //RTC failed
+    }
+}
+/*void test4(uint8_t t)
+{
+    if(t!=0)
+    {
+        gPC.printf("\n\rTesting No future blocks available");
+        schedule1[0] = {0,0,0,0,0,0,0,0};
+    }
+}*/
+void FCTN_CDMS_PL_MAIN(void const *args)
+{
+    uint8_t temp[3];
+    pl_main_flag|=PL_MAIN_STATUS;                       //Setting PL_MAIN_STATUS
+    PL_MAIN_COUNTER++;                                  
+    pl_main_flag&=~(PL_LOW_POWER);                      //Clearing PL_LOW_POWER
+    temp[0] = (pl_main_flag&STATE_SCIENCE)>>2;
+    temp[1] = (pl_main_flag&PL_DISABLED)>>4;
+    temp[2] = (PL_PREV_STATE & STATE_SCIENCE)>>2;
+    //test2(1);
+
+    if(PL_BEE_SW_STATUS==2 || PL_BEE_SW_STATUS==3)
+    {
+        gPC.printf("\n\rDevice Disabled or OC Fault");
+        pl_main_flag  = pl_main_flag & (~PL_DISABLED);
+        pl_main_flag |= PL_DISABLED;                        //setting PL_STATUS as PL_DISABLED
+        pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);    //Clearing pl_main status
+        print_exit(temp);
+        return;
+    }
+    PL_PREV_STATE = (pl_main_flag & STATE_SCIENCE);         //saving current pl_state
+    uint64_t temp_time;
+    temp_time=FCTN_CDMS_RD_RTC();
+    pl_time = (uint32_t)((temp_time>>7) & 0x000000000FFFFFFF);
+
+    //call test3() here
+    //test3(1);
+
+    //gPC.printf("\n\rprev_state    :%d",(PL_PREV_STATE)>>4);
+    gPC.printf("\n\n\rtTime_RTC:");
+    gPC.printf("\n\rYear :%d\t",((pl_time & 0x0C000000)>>26)+2016);
+    gPC.printf("Month :%d\t",((pl_time & 0x03C00000)>>22));
+    gPC.printf("Day :%d",((pl_time & 0x003E0000)>>17));
+    gPC.printf("\n\rHours :%d",((pl_time & 0x0001F000)>>12));
+    gPC.printf("\tMin :%d",((pl_time & 0x00000FC0)>>6));
+    gPC.printf("\t\tSec :%d",(pl_time & 0x0000003F));
+    if(pl_time!=0)                             //RTC read successful 
+    {
+        // run the loop until end of schedule is reached or month and day are both zeros or future block is found
+        gPC.printf("\n\rRTC read success");
+        for(i=0;(i<192)&&(((uint16_t)((pl_block[i] & 0x3FE00000)>>21))!=0);i++)     
+        {   
+            if(((pl_block[i]>>4) & 0x0FFFFFFF)>pl_time)     //Checking for future blocks       
+            {
+                pl_next_index=i;
+                gPC.printf("\n\rFuture block found at index = %d",pl_next_index);
+                break;
+            }
+        }
+    }
+    if((pl_next_index==-1) || pl_time==0)           //RTC read failed or Future block not found
+    {       
+        if(PL_PREV_STATE==STATE_SCIENCE)
+        {
+            pl_main_flag  = pl_main_flag & (~STATE_SCIENCE);
+            pl_main_flag |= STATE_HIBERNATE;
+        } 
+        else
+        {                        
+            pl_main_flag  = pl_main_flag & (~STATE_SCIENCE);
+            pl_main_flag |= PL_PREV_STATE;
+        }        
+        if(pl_time==0)
+        {
+            gPC.printf("\n\rRTC read failed");
+        }
+        if(pl_next_index==-1)
+        {
+            gPC.printf("\n\rFuture block not found");
+            gPC.printf("\n\rpl_next_index = %d",pl_next_index);
+        }
+        gPC.printf("\n\rNew pl_state  = %d",(pl_main_flag&(~STATE_SCIENCE))>>2);
+    }           
+    
+    //Processing the PL schedule
+    if(((pl_block[pl_next_index-1] & UNEXECUTED)==3)||((pl_block[pl_next_index-1] & UNEXECUTED)==2))
+    {        
+        if(((pl_block[pl_next_index-1] & UNEXECUTED)==3))
+            gPC.printf("\n\rElapsed blocked not executed");
+        else
+            gPC.printf("\n\rElapsed block marked for retry");
+
+        gPC.printf("\n\r Retrieving pl_state from schedule");
+        if((pl_block[pl_next_index-1] & 0x0000000C)==0)                
+        {            
+            pl_main_flag  = pl_main_flag & (~STATE_SCIENCE);
+            pl_main_flag |= STATE_OFF;
+        }
+        if((pl_block[pl_next_index-1] & 0x0000000C)==4)
+        {
+            pl_main_flag  = pl_main_flag & (~STATE_SCIENCE);
+            pl_main_flag |= STATE_STANDBY;
+        }
+        if((pl_block[pl_next_index-1] & 0x0000000C)==8)
+        {
+            pl_main_flag  = pl_main_flag & (~STATE_SCIENCE);
+            pl_main_flag |= STATE_HIBERNATE;
+        }
+        if((pl_block[pl_next_index-1] & 0x0000000C)==12)
+        {
+            pl_main_flag  = pl_main_flag & (~STATE_SCIENCE);
+            pl_main_flag |= STATE_SCIENCE;
+        }
+    }
+    else if((pl_block[pl_next_index-1] & UNEXECUTED)==1)
+    {
+        gPC.printf("\n\rElapsed block is executed");
+        pl_main_flag = pl_main_flag & (~PL_MAIN_STATUS);                  //Clearing pl_main status
+        print_exit(temp);
+        return;   
+    }      
+    else if((pl_block[pl_next_index-1] & UNEXECUTED)==0)  
+    {
+        gPC.printf("\n\rEmpty Schedule Block");        
+        pl_main_flag = pl_main_flag & (~PL_MAIN_STATUS);                  //Clearing pl_main status
+        print_exit(temp);
+        return;
+    }
+    switch(pl_main_flag & STATE_SCIENCE)                                  //Checking PL_STATE
+    {
+        case STATE_OFF: 
+        {
+            gPC.printf("\n\rEntered PL_OFF case");
+            if(PL_BEE_SW_STATUS!=0) 
+            {
+                gPC.printf("\n\rCommanding PL_BEE to go to Standby State");
+                Base_tm *tm_ptr_standby;
+                SET_PL_BEE_STANDBY(tm_ptr_standby);                             //No ack needed now
+                gPC.printf("\n\rPowering OFF PL_BEE");
+                SET_PL_BEE_OFF;
+                PL_BEE_SW_STATUS=0;    
+            }           
+            pl_main_flag  = pl_main_flag & (~PL_DISABLED);
+            pl_main_flag |= PL_OFF;    
+            pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
+            pl_block[pl_next_index-1] |= EXECUTED;                              
+            pl_main_flag = pl_main_flag & (~PL_MAIN_STATUS);                    //Clearing PL_MAIN_STATUS
+            print_processed_block(pl_next_index-1);
+            print_exit(temp);
+            return;
+        }
+        case STATE_STANDBY: 
+        {            
+            gPC.printf("\n\rEntered PL_STANDBY case");
+            if(PL_BEE_SW_STATUS==0) 
+            {
+                gPC.printf("\n\rPowering on PL_BEE");
+                SET_PL_BEE_ON;
+                PL_BEE_SW_STATUS=1;
+            }
+            gPC.printf("\n\rCommanding PL_BEE to go to Standby State");  
+            Base_tm *tm_ptr_standby;
+            SET_PL_BEE_STANDBY(tm_ptr_standby);
+            if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
+            {
+                pl_main_flag = pl_main_flag & (~PL_DISABLED);
+                pl_main_flag |= PL_STANDBY; 
+            }        
+            pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);               //Clearing PL_MAIN_STATUS 
+            print_processed_block(pl_next_index-1);
+            print_exit(temp);
+            return;                                                   
+                                                                            //////DELETE THE TM AND TC LATER
+        }
+        case STATE_HIBERNATE: 
+        {
+            gPC.printf("\n\rEntered PL_HIBERNATE case");
+            if(POWER_LEVEL==2 || POWER_LEVEL==3 || POWER_LEVEL==0)
+            {                
+                if(PL_BEE_SW_STATUS==0) 
+                {
+                    gPC.printf("Powering on PL_BEE\r\n");                    
+                    SET_PL_BEE_ON;
+                    PL_BEE_SW_STATUS=1;
+                }
+                gPC.printf("\n\rCommanding PL_BEE to go to Hibernate State");  
+                Base_tm *tm_ptr_hibernate;
+                SET_PL_BEE_HIBERNATE(tm_ptr_hibernate);
+                if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
+                {
+                    pl_main_flag = pl_main_flag & (~PL_DISABLED);
+                    pl_main_flag |= PL_HIBERNATE; 
+                }
+            }
+            else 
+            {
+                pl_main_flag |= PL_LOW_POWER;
+                if(PL_BEE_SW_STATUS==0) 
+                {
+                    gPC.printf("\n\rPowering on PL_BEE");
+                    SET_PL_BEE_ON;
+                    PL_BEE_SW_STATUS=1;
+                }
+                gPC.printf("\n\rCommanding PL_BEE to go to Standby State");
+                Base_tm *tm_ptr_standby;
+                SET_PL_BEE_STANDBY(tm_ptr_standby);
+                if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
+                {
+                    pl_main_flag = pl_main_flag & (~PL_DISABLED);
+                    pl_main_flag |= PL_STANDBY; 
+                }        
+                pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);               //Clearing PL_MAIN_STATUS 
+                print_processed_block(pl_next_index-1);
+                print_exit(temp);
+                return;
+                                                                                //////DELETE THE TM AND TC LATER
+            }            
+            pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);               //Clearing PL_MAIN_STATUS
+            print_processed_block(pl_next_index-1);
+            print_exit(temp);
+            return;
+                                                                            //////DELETE THE TM LATER
+        }
+        case STATE_SCIENCE:
+        {
+            gPC.printf("\n\rEntered PL_SCIENCE case");
+            if(POWER_LEVEL==3 || POWER_LEVEL==0)                            //POWER_LEVEL = 0 = NA
+            {
+                gPC.printf("\n\rPOWER_LEVEL = 3 or NA");
+                if(PL_BEE_SW_STATUS==0)
+                {
+                    gPC.printf("\n\rPowering on PL_BEE");
+                    SET_PL_BEE_ON;
+                    PL_BEE_SW_STATUS=1;
+                }
+                gPC.printf("\n\rCommanding PL_BEE to go to Science State");  
+                Base_tm *tm_ptr_science;
+                SET_PL_BEE_SCIENCE(tm_ptr_science);
+                if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
+                {
+                    pl_main_flag = pl_main_flag & (~PL_DISABLED);
+                    pl_main_flag |= PL_SCIENCE; 
+                }
+                pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);               //Clearing PL_MAIN_STATUS
+                print_processed_block(pl_next_index-1);
+                print_exit(temp);
+                return;
+            }
+            else
+            {   
+                gPC.printf("\n\rPower level = 2,3 or NA");
+                pl_main_flag |= PL_LOW_POWER;
+                if(POWER_LEVEL==2 || POWER_LEVEL==3 || POWER_LEVEL==0)
+                {                
+                    if(PL_BEE_SW_STATUS==0) 
+                    {
+                        gPC.printf("\n\rPowering on PL_BEE");
+                        SET_PL_BEE_ON;
+                        PL_BEE_SW_STATUS=1;
+                    }
+                    gPC.printf("\n\rCommanding PL_BEE to go to Hibernate State");  
+                    Base_tm *tm_ptr_hibernate;
+                    SET_PL_BEE_HIBERNATE(tm_ptr_hibernate);
+                    if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
+                    {
+                        pl_main_flag = pl_main_flag & (~PL_DISABLED);
+                        pl_main_flag |= PL_HIBERNATE; 
+                    }
+                }
+                else 
+                {
+                    pl_main_flag |= PL_LOW_POWER;
+                    if(PL_BEE_SW_STATUS==0) 
+                    {
+                        gPC.printf("\n\rPowering on PL_BEE");
+                        SET_PL_BEE_ON;
+                        PL_BEE_SW_STATUS=1;
+                    }
+                    gPC.printf("\n\rCommanding PL_BEE to go to Standby State");  
+                    Base_tm *tm_ptr_standby;
+                    SET_PL_BEE_STANDBY(tm_ptr_standby);
+                    if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
+                    {
+                        pl_main_flag = pl_main_flag & (~PL_DISABLED);
+                        pl_main_flag |= PL_STANDBY; 
+                    }        
+                    pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);               //Clearing PL_MAIN_STATUS 
+                    print_processed_block(pl_next_index-1);
+                    print_exit(temp);
+                    return;                                               
+                                                                                    //////DELETE THE TM AND TC LATER
+                }            
+                pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);               //Clearing PL_MAIN_STATUS
+                print_processed_block(pl_next_index-1);
+                print_exit(temp);
+                return;
+                                                                                //////DELETE THE TM LATER
+            }
+        }
+        default:
+        {
+            gPC.printf("\n\rInvalid PL_STATE in block at index = %d",pl_next_index-1);
+            pl_main_flag  = pl_main_flag & (~PL_DISABLED);
+            pl_main_flag |= PL_INVALID_STATE;
+            pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
+            pl_block[pl_next_index-1] |= EXECUTED;
+            pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);               //Clearing PL_MAIN_STATUS
+            print_processed_block(pl_next_index-1);
+            print_exit(temp);
+            return;
+        }
+    }    
+}
\ No newline at end of file
--- a/COM_MNG_TMTC.h	Wed Jun 29 14:17:34 2016 +0000
+++ b/COM_MNG_TMTC.h	Thu Jun 30 14:00:33 2016 +0000
@@ -11,7 +11,6 @@
 //added included related files
 
 //#include "Flash.h"
-#include "cdms_rtc.h"
 
 DigitalIn tm_status_4m_slv(PIN39); //I2C interrupt to CDMS from BAE
 //DigitalIn tm_status_4m_pl(PIN61); //I2C interrupt to CDMS from PL
@@ -27,6 +26,11 @@
 uint16_t mid;
 uint32_t block;
 
+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);
+
 #define delete_TC(tc_ptr) {\
     if(tc_ptr == gHEAD_NODE_TCL){\
         gHEAD_NODE_TCL = tc_ptr->next_TC;\
@@ -195,636 +199,732 @@
 /*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
-inline Base_tm* FCTN_CDMS_RLY_TMTC(Base_tc *tc_ptr){
-    uint8_t ACKCODE = 0x00;
-    //printf("\rTC execution in progress\r\n");
+void ACK_L234(Base_tm* tm_pointer_short1,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;
+    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;                                                 
+}
+void TM_PMS_PL_SCHEDULE(Base_tm *tm_pointer1,uint8_t psc)
+{
+    Base_tm *tm_pointer2 = NULL;
+    if(!(tm_pointer2  = new Long_tm))
+    {
+       gPC.printf("\n\rError: out of memory");
+       return;   
+    }
+    Base_tm *tm_pointer3 = NULL;
+    if(!(tm_pointer3  = new Long_tm))
+    {
+       gPC.printf("\n\rError: out of memory");
+       return;   
+    }
+    Base_tm *tm_pointer4 = NULL;
+    if(!(tm_pointer4  = new Long_tm))
+    {
+       gPC.printf("\n\rError: out of memory");
+       return;   
+    }
+    Base_tm *tm_pointer5 = NULL;
+    if(!(tm_pointer5  = new Long_tm))
+    {
+       gPC.printf("\n\rError: out of memory");
+       return;   
+    }    
+    Base_tm *tm_pointer6 = NULL;
+    if(!(tm_pointer6  = new Long_tm))
+    {
+       gPC.printf("\n\rError: out of memory");
+       return;   
+    }
+    tm_pointer1->TM_string[0] = 0x48;     
+    tm_pointer1->TM_string[1] = psc;
+    tm_pointer1->TM_string[2] = 0xA0;
+    tm_pointer1->TM_string[3] = 0x01;
+    for(uint8_t i=0;i<128;i+=4)
+    {
+        tm_pointer1->TM_string[i+4] = (uint8_t)((pl_block[i/4] & 0xFF000000)>>24);
+        tm_pointer1->TM_string[i+5] = (uint8_t)((pl_block[i/4] & 0x00FF0000)>>16);
+        tm_pointer1->TM_string[i+6] = (uint8_t)((pl_block[i/4] & 0x0000FF00)>>8);
+        tm_pointer1->TM_string[i+7] = (uint8_t)((pl_block[i/4] & 0x000000FF));                        
+    }
+    crc16 = crc16_gen(tm_pointer1->TM_string,132);
+    tm_pointer1->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
+    tm_pointer1->TM_string[133] = (uint8_t)(crc16&0x00FF);    
+    tm_pointer1->next_TM = tm_pointer2;
+    
+    tm_pointer2->TM_string[0] = 0x48;     
+    tm_pointer2->TM_string[1] = psc;
+    tm_pointer2->TM_string[2] = 0xA0;
+    tm_pointer2->TM_string[3] = 0x02;
+    for(uint8_t i=0;i<128;i+=4)
+    {
+        tm_pointer2->TM_string[i+4] = (uint8_t)((pl_block[(i/4)+32] & 0xFF000000)>>24);
+        tm_pointer2->TM_string[i+5] = (uint8_t)((pl_block[(i/4)+32] & 0x00FF0000)>>16);
+        tm_pointer2->TM_string[i+6] = (uint8_t)((pl_block[(i/4)+32] & 0x0000FF00)>>8);
+        tm_pointer2->TM_string[i+7] = (uint8_t)((pl_block[(i/4)+32] & 0x000000FF));                        
+    }
+    crc16 = crc16_gen(tm_pointer2->TM_string,132);
+    tm_pointer2->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
+    tm_pointer2->TM_string[133] = (uint8_t)(crc16&0x00FF);
+    tm_pointer2->next_TM = tm_pointer3;
+
+    tm_pointer3->TM_string[0] = 0x48;     
+    tm_pointer3->TM_string[1] = psc;
+    tm_pointer3->TM_string[2] = 0xA0;
+    tm_pointer3->TM_string[3] = 0x03;
+    for(uint8_t i=0;i<128;i+=4)
+    {
+        tm_pointer3->TM_string[i+4] = (uint8_t)((pl_block[(i/4)+64] & 0xFF000000)>>24);
+        tm_pointer3->TM_string[i+5] = (uint8_t)((pl_block[(i/4)+64] & 0x00FF0000)>>16);
+        tm_pointer3->TM_string[i+6] = (uint8_t)((pl_block[(i/4)+64] & 0x0000FF00)>>8);
+        tm_pointer3->TM_string[i+7] = (uint8_t)((pl_block[(i/4)+64] & 0x000000FF));                        
+    }
+    crc16 = crc16_gen(tm_pointer3->TM_string,132);
+    tm_pointer3->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
+    tm_pointer3->TM_string[133] = (uint8_t)(crc16&0x00FF);
+    tm_pointer3->next_TM = tm_pointer4;
+
+    tm_pointer4->TM_string[0] = 0x48;     
+    tm_pointer4->TM_string[1] = psc;
+    tm_pointer4->TM_string[2] = 0xA0;
+    tm_pointer4->TM_string[3] = 0x04;
+    for(uint8_t i=0;i<128;i+=4)
+    {
+        tm_pointer4->TM_string[i+4] = (uint8_t)((pl_block[(i/4)+96] & 0xFF000000)>>24);
+        tm_pointer4->TM_string[i+5] = (uint8_t)((pl_block[(i/4)+96] & 0x00FF0000)>>16);
+        tm_pointer4->TM_string[i+6] = (uint8_t)((pl_block[(i/4)+96] & 0x0000FF00)>>8);
+        tm_pointer4->TM_string[i+7] = (uint8_t)((pl_block[(i/4)+96] & 0x000000FF));                        
+    }
+    crc16 = crc16_gen(tm_pointer4->TM_string,132);
+    tm_pointer4->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
+    tm_pointer4->TM_string[133] = (uint8_t)(crc16&0x00FF);                        
+    tm_pointer4->next_TM = tm_pointer5;
+
+    tm_pointer5->TM_string[0] = 0x48;     
+    tm_pointer5->TM_string[1] = psc;
+    tm_pointer5->TM_string[2] = 0xA0;
+    tm_pointer5->TM_string[3] = 0x05;
+    for(uint8_t i=0;i<128;i+=4)
+    {
+        tm_pointer5->TM_string[i+4] = (uint8_t)((pl_block[(i/4)+128] & 0xFF000000)>>24);
+        tm_pointer5->TM_string[i+5] = (uint8_t)((pl_block[(i/4)+128] & 0x00FF0000)>>16);
+        tm_pointer5->TM_string[i+6] = (uint8_t)((pl_block[(i/4)+128] & 0x0000FF00)>>8);
+        tm_pointer5->TM_string[i+7] = (uint8_t)((pl_block[(i/4)+128] & 0x000000FF));                        
+    }
+    crc16 = crc16_gen(tm_pointer5->TM_string,132);
+    tm_pointer5->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
+    tm_pointer5->TM_string[133] = (uint8_t)(crc16&0x00FF);
+    tm_pointer5->next_TM = tm_pointer6;
+
+    tm_pointer6->TM_string[0] = 0x48;     
+    tm_pointer6->TM_string[1] = psc;
+    tm_pointer6->TM_string[2] = 0xA0;
+    tm_pointer6->TM_string[3] = 0x06;
+    for(uint8_t i=0;i<128;i+=4)
+    {
+        tm_pointer6->TM_string[i+4] = (uint8_t)((pl_block[(i/4)+160] & 0xFF000000)>>24);
+        tm_pointer6->TM_string[i+5] = (uint8_t)((pl_block[(i/4)+160] & 0x00FF0000)>>16);
+        tm_pointer6->TM_string[i+6] = (uint8_t)((pl_block[(i/4)+160] & 0x0000FF00)>>8);
+        tm_pointer6->TM_string[i+7] = (uint8_t)((pl_block[(i/4)+160] & 0x000000FF));                        
+    }
+    crc16 = crc16_gen(tm_pointer6->TM_string,132);
+    tm_pointer6->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
+    tm_pointer6->TM_string[133] = (uint8_t)(crc16&0x00FF);
+    tm_pointer6->next_TM = NULL;
+}
+Base_tm* FCTN_CDMS_RLY_TMTC(Base_tc *tc_ptr)
+{
+    //uint8_t ACKCODE = 0xB0;
+    bool y;
+    //gPC.printf("\rTC execution in progress\r\n");
     Base_tm *tm_pointer = new Long_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))
+    switch(GETapid(tc_ptr))             //checking APID's
     {
         case 1: //apid=01 implies it corresponds to bae
         {
-            //printf("Telecommand is for BAE\r\n");
-            //printf("Sending TC to BAE...\r\n"); //interrupt to be sent to the bae
-            FCTN_I2C_WRITE((char*)tc_ptr->TC_string);
-            while(1)
+            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;
+            }                                  
+            else if(y==0)
             {
-                wait(1); //TimeOut instead of wait
-                if(tm_status_4m_slv == 1)
+                //wait(0.2);
+                uint8_t poll=0;
+                while(poll<60 && BAE_I2C_GPIO==0)
+                {
+                    wait_ms(50);
+                    poll+=1;
+                }    
+                if(BAE_I2C_GPIO == 1)                                            //name to be changed later
                 {
-                    //printf("receiving...\r\n");
-                    FCTN_I2C_READ((char*)tm_pointer->TM_string,134);
-                    //printf("%s", tm_pointer->TM_string);
-                    received+=1;
-                    tm_pointer = tm_pointer->next_TM;
-                    break; //only for testing purpose
+                    gPC.printf("BAE_I2C_GPIO is high\r\n");
+                    
+                    y=FCTN_I2C_READ((char*)tm_pointer->TM_string,TM_LONG_SIZE);            //rify later about the size
+                    if(y==0)
+                    {
+                        tm_pointer->next_TM=NULL;
+                        return tm_ptr;
+                    }
+                    else if(y==1)
+                    {
+                        ACK_L234(tm_ptr_short,0x85,GETpacket_seq_count(tc_ptr));                
+                        //gPC.printf("\n\rPTE->PDIR = 0x%08X",PTE->PDIR);
+                        return tm_ptr_short;
+                        break;  
+                    }
                 }
-                else
+                else if(BAE_I2C_GPIO == 0)                                       //name to be changed later
                 {
-                    break;
+                    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;
                 }
             }
-            if(received >= 1 )
-            {
-                //printf("Telemetry is received from BAE\r\n");
-                Base_tm *tm_print = tm_ptr;
-                for(uint8_t i=0;i<received;i++)
-                {  
-                    //printf("%s", tm_print->TM_string);
-                    tm_print = tm_print->next_TM; //for check
-                }
-                received  = 0;
-                tm_pointer->next_TM = NULL;                 return tm_ptr;
-            }
-            else
-            {
-                //printf("Telemetry is not received from BAE\r\n");
-                //tm_pointer = new Short_tm;
-                tm_pointer->TM_string[0] = 0xB0;
-                tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);
-                tm_pointer->TM_string[2] = 0x01;
-                tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                tm_pointer->TM_string[4] = 0x01;
-                for(uint8_t i=0;i<6;i++)
-                {
-                    tm_pointer->TM_string[i+5] = 0;
-                }
-                crc16 = crc16_gen(tm_ptr->TM_string,11);
-                tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                tm_pointer->next_TM = NULL;                 return tm_ptr;
-            }
-            break;
         }
-        case 2:
+
+        case 2: // APID = 2 corresponds to CDMS
         {
             //gPC.printf("Telecommand is for CDMS\r\n"); //apid = 10 corresponds to cdms
             switch(GETservice_type(tc_ptr))
             {
-                case 0x60:
+                /*case 0x60:  //service type = 0x6
                 {
-                        //gPC.printf("service:MMS\r\n");
-                        switch(GETservice_subtype(tc_ptr))
-                        {
-                            case 0x1:
-                            {
-                            //printf("sub_service:Read from RAM_Memory\r\n");
-                            mid=(uint16_t)(tc_ptr->TC_string[3]<<4)+(uint16_t)(tc_ptr->TC_string[4]);                                                       
-                                                             
-                                    //Data[0] = FCTN_CDMS_RD_FLASH(0);
-                                    //Data[1] = FCTN_CDMS_RD_FLASH(1);
-                                    //tm_pointer = new Long_tm;
-                                    tm_pointer->TM_string[0] = 0x30;
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);
-                                    tm_pointer->TM_string[2] = (uint8_t)(flash_counter);
-                                    for(uint8_t i=0;i<4;i++)
-                                    {
-                                        tm_pointer->TM_string[3+i] = (uint8_t)((Data[0]>>(8*i))&0x00FF);
-                                    }
-                                    for(uint8_t i=4;i<8;i++)
-                                    {
-                                        tm_pointer->TM_string[3+i] = (uint8_t)((Data[1]>>(8*i))&0x00FF);
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,9);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                tm_pointer->next_TM = NULL;                 return tm_ptr;
-                            }                            
-                            case 0x2:
-                            {
-                            //printf("sub_service:Read from Flash_Memory\r\n");
-                            mid=(uint16_t)(tc_ptr->TC_string[3]<<4)+(uint16_t)(tc_ptr->TC_string[4]);                           
-                                                                                             
-                                    //Data[0] = FCTN_CDMS_RD_FLASH(0);
-                                    //Data[1] = FCTN_CDMS_RD_FLASH(1);                                                                                             
-                                    //tm_pointer = new Long_tm;
-                                    tm_pointer->TM_string[0] = 0x30;
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);
-                                    tm_pointer->TM_string[2] = (uint8_t)(flash_counter);
-                                    for(uint8_t i=0;i<4;i++)
-                                    {
-                                        tm_pointer->TM_string[3+i] = (uint8_t)((Data[0]>>(8*i))&0x00FF);
-                                    }
-                                    for(uint8_t i=4;i<8;i++)
-                                    {
-                                        tm_pointer->TM_string[3+i] = (uint8_t)((Data[1]>>(8*i))&0x00FF);
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,9);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                tm_pointer->next_TM = NULL;                 return tm_ptr;
-                            }                           
-                            case 0x5:
-                            {
-                            //printf("WRITE ON FLASH_MEMORY\r\n");                          
-                                    mid=(uint16_t)(tc_ptr->TC_string[3]<<4)+(uint16_t)(tc_ptr->TC_string[4]);                                   
-                                    block = (((uint32_t)(tc_ptr->TC_string[5])<<24)|((uint32_t)(tc_ptr->TC_string[5])<<16)|((uint32_t)(tc_ptr->TC_string[5])<<8)|((uint32_t)(tc_ptr->TC_string[5])));
-                                    //FCTN_CDMS_WR_FLASH(mid,block);
-                                    //tm_pointer = new Short_tm;
-                                    tm_pointer->TM_string[0] = 0xB0;
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);
-                                    tm_pointer->TM_string[2] = 0x01;
-                                    tm_pointer->TM_string[3] = ACKCODE;  //ackcode to be decided
-                                    tm_pointer->TM_string[4] = 0x01;
-                                    for(uint8_t i=0;i<6;i++)
-                                    {
-                                        tm_pointer->TM_string[i+5] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                            }                                                    
-                            default:
-                            {
-                                //printf("INVALID TC\r\n"); //Send Invalid TC Telemetry
-                                //tm_pointer = new Short_tm;
-                                tm_pointer->TM_string[0] = 0xB0;
-                                tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);
-                                tm_pointer->TM_string[2] = 0x01;
-                                tm_pointer->TM_string[3] = ACKCODE;  //ackcode to be decided
-                                tm_pointer->TM_string[4] = 0x01;
-                                for(uint8_t i=0;i<6;i++)
-                                {
-                                    tm_pointer->TM_string[i+5] = 0;
-                                }
-                                crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                tm_pointer->next_TM = NULL;                 return tm_ptr;                               
-                            }
-                            break;
-                        }
-                    }                                                                   
-                    case 0x8:
+                    gPC.printf("service:MMS\r\n");
+                    switch(GETservice_subtype(tc_ptr))      
                     {
-                        //printf("service:FUNCTION MANAGEMENT SERVICE\r\n"); 
-                        if(GETservice_subtype(tc_ptr)==0x1)
-                            {                               
-                              if(GETpid(tc_ptr)==0x01)
-                                {
-                                    //printf("TC_PL_INIT\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                   
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }                                
-                                else if(GETpid(tc_ptr)==0x02)
-                                {
-                                    //printf("TC_PL_MAIN\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                   
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                               else if(GETpid(tc_ptr)==0x03)
-                                {
-                                    //printf("TC_COM_INIT\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                   
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                                else if(GETpid(tc_ptr)==0x04)
-                                {
-                                    //printf("TC_CDMS_HK_MAIN\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                    
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;                                   
-                                }
-                                else if(GETpid(tc_ptr)==0x11)
-                                {
-                                    //printf("TC_SW_ON_SD\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                   
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                                else if(GETpid(tc_ptr)==0x12)
-                                {
-                                    //printf("TC_SW_ON_RTC\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                    
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                                else if(GETpid(tc_ptr)==0x13)
-                                {
-                                    //printf("TC_SW_ON_BAE\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                   
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                                else if(GETpid(tc_ptr)==0x14)
-                                {
-                                    //printf("TC_SW_ON_PL_DL\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                    
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                                 else if(GETpid(tc_ptr)==0x15)
-                                {
-                                    //printf("TC_SW_ON_PL_AG_HV\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                   
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                                 else if(GETpid(tc_ptr)==0x16)
-                                {
-                                    //printf("TC_SW_ON_V_A_EN\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                   
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;                                   
-                                }
-                                else if(GETpid(tc_ptr)==0x21)
-                                {
-                                    //printf("TC_SW_OFF_SD\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                   
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                                else if(GETpid(tc_ptr)==0x22)
-                                {
-                                    //printf("TC_SW_OFF_RTC\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                    
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                                else if(GETpid(tc_ptr)==0x23)
-                                {
-                                    //printf("TC_SW_OFF_BAE\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                   
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                                else if(GETpid(tc_ptr)==0x24)
-                                {
-                                    //printf("TC_SW_OFF_PL_DL\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                   
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                                 else if(GETpid(tc_ptr)==0x25)
-                                {
-                                    //printf("TC_SW_OFF_PL_AG_HV\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                   
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                                 else if(GETpid(tc_ptr)==0x26)
-                                {
-                                    //printf("TC_SW_OFF_V_A_EN\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                    
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                                else if(GETpid(tc_ptr)==0x31)
-                                {
-                                    //printf("TC_RST_SD\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                   
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                                else if(GETpid(tc_ptr)==0x32)
-                                {
-                                    //printf("TC_RST_RTC\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                   
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                                else if(GETpid(tc_ptr)==0x33)
-                                {
-                                    //printf("TC_RST_BAE\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                   
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                                else if(GETpid(tc_ptr)==0x34)
-                                {
-                                    //printf("TC_RST_PL_DL\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                   
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                    break;
-                                }
-                                else if(GETpid(tc_ptr)==0xC1)
-                                {
-                                    //printf("RESET_HK_COUNTER\r\n"); // call PWR_SWCH_ON function
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = ACKCODE;                                   
-                                    for(uint8_t i=3;i<11;i++)
-                                    {
-                                        tm_pointer->TM_string[i] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                    break;
-                                }
-                                else if(GETpid(tc_ptr)==0xF1)
-                                {
-                                    //printf("RD_RTC\r\n"); //call RD_RTC
-                                    //tm_pointer = new Short_tm;
-                                    tm_pointer->TM_string[0] = 0xD0;
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);
-                                    tm_pointer->TM_string[2] = 0x01;
-                                    uint64_t time = FCTN_CDMS_RD_RTC();
-                                    tm_pointer->TM_string[3] = (uint8_t)((time&0xFF00000000000000)>>56);
-                                    tm_pointer->TM_string[4] = (uint8_t)((time&0x00FF000000000000)>>48);
-                                    tm_pointer->TM_string[5] = (uint8_t)((time&0x0000FF0000000000)>>40);
-                                    tm_pointer->TM_string[6] = (uint8_t)((time&0x000000FF00000000)>>32);
-                                    tm_pointer->TM_string[7] = (uint8_t)((time&0x00000000FF000000)>>24);
-                                    tm_pointer->TM_string[8] = (uint8_t)((time&0x0000000000FF0000)>>16);
-                                    tm_pointer->TM_string[9] = (uint8_t)((time&0x000000000000FF00)>>8);
-                                    tm_pointer->TM_string[10] = (uint8_t)(time&0x00000000000000FF);
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                }                               
-                                else 
-                                {
-                                    //printf("INVALID TC\r\n");
-                                    //tm_pointer = new Short_tm; 
-                                    tm_pointer->TM_string[0] = 0xB0; 
-                                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                                    tm_pointer->TM_string[2] = 0x01;
-                                    tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                                    tm_pointer->TM_string[4] = 0x01;
-                                    for(uint8_t i=0;i<6;i++)
-                                    {
-                                        tm_pointer->TM_string[i+5] = 0;
-                                    }
-                                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                                }
-                            }                          
-                                              
-                                     
+                        case 0x2:   //service subtye                        
+                        {
+                            //gPC.printf("sub_service:Read LONG_RAM_Memory\r\n");
+                            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;
+                            }
+                            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
+                            
+                            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->next_TM = NULL;
+                            return tm_ptr;
+                        }                           
+                        case 0x5:                           // change the cases appropriately
+                        {
+                            //gPC.printf("WRITE ON SHORT_FLASH_MEMORY\r\n");                          
+                            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?
+                            {
+                                ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr_short;
+                            }                                                        
+
+                            FCTN_CDMS_WR_S_FLASH(tc_ptr);                            
+
+                            ACK_L234(tm_ptr_short,???,GETpacket_seq_count(tc_ptr));                     //ackCode = ? for success
+
+                            return tm_ptr_short;
+                        }
+                        case 0x6:
+                        {
+                            //gPC.printf("WRITE ON LONG_FLASH_MEMORY\r\n");                          
+                            mid=(uint16_t)(tc_ptr->TC_string[3]<<4)+(uint16_t)(tc_ptr->TC_string[4]);           
+                            //1024 bits at a time
+
+                            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;
+                            }    
+
+                            ///Send TM for success after writing onto flash
+                            FCTN_CDMS_WR_L_FLASH(mid);
+
+                            return tm_ptr_short;
+                        }                                                    
+                        default:
+                        {
+                            gPC.printf("INVALID SERVICE SUBTYPE\r\n");
+                            ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;
+                        }
+                    } 
+                }  */                                                                 
+                case 0x80:
+                {
+                    gPC.printf("service:FUNCTION MANAGEMENT SERVICE\r\n"); 
+                    if(GETservice_subtype(tc_ptr)==0x1)
+                    {                               
+                        if(GETpid(tc_ptr)==0x01)
+                        {
+                            if(CDMS_STANDBY==1)
+                            {   
+                                //gPC.printf("TC_PL_INIT\r\n");                         // call PWR_SWCH_ON function
+                                
+                                P_PL_INIT();
+
+                                ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr_short;
+                            }                
+                            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;
+                            }
+                        }                                
+                        else if(GETpid(tc_ptr)==0x02)
+                        {
+                            if(CDMS_STANDBY==1)
+                            {   
+                                //gPC.printf("TC_PL_MAIN\r\n");                         // call PWR_SWCH_ON function
+                                
+                                P_PL_MAIN();
+
+                                ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr_short;
+                            }                
+                            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;
+                            }
+                        }
+                        else if(GETpid(tc_ptr)==0x03)
+                        {                            
+                            if(CDMS_STANDBY==1)
+                            {   
+                                //gPC.printf("PL_COM_INIT\r\n");                        // call PWR_SWCH_ON function
+                                
+                                P_COM_INIT();
+
+                                ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr_short;
+                            }                
+                            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;
+                            }
+                        }
+                        else if(GETpid(tc_ptr)==0x04)
+                        {                            
+                            if(CDMS_STANDBY==1)
+                            {   
+                                //gPC.printf("TC_P_CDMS_HK_MAIN\r\n"); // call PWR_SWCH_ON function
+                                
+                                P_CDMS_HK_MAIN();
+
+                                ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr_short;
+                            }                
+                            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;
+                            }
+                        }
+                        else if(GETpid(tc_ptr)==0x05)
+                        {                            
+                            if(CDMS_STANDBY==1)
+                            {   
+                                //gPC.printf("TC_SW_ON_SD\r\n");                        // call PWR_SWCH_ON function
+                                
+                                P_PL_RCV_SC_DATA();
+
+                                ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr_short;
+                            }                
+                            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;
+                            }
+                        }
+                        else if(GETpid(tc_ptr)==0x06)
+                        {                            
+                            if(CDMS_STANDBY==1)
+                            {   
+                                //gPC.printf("TC_SW_ON_RTC\r\n"); // call PWR_SWCH_ON function
+                                
+                                P_CDMS_INIT_SD();
+
+                                ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr_short;
+                            }                
+                            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;
+                            }    
+                        }
+                        else if(GETpid(tc_ptr)==0x11)
+                        {       
+                            //gPC.printf("TC_SW_ON_SD\r\n"); // call PWR_SWCH_ON function
+                            
+                            CDMS_SD_SW_ON();
+
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;                                                       
+                        }
+                        else if(GETpid(tc_ptr)==0x21)
+                        {       
+                            //gPC.printf("TC_SW_ON_SD\r\n"); // call PWR_SWCH_ON function
+                            
+                            CDMS_SD_SW_OFF();
+
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;     
+                        }
+                        else if(GETpid(tc_ptr)==0x13)
+                        {                           
+                            //gPC.printf("TC_SW_ON_BAE\r\n"); // call PWR_SWCH_ON function
+                            
+                            SW_ON_BAE();
+
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;                            
+                        }
+                        else if(GETpid(tc_ptr)==0x23)
+                        {                           
+                            //gPC.printf("TC_SW_OFF_BAE\r\n"); // call PWR_SWCH_ON function
+                            
+                            SW_OFF_BAE();
+
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;                                                        
+                        }
+                        else if(GETpid(tc_ptr)==0x14)
+                        {                            
+                            //gPC.printf("TC_SW_ON_PL_BEE\r\n"); // call PWR_SWCH_ON function
+                            
+                            SW_ON_PL_BEE();
+
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;                                                        
+                        }
+                        else if(GETpid(tc_ptr)==0x24)
+                        {                   
+                            //gPC.printf("TC_SW_OFF_PL_BEE\r\n"); // call PWR_SWCH_ON function
+                            
+                            SW_OFF_PL_BEE();
+
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;    
+                        }
+                        else if(GETpid(tc_ptr)==0x15)
+                        {                            
+                            //gPC.printf("TC_SW_ON_PL_EPS\r\n"); // call PWR_SWCH_ON function
+                            
+                            SW_ON_PL_EPS();
+
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));     
+                            return tm_ptr_short;                            
+                        }
+                        else if(GETpid(tc_ptr)==0x25)
+                        {                            
+                            //gPC.printf("TC_SW_OFF_PL_EPS\r\n"); // call PWR_SWCH_ON function
+                            
+                            SW_OFF_PL_EPS();
+
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;                            
+                        }
+                        else if(GETpid(tc_ptr)==0x16)
+                        {                            
+                                                                                       
+                            //gPC.printf("TC_SW_ON_V_A_EN\r\n"); // call PWR_SWCH_ON function
+                            
+                            SW_ON_V_A_EN();
+
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;                        
+                        }
+                        else if(GETpid(tc_ptr)==0x26)
+                        {                        
+                            //gPC.printf("TC_SW_OFF_V_A_EN\r\n"); // call PWR_SWCH_ON function
+                            
+                            SW_OFF_V_A_EN();
+
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;                                                    
+                        }
+                        else if(GETpid(tc_ptr)==0x31)
+                        {                            
+                            //gPC.printf("TC_RST_SD\r\n"); // call PWR_SWCH_ON function
+                            
+                            RST_SD();
+
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;                                                        
+                        }
+                        else if(GETpid(tc_ptr)==0x33)
+                        {                            
+                            //gPC.printf("TC_RST_BAE\r\n"); // call PWR_SWCH_ON function
+                            
+                            RST_BAE();
+
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;                            
+                        }
+                        else if(GETpid(tc_ptr)==0x34)
+                        {                            
+                            //gPC.printf("TC_RST_PL_BEE\r\n"); // call PWR_SWCH_ON function
+                            
+                            RST_PL_BEE();
+
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;                                                        
+                        }
+                        else if(GETpid(tc_ptr)==0x40)
+                        {                         
+                            //gPC.printf("RESET_HK_COUNTER\r\n"); // call PWR_SWCH_ON function
+                            
+                            RST_HK_COUNTER();
+
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;                                                    
+                        }
+                        else if(GETpid(tc_ptr)==0x41)
+                        {                            
+                            //gPC.printf("RESET_HK_COUNTER\r\n"); // call PWR_SWCH_ON function
+                            
+                            RST_HK_COUNTER();
+
+                            ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;    
+                        }
+                        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
+                            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[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));
+                            for(uint8_t i=0;i<124;i++)
+                            {
+                                tm_pointer->TM_string[i+8] = 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;                 
+                            return tm_ptr;                                                     //incomplete
+                        }                               
+                        else 
+                        {
+                            gPC.printf("INVALID FID\r\n");
+                            ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));  
+                            return tm_ptr_short;
+                        }
+                    }
+                    else 
+                    {
+                        gPC.printf("INVALID SERVICE SUBTYPE\r\n");
+                        ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));  
+                        return tm_ptr_short;
+                    }                                    
+                }*/
+                case 0x70:
+                {
+                    gPC.printf("\n\n\rService:PAYLOAD MANAGEMENT SERVICE"); 
+                    if(GETservice_subtype(tc_ptr)==0x1)                     //make sure it is LONG TC before executing else INVALID TC
+                    {
+                        gPC.printf("\n\rTC with subtype = 0x1");
+                        TM_PMS_PL_SCHEDULE(tm_ptr,GETpacket_seq_count(tc_ptr));
+                        for(uint8_t i=0,j=0;i<32;i++)
+                        {
+                            pl_block[i] &= 0x00000000;
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[3+j]))<<24);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[4+j]))<<16);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[5+j]))<<8);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[6+j])));
+                            j+=4;
+                        }                        
+                        return tm_ptr;
+                    }                    
+                    else if(GETservice_subtype(tc_ptr)==0x2)
+                    {
+                        gPC.printf("\n\rTC with subtype = 0x2");
+                        TM_PMS_PL_SCHEDULE(tm_ptr,GETpacket_seq_count(tc_ptr));
+                        for(uint8_t i=32,j=0;i<64;i++,j+=4)
+                        {
+                            pl_block[i] &= 0x00000000;
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[3+j]))<<24);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[4+j]))<<16);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[5+j]))<<8);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[6+j])));
+                        }
+                        return tm_ptr;
+                    }
+                    else if(GETservice_subtype(tc_ptr)==0x3)
+                    {
+                        gPC.printf("\n\rTC with subtype = 0x3");
+                        TM_PMS_PL_SCHEDULE(tm_ptr,GETpacket_seq_count(tc_ptr));
+                        for(uint8_t i=64,j=0;i<96;i++,j+=4)
+                        {
+                            pl_block[i] &= 0x00000000;
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[3+j]))<<24);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[4+j]))<<16);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[5+j]))<<8);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[6+j])));
+                        }
+                        return tm_ptr;
+                    }
+                    else if(GETservice_subtype(tc_ptr)==0x4)
+                    {
+                        gPC.printf("\n\rTC with subtype = 0x4");
+                        TM_PMS_PL_SCHEDULE(tm_ptr,GETpacket_seq_count(tc_ptr));
+                        for(uint8_t i=96,j=0;i<128;i++,j+=4)
+                        {
+                            pl_block[i] &= 0x00000000;
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[3+j]))<<24);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[4+j]))<<16);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[5+j]))<<8);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[6+j])));
+                        }
+                        return tm_ptr;
+                    }
+                    else if(GETservice_subtype(tc_ptr)==0x5)
+                    {   
+                        gPC.printf("\n\rTC with subtype = 0x5");
+                        TM_PMS_PL_SCHEDULE(tm_ptr,GETpacket_seq_count(tc_ptr));
+                        for(uint8_t i=128,j=0;i<160;i++,j+=4)
+                        {
+                            pl_block[i] &= 0x00000000;
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[3+j]))<<24);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[4+j]))<<16);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[5+j]))<<8);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[6+j])));
+                        }
+                        return tm_ptr;    
+                    }
+                    else if(GETservice_subtype(tc_ptr)==0x6)
+                    {
+                        gPC.printf("\n\rTC with subtype = 0x6");
+                        TM_PMS_PL_SCHEDULE(tm_ptr,GETpacket_seq_count(tc_ptr));
+                        for(uint8_t i=160,j=0;i<192;i++,j+=4)
+                        {
+                            pl_block[i] &= 0x00000000;
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[3+j]))<<24);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[4+j]))<<16);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[5+j]))<<8);
+                            pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[6+j])));
+                        }
+                        return tm_ptr;
+                    }
+                    else if(GETservice_subtype(tc_ptr)==0xF)
+                    {
+                        gPC.printf("\n\rTC with subtype = 0xF");
+                        TM_PMS_PL_SCHEDULE(tm_ptr,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;
+                    }
                 }
                 default:
                 {
-                    //printf("INVALID TC"); //send invalid TC TM
-                    //tm_pointer = new Short_tm; 
-                    tm_pointer->TM_string[0] = 0xB0; 
-                    tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-                    tm_pointer->TM_string[2] = 0x01;
-                    tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-                    tm_pointer->TM_string[4] = 0x01;
-                    for(uint8_t i=0;i<6;i++)
-                    {
-                        tm_pointer->TM_string[i+5] = 0;
-                    }
-                    crc16 = crc16_gen(tm_ptr->TM_string,11);
-                    tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-                    tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-                    tm_pointer->next_TM = NULL;                 return tm_ptr;
-                    break;
+                    gPC.printf("ILLEGAL TC"); 
+                    ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
+                    return tm_ptr_short;                    
                 }
             }
         }
         case 3:
         {
-            //printf("Telecommand is for PL\r\n");
-            // printf("Informing PL about TC using I2C\r\n"); //interrupt to be sent to the bae
-            // FCTN_I2C_WRITE((char*)tc_ptr->TC_string);
-            // printf("sent the Telecommand to BAE\r\n"); //packet along with crc will be sent when bae is ready
-            // wait(1); //wait time is to be optimised
-            // if(tm_status_4m_slv == 1)
-            // {
-            //     FCTN_I2C_READ((char*)tm_ptr->TM_string);
-            //     received = 1;
-            // }
-            // if(received == 1 )
-            // {
-            //     printf("Telemetry is received from BAE\r\n");
-            //     printf("%s", tm_ptr->TM_string);
-            //     received  = 0;
-            // } // similar to BAE functions
-            break;
+            //#if DEBUG
+            gPC.printf("\n\rTelecommand is for PL\r\n");
+            //#endif
+            if(GETshort_or_long_tc(tc_ptr) == LONG_TC_CODE)
+                y=FCTN_I2C_WRITE_PL((char*)tc_ptr->TC_string,TC_LONG_SIZE);
+            else if(GETshort_or_long_tc(tc_ptr) == SHORT_TC_CODE)
+                y=FCTN_I2C_WRITE_PL((char*)tc_ptr->TC_string,TC_SHORT_SIZE);
+                
+            if(y==1)                                                            
+            {                                                                               
+                ACK_L234(tm_ptr_short,0x03,GETpacket_seq_count(tc_ptr));
+                return tm_ptr_short;
+                break;
+            }                                  
+            else if(y==0)
+            {
+                //wait(0.2);
+                uint8_t poll=0;
+                while(poll<60 && PL_I2C_Intr==0)
+                {
+                    wait_ms(50);
+                    poll+=1;
+                }    
+                if(PL_I2C_Intr == 1)                                            //name to be changed later
+                {
+                    gPC.printf("PL_I2C_Intr is high\r\n");
+                    
+                    y=FCTN_I2C_READ_PL((char*)tm_pointer->TM_string,TM_LONG_SIZE);            //rify later about the size
+                    if(y==0)
+                    {
+                        tm_pointer->next_TM=NULL;
+                        return tm_ptr;
+                        break;  
+                    }
+                    else if(y==1)
+                    {
+                        ACK_L234(tm_ptr_short,0x85,GETpacket_seq_count(tc_ptr));                
+                        //gPC.printf("\n\rPTE->PDIR = 0x%08X",PTE->PDIR);
+                        return tm_ptr_short;
+                        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;
+                }
+            }                             
         }
-        default: //invalid TC
+        default:    //invalid TC
         {
-            //printf("INVALID TC\r\n");
-            //tm_pointer = new Short_tm; 
-            tm_pointer->TM_string[0] = 0xB0; 
-            tm_pointer->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
-            tm_pointer->TM_string[2] = 0x01;
-            tm_pointer->TM_string[3] = ACKCODE; //ackcode to be decided
-            tm_pointer->TM_string[4] = 0x01;
-            for(uint8_t i=0;i<6;i++)
-            {
-                tm_pointer->TM_string[i+5] = 0;
-            }
-            crc16 = crc16_gen(tm_ptr->TM_string,11);
-            tm_pointer->TM_string[11] = (uint8_t)((crc16&0xFF00)>>8);
-            tm_pointer->TM_string[12] = (uint8_t)(crc16&0x00FF);
-            tm_pointer->next_TM = NULL;                 return tm_ptr;
-            break;
+            gPC.printf("INVALID TC\r\n");                 
+            ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
+            return tm_ptr_short;
         }
-     }
+    }
+    return;
 }
 // CDMS TEAM CODE END
 // EXECUTE OBOSC
--- a/Compression.h	Wed Jun 29 14:17:34 2016 +0000
+++ b/Compression.h	Thu Jun 30 14:00:33 2016 +0000
@@ -280,7 +280,8 @@
                 if(id == 1){    //below thereshold
                     space = adjust(1,0,pointer,8);
                     space = adjust(4,3,pointer,space);
-                    FSC_science = FCTN_SD_MNGR(3);
+                    FCTN_SD_MNGR();
+                    FSC_science = FSC_CURRENT[3];
                     //gPC.printf("SID = 3, FSC = %02X\r\n", FSC_science);
                     frames[id][1] = (FSC_science>>24)&0xff;
                     frames[id][2] = (FSC_science>>16)&0xff;
@@ -573,7 +574,8 @@
                 pointer = frames[id];
                 space = adjust(1,0,pointer,8);
                 space = adjust(4,1,pointer,space);
-                FSC_science = FCTN_SD_MNGR(1);
+                FCTN_SD_MNGR();
+                FSC_science = FSC_CURRENT[1];
                 //gPC.printf("SID = 1, FSC = %02X\r\n", FSC_science);
                 frames[id][1] = (FSC_science>>16)&0xff;
                 frames[id][2] = (FSC_science>>8)&0xff;
--- a/DefinitionsAndGlobals.h	Wed Jun 29 14:17:34 2016 +0000
+++ b/DefinitionsAndGlobals.h	Thu Jun 30 14:00:33 2016 +0000
@@ -11,11 +11,11 @@
     #define COM_RX_UART_RX USBRX
     
 // COMMON SPI
-    #define SPI_MOSI PTD6
-    #define SPI_MISO PTD7
-    #define SPI_CLK PTD5
+    #define SPI_MOSI PTE1
+    #define SPI_MISO PTE3
+    #define SPI_CLK PTE2
     #define SPI_CS_ADF PTA15
-    #define SPI_CS_SDC PTD4
+    #define SPI_CS_SDC PIN20
     #define SPI_CS_RTC PIN26
     
 // COM_TX
@@ -24,6 +24,15 @@
 
 // ADF INTERRUPT
     #define ADF_IRQ PTA14
+    
+//I2C - Payload to CDMS (need to change while using CDMS hardware);
+    I2C master(PIN32,PIN31);
+    DigitalIn PL_I2C_Intr(PTC13);
+    DigitalOut PL_I2C_GPIO(PTC1);
+            
+//I2C - CDMS to BAE
+    DigitalIn BAE_I2C_GPIO(PIN39);
+    DigitalOut CDMS_I2C_GPIO(PIN67);
 
 // TC LIST
     #define TCL_STATE_INCOMPLETE 0x00
@@ -211,13 +220,11 @@
 const uint8_t gCALL_SIGN_STRING[TM_SHORT_SIZE] = {0xE0, 0x00, 0x00, 0x00, 0x56, 0x55, 0x32, 0x4E, 0x43, 0x46, 0x00, 0xAC, 0x11};
 
 // CDMS HK
-uint8_t CDMS_HEALTH_DATA[134];
+uint8_t CDMS_HEALTH_DATA[128];
 uint8_t CDMS_RAM[59];
 uint16_t GPIO_STATUS;
-uint8_t CDMS_HK_MAIN_STATUS;  
-uint16_t CDMS_HK_MAIN_COUNTER;  
+uint8_t CDMS_HK_MAIN_STATUS;    
 uint8_t COMRX_STATUS;
-uint16_t COMRX_RESET_COUNTER;
 uint8_t RTC_FAULTCOUNT;
 uint16_t SD_FAULTCOUNT;
 uint8_t SD_STATUS;
@@ -227,8 +234,6 @@
 uint16_t PL_FAULTCOUNT;
 bool BAE_HK_I2C;
 uint8_t RTC_STATUS;
-uint32_t CDMS_HK_FSC;
-uint32_t BAE_HK_FSC;
 float RSSI_volatge;
 
 CDMS_HK_actual actual_data;
@@ -238,4 +243,20 @@
 
 //SD HK
 uint32_t FSC_CURRENT[6];
-uint32_t FSC_LAST[6];
\ No newline at end of file
+uint32_t FSC_LAST[6];
+
+//Time's
+uint32_t TIME_LATEST_RTC;
+uint32_t TIME_LATEST_I2C_BAE;
+uint32_t TIME_LATEST_I2C_SPEED;
+uint32_t TIME_LATEST_SD_WR;
+uint32_t TIME_LATEST_SD_RD;
+uint32_t TIME_LATEST_SPI_SPEED;
+
+//Counter's
+uint16_t CDMS_I2C_ERR_SPEED_COUNTER;
+uint16_t CDMS_I2C_ERR_BAE_COUNTER;
+uint16_t CDMS_HK_MAIN_COUNTER;
+uint16_t COMRX_RESET_COUNTER;
+uint16_t PL_MAIN_COUNTER;
+uint16_t PL_RCV_SC_DATA_COUNTER;
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FMS_all.h	Thu Jun 30 14:00:33 2016 +0000
@@ -0,0 +1,121 @@
+
+void P_PL_INIT();
+void P_PL_MAIN();
+void P_COM_INIT();
+void P_CDMS_HK_MAIN();
+void P_PL_RCV_SC_DATA();
+void P_CDMS_INIT_SD();
+void CDMS_SD_SW_ON();
+void CDMS_SD_SW_OFF();
+void SW_ON_BAE();
+void SW_OFF_BAE();
+void SW_ON_PL_BEE();
+void SW_OFF_PL_BEE();
+void SW_ON_PL_EPS();
+void SW_OFF_PL_EPS();
+void SW_ON_V_A_EN();
+void SW_OFF_V_A_EN();
+void RST_SD();
+void RST_BAE();
+void RST_PL_BEE();
+void RST_HK_COUNTER();
+
+uint8_t P_PL_INIT()
+{
+    
+}
+
+uint8_t P_PL_MAIN()
+{
+    
+}
+
+uint8_t P_COM_INIT()
+{
+    
+}
+
+uint8_t P_CDMS_HK_MAIN()
+{
+    FCTN_CDMS_HK_MAIN();
+}
+
+uint8_t P_PL_RCV_SC_DATA()
+{
+    
+}
+
+uint8_t P_CDMS_INIT_SD()
+{
+    FCTN_CDMS_SD_INIT();
+}
+
+void CDMS_SD_SW_ON()
+{
+    SD_SW_EN_DS = 0; //powering on SD
+}
+
+void CDMS_SD_SW_OFF()
+{
+    SD_SW_EN_DS = 0; //powering on SD
+}
+
+void SW_ON_BAE()
+{
+
+}
+
+void SW_OFF_BAE()
+{
+
+}
+
+void SW_ON_PL_BEE()
+{
+
+}
+
+void SW_OFF_PL_BEE()
+{
+    
+}
+
+void SW_ON_PL_EPS()
+{
+    
+}
+
+void SW_OFF_PL_EPS()
+{
+
+}
+
+void SW_ON_V_A_EN()
+{
+
+}
+
+void SW_OFF_V_A_EN()
+{
+
+}
+
+void RST_SD()
+{
+    
+}
+
+void RST_BAE()
+{
+    
+}
+
+void RST_PL_BEE()
+{
+    
+}
+
+void RST_HK_COUNTER()
+{
+    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FMS_functions.h	Thu Jun 30 14:00:33 2016 +0000
@@ -0,0 +1,187 @@
+#include "CDMS_HK.h"
+
+uint8_t ACK_CODE;
+
+void CDMS_standby_prcs();
+void CDMS_run_prcs();
+void CDMS_rst_pwr();
+void CDMS_swch_pwr_on();
+void CDMS_swch_pwr_off();
+void CDMS_rst_hk_counter();
+void CDMS_rd_rtc();
+void CDMS_mng_rtc();
+void CDMS_rd_sd_hk();
+
+extern uint8_t PL_STANDBY_STATE;
+extern bool STANDBY_PL;
+extern uint8_t CDMS_HK_STANDBY_STATE;
+extern bool STANDBY_CDMS_HK;
+
+extern bool SD_RD_ERROR;
+
+extern uint8_t BAE_STATUS;
+extern uint8_t PL_STATUS;
+extern uint8_t SD_STATUS;
+
+// These are defined in CDMS_HK.h Please check
+#define DEVICE_ENABLED 0x00;
+#define DEVICE_POWERED 0x01;
+#define DEVICE_OC_FAULT 0x02;
+#define DEVICE_DISABLED 0x03;
+DigitalOut SD_SW_EN_DS (PIN97);
+DigitalOut BAE_SW_EN_DS (PIN47);
+
+
+void CDMS_standby_prcs()    // FID = 0x40
+{
+    if(PL_STANDBY_STATE = 0x00)
+        STANDBY_PL = 0;
+    if(PL_STANDBY_STATE = 0x01)
+        STANDBY_PL = 1;
+    if(CDMS_HK_STANDBY_STATE = 0x00)
+        STANDBY_CDMS_HK = 0;
+    if(CDMS_HK_STANDBY_STATE = 0x01)
+        STANDBY_CDMS_HK = 1;
+    if(RCV_SC_STANDBY_STATE = 0x00)
+        RCV_SC = 0;
+    if(RCV_SC_STANDBY_STATE = 0x01)
+        RCV_SC = 1;
+    ACK_CODE = 0xC0;
+    // Generate the TM
+}
+
+void CDMS_run_prcs()    // FID = 0x0_
+{
+    
+}
+
+void CDMS_rst_pwr(uint8_t FID)      // FID = 0x3_
+{
+    switch(FID){
+        case 0x31 : //power reset SD
+            SD_SW_EN_DS = 0; //switching OFF
+            wait_ms(10);
+            SD_SW_EN_DS = 1; //switching ON
+            ACK_CODE = 0xA0;
+            // Generate TM
+    }
+}
+
+void CDMS_swch_pwr_on(uint8_t FID)      // FID = 0x1_
+{
+    if(FID = 0x14)
+    {
+        PL_STATUS = DEVICE_ENABLED;
+        ACK_CODE = 0xA0;
+        // Generate TM
+    }
+    else if(FID = 0x13)
+    {
+        BAE_STATUS = DEVICE_POWERED;
+        BAE_SW_EN_DS = 1; //Power ON BAE
+        ACK_CODE = 0xA0;
+        // Generate TM
+    }
+    else if(FID = 0x11)
+    {
+        SD_STATUS = DEVICE_POWERED;
+        SD_SW_EN_DS = 1; //Power ON SD
+        FCTN_CDMS_SD_INIT();
+        ACK_CODE = 0xA0;
+        // Generate TM
+    }
+    else
+    {
+        ACK_CODE = 0x02;
+        // Generate TM
+    }
+}
+
+void CDMS_swch_pwr_off(uint8_t FID) // FID = 0x2_
+{
+        if(FID = 0x24)
+    {
+        PL_SW_EN_DS = 0; //switching OFF PL
+        PL_STATUS = DEVICE_DISABLED;
+        ACK_CODE = 0xA0;
+        // Generate TM
+    }
+    else if(FID = 0x23)
+    {
+        BAE_SW_EN_DS = 0; //Switching OFF BAE
+        BAE_STATUS = DEVICE_DISABLED;
+        ACK_CODE = 0xA0;
+        // Generate TM
+    }
+    else if(FID = 0x21)
+    {
+        SD_SW_EN_DS = 0; //Switching OFF SD
+        SD_STATUS = DEVICE_DISABLED;
+        FCTN_CDMS_SD_INIT();
+        ACK_CODE = 0xA0;
+        // Generate TM
+    }
+    else
+    {
+        ACK_CODE = 0x02;
+        // Generate TM
+    }
+}
+
+void CDMS_rst_hk_counter()      // FID = 0x41
+{
+    
+}
+
+void CDMS_rd_rtc()      // FID = 0xF1
+{
+    uint8_t time = FCTN_CDMS_RD_RTC();
+    // Check if time is read properly
+    if( /*if error in reading*/ )
+    {
+        ACK_CODE = 0x88;
+        // Generate TM
+    }
+    else if( /*if Time is read properly*/ )
+    {
+        ACK_CODE = 0xA0;
+        // Generate TM
+    }
+}
+
+void CDMS_mng_rtc(uint64_t time)
+{
+    
+}
+
+void CDMS_rd_sd_hk()
+{
+    uint8_t sd_statusbits[64];
+    uint8_t *p;
+    int i;
+    if(SD_SW_EN_DS != DEVICE_POWERED)
+    {
+        ACK_CODE = 0x89;
+        SD_RD_ERROR = 1;
+        //Generate TM
+    }
+    else
+    {
+        p = disk_read_statusbits();
+        if(p != NULL)
+        {
+            for(i = 0; i<64; i++)
+                sd_statusbits[i] = *(p + i);
+            SD_RD_ERROR = 0;
+            ACK_CODE = 0xA0;
+            // Generate TM
+        }
+        else
+        {
+            ACK_CODE = 0x89;
+            SD_RD_ERROR = 1;
+            //Generate TM
+        }
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Reset_functions.h	Thu Jun 30 14:00:33 2016 +0000
@@ -0,0 +1,36 @@
+DigitalOut pwr_comrx (PIN72)
+
+void RESET_COMRX()
+{
+    pwr_comrx = 0;
+    wait_ms(10);
+    pwr_comrx =1;
+}
+
+void RESET_RTC()
+{
+    gCS_RTC=1;
+    gCS_RTC=0;
+    spi.write(0x81); //register address with write flag
+    spi.write(0x80);//enabling stop bit in the seconds register
+ 
+    gCS_RTC=1;
+    gCS_RTC=0;
+    spi.write(0x81);
+    spi.write(0x00);//disabling the stop bit to restart the oscillator 
+
+    wait(4000);
+    //clearing the halt bit 
+    gCS_RTC=1;
+    gCS_RTC=0;
+    spi.write(0x8C);
+    spi.write(0x00);
+     
+    //clearing the OF bit 
+    gCS_RTC=1;
+    gCS_RTC=0;
+    spi.write(0x8F);
+    spi.write(0x00);
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TEST_PL.h	Thu Jun 30 14:00:33 2016 +0000
@@ -0,0 +1,512 @@
+//F11 to exit full screen mode
+/*---------------------------------------------------------------------------
+                            Testing PL_MAIN
+----------------------------------------------------------------------------*/
+
+
+/* Testing as per the flow chart  */
+/* columns of the array schedule1[][] correspond to Year,Month,Day,Hours,Minutes,Seconds,SID and EXEC_STATUS in integer formats*/
+Base_tm* FCTN_CDMS_RLY_TMTC(Base_tc *tc_ptr);
+
+extern uint8_t schedule1[32][8]=
+                        {
+                            {0,1,1,0,0,15,3,3},
+                            {0,1,1,0,0,45,2,3},
+                            {0,1,1,0,1,00,1,3},
+                            {0,1,1,0,1,25,0,3},     
+                            {0,1,1,0,2,10,3,3},
+                            {0,1,1,0,1,50,2,3},
+                            {0,1,1,0,2, 0,2,3},
+                            {0,1,1,0,2,10,2,3},
+                            {0,1,1,0,2,20,2,0},
+                            {0,1,1,0,2,30,2,3},
+                            
+                            {0,1,1,0,2,50,0,1},
+                            {0,1,1,0,4,10,3,3},
+                            {0,1,1,0,5,30,3,3},
+                            {0,1,1,0,4,15,2,3},
+                            {0,1,1,0,4,10,1,0},
+                            {0,1,1,0,3,10,2,3},
+                            {0,1,1,0,4,10,3,3},
+                            {0,1,1,0,5,10,0,3},
+                            {0,1,1,0,6,10,1,3},
+                            {0,1,1,0,6,20,1,2},                           
+
+                            {0,1,1,0,1,40,2,3},
+                            {0,1,1,0,1,20,3,1},
+                            {0,1,1,0,1,30,3,3},                         
+                            {0,1,1,0,1,10,2,1},
+                            {0,1,1,0,0, 0,0,0},
+                            {0,1,1,0,0, 0,0,0},
+                            {0,1,1,0,0, 0,0,0},
+                            {0,1,1,0,0, 0,0,0},
+                            {0,1,1,0,0, 0,0,0},
+                            {0,1,1,0,0, 0,0,0},
+
+                            {0,1,1,0,0, 0,0,0},
+                            {0,1,1,0,0, 0,0,0},
+                        };
+
+void convert(Base_tc* test_tc)
+{    
+    for(uint8_t k=3;k<=127;k+=4)
+    {
+        /*-----------1st byte of a block----------*/
+        test_tc->TC_string[k] &= 0x00;
+        //setting Year (2 bits)
+        //test_tc->TC_string[k] &= (~0xC0);
+        test_tc->TC_string[k] |= ((schedule1[(k-3)/4][0] & 0x03)<<6);
+
+        //setting Month (4 bits)
+        //test_tc->TC_string[k] &= (~0x3C);
+        test_tc->TC_string[k] |= ((schedule1[(k-3)/4][1] & 0x0F)<<2);
+
+        //setting first two bits(MSB) of Day
+        //test_tc->TC_string[k] &= (~0x03);
+        test_tc->TC_string[k] |= ((schedule1[(k-3)/4][2] & 0x18)>>3);
+        /*------------end of 1st byte-------------*/
+
+        /*-----------2nd byte of a block----------*/
+        test_tc->TC_string[k+1] &= 0x00000000;
+        //setting remaining 3 bits of Day
+        //test_tc->TC_string[k+1] &= (~0xE0);
+        test_tc->TC_string[k+1] |= ((schedule1[(k-3)/4][2] & 0x07)<<5);
+
+        //setting Hours (5 bits)
+        //test_tc->TC_string[k+1] &= (~0x1F);
+        test_tc->TC_string[k+1] |= ((schedule1[(k-3)/4][3] & 0x1F));
+        /*------------end of 2nd byte-------------*/
+
+        /*-----------3rd byte of a block----------*/
+        test_tc->TC_string[k+2] &= 0x00000000;
+        //setting Minutes (6 bits)
+        //test_tc->TC_string[k+2] &= (~0xFC);
+        test_tc->TC_string[k+2] |= ((schedule1[(k-3)/4][4] & 0x3F)<<2);
+
+        //setting first 2 bits(MSB) of Seconds
+        //test_tc->TC_string[k+2] &= (~0x03);
+        test_tc->TC_string[k+2] |= ((schedule1[(k-3)/4][5] & 0x30)>>4);
+        /*------------end of 3rd byte-------------*/        
+
+        /*-----------4th byte of a block----------*/
+        test_tc->TC_string[k+3] &= 0x00;
+        //setting remaining 4 bits of Seconds
+        //test_tc->TC_string[k+3] &= (~0xF0);
+        test_tc->TC_string[k+3] |= ((schedule1[(k-3)/4][5] & 0x0F)<<4);
+
+        //setting STATE_ID (2 bits)
+        //test_tc->TC_string[k+3] &= (~0x0C);
+        test_tc->TC_string[k+3] |= ((schedule1[(k-3)/4][6] & 0x03)<<2);
+
+        //setting EXEC_STATUS (2 bits)
+        //test_tc->TC_string[k+3] &= (~0x03);
+        test_tc->TC_string[k+3] |= ((schedule1[(k-3)/4][7] & 0x03));
+        /*------------end of 4th byte-------------*/                
+    }    
+}
+/*void test_schedule_1(Base_tm *test_tm_ptr)
+{
+    Base_tc *test_tc = new Long_tc;
+    test_tc->next_TC = NULL;
+    test_tc->TC_string[0] = 0x01;
+    test_tc->TC_string[1] = 0xB2;
+    test_tc->TC_string[2] = 0x71;
+    convert(test_tc);
+    test_tc->TC_string[131] = 0x00;
+    test_tc->TC_string[132] = 0x00;
+    uint16_t crc16 = crc16_gen(test_tc->TC_string,133);
+    test_tc->TC_string[133] = (uint8_t)(crc16 & 0xFF00)>>8;
+    test_tc->TC_string[134] = (uint8_t)(crc16 & 0x00FF);
+    test_tm_ptr =  FCTN_CDMS_RLY_TMTC(test_tc);
+}
+
+void test_schedule_2(Base_tm *test_tm_ptr)
+{
+    Base_tc *test_tc = new Long_tc;
+    test_tc->next_TC = NULL;
+    test_tc->TC_string[0] = 0x01;
+    test_tc->TC_string[1] = 0xB2;
+    test_tc->TC_string[2] = 0x72;
+    convert(test_tc);
+    test_tc->TC_string[131] = 0x00;
+    test_tc->TC_string[132] = 0x00;
+    uint16_t crc16 = crc16_gen(test_tc->TC_string,133);
+    test_tc->TC_string[133] = (uint8_t)(crc16 & 0xFF00)>>8;
+    test_tc->TC_string[134] = (uint8_t)(crc16 & 0x00FF);
+    test_tm_ptr =  FCTN_CDMS_RLY_TMTC(test_tc);
+}
+
+void test_schedule_3(Base_tm *test_tm_ptr)
+{
+    Base_tc *test_tc = new Long_tc;
+    test_tc->next_TC = NULL;
+    test_tc->TC_string[0] = 0x01;
+    test_tc->TC_string[1] = 0xB2;
+    test_tc->TC_string[2] = 0x73;
+    convert(test_tc);
+    test_tc->TC_string[131] = 0x00;
+    test_tc->TC_string[132] = 0x00;
+    uint16_t crc16 = crc16_gen(test_tc->TC_string,133);
+    test_tc->TC_string[133] = (uint8_t)(crc16 & 0xFF00)>>8;
+    test_tc->TC_string[134] = (uint8_t)(crc16 & 0x00FF);
+    test_tm_ptr =  FCTN_CDMS_RLY_TMTC(test_tc);
+}
+
+void test_schedule_4(Base_tm *test_tm_ptr)
+{
+    Base_tc *test_tc = new Long_tc;
+    test_tc->next_TC = NULL;
+    test_tc->TC_string[0] = 0x01;
+    test_tc->TC_string[1] = 0xB2;
+    test_tc->TC_string[2] = 0x74;
+    convert(test_tc);
+    test_tc->TC_string[131] = 0x00;
+    test_tc->TC_string[132] = 0x00;
+    uint16_t crc16 = crc16_gen(test_tc->TC_string,133);
+    test_tc->TC_string[133] = (uint8_t)(crc16 & 0xFF00)>>8;
+    test_tc->TC_string[134] = (uint8_t)(crc16 & 0x00FF);
+    test_tm_ptr =  FCTN_CDMS_RLY_TMTC(test_tc);
+}
+
+void test_schedule_5(Base_tm *test_tm_ptr)
+{
+    Base_tc *test_tc = new Long_tc;
+    test_tc->next_TC = NULL;
+    test_tc->TC_string[0] = 0x01;
+    test_tc->TC_string[1] = 0xB2;
+    test_tc->TC_string[2] = 0x75;
+    convert(test_tc);
+    test_tc->TC_string[131] = 0x00;
+    test_tc->TC_string[132] = 0x00;
+    uint16_t crc16 = crc16_gen(test_tc->TC_string,133);
+    test_tc->TC_string[133] = (uint8_t)(crc16 & 0xFF00)>>8;
+    test_tc->TC_string[134] = (uint8_t)(crc16 & 0x00FF);
+    test_tm_ptr =  FCTN_CDMS_RLY_TMTC(test_tc);
+}
+
+void test_schedule_6(Base_tm *test_tm_ptr)
+{
+    Base_tc *test_tc = new Long_tc;
+    test_tc->next_TC = NULL;
+    test_tc->TC_string[0] = 0x01;
+    test_tc->TC_string[1] = 0xB2;
+    test_tc->TC_string[2] = 0x76;
+    convert(test_tc);
+    test_tc->TC_string[131] = 0x00;
+    test_tc->TC_string[132] = 0x00;
+    uint16_t crc16 = crc16_gen(test_tc->TC_string,133);
+    test_tc->TC_string[133] = (uint8_t)(crc16 & 0xFF00)>>8;
+    test_tc->TC_string[134] = (uint8_t)(crc16 & 0x00FF);
+    test_tm_ptr =  FCTN_CDMS_RLY_TMTC(test_tc);
+}
+
+void test_schedule_F(Base_tm *test_tm_ptr)
+{
+    Base_tc *test_tc = new Long_tc;
+    test_tc->next_TC = NULL;
+    test_tc->TC_string[0] = 0x01;
+    test_tc->TC_string[1] = 0xB2;
+    test_tc->TC_string[2] = 0x7F;
+    convert(test_tc);
+    test_tc->TC_string[131] = 0x00;
+    test_tc->TC_string[132] = 0x00;
+    uint16_t crc16 = crc16_gen(test_tc->TC_string,133);
+    test_tc->TC_string[133] = (uint8_t)(crc16 & 0xFF00)>>8;
+    test_tc->TC_string[134] = (uint8_t)(crc16 & 0x00FF);
+    test_tm_ptr =  FCTN_CDMS_RLY_TMTC(test_tc);
+}*/
+#define test_schedule_1(test_tm_ptr) {\
+    Base_tc *test_tc = NULL;\
+    if(!(test_tc  = new Long_tc))\
+    {\
+       gPC.printf("\n\rError: out of memory");\
+       return;\
+    }\
+    test_tc->next_TC = NULL;\
+    test_tc->TC_string[0] = 0x01;\
+    test_tc->TC_string[1] = 0xB2;\
+    test_tc->TC_string[2] = 0x71;\
+    convert(test_tc);\
+    test_tc->TC_string[131] = 0x00;\
+    test_tc->TC_string[132] = 0x00;\
+    uint16_t crc16 = crc16_gen(test_tc->TC_string,133);\
+    test_tc->TC_string[133] = (uint8_t)(crc16 & 0xFF00)>>8;\
+    test_tc->TC_string[134] = (uint8_t)(crc16 & 0x00FF);\
+    test_tm_ptr =  FCTN_CDMS_RLY_TMTC(test_tc);\
+}
+#define test_schedule_2(test_tm_ptr) {\
+    Base_tc *test_tc = new Long_tc;\
+    test_tc->next_TC = NULL;\
+    test_tc->TC_string[0] = 0x01;\
+    test_tc->TC_string[1] = 0xB2;\
+    test_tc->TC_string[2] = 0x72;\
+    convert(test_tc);\
+    test_tc->TC_string[131] = 0x00;\
+    test_tc->TC_string[132] = 0x00;\
+    uint16_t crc16 = crc16_gen(test_tc->TC_string,133);\
+    test_tc->TC_string[133] = (uint8_t)(crc16 & 0xFF00)>>8;\
+    test_tc->TC_string[134] = (uint8_t)(crc16 & 0x00FF);\
+    test_tm_ptr =  FCTN_CDMS_RLY_TMTC(test_tc);\
+}
+#define test_schedule_3(test_tm_ptr) {\
+    Base_tc *test_tc = new Long_tc;\
+    test_tc->next_TC = NULL;\
+    test_tc->TC_string[0] = 0x01;\
+    test_tc->TC_string[1] = 0xB2;\
+    test_tc->TC_string[2] = 0x73;\
+    convert(test_tc);\
+    test_tc->TC_string[131] = 0x00;\
+    test_tc->TC_string[132] = 0x00;\
+    uint16_t crc16 = crc16_gen(test_tc->TC_string,133);\
+    test_tc->TC_string[133] = (uint8_t)(crc16 & 0xFF00)>>8;\
+    test_tc->TC_string[134] = (uint8_t)(crc16 & 0x00FF);\
+    test_tm_ptr =  FCTN_CDMS_RLY_TMTC(test_tc);\
+}
+#define test_schedule_4(test_tm_ptr) {\
+    Base_tc *test_tc = new Long_tc;\
+    test_tc->next_TC = NULL;\
+    test_tc->TC_string[0] = 0x01;\
+    test_tc->TC_string[1] = 0xB2;\
+    test_tc->TC_string[2] = 0x74;\
+    convert(test_tc);\
+    test_tc->TC_string[131] = 0x00;\
+    test_tc->TC_string[132] = 0x00;\
+    uint16_t crc16 = crc16_gen(test_tc->TC_string,133);\
+    test_tc->TC_string[133] = (uint8_t)(crc16 & 0xFF00)>>8;\
+    test_tc->TC_string[134] = (uint8_t)(crc16 & 0x00FF);\
+    test_tm_ptr =  FCTN_CDMS_RLY_TMTC(test_tc);\
+}
+#define test_schedule_5(test_tm_ptr) {\
+    Base_tc *test_tc = new Long_tc;\
+    test_tc->next_TC = NULL;\
+    test_tc->TC_string[0] = 0x01;\
+    test_tc->TC_string[1] = 0xB2;\
+    test_tc->TC_string[2] = 0x75;\
+    convert(test_tc);\
+    test_tc->TC_string[131] = 0x00;\
+    test_tc->TC_string[132] = 0x00;\
+    uint16_t crc16 = crc16_gen(test_tc->TC_string,133);\
+    test_tc->TC_string[133] = (uint8_t)(crc16 & 0xFF00)>>8;\
+    test_tc->TC_string[134] = (uint8_t)(crc16 & 0x00FF);\
+    test_tm_ptr =  FCTN_CDMS_RLY_TMTC(test_tc);\
+}
+#define test_schedule_6(test_tm_ptr) {\
+    Base_tc *test_tc = new Long_tc;\
+    test_tc->next_TC = NULL;\
+    test_tc->TC_string[0] = 0x01;\
+    test_tc->TC_string[1] = 0xB2;\
+    test_tc->TC_string[2] = 0x76;\
+    convert(test_tc);\
+    test_tc->TC_string[131] = 0x00;\
+    test_tc->TC_string[132] = 0x00;\
+    uint16_t crc16 = crc16_gen(test_tc->TC_string,133);\
+    test_tc->TC_string[133] = (uint8_t)(crc16 & 0xFF00)>>8;\
+    test_tc->TC_string[134] = (uint8_t)(crc16 & 0x00FF);\
+    test_tm_ptr =  FCTN_CDMS_RLY_TMTC(test_tc);\
+}
+#define test_schedule_F(test_tm_ptr) {\
+    Base_tc *test_tc = NULL;\
+    if(!(test_tc  = new Long_tc))\
+    {\
+       gPC.printf("\n\rError: out of memory");\
+       return;\
+    }\
+    test_tc->next_TC = NULL;\
+    test_tc->TC_string[0] = 0x01;\
+    test_tc->TC_string[1] = 0xB2;\
+    test_tc->TC_string[2] = 0x7F;\
+    for(uint8_t i=3;i<133;i++)\
+    {\
+        test_tc->TC_string[i] = 0;\
+    }\
+    uint16_t crc16 = crc16_gen(test_tc->TC_string,133);\
+    test_tc->TC_string[133] = (uint8_t)(crc16 & 0xFF00)>>8;\
+    test_tc->TC_string[134] = (uint8_t)(crc16 & 0x00FF);\
+    test_tm_ptr =  FCTN_CDMS_RLY_TMTC(test_tc);\
+}
+void send_verify_0xF(Base_tm* test_tm_ptr)
+{
+    uint32_t temp_blk[4] = {0};
+    uint8_t j=0,tm_data_err_count=0;
+    Base_tm* temp_ptr;
+    test_schedule_F(test_tm_ptr);
+    //gPC.printf("\n\r----------------------------------------------------------------------------------");
+    //gPC.printf("\n\rTC_Schedule_Report(0xF):\n");
+    temp_ptr=test_tm_ptr;
+    while(temp_ptr!=NULL)
+    {
+        /*gPC.printf("\n\rTM[0] = %02X",temp_ptr->TM_string[0]);
+        gPC.printf("\tTM[1] = %02X",temp_ptr->TM_string[1]);
+        gPC.printf("\tTM[2] = %02X",temp_ptr->TM_string[2]);
+        gPC.printf("\tTM[3] = %02X",temp_ptr->TM_string[3]);*/
+        for(uint8_t i=0;i<=112;i+=16)
+        {
+            temp_blk[0] &= 0x00000000;
+            temp_blk[0] |= (((uint32_t)(temp_ptr->TM_string[i+4]))&0x000000FF)<<24;
+            temp_blk[0] |= (((uint32_t)(temp_ptr->TM_string[i+5]))&0x000000FF)<<16;
+            temp_blk[0] |= (((uint32_t)(temp_ptr->TM_string[i+6]))&0x000000FF)<<8;
+            temp_blk[0] |= (((uint32_t)(temp_ptr->TM_string[i+7]))&0x000000FF);
+
+            temp_blk[1] &= 0x00000000;
+            temp_blk[1] |= (((uint32_t)(temp_ptr->TM_string[i+8]))&0x000000FF)<<24;
+            temp_blk[1] |= (((uint32_t)(temp_ptr->TM_string[i+9]))&0x000000FF)<<16;
+            temp_blk[1] |= (((uint32_t)(temp_ptr->TM_string[i+10]))&0x000000FF)<<8;
+            temp_blk[1] |= (((uint32_t)(temp_ptr->TM_string[i+11]))&0x000000FF);
+
+            temp_blk[2] &= 0x00000000;
+            temp_blk[2] |= (((uint32_t)(temp_ptr->TM_string[i+12]))&0x000000FF)<<24;
+            temp_blk[2] |= (((uint32_t)(temp_ptr->TM_string[i+13]))&0x000000FF)<<16;
+            temp_blk[2] |= (((uint32_t)(temp_ptr->TM_string[i+14]))&0x000000FF)<<8;
+            temp_blk[2] |= (((uint32_t)(temp_ptr->TM_string[i+15]))&0x000000FF);
+            
+            temp_blk[3] &= 0x00000000;
+            temp_blk[3] |= (((uint32_t)(temp_ptr->TM_string[i+16]))&0x000000FF)<<24;
+            temp_blk[3] |= (((uint32_t)(temp_ptr->TM_string[i+17]))&0x000000FF)<<16;
+            temp_blk[3] |= (((uint32_t)(temp_ptr->TM_string[i+18]))&0x000000FF)<<8;
+            temp_blk[3] |= (((uint32_t)(temp_ptr->TM_string[i+19]))&0x000000FF);
+
+            if(temp_blk[0]!=pl_block[(i/4)+j])
+                tm_data_err_count++;
+            if(temp_blk[1]!=pl_block[(i/4)+j+1])
+                tm_data_err_count++;
+            if(temp_blk[2]!=pl_block[(i/4)+j+2])
+                tm_data_err_count++;
+            if(temp_blk[3]!=pl_block[(i/4)+j+3])
+                tm_data_err_count++;
+            //gPC.printf("\n\r%03d-0x%08X\t%03d-0x%08X",(i/4)+j,temp_blk[0],(i/4)+1+j,temp_blk[1]);
+            //gPC.printf("\t%03d-0x%08X\t%03d-0x%08X\n",(i/4)+2+j,temp_blk[2],(i/4)+3+j,temp_blk[3]);             
+        }
+        j+=32;
+        //gPC.printf("\n");
+        temp_ptr = temp_ptr->next_TM;
+    }
+    if(tm_data_err_count!=0)
+    {
+        gPC.printf("\n\rTM Data(0xF) not matching pl_block!!!");
+        gPC.printf("\n\rNumber of mismatches = %d",tm_data_err_count);
+    }
+    else if(tm_data_err_count==0)
+    {
+        gPC.printf("\n\rNo errors in TM Data(0xF)");
+    }
+    //gPC.printf("\n\r----------------------------------------------------------------------------------");
+}
+void verify_extracted(uint8_t c)
+{
+    uint8_t wrong_extracted=0;
+    uint8_t temp_extracted[8] = {0};
+    for(uint8_t z=(32*(c-1));z<(32*c);z++)
+    {        
+        temp_extracted[0] = (uint8_t)((pl_block[z]&0xC0000000)>>30);
+        temp_extracted[1] = (uint8_t)((pl_block[z]&0x3C000000)>>26);
+        temp_extracted[2] = (uint8_t)((pl_block[z]&0x03E00000)>>21);
+        temp_extracted[3] = (uint8_t)((pl_block[z]&0x001F0000)>>16);
+        temp_extracted[4] = (uint8_t)((pl_block[z]&0x0000FC00)>>10);
+        temp_extracted[5] = (uint8_t)((pl_block[z]&0x000003F0)>>4);
+        temp_extracted[6] = (uint8_t)((pl_block[z]&0x0000000C)>>2);
+        temp_extracted[7] = (uint8_t)((pl_block[z]&0x00000003));
+        
+        for(uint8_t y=0;y<8;y++)
+        {
+            if(temp_extracted[y]!=schedule1[z-(32*(c-1))][y])
+                wrong_extracted++;
+        }
+    }
+    if(wrong_extracted!=0)
+    {
+        gPC.printf("\n\rExtracted values do not match input values!!!");
+        gPC.printf("\n\rNumber of wrong extracted values = %d",wrong_extracted);
+        wrong_extracted = 0;
+    }
+    else if(wrong_extracted==0)
+        gPC.printf("\n\rExtracted values are correct(0x%d)",c);        
+}
+void test_pl_main()
+{
+    //uint32_t temp_pl_block[192] = {0};
+    uint8_t wrong_modify=0,z;
+    Base_tm* test_tm_ptr;    
+    //Base_tm* temp;
+    //for(z=0;z<192;z++)
+    //    pl_block[z] = 0xFFFFFFFF;
+    //testing 0xF
+    send_verify_0xF(test_tm_ptr);
+    //for(z=0;z<192;z++)  
+    //    temp_pl_block[z] = pl_block[z];
+        
+    //testing TC = 0x1,0x2...0x6
+    uint8_t w =1;
+    for(uint8_t v=1;w<7;w++)
+    {        
+        /*switch(v)
+        {
+            case 1: test_schedule_1(test_tm_ptr);
+                    break;
+            case 2: test_schedule_2(test_tm_ptr);
+                    break;
+            case 3: test_schedule_3(test_tm_ptr);
+                    break;
+            case 4: test_schedule_4(test_tm_ptr);
+                    break;
+            case 5: test_schedule_5(test_tm_ptr);
+                    break;
+            case 6: test_schedule_6(test_tm_ptr);
+                    break;
+        }*/
+        if(v==1)
+        {
+            test_schedule_1(test_tm_ptr);
+        }
+        else if(v==2)
+        {
+            test_schedule_2(test_tm_ptr);
+        }
+        else if(v==3)
+        {
+            test_schedule_3(test_tm_ptr);
+        }
+        else if(v==4)        
+        {
+            test_schedule_4(test_tm_ptr);
+        }
+        else if(v==5)
+        {
+            test_schedule_5(test_tm_ptr);
+        }
+        else if(v==6)
+        {
+            test_schedule_6(test_tm_ptr);
+        }
+        /*for(z=0;z<192;z++)
+        {
+            if((z>=((v-1)*32)) && (z<=((v*32)-1)))
+                z++;
+            else
+            {
+                if(temp_pl_block[z]!=pl_block[z])
+                    wrong_modify++;
+            }
+        }
+        if(wrong_modify!=0)
+        {
+            gPC.printf("\n\rWrong values of pl_block modified by TC = 0x%d",v);
+            gPC.printf("\n\rNumber of wrong modifications = %d",wrong_modify);
+            wrong_modify = 0;
+        }
+        else if(wrong_modify==0)
+        {
+            gPC.printf("\n\rTC(0x%d) updated pl_block correctly",v);
+            for(uint8_t i=0;i<=188;i+=4)
+            {
+                //gPC.printf("\n\r%03d-0x%08X\t%03d-0x%08X",(i),pl_block[i],(i)+1,pl_block[(i)+1]);
+                //gPC.printf("\t%03d-0x%08X\t%03d-0x%08X\n",(i)+2,pl_block[(i)+2],(i)+3,pl_block[(i)+3]);
+            }
+        }*/
+        send_verify_0xF(test_tm_ptr);
+        verify_extracted(v);
+        //for(z=0;z<192;z++)
+        //    temp_pl_block[z] = pl_block[z];
+        wait(1);
+    }
+    gPC.printf("\n\rTesting TC_TM block processing complete");
+}
\ No newline at end of file
--- a/cdms_rtc.h	Wed Jun 29 14:17:34 2016 +0000
+++ b/cdms_rtc.h	Thu Jun 30 14:00:33 2016 +0000
@@ -4,134 +4,129 @@
     gCS_RTC=1;
     spi.format(8,0);
     spi.frequency(1000000);
-    //clearing the halt bit 
+    //clearing the halt bit
     gCS_RTC=1;
     gCS_RTC=0;
     spi.write(0x8C);
     spi.write(0x00);
-     
-    //clearing the OF bit 
+
+    //clearing the OF bit
     gCS_RTC=1;
     gCS_RTC=0;
     spi.write(0x8F);
     spi.write(0x00);
-     
+
     //century bits
     gCS_RTC=1;
     gCS_RTC=0;
     spi.write(0x80|0x03);
     spi.write(0x00);
- 
-    //Kick starting the oscillator 
+
+    //Kick starting the oscillator
     gCS_RTC=1;
     gCS_RTC=0;
     spi.write(0x81); //register address with write flag
     spi.write(0x80);//enabling stop bit in the seconds register
- 
+
     gCS_RTC=1;
     gCS_RTC=0;
     spi.write(0x81);
-    spi.write(0x00);//disabling the stop bit to restart the oscillator 
- 
-    
+    spi.write(0x00);//disabling the stop bit to restart the oscillator
+
+
     gCS_RTC=1;
     gCS_RTC=0;
-    spi.write(0x80); 
+    spi.write(0x80);
     spi.write(0x01); // set milliseconds value to 00
     gCS_RTC=1;
- 
+
     gCS_RTC=0;
-    spi.write(0x81); 
+    spi.write(0x81);
     spi.write(0x01); //set seconds value to 00
     gCS_RTC=1;
- 
+
     gCS_RTC=0;
-    spi.write(0x82); 
+    spi.write(0x82);
     spi.write(0x01);//set minutes value to 00
     gCS_RTC=1;
- 
+
     gCS_RTC=0;
-    spi.write(0x83); 
+    spi.write(0x83);
     spi.write(0x23); //set the hours to 01
     gCS_RTC=1;
-    
+
     gCS_RTC=0;
-    spi.write(0x84); 
+    spi.write(0x84);
     spi.write(0x01); //set day of the week to 01
     gCS_RTC=1;
-    
+
     gCS_RTC=0;
-    spi.write(0x85); 
+    spi.write(0x85);
     spi.write(0x31); //set date of the month to 01
     gCS_RTC=1;
-    
+
     gCS_RTC=0;
-    spi.write(0x86); 
+    spi.write(0x86);
     spi.write(0x12); //set month to 01
     gCS_RTC=1;
-    
+
     gCS_RTC=0;
-    spi.write(0x87); 
+    spi.write(0x87);
     spi.write(0x01); //set year to 00(2000)
     gCS_RTC=1;
     gPC.puts("\r\nrtc initalised\r\n");
     SPI_mutex.unlock();
 }
- 
+
 uint64_t FCTN_CDMS_RD_RTC()
-{    
-  SPI_mutex.lock();
-  uint8_t response;
+{
+    SPI_mutex.lock();
+    uint8_t response;
+
+    gCS_RTC=1;
+    gCS_RTC=0;
+    spi.write(0x0F);
+    response = (spi.write(0x00))&0x04;
 
-  gCS_RTC=1;
-  gCS_RTC=0;
-  spi.write(0x0F);
-  response = (spi.write(0x00))&0x04;
-/*  if(response =! 0x00)
-  {
-    time = 0x0000000000000000;
-    return time;
-  }*/
+    gCS_RTC=1;
+    gCS_RTC=0;
+    spi.write(0x00);                //reading centiseconds register
+    response = spi.write(0x00);     // read the value by sending dummy byte
+    uint8_t centiseconds = ((response&0xF0)>>4)*10+(response&0x0F)*1;
 
-  gCS_RTC=1;
-  gCS_RTC=0;
-  spi.write(0x00);                //reading centiseconds register
-  response = spi.write(0x00);     // read the value by sending dummy byte
-  uint8_t centiseconds = ((response&0xF0)>>4)*10+(response&0x0F)*1;
+    response =spi.write(0x00);
+    uint8_t seconds = ((response&0x70)>>4)*10+(response&0x0F)*1;
 
-  response =spi.write(0x00);
-  uint8_t seconds = ((response&0x70)>>4)*10+(response&0x0F)*1;
+    response =spi.write(0x00);
+    uint8_t minutes = ((response&0x70)>>4)*10+(response&0x0F)*1;
 
-  response =spi.write(0x00);
-  uint8_t minutes = ((response&0x70)>>4)*10+(response&0x0F)*1;
+    response=spi.write(0x00);
+    uint8_t hours = ((response&0x30)>>4)*10+(response&0x0F)*1;
 
-  response=spi.write(0x00);
-  uint8_t hours = ((response&0x30)>>4)*10+(response&0x0F)*1;
+    uint8_t day =spi.write(0x00);
 
-  uint8_t day =spi.write(0x00);
+    response =spi.write(0x00);
+    uint8_t date = ((response&0x30)>>4)*10+(response&0x0F)*1;
 
-  response =spi.write(0x00);
-  uint8_t date = ((response&0x30)>>4)*10+(response&0x0F)*1;
+    response =spi.write(0x00);
+    uint8_t month = ((response&0x10)>>4)*10+(response&0x0F)*1;
 
-  response =spi.write(0x00);
-  uint8_t month = ((response&0x10)>>4)*10+(response&0x0F)*1;
+    response =spi.write(0x00);
+    uint8_t year = ((response&0xF0)>>4)*10+(response&0x0F)*1;
+    year = (year == 17)?0x00:(year == 18)?0x01:(year == 19)?0x02:(year == 20)?0x03:0x00;
+    gCS_RTC=1;
 
-  response =spi.write(0x00);
-  uint8_t year = ((response&0xF0)>>4)*10+(response&0x0F)*1;
-  year = (year == 17)?0x00:(year == 18)?0x01:(year == 19)?0x02:(year == 20)?0x03:0x00;
-  gCS_RTC=1;
+    uint64_t time;
+    time = 0;
+    time = time|(((uint64_t)(centiseconds&0x7F)));
+    time = time|(((uint64_t)(seconds&0x3F))<<7);
+    time = time|(((uint64_t)(minutes&0x3F))<<13);
+    time = time|(((uint64_t)(hours&0x1F))<<19);
+    time = time|(((uint64_t)(day&0x1F))<<24);
+    time = time|(((uint64_t)(month&0x07))<<29);
+    time = time|(((uint64_t)(year&0x03))<<33);
+    time = (time&0x00000007FFFFFFFF);
 
-  uint64_t time;
-  time = 0;
-  time = time|(((uint64_t)(centiseconds&0x7F)));
-  time = time|(((uint64_t)(seconds&0x3F))<<7);
-  time = time|(((uint64_t)(minutes&0x3F))<<13);
-  time = time|(((uint64_t)(hours&0x1F))<<19);
-  time = time|(((uint64_t)(day&0x1F))<<24);
-  time = time|(((uint64_t)(month&0x07))<<29);
-  time = time|(((uint64_t)(year&0x03))<<33);
-  time = (time&0x00000007FFFFFFFF);
-  
-  SPI_mutex.unlock();
-  return time;
+    SPI_mutex.unlock();
+    return time;
 }
\ No newline at end of file
--- a/cdms_sd.h	Wed Jun 29 14:17:34 2016 +0000
+++ b/cdms_sd.h	Thu Jun 30 14:00:33 2016 +0000
@@ -1,6 +1,4 @@
-
-
-
+#include "cdms_rtc.h"
 //SPI spi(PTE1, PTE3, PTE2);      // MOSI,MISO, CLOCK microcontroller(in order)     
 //DigitalOut cs_sd(PTE22);
 
@@ -34,6 +32,7 @@
 uint32_t LOG_FIRST =5001;
 uint32_t LOG_LAST=6000;
 uint32_t SD_MNG_SECT=7000;
+uint16_t SD_LIB_WRITES = 0;
 
 extern uint8_t SD_INIT_FLAGS;
 
@@ -44,7 +43,7 @@
 int disk_read(uint8_t *, uint64_t);
 int disk_erase(int,int);
 
-uint32_t FCTN_SD_MNG(uint8_t);
+void FCTN_SD_MNG();
 int INCREMENT_SD_LIB(uint8_t);
 
 
@@ -57,12 +56,12 @@
 static uint32_t ext_bits(unsigned char *, int , int );
 int SD_WRITE(uint8_t*,uint32_t,uint8_t);
 int FCTN_CDMS_SD_INIT();
-int SD_READ(uint8_t*,uint32_t,uint8_t);
+uint8_t SD_READ(uint8_t*,uint32_t,uint8_t);
 #define SDCARD_FAIL 0
 #define SDCARD_V1   1
 #define SDCARD_V2   2
 #define SDCARD_V2HC 3
-
+DigitalOut SD_SW_EN_DS (PIN97);
 
 int cdv;
 uint64_t sd_sectors();
@@ -86,57 +85,47 @@
     return 0;
 }
 
-    
-
-uint32_t  FCTN_SD_MNGR(uint8_t sid)
+void  FCTN_SD_MNGR()
 {
     uint32_t fsc;
     uint32_t start_fsc;
     uint8_t buffer[512];
     int b=disk_read(buffer, SD_MNG_SECT);
-    if(sid==0x01)
-    {
-        fsc=(uint32_t)(buffer[0]<<24)+(uint32_t)(buffer[1]<<16)+(uint32_t)(buffer[2]<<8)+(uint32_t)buffer[3];
-        start_fsc=(uint32_t)(buffer[4]<<24)+(uint32_t)(buffer[5]<<16)+(uint32_t)(buffer[6]<<8)+(uint32_t)buffer[7];
-        FSC_CURRENT[1] = fsc;
-        FSC_LAST[1] = start_fsc;
-    }
-    if(sid==0x02)
-    {
-        fsc=(uint32_t)(buffer[8]<<24)+(uint32_t)(buffer[9]<<16)+(uint32_t)(buffer[10]<<8)+(uint32_t)buffer[11];
-        start_fsc=(uint32_t)(buffer[12]<<24)+(uint32_t)(buffer[13]<<16)+(uint32_t)(buffer[14]<<8)+(uint32_t)buffer[15];
-        FSC_CURRENT[2] = fsc;
-        FSC_LAST[2] = start_fsc;
-    }
-    if(sid==0x03)
-    {
-        fsc=(uint32_t)(buffer[16]<<24)+(uint32_t)(buffer[17]<<16)+(uint32_t)(buffer[18]<<8)+(uint32_t)buffer[19];
-        start_fsc=(uint32_t)(buffer[20]<<24)+(uint32_t)(buffer[21]<<16)+(uint32_t)(buffer[22]<<8)+(uint32_t)buffer[23];
-        FSC_CURRENT[3] = fsc;
-        FSC_LAST[3] = start_fsc;
-    }
-    if(sid==0x04)
-    {
-        fsc=(uint32_t)(buffer[24]<<24)+(uint32_t)(buffer[25]<<16)+(uint32_t)(buffer[26]<<8)+(uint32_t)buffer[27];
-        start_fsc=(uint32_t)(buffer[28]<<24)+(uint32_t)(buffer[29]<<16)+(uint32_t)(buffer[30]<<8)+(uint32_t)buffer[31];
-        FSC_CURRENT[4] = fsc;
-        FSC_LAST[4] = start_fsc;
-    }
-    if(sid==0x05)
-    {
-        fsc=(uint32_t)(buffer[32]<<24)+(uint32_t)(buffer[33]<<16)+(uint32_t)(buffer[34]<<8)+(uint32_t)buffer[35];
-        start_fsc=(uint32_t)(buffer[36]<<24)+(uint32_t)(buffer[37]<<16)+(uint32_t)(buffer[38]<<8)+(uint32_t)buffer[39];
-        FSC_CURRENT[5] = fsc;
-        FSC_LAST[5] = start_fsc;
-    }
-    return fsc;
+    
+    fsc=(uint32_t)(buffer[0]<<24)+(uint32_t)(buffer[1]<<16)+(uint32_t)(buffer[2]<<8)+(uint32_t)buffer[3];
+    start_fsc=(uint32_t)(buffer[4]<<24)+(uint32_t)(buffer[5]<<16)+(uint32_t)(buffer[6]<<8)+(uint32_t)buffer[7];
+    FSC_CURRENT[1] = fsc;
+    FSC_LAST[1] = start_fsc;
+
+    fsc=(uint32_t)(buffer[8]<<24)+(uint32_t)(buffer[9]<<16)+(uint32_t)(buffer[10]<<8)+(uint32_t)buffer[11];
+    start_fsc=(uint32_t)(buffer[12]<<24)+(uint32_t)(buffer[13]<<16)+(uint32_t)(buffer[14]<<8)+(uint32_t)buffer[15];
+    FSC_CURRENT[2] = fsc;
+    FSC_LAST[2] = start_fsc;
+
+    fsc=(uint32_t)(buffer[16]<<24)+(uint32_t)(buffer[17]<<16)+(uint32_t)(buffer[18]<<8)+(uint32_t)buffer[19];
+    start_fsc=(uint32_t)(buffer[20]<<24)+(uint32_t)(buffer[21]<<16)+(uint32_t)(buffer[22]<<8)+(uint32_t)buffer[23];
+    FSC_CURRENT[3] = fsc;
+    FSC_LAST[3] = start_fsc;
+
+    fsc=(uint32_t)(buffer[24]<<24)+(uint32_t)(buffer[25]<<16)+(uint32_t)(buffer[26]<<8)+(uint32_t)buffer[27];
+    start_fsc=(uint32_t)(buffer[28]<<24)+(uint32_t)(buffer[29]<<16)+(uint32_t)(buffer[30]<<8)+(uint32_t)buffer[31];
+    FSC_CURRENT[4] = fsc;
+    FSC_LAST[4] = start_fsc;
+
+    fsc=(uint32_t)(buffer[32]<<24)+(uint32_t)(buffer[33]<<16)+(uint32_t)(buffer[34]<<8)+(uint32_t)buffer[35];
+    start_fsc=(uint32_t)(buffer[36]<<24)+(uint32_t)(buffer[37]<<16)+(uint32_t)(buffer[38]<<8)+(uint32_t)buffer[39];
+    FSC_CURRENT[5] = fsc;
+    FSC_LAST[5] = start_fsc;
 }
 
 int INCREMENT_SD_LIB(uint8_t sid)
 {
     uint32_t fsc;
     uint32_t start_fsc;
+    int i;
     uint8_t buffer[512];
+    SD_MNG_SECT += SD_LIB_WRITES/(int)0xFFFF;
+    SD_LIB_WRITES = SD_LIB_WRITES%(int)0xFFFF;
     disk_read(buffer,SD_MNG_SECT);
     if(sid==0x01)
     {
@@ -242,7 +231,10 @@
         block_number=SD_SCP_FIRST+fsc;
         result= disk_write(buffer,block_number);
         if(result == 0)
-            INCREMENT_SD_LIB(sid);
+        {
+            if(INCREMENT_SD_LIB(sid) == 0)
+                SD_LIB_WRITES++;
+        }
         return result;
     }
    if(sid==0x02)
@@ -251,7 +243,10 @@
         block_number= SD_SFF_AT_FIRST+fsc;
         result= disk_write(buffer,block_number);
         if(result == 0)
-            INCREMENT_SD_LIB(sid);
+         {
+            if(INCREMENT_SD_LIB(sid) == 0)
+                SD_LIB_WRITES++;
+        }
         return result;
     }
      if(sid==0x03)
@@ -260,7 +255,10 @@
         block_number= SD_SFF_BT_FIRST +fsc;
         result= disk_write(buffer,block_number);
         if(result == 0)
-            INCREMENT_SD_LIB(sid);
+        {
+            if(INCREMENT_SD_LIB(sid) == 0)
+                SD_LIB_WRITES++;
+        }
         return result;
     }
      if(sid==0x04)
@@ -269,7 +267,10 @@
         block_number=SD_HK_ARCH_FIRST +fsc;
         result= disk_write(buffer,block_number);
         if(result == 0)
-            INCREMENT_SD_LIB(sid);
+        {
+            if(INCREMENT_SD_LIB(sid) == 0)
+                SD_LIB_WRITES++;
+        }
         return result;
     }
      if(sid==0x05)
@@ -278,7 +279,10 @@
         block_number= LOG_FIRST +fsc;
         result= disk_write(buffer,block_number);
         if(result == 0)
-            INCREMENT_SD_LIB(sid);
+        {
+            if(INCREMENT_SD_LIB(sid) == 0)
+                SD_LIB_WRITES++;
+        }
         return result;
     }
     return 1;
@@ -286,11 +290,11 @@
 
 uint8_t SD_READ(uint8_t* buffer,uint32_t fsc,uint8_t sid)
 {
-    FCTN_SD_MNGR(sid);
+    FCTN_SD_MNGR();
+    uint32_t block_number;
+    int result;
     if(SD_SW_EN_DS == 1)
         return 0x89;
-    uint32_t block_number;
-    int result;
     if(sid==0x01)
     {
         if(!(FSC_LAST[1]<=fsc && fsc<=FSC_CURRENT[1])){
@@ -604,11 +608,14 @@
     if (cmd(24, block_number * cdv) != 0) {
         return 1;
     }
-
-    // send the data block
-    write(buffer, 512);
-    //printf("Written Successfully bro \n");
-    return 0;
+    
+    uint64_t temp;
+    int r = write(buffer, 512);
+    if(r == 0 ){
+        temp = FCTN_CDMS_RD_RTC();
+        TIME_LATEST_SD_RD = temp >> 7;
+    }
+    return  r;
 }
 
 int write(const uint8_t*buffer, uint32_t length)
@@ -651,6 +658,8 @@
 
     // receive the data
     read(buffer, 512);
+    uint64_t temp = FCTN_CDMS_RD_RTC();
+    TIME_LATEST_SD_RD = temp >> 7;
     return 0;
 }
 
--- a/crc.h	Wed Jun 29 14:17:34 2016 +0000
+++ b/crc.h	Thu Jun 30 14:00:33 2016 +0000
@@ -7,7 +7,7 @@
 #define POLYNOMIAL16 0x1021
 #define POLYNOMIAL8 0x07
 
-typedef uint16_t crctype16; 
+typedef uint16_t crctype16;
 crctype16 crc16_gen(const unsigned char message[], unsigned int nBytes){
     crctype16 remainder = 0xffff;
     int byte;
--- a/i2c.h	Wed Jun 29 14:17:34 2016 +0000
+++ b/i2c.h	Thu Jun 30 14:00:33 2016 +0000
@@ -2,62 +2,161 @@
 #define tc_len 135
 #define tc_test_len 135
 
-InterruptIn irpt_4m_slv(D3);                                      //I2c interrupt from CDMS
-DigitalOut irpt_2_slv(D4);                                        //I2C interrupt to CDMS
-//I2C master(PIN32,PIN31);
-I2C master(PTC9,PTC8);
-//DigitalOut led1(PIN46);
-//DigitalOut led2(PIN95);
 const int addr = 0x20;                                            //slave address 
-char telecommand[tc_len];
-char telemetry[tm_len];
-//Timer test;
-//Timer t_read;
-bool write_ack = true;
-bool read_ack = true;
+bool write_ack = false;
+bool read_ack = false;
+const int addr_pl = 0x20<<1;  //PL address
+const int addr_bae = 0x20; ///bae address
+//uint8_t rcv_isr = 0;
 
 int count = 0;
 
-void FCTN_I2C_WRITE(char *data)
+char PL_I2C_DATA[134];//Payload i2c array
+uint8_t PL_TM_SIZE;//size of data to bev read from i2c
+uint32_t pdirr1;
+uint32_t pdirw1;
+
+void I2C_busreset()
+{
+    PORTE->PCR[1] &= 0xfffffffb;    //Enabling high slew rates for SDA and SCL lines
+    PORTE->PCR[0] &= 0xfffffffb;    //Enabling high slew rates for SDA and SCL lines
+    I2C0->C1 &= 0x7f;               //Disabling I2C module
+    SIM->SCGC4 &= 0xffffffbf;       //Disabling clock to I2C module
+    SIM->SCGC4 |= 0x00000040;       //Enabling clock to I2C module
+    I2C0->C1 |= 0x80;               //Enabling I2C module
+    PORTE->PCR[1] |= 0x00000004;    //Disabling high slew rates for SDA and SCL lines
+    PORTE->PCR[0] |= 0x00000004;    //Disabling high slew rates for SDA and SCL lines
+    Thread::wait(1);                //Wait for all I2C registers to be updates to their their values
+}
+bool FCTN_I2C_READ_PL(char *data,int length)
+{    
+    PL_I2C_GPIO = 1;
+    read_ack = master.read(addr_pl|1,data,length);
+    Thread::wait(1);        //as per tests Thread::wait not required on master side. But its safe to give 1ms 
+    pdirr1=PTE->PDIR;
+    uint8_t i2c_count = 0;    
+    if(read_ack == 0)   //if read_ack says success, it may or may not be successful.Hence we check SCL and SDA
+    {
+        while(((pdirr1 & 0x03000000)!=0x03000000)&& i2c_count<10)//checking SCL and SDA for time=10ms
+        {
+            Thread::wait(1);
+            pdirr1=PTE->PDIR;
+            i2c_count++;
+        }
+        if(((pdirr1 & 0x03000000)==0x03000000))//if SCL and SDA are both high
+        {    
+            gPC.printf("\n\rData received from BAE");
+        }
+        else 
+        {            
+            I2C_busreset();
+            read_ack = 1;
+        }
+    }
+    else if (read_ack == 1)
+    {
+        I2C_busreset();
+    }
+    PL_I2C_GPIO = 0;
+    i2c_count = 0;
+    return read_ack;
+    
+}
+bool FCTN_I2C_WRITE_PL(char *data2,uint8_t tc_len2)
 {
-    irpt_2_slv = 1;
-    //t.start();
-    write_ack = master.write(addr|0x00,data,tc_len);
-    //t.stop();
-    //if(write_ack == 0)
-        //gPC.printf("\n\r data not sent \n");
-       
-if (write_ack == 1)
+    write_ack = master.write(addr_pl|0x00,data2,tc_len2);//address to be defined in payload    
+    Thread::wait(1);    //As per the tests Thread::wait is not required on master side but its safe to give 1ms
+    pdirw1=PTE->PDIR;
+    uint8_t i2c_count = 0;
+    if(write_ack == 0)
+    {
+        while(((pdirw1 & 0x03000000)!=0x03000000)&& i2c_count<10)
+        {
+            Thread::wait(1);
+            pdirw1=PTE->PDIR;
+            i2c_count++;
+        }
+        if(((pdirw1 & 0x03000000)==0x03000000))
+        {
+            gPC.printf("\n\r Data sent");        
+        }
+        else
+        {
+            I2C_busreset();
+            write_ack = 1;
+        }
+    }      
+    if (write_ack == 1)
     {
-//        led2 = 1;
-        //gPC.printf("\n\r data not sent \n");
-//        led2 = 0;    
+        I2C_busreset();
     }
-    irpt_2_slv = 0;
-    //gPC.printf("\n\r %d \n",t.read_us());
-    //t.reset();
+    i2c_count = 0;
+    return write_ack;
+}
+bool FCTN_I2C_READ(char *data,int length)
+{    
+    CDMS_I2C_GPIO = 1;
+    read_ack = master.read(addr_bae|1,data,length);
+    Thread::wait(1);        //as per tests Thread::wait not required on master side. But its safe to give 1ms 
+    pdirr1=PTE->PDIR;
+    uint8_t i2c_count = 0;    
+    if(read_ack == 0)   //if read_ack says success, it may or may not be successful.Hence we check SCL and SDA
+    {
+        while(((pdirr1 & 0x03000000)!=0x03000000)&& i2c_count<10)//checking SCL and SDA for time=10ms
+        {
+            Thread::wait(1);
+            pdirr1=PTE->PDIR;
+            i2c_count++;
+        }
+        if(((pdirr1 & 0x03000000)==0x03000000))//if SCL and SDA are both high
+        {    
+            gPC.printf("\n\rData received from BAE");
+        }
+        else 
+        {            
+            I2C_busreset();
+            read_ack = 1;
+        }
+    }
+    else if (read_ack == 1)
+    {
+        I2C_busreset();
+    }
+    CDMS_I2C_GPIO = 0;
+    i2c_count = 0;
+    return read_ack;
 }
 
-
-void FCTN_I2C_READ(char *data,int length)
-{
-    irpt_2_slv = 1;
-    //t_read.start();
-    read_ack = master.read(addr|1,data,length);
-    //t_read.stop();
-  
-    //if(read_ack == 0)
-      //  printf("\n\rData received from BAE %s \n",data);
- if (read_ack == 1)
+bool FCTN_I2C_WRITE(char *data,int tc_len2)
+{    
+    CDMS_I2C_GPIO = 1;
+    write_ack = master.write(addr_bae|0x00,data,tc_len2);    
+    Thread::wait(1);    //As per the tests Thread::wait is not required on master side but its safe to give 1ms
+    pdirw1=PTE->PDIR;
+    uint8_t i2c_count = 0;
+    if(write_ack == 0)
     {
-        gLEDR = 1;
-        printf("\n \r data not received \n");
-        gLEDR = 0;
+        while(((pdirw1 & 0x03000000)!=0x03000000)&& i2c_count<10)
+        {
+            Thread::wait(1);
+            pdirw1=PTE->PDIR;
+            i2c_count++;
+        }
+        if(((pdirw1 & 0x03000000)==0x03000000))
+        {
+            gPC.printf("\n\r Data sent");        
+        }
+        else
+        {
+            I2C_busreset();
+            write_ack = 1;
+        }
+    }      
+    if (write_ack == 1)
+    {
+        I2C_busreset();
     }
-//if(read_ack == 1)
-//pc.printf("\n \r data not received \n");
-
-    irpt_2_slv = 0;
-    //printf("\n\r %d \n",t.read_us());
-    //t.reset();
+    CDMS_I2C_GPIO = 0;
+    i2c_count = 0;
+    return write_ack;
 }
\ No newline at end of file
--- a/main.cpp	Wed Jun 29 14:17:34 2016 +0000
+++ b/main.cpp	Thu Jun 30 14:00:33 2016 +0000
@@ -19,7 +19,7 @@
 #include "COM_SND_TM.h"
 #include "cdms_sd.h"
 #include "common_functions.h"
-//#include "CDMS_HK.h"
+#include "CDMS_HK.h"
 #include "OBSRS.h"
 #include "adf.h"
 #include "COM_RCV_TC.h"
@@ -29,8 +29,6 @@
 #include "Compression.h"
 #include "ThreadsAndFunctions.h"
 
-DigitalOut SD_SW_EN_DS (PIN97);
-
 //void set_sig(){gSCIENCE_THREAD->signal_set(SCIENCE_SIGNAL);}
 int main()
 {
@@ -58,25 +56,7 @@
     gCS_ADF = 1;
     
     FCTN_CDMS_INIT_RTC(); /* rtc initialization*/
-    FCTN_CDMS_SD_INIT(); /*sd card initialization*/
-    uint8_t test[512];
-    uint8_t data[512];
-    for(int i=0;i<512;i++)
-        data[i] = i%100;
-    for(int i=0;i<512;i++)
-        test[i] = 1;
-    disk_write(test,7000);
-    disk_read(test,7000);
-    uint32_t fsc = FCTN_SD_MNGR(3);
-    if(SD_WRITE(data,fsc,3) == 0)
-    {
-        SD_READ(test,fsc,3);
-        for(int i=0;i<15;i++)
-        {
-            gPC.printf("\r0x%02X 0x%02X\n",data[i],test[i]);
-        }
-    }
-    
+    FCTN_CDMS_SD_INIT(); /*sd card initialization*/    
     
     if (DEBUG)
         gPC.puts("\rwelcome to mng_22tmtc\r\n");
@@ -90,6 +70,7 @@
     gSCIENCE_THREAD = new Thread(SCIENCE_FUN);
    // gPC.puts("step one complete\r\n");
     gSCIENCE_THREAD->set_priority(osPriorityBelowNormal);
+    gHK_THREAD = new Thread(HK_);
     if (DEBUG)
         gPC.puts("competed allocating threads\r\n");