working version

Dependencies:   mbed mbed-rtos SimpleDMA FreescaleIAP eeprom

Fork of CDMS_CODE_FM_28JAN2017 by samp Srinivasan

Files at this revision

API Documentation at this revision

Comitter:
samp1234
Date:
Sat Feb 10 09:06:26 2018 +0000
Parent:
355:a5e72222ff26
Child:
357:f3d48d62e00e
Commit message:
Version used in QM

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
COM_POWER_ON_TX.h Show annotated file Show diff for this revision Revisions of this file
COM_RCV_TC.h Show annotated file Show diff for this revision Revisions of this file
COM_SND_TM.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
Flash.h Show annotated file Show diff for this revision Revisions of this file
OBSRS.h Show annotated file Show diff for this revision Revisions of this file
ThreadsAndFunctions.h Show annotated file Show diff for this revision Revisions of this file
adf.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
common_functions.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	Tue Mar 28 08:50:07 2017 +0000
+++ b/CDMS_HK.h	Sat Feb 10 09:06:26 2018 +0000
@@ -10,6 +10,8 @@
 void FUNC_CDMS_GPIO_STATUS();
 void minMaxHkData();
 void COLLECT_CDMS_RAM();
+void IS_BAE_IN_DISTRESS();
+uint8_t SAVE_BAE(uint8_t);
 
 extern uint8_t beacon_array[134];
 //extern RSSI_MAX;
@@ -74,17 +76,21 @@
             continue;
         CDMS_HK_MAIN_STATUS = 0x01;
         CDMS_HK_MAIN_COUNTER++;
-
-       
-        FCTN_CDMS_HK();         //collects temperatures
+        CDMS_I2C_ERR_BAE_CURRENT = CDMS_I2C_ERR_BAE_COUNTER;
+        
        
         if(!COM_RSSI_SET)
         {
             RSSI_VOLTAGE = COMRX_RSSI_VOLTAGE.read() * 3.3;//to be checked
+            gPC.printf("RSSI_MAX = %f",RSSI_MAX);
         }
-        else COM_RSSI_SET = 0;
+        else {
+          COM_RSSI_SET = 0;
           gPC.printf("RSSI_MAX = %f",RSSI_MAX);
-  //        gPC.printf("RSSI= %f",RSSI_VOLTAGE);
+          RSSI_VOLTAGE = COMRX_RSSI_VOLTAGE.read() * 3.3;
+          gPC.printf("RSSI= %f",RSSI_VOLTAGE);
+          }
+          FCTN_CDMS_HK();         //collects temperatures
         //VERIFY_COMRX();
         VERIFY_RTC();
         HANDLE_HW_FAULTS();
@@ -109,9 +115,9 @@
             CDMS_HEALTH_DATA[86+i] = CDMS_quant[i];
 
         // Here: Have to FIT flash data.
-        CDMS_HEALTH_DATA[106] = (EPS_V_A_EN_STATUS<<7) | ((BAE_STATUS<<5)&0x60) | ((SD_STATUS<<3)&0x18) | ((PL_STATUS<<1)&0x06) | (PL_EPS_LATCH_SW_EN & 0x01);
-     //  CDMS_HEALTH_DATA[107] = (RTC_INIT_STATUS<<6) | ((CDMS_RTC_DISABLE<<5)&0x20);
-         CDMS_HEALTH_DATA[107] = (RTC_INIT_STATUS<<6) | ((EN_RTC<<5)&0x20); // changed by samp
+        CDMS_HEALTH_DATA[106] = (EPS_V_A_EN_STATUS<<7) | ((BAE_STATUS<<5)&0x60) | ((SD_STATUS<<3)&0x18) | ((PL_BEE_SW_STATUS<<1)&0x06) | (PL_EPS_LATCH_SW_EN & 0x01);
+       CDMS_HEALTH_DATA[107] = (RTC_INIT_STATUS<<6) | ((CDMS_RTC_DISABLE<<5)&0x20);
+     //    CDMS_HEALTH_DATA[107] = (RTC_INIT_STATUS<<6) | ((EN_RTC<<5)&0x20); // changed by samp
          gPC.printf("\n\r 0x%08X",EN_RTC);
         CDMS_HEALTH_DATA[108] = CDMS_RESET_COUNTER >>8;
         CDMS_HEALTH_DATA[109] = CDMS_RESET_COUNTER;
@@ -185,25 +191,27 @@
 
         /*---------------------------------- BAE HK --------------------------------------------*/
         
-    //    gPC.printf("endHK_SD2");
+        //gPC.printf("\n\rDO IT NOW\r");
         BAE_HK_I2C = FCTN_I2C_READ(BAE_HK,134);
         //  gPC.printf("\nEntering BAE HK\t");
         if(BAE_HK_I2C == 0) {
-     //  gPC.printf("BAE_HK_");
+        gPC.printf("BAE_HK_");
             crc = crc16_gen((unsigned char *)BAE_HK,132);
             if(crc == ((uint16_t)BAE_HK[132] << 8) | (uint16_t)BAE_HK[133]) {
                 gPC.printf("BAE HK data recieved through I2C\t");
                 TIME_LATEST_I2C_BAE = FCTN_CDMS_RD_RTC() >> 7;
-                /*for(int i = 0; i<15; i++)
-                    gPC.printf("\r 0x%02X\n",BAE_HK[i]);*/
-                for(int i = 0; i<4; i++)
-                    BAE_HK[i] = time >> (3-i)*8;
+//                for(int i = 0; i<15; i++)                        //Changed to test - J
+//                    gPC.printf("%d\t",BAE_HK[i]);
+//                for(int i = 0; i<4; i++)
+//                    BAE_HK[i] = time >> (3-i)*8;
+                for(int i = 128; i<132; i++)
+                    BAE_HK[i] = time >> (131-i)*8;
                 BAE_HK_FRAME[0] = 0x28;
                 BAE_HK_FRAME[1] = (FSC_CURRENT[5]+1) >> 16;
                 BAE_HK_FRAME[2] = (FSC_CURRENT[5]+1) >> 8;
                 BAE_HK_FRAME[3] = FSC_CURRENT[5]+1;
                 for(int i = 0; i<128; i++)                       /*Adding actual CDMS Health data to TM frame*/
-                    BAE_HK_FRAME[4+i] = BAE_HK[i];
+                    BAE_HK_FRAME[4+i] = BAE_HK[4+i];
                 crc = crc16_gen(BAE_HK_FRAME,132);               /*Adding CRC to TM frame*/
                 BAE_HK_FRAME[132] = crc >> 8;
                 BAE_HK_FRAME[133] = crc;
@@ -285,6 +293,9 @@
         } else
             gPC.printf("long Bcn not sent\r\n");
         //gPC.printf("\rCompleted Beacon\n");
+        if (GIVE_UP_ON_BAE == 0)
+        IS_BAE_IN_DISTRESS();
+        
         gPC.printf("\rreleasing mutex for HK_MAIN \n\r");
       SCI_LED1 =0;
        gMutex.unlock();
@@ -311,13 +322,14 @@
 
 void minMaxHkData()
 {
-    MAX_COM_ADF_TMP = (MAX_COM_ADF_TMP < COM_ADF_TMP)?COM_ADF_TMP:MAX_COM_ADF_TMP;
-    MIN_COM_ADF_TMP = (MIN_COM_ADF_TMP > COM_ADF_TMP)?COM_ADF_TMP:MIN_COM_ADF_TMP;
-    
-    MAX_RSSI_VOLTAGE = (MAX_RSSI_VOLTAGE < RSSI_VOLTAGE)?RSSI_VOLTAGE:MAX_RSSI_VOLTAGE;
-    MIN_RSSI_VOLTAGE = (MIN_RSSI_VOLTAGE > RSSI_VOLTAGE)?RSSI_VOLTAGE:MIN_RSSI_VOLTAGE;
-    
+
     if(firstCount==true) {
+        MAX_COM_ADF_TMP = COM_ADF_TMP;
+        MIN_COM_ADF_TMP = COM_ADF_TMP;
+        
+        MAX_RSSI_VOLTAGE = RSSI_VOLTAGE;
+        MIN_RSSI_VOLTAGE = RSSI_VOLTAGE;
+        
         for (int i = 0; i < 16; ++i) {
             min_max_data.temp_min[i] = quant_data.temp_quant[i];
             min_max_data.temp_max[i] = quant_data.temp_quant[i];
@@ -325,7 +337,15 @@
 
         min_max_data.CDMS_temp_min=quant_data.CDMS_temp_quant;
         min_max_data.CDMS_temp_max=quant_data.CDMS_temp_quant;
-    } else {
+        
+        GPIO_FAULTS = 0;
+    } else {    
+        MAX_COM_ADF_TMP = (MAX_COM_ADF_TMP < COM_ADF_TMP)?COM_ADF_TMP:MAX_COM_ADF_TMP;
+        MIN_COM_ADF_TMP = (MIN_COM_ADF_TMP > COM_ADF_TMP)?COM_ADF_TMP:MIN_COM_ADF_TMP;
+    
+        MAX_RSSI_VOLTAGE = (MAX_RSSI_VOLTAGE < RSSI_VOLTAGE)?RSSI_VOLTAGE:MAX_RSSI_VOLTAGE;
+        MIN_RSSI_VOLTAGE = (MIN_RSSI_VOLTAGE > RSSI_VOLTAGE)?RSSI_VOLTAGE:MIN_RSSI_VOLTAGE;
+    
         for (int i = 0; i < 16; ++i) {
             min_max_data.temp_min[i] = saveMin(min_max_data.temp_min[i],quant_data.temp_quant[i]);
             min_max_data.temp_max[i] = saveMax(min_max_data.temp_max[i],quant_data.temp_quant[i]);
@@ -373,9 +393,9 @@
         if(Iteration<4) {
             actual_data.temp_actual[Iteration]=actual_data.temp_actual[Iteration]*3.3*2*10;
         } else if(Iteration<14) {
-            resistance=22000*actual_data.temp_actual[Iteration]*3.3/(3.3-actual_data.temp_actual[Iteration]*3.3);
+            resistance=24300*actual_data.temp_actual[Iteration]*3.3/(3.3-actual_data.temp_actual[Iteration]*3.3);
 
-            if(actual_data.temp_actual[Iteration]*3.3<1.47)      //Document says 1.378 .Pls Check
+            if(actual_data.temp_actual[Iteration]*3.3<1.378)      //Document says 1.378 .Pls Check
 
                 actual_data.temp_actual[Iteration]=(3694/log(24.032242*resistance))-273;
             else
@@ -393,7 +413,7 @@
         } else if(Iteration<14)
            {
             quant_data.temp_quant[Iteration]=quantiz(tstart_thermistor,tstep_thermistor,actual_data.temp_actual[Iteration]);
-        // quant_data.temp_quant[Iteration]=quantiz(0,1,actual_data.temp_actual[Iteration]);
+            //quant_data.temp_quant[Iteration]=quantiz(0,1,actual_data.temp_actual[Iteration]);
             quant_data.temp_quant[13]= COM_PA_TMP_IN_TX;
             }
         else
@@ -446,6 +466,10 @@
     GPIO_STATUS=(COM_TX_CNTRL)?(GPIO_STATUS)|((uint16_t)(0x1<<1)):(GPIO_STATUS)&(~((uint16_t)(0x1<<1)));
     //  gPC.printf("%04x\n",GPIO_STATUS);
     gPC.printf("\n\rBAE_OC STATE = %04x",GPIO_STATUS);
+    if (((GPIO_STATUS) | (GPIO_FAULTS)) != 0xFFFF)
+    {
+        GPIO_FAULTS = (GPIO_FAULTS | (~(GPIO_STATUS)));
+    }
 }
 
 void VERIFY_COMRX()
@@ -548,8 +572,8 @@
 
 void HANDLE_HW_FAULT_PL()
 {
-    if(PL_STATUS != DEVICE_DISABLED) {
-        if(PL_STATUS == DEVICE_OC_FAULT) {
+    if(PL_BEE_SW_STATUS != DEVICE_DISABLED) {
+        if(PL_BEE_SW_STATUS == DEVICE_OC_FAULT) {
             gPC.printf("Switching on PL_BEE");
             PYLD_DFF_CLK = 0;
             PYLD_DFF = 1;           // Switching ON PL
@@ -569,25 +593,15 @@
             PYLD_DFF_CLK = 0;
             wait_us(1);
             PL_FAULTCOUNT++;
-            PL_STATUS = (PL_FAULTCOUNT == 3)?DEVICE_DISABLED:DEVICE_OC_FAULT;
+            PL_BEE_SW_STATUS = (PL_FAULTCOUNT == 3)?DEVICE_DISABLED:DEVICE_OC_FAULT;
             if(PL_FAULTCOUNT == 3) {
-               FCTN_CDMS_WR_FLASH(3,DEVICE_DISABLED);
+               FCTN_CDMS_WR_FLASH(4,DEVICE_DISABLED);
                 gPC.printf("Declaring PL_BEE permanantly Disabled");
             }
         } else {
-            if(PL_STATUS == DEVICE_OC_FAULT) {
-                gPC.printf("Switching off PL_BEE");
-                PYLD_DFF_CLK = 0;
-                PYLD_DFF = 0;             //Switching OFF PL
-                wait_us(1);
-                PYLD_DFF_CLK = 1;
-                wait_us(1);
-                PYLD_DFF_CLK = 0;
-                wait_us(1);
-            }
-            PL_STATUS = DEVICE_ENABLED;
-            if(PL_STATUS != DEVICE_ENABLED)
-                FCTN_CDMS_WR_FLASH(3,DEVICE_ENABLED);
+            PL_BEE_SW_STATUS = DEVICE_POWERED;
+            if(SD_STATUS != DEVICE_POWERED)
+            FCTN_CDMS_WR_FLASH(4,DEVICE_POWERED);
             PL_FAULTCOUNT = 0;
         }
     }
@@ -662,4 +676,41 @@
         CDMS_RAM[79+i] = FSC_OLD[5] >> (3-i)*8;
     CDMS_RAM[83] = 0x00;
     gPC.printf("\n\r%u %u %u %u %u",FSC_CURRENT[1],FSC_CURRENT[2],FSC_CURRENT[3],FSC_CURRENT[4],FSC_CURRENT[5]);
+}
+
+void IS_BAE_IN_DISTRESS()
+{
+    if((BAE_STATUS == 1)&&(CDMS_I2C_ERR_BAE_COUNTER == (CDMS_I2C_ERR_BAE_CURRENT + 2)))
+        {
+            if (CDMS_HK_MAIN_COUNTER == (CDMS_I2C_ERR_HK_LATEST + 1))
+            {
+                CDMS_I2C_ERR_HK_LATEST = CDMS_HK_MAIN_COUNTER;
+                KILL_BAE_COUNTER++;
+                gPC.printf("\n Takin aim, Boss");
+            }
+            else
+            {
+                CDMS_I2C_ERR_HK_LATEST = CDMS_HK_MAIN_COUNTER;
+                KILL_BAE_COUNTER = 1;
+                gPC.printf("\n Target lost");
+            } 
+            if (KILL_BAE_COUNTER >= 3)
+            {
+                GIVE_UP_ON_BAE = SAVE_BAE(KILL_BAE_COUNTER);
+                gPC.printf("\n Kill Confirmed");
+            }
+        }
+}
+
+uint8_t SAVE_BAE(uint8_t counter)
+{
+    BAE_SW_EN_DS = 0;
+    wait(5);
+    BAE_SW_EN_DS = 1;
+    
+    BAE_STATUS = DEVICE_POWERED;
+    FCTN_CDMS_WR_FLASH(1,DEVICE_POWERED);
+    if (counter == 6)
+    return 1;
+    return 0;
 }
\ No newline at end of file
--- a/CDMS_PL.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/CDMS_PL.h	Sat Feb 10 09:06:26 2018 +0000
@@ -1,5 +1,5 @@
 int pl_next_index=-1;   
-uint8_t pl_main_flag=0;
+uint8_t pl_main_flag=0x04;
 uint8_t PL_PREV_STATE=0x00;
 uint8_t POWER_LEVEL = 3;                                //Would be present in HK data extracted 
 uint32_t pl_block[192]={0};
@@ -172,9 +172,9 @@
     //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]]);
+    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)
 {
@@ -318,16 +318,20 @@
     temp[2] = (PL_PREV_STATE & STATE_SCIENCE)>>2;
     //test2(1);
 
-    if(PL_STATUS==2 || PL_STATUS==3)
+    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_STATUS = 0x07;
-        pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);    //Clearing pl_main status
-        PL_MAIN_status = 0;
-        print_exit(temp);
-        return;
+        if(PL_STATUS != 0x00)
+        {
+            pl_main_flag  = pl_main_flag & (~PL_DISABLED);
+            pl_main_flag |= PL_DISABLED;                        //setting PL_STATUS as PL_DISABLED
+            PL_STATUS = 0x07;
+            pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);    //Clearing pl_main status
+            PL_MAIN_status = 0;
+            print_exit(temp);
+            return;
+        }
+        gPC.printf("\n\r PL IN OFF BY PMS - continuing...");
     }
     PL_PREV_STATE = (pl_main_flag & STATE_SCIENCE);         //saving current pl_state
     uint64_t temp_time;
@@ -357,6 +361,8 @@
                 gPC.printf("\n\rFuture block found at index = %d",pl_next_index);
                 break;
             }
+            if(((pl_block[i+1] & 0x3FE00000) >> 21) == 0)
+            {pl_next_index = i + 1;}
         }
     }
     if((pl_next_index==-1) || pl_time==0)           //RTC read failed or Future block not found
@@ -385,67 +391,71 @@
     }           
     
     //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");
+    if(pl_next_index != -1 && pl_next_index != 0)
+    {    
+        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;
-            PL_STATE = 0x00;
-        }
-        if((pl_block[pl_next_index-1] & 0x0000000C)==4)
-        {
-            pl_main_flag  = pl_main_flag & (~STATE_SCIENCE);
-            pl_main_flag |= STATE_STANDBY;
-            PL_STATE = 0x01;
-        }
-        if((pl_block[pl_next_index-1] & 0x0000000C)==8)
-        {
-            pl_main_flag  = pl_main_flag & (~STATE_SCIENCE);
-            pl_main_flag |= STATE_HIBERNATE;
-            PL_STATE = 0x02;
-        }
-        if((pl_block[pl_next_index-1] & 0x0000000C)==12)
-        {
+            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;
+                PL_STATE = 0x00;
+            }
+            if((pl_block[pl_next_index-1] & 0x0000000C)==4)
+            {
+                pl_main_flag  = pl_main_flag & (~STATE_SCIENCE);
+                pl_main_flag |= STATE_STANDBY;
+                PL_STATE = 0x01;
+            }
+            if((pl_block[pl_next_index-1] & 0x0000000C)==8)
+            {
+                pl_main_flag  = pl_main_flag & (~STATE_SCIENCE);
+                pl_main_flag |= STATE_HIBERNATE;
+                PL_STATE = 0x02;
+            }
+            if((pl_block[pl_next_index-1] & 0x0000000C)==12)
+            {
             pl_main_flag  = pl_main_flag & (~STATE_SCIENCE);
             pl_main_flag |= STATE_SCIENCE;
             PL_STATE = 0x03;
+            }
         }
-    }
-    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
-        PL_MAIN_status = 0;
-        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
-        PL_MAIN_status = 0;
-        print_exit(temp);
-        return;
-    }
+        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
+            PL_MAIN_status = 0;
+            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
+            PL_MAIN_status = 0;
+            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_STATUS!=0) 
+            if(PL_BEE_SW_STATUS != 0x03) 
             {
                 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 = 0x03;
+                FCTN_CDMS_WR_FLASH(4,DEVICE_DISABLED);
                 PL_STATUS=0;    
             }           
             pl_main_flag  = pl_main_flag & (~PL_DISABLED);
@@ -462,11 +472,12 @@
         case STATE_STANDBY: 
         {            
             gPC.printf("\n\rEntered PL_STANDBY case");
-            if(PL_STATUS==0) 
+            if(PL_BEE_SW_STATUS == 0x03) 
             {
                 gPC.printf("\n\rPowering on PL_BEE");
                 SET_PL_BEE_ON;
-                PL_STATUS=1;
+                PL_BEE_SW_STATUS = 0x01;
+                FCTN_CDMS_WR_FLASH(4,DEVICE_POWERED);
             }
             gPC.printf("\n\rCommanding PL_BEE to go to Standby State");  
             Base_tm *tm_ptr_standby;
@@ -489,11 +500,12 @@
             gPC.printf("\n\rEntered PL_HIBERNATE case");
             if(POWER_LEVEL==2 || POWER_LEVEL==3 || POWER_LEVEL==0)
             {                
-                if(PL_STATUS==0) 
+                if(PL_BEE_SW_STATUS == 0x03) 
                 {
                     gPC.printf("Powering on PL_BEE\r\n");                    
                     SET_PL_BEE_ON;
-                    PL_STATUS=1;
+                    PL_BEE_SW_STATUS = 0x01;
+                    FCTN_CDMS_WR_FLASH(4,DEVICE_POWERED);
                 }
                 gPC.printf("\n\rCommanding PL_BEE to go to Hibernate State");  
                 Base_tm *tm_ptr_hibernate;
@@ -509,11 +521,12 @@
             {
                 pl_main_flag |= PL_LOW_POWER;
                 PL_LOW_power = 1;
-                if(PL_STATUS==0) 
+                if(PL_BEE_SW_STATUS == 0x03) 
                 {
                     gPC.printf("\n\rPowering on PL_BEE");
                     SET_PL_BEE_ON;
-                    PL_STATUS=1;
+                    PL_BEE_SW_STATUS = 1;
+                    FCTN_CDMS_WR_FLASH(4,DEVICE_POWERED);
                 }
                 gPC.printf("\n\rCommanding PL_BEE to go to Standby State");
                 Base_tm *tm_ptr_standby;
@@ -544,11 +557,12 @@
             if(POWER_LEVEL==3 || POWER_LEVEL==0)                            //POWER_LEVEL = 0 = NA
             {
                 gPC.printf("\n\rPOWER_LEVEL = 3 or NA");
-                if(PL_STATUS==0)
+                if(PL_BEE_SW_STATUS == 0x03)
                 {
                     gPC.printf("\n\rPowering on PL_BEE");
                     SET_PL_BEE_ON;
-                    PL_STATUS=1;
+                    PL_BEE_SW_STATUS = 1;
+                    FCTN_CDMS_WR_FLASH(4,DEVICE_POWERED);
                 }
                 gPC.printf("\n\rCommanding PL_BEE to go to Science State");  
                 Base_tm *tm_ptr_science;
@@ -572,11 +586,12 @@
                 PL_LOW_power = 1;
                 if(POWER_LEVEL==2 || POWER_LEVEL==3 || POWER_LEVEL==0)
                 {                
-                    if(PL_STATUS==0) 
+                    if(PL_BEE_SW_STATUS == 0x03) 
                     {
                         gPC.printf("\n\rPowering on PL_BEE");
                         SET_PL_BEE_ON;
-                        PL_STATUS=1;
+                        PL_BEE_SW_STATUS = 1;
+                        FCTN_CDMS_WR_FLASH(4,DEVICE_POWERED);
                     }
                     gPC.printf("\n\rCommanding PL_BEE to go to Hibernate State");  
                     Base_tm *tm_ptr_hibernate;
@@ -592,11 +607,12 @@
                 {
                     pl_main_flag |= PL_LOW_POWER;
                     PL_LOW_power = 1;
-                    if(PL_STATUS==0) 
+                    if(PL_BEE_SW_STATUS == 0x03) 
                     {
                         gPC.printf("\n\rPowering on PL_BEE");
                         SET_PL_BEE_ON;
-                        PL_STATUS=1;
+                        PL_BEE_SW_STATUS = 1;
+                        FCTN_CDMS_WR_FLASH(4,DEVICE_POWERED);
                     }
                     gPC.printf("\n\rCommanding PL_BEE to go to Standby State");  
                     Base_tm *tm_ptr_standby;
@@ -636,5 +652,6 @@
             print_exit(temp);
             return;
         }
+    }
     }    
 }
\ No newline at end of file
--- a/COM_MNG_TMTC.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/COM_MNG_TMTC.h	Sat Feb 10 09:06:26 2018 +0000
@@ -385,9 +385,11 @@
                 }
                 if(BAE_I2C_GPIO == 1)
                 {
-                    //gPC.printf("BAE_I2C_GPIO is high\r\n");
+                    gPC.printf("BAE_I2C_GPIO is high\r\n");
                     Base_tm *tm_ptr = new Long_tm;
+                                    gPC.printf("en");
                     y=FCTN_I2C_READ((char*)tm_ptr->TM_string,TM_LONG_SIZE);
+                                    gPC.printf("ex");
                     if(y==0)
                     {
                         TIME_LATEST_I2C_BAE = FCTN_CDMS_RD_RTC() >> 7;
@@ -405,7 +407,7 @@
                         return tm_ptr_short;
                     }
                 }
-                else if(BAE_I2C_GPIO == 0)
+                else
                 {
                     gPC.printf("BAE_I2C_GPIO is not high\r\n");                    
                     Base_tm *tm_ptr_short = new Short_tm;
@@ -927,9 +929,9 @@
                             
                             if(tc_ptr->TC_string[4] == 0x00 || tc_ptr->TC_string[4] == 0x01)
                             EN_PL = tc_ptr->TC_string[4];
-                            else if(tc_ptr->TC_string[5] == 0x00 || tc_ptr->TC_string[5] == 0x01)
+                            if(tc_ptr->TC_string[5] == 0x00 || tc_ptr->TC_string[5] == 0x01)        //was else if changed to if to match the flowcharts - J
                             EN_CDMS_HK = tc_ptr->TC_string[5];
-                            else if(tc_ptr->TC_string[6] == 0x00 || tc_ptr->TC_string[6] == 0x01)
+                            if(tc_ptr->TC_string[6] == 0x00 || tc_ptr->TC_string[6] == 0x01)
                             EN_RCV_SC = tc_ptr->TC_string[6];
                             
                             Base_tm *tm_ptr_short = new Short_tm;
@@ -949,19 +951,22 @@
                         {          
                             //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
+                            uint64_t time_temp = FCTN_CDMS_RD_RTC();                        //RTC_RD_EROR has to incorporated
                             Base_tm *tm_ptr = new Long_tm;
                             tm_ptr->TM_string[0] = 0x78;     
                             tm_ptr->TM_string[1] = GETpacket_seq_count(tc_ptr);                                    
                             tm_ptr->TM_string[2] = 0xA0;
                             tm_ptr->TM_string[3] = 0x00;
-                            tm_ptr->TM_string[4] = (uint8_t)((time_temp & 0xFF000000)>>24);
-                            tm_ptr->TM_string[5] = (uint8_t)((time_temp & 0x00FF0000)>>16);
-                            tm_ptr->TM_string[6] = (uint8_t)((time_temp & 0x0000FF00)>>8);
-                            tm_ptr->TM_string[7] = (uint8_t)((time_temp & 0x000000FF));
-                            for(uint8_t i=0;i<124;i++)
+                            tm_ptr->TM_string[4] = (uint8_t)((time_temp & 0x600000000)>>33);
+                            tm_ptr->TM_string[5] = (uint8_t)((time_temp & 0x1E0000000)>>29);
+                            tm_ptr->TM_string[6] = (uint8_t)((time_temp & 0x01F000000)>>24);
+                            tm_ptr->TM_string[7] = (uint8_t)((time_temp & 0x000F80000)>>19);
+                            tm_ptr->TM_string[8] = (uint8_t)((time_temp & 0x00007E000)>>13);
+                            tm_ptr->TM_string[9] = (uint8_t)((time_temp & 0x000001F80)>>7);
+                            tm_ptr->TM_string[10] = (uint8_t)((time_temp & 0x00000007F));
+                            for(uint8_t i=0;i<121;i++)
                             {
-                                tm_ptr->TM_string[i+8] = 0x00;
+                                tm_ptr->TM_string[i+11] = 0x00;
                             }
                             crc16 = crc16_gen(tm_ptr->TM_string,132);
                             tm_ptr->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
@@ -1000,9 +1005,9 @@
                                 return tm_ptr;
                             }
                         }
-                        else if(GETpid(tc_ptr)==0xF1)
+                        else if(GETpid(tc_ptr)==0xF2)
                         {
-                            CDMS_CALIB_RTC((uint64_t)(tc_ptr->TC_string[4])<<32 + (uint64_t)(tc_ptr->TC_string[5])<<24 + (uint64_t)(tc_ptr->TC_string[5])<<16 + (uint64_t)(tc_ptr->TC_string[6])<<8  +(uint64_t)(tc_ptr->TC_string[7]));
+                            CDMS_CALIB_RTC(((uint32_t)(tc_ptr->TC_string[5])<<24) + ((uint32_t)(tc_ptr->TC_string[6])<<16) + ((uint32_t)(tc_ptr->TC_string[7])<<8) + ((uint32_t)(tc_ptr->TC_string[8])));
                             Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xC0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;
@@ -1162,10 +1167,11 @@
                 TIME_LATEST_I2C_SPEED = FCTN_CDMS_RD_RTC() >> 7;
                 //wait(0.2);
                 int poll=0;
-                while(poll<2000 && PL_I2C_Intr==0)
+                //gPC.printf("\n\r%d",PL_I2C_Intr);
+                while(poll<12000 && PL_I2C_Intr==0)
                 {
-                 // gPC.printf("\n\rPL_I2C_Intr is low");
-                    wait_us(10);
+                    //gPC.printf("\n\rPL_I2C_Intr is low");
+                    wait_us(500);
                     poll+=1;
                 }    
             if(PL_I2C_Intr == 1)
@@ -1447,6 +1453,12 @@
         /*gLEDR = !gLEDR;*/\
         Base_tc* current_TC = gHEAD_NODE_TCL;\
         int overflowCount = 0;\
+        P_COM_HK;\
+        if( gFLAGS & COM_PA_HOT_FLAG ){\
+        gPC.printf("PA HOT");\
+        execute_psc = PSC_START_VALUE;\
+        continue;\
+        }\
         while(current_TC != NULL){\
             if( overflowCount < TCL_OVERFLOW_CONSTANT ){\
                 if( (GETcrc_pass(current_TC) == 1) && (GETpacket_seq_count(current_TC) == execute_psc) ){\
@@ -1683,6 +1695,7 @@
             gPC.printf("completed or session timed out: %x\r\n", gMASTER_STATE);\
         /*COMPLETED EXECUTION OF TC*/\
         gMASTER_STATE = TCL_STATE_COMPLETED;\
+        gPC.printf("\n flags2 = %u \n", gFLAGS);\
         COM_POWER_OFF_TX;\
         reset_all;\
         /*ENABLE THREADS*/\
--- a/COM_POWER_ON_TX.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/COM_POWER_ON_TX.h	Sat Feb 10 09:06:26 2018 +0000
@@ -116,10 +116,23 @@
     if( !(gFLAGS & COM_TX_FLAG) ){
         gFLAGS = gFLAGS | COM_TX_FLAG;
         //gPC.puts("Inside COM_TX_FLAG\r\n");
-        gFLAGS = gFLAGS | BAE_SW_EN_FLAG ;
+        if ((BAE_STATUS & 0x03) == 1)
+        {
+            gFLAGS = gFLAGS | BAE_SW_EN_FLAG ;
+        }
         if( gFLAGS & BAE_SW_EN_FLAG ){
             /*WARNING: INFINITE WHILE LOOP POSSIBLE: if standby ack received and bcn tx main status = 0*/
             bool retryFlag = true;
+            pdir_ss1=PTE->PDIR;
+            i2c_count_cdms_bae = 0;
+            gPC.printf("\npdir %d\n",pdir_ss1); 
+            while(((pdir_ss1 & 0x03000000)!=0x03000000)&& i2c_count_cdms_bae<30)   //SDA and SCL are high - bus idle
+            {
+                wait_us(10000);
+                pdir_ss1=PTE->PDIR;
+                i2c_count_cdms_bae++;
+                gPC.printf("waiting for i2c to clear\n"); 
+            }
             while( retryFlag == true ){
                 Base_tm *tm_ptr = NULL;
                 gPC.puts("setting Bcn off\r\n");
@@ -146,7 +159,7 @@
                 else{
                     gPC.puts("not A0\r\n");
                     SW_RST_BAE();
-                    wait_ms(100);
+                    wait_ms(2000);
                     Base_tm *tm_ptr2 = NULL;
                     gPC.puts("settin bcn off\r\n");
                     SET_BCN_STANDBY(tm_ptr2);
@@ -213,6 +226,7 @@
         }
         else{
             gPC.puts("RF_SW_CNTRL_TX\r\n");
+            gPC.printf("You so cool Man");
             RF_SW_CNTRL_TX = 1;
             Thread::wait(25);
             RF_SW_CNTRL_TX = 0;
@@ -253,8 +267,8 @@
         }
         get_tc_list(power_on_tm->next_TM, 0x00);
         /*Call Sign, ACK_L1, TC_LIST*/
-        /*snd_tm.head_pointer(power_on_tm_head);*/
-        /*transmit_adf();*/
+        snd_tm.head_pointer(power_on_tm_head);
+        transmit_adf();
         uint8_t transmissionPass = 0xFF;
         /*PENDING: get acknowledgement of transmission*/
         /*deleting the telemetry*/
--- a/COM_RCV_TC.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/COM_RCV_TC.h	Sat Feb 10 09:06:26 2018 +0000
@@ -7,7 +7,7 @@
         tc_type = LONG_TC_CODE;\
     }\
     else{\
-        /*gPC.puts("incorrect size\r\n");*/\
+        gPC.puts("incorrect size\r\n");\
         /*gPC.printf("%u, %u\r\n", bytes, outState);*/\
         ++gTOTAL_INCORRECT_SIZE_TC;\
     }\
@@ -39,6 +39,7 @@
                 /*gPC.puts("printing tx string\r\n");*/\
                 for(int i = 0 ; i < TC_SHORT_SIZE ; ++i){\
                     gLAST_NODE_TCL->TC_string[i] = rx_tempString[i];\
+                    /*if (i==5) break;*/\
                 }\
                 PUTshort_or_long(gLAST_NODE_TCL, tc_type);\
                 temp16 = 1;\
--- a/COM_SND_TM.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/COM_SND_TM.h	Sat Feb 10 09:06:26 2018 +0000
@@ -80,7 +80,6 @@
                     type1_no++;
                     break;
             }
- 
             head=head->next_TM;
         }
         type1_frame_no = ( type1_no % 10 == 0 )? (type1_no / 10) : ( (type1_no / 10) + 1);
--- a/Compression.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/Compression.h	Sat Feb 10 09:06:26 2018 +0000
@@ -1891,7 +1891,7 @@
                 }
                 if(sd_stat)
                 {
-                    gPC.printf("\n\n\rsd write fail---------------------------------------");
+                    //gPC.printf("\n\n\rsd write fail---------------------------------------");
                 }
                 
              //  gPC.printf("timer end %d::",timer_test.read_ms());                                                    ///changed recently
--- a/DefinitionsAndGlobals.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/DefinitionsAndGlobals.h	Sat Feb 10 09:06:26 2018 +0000
@@ -2,7 +2,7 @@
 // **************DEFINITIONS*********************
 
 #define dma 1
-
+Base_tm *ack_head = NULL;
 // COM_RX
     #define RX_TIMEOUT_LIMIT 0.5
     #define COM_RX_UART_TX PTE20    // For bypassing RX1M (SET BAUD RATE 1200) #define COM_RX_UART_TX USBTX
@@ -17,8 +17,8 @@
     #define SPI_CLK PTE2
     #define SPI_CS_ADF PTA15
     #define SPI_CS_SDC PIN20
-  //   #define SPI_CS_RTC PIN26 // actual
-     #define SPI_CS_RTC PIN60 // sbc with jumper v1.0
+   // #define SPI_CS_RTC PIN26 // actual
+    #define SPI_CS_RTC PIN60 // sbc with jumper v1.0
     
 // COM_TX
     #define COM_TX_CONFIG_LIMIT 3
@@ -41,6 +41,8 @@
   //  #if SBC
     DigitalOut CDMS_I2C_GPIO(PIN67,1);
     DigitalIn BAE_I2C_GPIO(PIN1);
+    uint32_t pdir_ss1;
+    uint8_t i2c_count_cdms_bae;
   //  #endif
     
   //  #if !SBC
@@ -55,22 +57,22 @@
     #define TCL_STATE_COMPLETED 0x03
 
 // LIST OF FLAGS
-    #define UART_INT_FLAG 0x0001
+    #define UART_INT_FLAG 0x0001//
     #define NEW_TC_RECEIVED 0x0002
-    #define COM_SESSION_FLAG 0x0004
+    #define COM_SESSION_FLAG 0x0004//
     #define COM_RX_FLAG 0x0008
     #define COM_MNG_TMTC_RUNNING_FLAG 0x0010
-    #define COM_SESSION_VALIDITY 0x0020
+    #define COM_SESSION_VALIDITY 0x0020//
     #define ALL_CRC_PASS_FLAG 0x0040
     #define COM_PA_HOT_FLAG 0x0080
     #define COM_PA_OC_FLAG 0x8000
     #define COM_TX_FLAG 0x0100
     #define COM_SESSION_TIMEOUT_FLAG 0x0200
     #define COM_AUTO_POWER_OFF_BAE_FLAG 0x0400
-    #define BAE_SW_EN_FLAG 0x0800
+    #define BAE_SW_EN_FLAG 0x0800//
     #define RF_SW_STATUS_FLAG 0x1000
     #define COM_INIT_STATUS_FLAG 0x2000
-    #define COM_TX_STATUS_FLAG 0x4000
+    #define COM_TX_STATUS_FLAG 0x4000//
 
 //RF relay STATUS
     #define RF_COM_TX 0
@@ -210,7 +212,7 @@
 
 //COM_RX
 DigitalOut COM_RX_CNTRL(PIN55,1);//changed!! earlier version had PIN72
-DigitalOut COM_TX_CNTRL(PIN56,1);
+DigitalOut COM_TX_CNTRL(PIN56,0);
 //DigitalIn COM_TX_OC_FAULT(PIN69);
 DigitalIn COM_TX_OC_FAULT(PIN58);
 
@@ -255,8 +257,9 @@
 // CDMS HK
 uint8_t CDMS_STANDBY;
 uint8_t CDMS_HEALTH_DATA[128];
-uint8_t CDMS_RAM[59] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+uint8_t CDMS_RAM[84] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
 uint16_t GPIO_STATUS;
+uint16_t GPIO_FAULTS;
 uint8_t CDMS_HK_MAIN_STATUS;    
 uint8_t COMRX_STATUS;
 uint8_t RTC_FAULTCOUNT = 0;
@@ -293,7 +296,7 @@
 //DigitalIn COMTX_OC_FAULT (PIN69);
 DigitalIn BAE_OC_FAULT (PIN92);
 DigitalOut PL_GPIO_1_STATUS (PIN71); // PYLD EPS CNTRL Signal1 / PYLD_EPS_CNTRL_SIG1
-DigitalOut PL_GPIO_2_STATUS (PIN81);//PYLD EPS CNTRL 2 / PYLD_EPS_CNTRL_2
+DigitalOut PL_GPIO_2_STATUS (PIN81,0);//PYLD EPS CNTRL 2 / PYLD_EPS_CNTRL_2
 DigitalOut PL_GPIO_3_STATUS (PIN80,1);// PYLD EPS CNTRL 3 / PYLD_EPS_CNTRL_3
 DigitalIn PL_BEE_SW_OC_FAULT (PIN91);
 DigitalIn PL_EPS_LATCH_SW_OC_FAULT (PIN5);
@@ -345,7 +348,11 @@
 //Counter's
 uint16_t CDMS_I2C_ERR_SPEED_COUNTER = 0;
 uint16_t CDMS_I2C_ERR_BAE_COUNTER = 0;
+uint16_t CDMS_I2C_ERR_BAE_CURRENT = 0;
+uint8_t KILL_BAE_COUNTER = 0;
+uint8_t GIVE_UP_ON_BAE = 0;
 uint16_t CDMS_HK_MAIN_COUNTER = 0;
+uint16_t CDMS_I2C_ERR_HK_LATEST = 0;
 uint16_t COMRX_RESET_COUNTER = 0;
 uint16_t PL_MAIN_COUNTER = 0;
 uint16_t PL_RCV_SC_DATA_COUNTER = 0;
@@ -378,9 +385,9 @@
 uint8_t EPS_V_A_EN_STATUS;
 //uint8_t BAE_SW_STATUS;
 //uint8_t CDMS_SD_SW_STATUS;
-//uint8_t PL_BEE_SW_STATUS;*
+uint8_t PL_BEE_SW_STATUS;
 uint8_t PL_EPS_LATCH_SW_EN;
-uint8_t RTC_INIT_STATUS;
+uint8_t RTC_INIT_STATUS; //added on 27-5-2017 - Joel
 uint8_t CDMS_RTC_DISABLE;
 uint16_t CDMS_RESET_COUNTER;
 uint32_t TIME_LATEST_CDSMS_RESET;
@@ -390,9 +397,11 @@
 uint8_t COM_PA_TMP_HIGH;
 uint8_t COM_PA_TMP_IN_TX;
 uint8_t COM_PA_RECOVERY_TIMEOUT;
-uint8_t COM_SESSION_TIMEOUT = 300;  //confirm with Anirudh 
+uint8_t COM_SESSION_TIMEOUT = 240;  //confirm with Anirudh 
 uint8_t COM_RSSI_MIN;
-uint16_t SD_LIB_BLK_CURRENT;
+//uint16_t SD_LIB_BLK_CURRENT_1;
+//uint16_t SD_LIB_BLK_CURRENT_2;
+uint32_t SD_LIB_BLK_CURRENT;
 
 // Ambigouties
 uint8_t EN_RTC = 0x01;
@@ -402,4 +411,3 @@
 uint8_t COM_OC_SET = 0;
 uint8_t COM_RSSI_SET = 0;
 uint8_t resume_pl_dma = 0;
-
--- a/FMS_all.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/FMS_all.h	Sat Feb 10 09:06:26 2018 +0000
@@ -81,8 +81,8 @@
 
 void CDMS_SD_SW_ON()
 {
-    SD_CARD_fromuC_ENA3 = 1; 
-    SD_CARD_fromISO_ENA4 = 1;
+   // SD_CARD_fromuC_ENA3 = 1; 
+ //   SD_CARD_fromISO_ENA4 = 1;
     SD_SW_EN_DS = 1;
     SD_STATUS = DEVICE_POWERED;
     FCTN_CDMS_WR_FLASH(2,DEVICE_POWERED);
@@ -91,11 +91,11 @@
 
 void CDMS_SD_SW_OFF()
 {
-    SD_CARD_fromuC_ENA3 = 0; 
-    SD_CARD_fromISO_ENA4 = 0;
+ //   SD_CARD_fromuC_ENA3 = 0; 
+ //   SD_CARD_fromISO_ENA4 = 0;
     SD_SW_EN_DS = 0;
-   // SD_STATUS = DEVICE_DISABLED;
-   SD_STATUS = 0;
+    SD_STATUS = DEVICE_DISABLED;
+ //  SD_STATUS = 0;
    FCTN_CDMS_WR_FLASH(2,DEVICE_DISABLED);
     gPC.printf("sw off sd\n");
 }
@@ -106,14 +106,20 @@
     SPI_mutex.lock();
     gCS_RTC=1;
     gCS_RTC=0;
-    spi.write(0x81); //register address with write flag
-    spi.write(0x00);//disabling stop bit in the seconds register
-   gCS_RTC=1;
-   gCS_RTC=0;
+    spi.write(0x01); //reading seconds register
+    uint8_t response =spi.write(0x01);
+    response = response & 0x7F;
+    gCS_RTC=1;
+    gCS_RTC=0;
     spi.write(0x8C); //register address with write flag
     spi.write(0x00);//enabling halt bit in the seconds register
     gCS_RTC=1;
-     EN_RTC = 0x01;
+    gCS_RTC=0;
+    spi.write(0x81); //register address with write flag
+    spi.write(response);//disabling stop bit in the seconds register
+    gCS_RTC=1;
+
+     //EN_RTC = 0x01;
     CDMS_RTC_DISABLE =1;
     FCTN_CDMS_WR_FLASH(6,DEVICE_POWERED);
   //  gPC.printf("0x%08X \n", READ_FROM_EERPOM(6));
@@ -126,15 +132,20 @@
     SPI_mutex.lock();
     gCS_RTC=1;
     gCS_RTC=0;
+    spi.write(0x01); //reading seconds register
+    uint8_t response =spi.write(0x01);
+    response = response + 0x80;
+    gCS_RTC=1;
+    gCS_RTC=0;
     spi.write(0x81); //register address with write flag
-    spi.write(0x80);//enabling stop bit in the seconds register
+    spi.write(response);//enabling stop bit in the seconds register + also preserving the second bits in the register
     gCS_RTC=1;
     gCS_RTC=0;
     spi.write(0x8C); //register address with write flag
     spi.write(0x40);//enabling halt bit in the seconds register
     gCS_RTC=1;
    
-   EN_RTC  = 0x00;
+   //EN_RTC  = 0x00;
    CDMS_RTC_DISABLE = 0;
     FCTN_CDMS_WR_FLASH(6,0x00);
     SPI_mutex.unlock();
@@ -152,8 +163,8 @@
 void SW_OFF_BAE()
 {
     BAE_SW_EN_DS = 0;
- //   BAE_STATUS = DEVICE_DISABLED;
-     BAE_STATUS = 0;
+    BAE_STATUS = DEVICE_DISABLED;
+    // BAE_STATUS = 0;
      FCTN_CDMS_WR_FLASH(1,DEVICE_DISABLED);
     gPC.printf("sw off bae\n");
 }
@@ -167,8 +178,8 @@
     wait_us(1);
     PYLD_DFF_CLK = 0;
     wait_us(1);
-    PL_STATUS = DEVICE_POWERED;
-    FCTN_CDMS_WR_FLASH(3,DEVICE_POWERED);
+    PL_BEE_SW_STATUS = DEVICE_POWERED;
+    FCTN_CDMS_WR_FLASH(4,DEVICE_POWERED);
 }
 
 void SW_OFF_PL_BEE()
@@ -180,9 +191,8 @@
     wait_us(1);
     PYLD_DFF_CLK = 0;
     wait_us(1);
- //   PL_STATUS = DEVICE_DISABLED;
-    PL_STATUS = 0;
-   FCTN_CDMS_WR_FLASH(3,DEVICE_DISABLED);
+    PL_BEE_SW_STATUS = DEVICE_DISABLED;
+    FCTN_CDMS_WR_FLASH(4,DEVICE_DISABLED);
 }
 
 void SW_ON_PL_EPS()
@@ -196,7 +206,7 @@
     wait_us(1);
     PL_EPS_LATCH_SW_EN = 1;
     
-    FCTN_CDMS_WR_FLASH(4,1);
+    //FCTN_CDMS_WR_FLASH(4,1);
 }
 
 void SW_OFF_PL_EPS()
@@ -210,7 +220,7 @@
     wait_us(1);
     PL_EPS_LATCH_SW_EN = 0;
     
-    FCTN_CDMS_WR_FLASH(4,0);
+    //FCTN_CDMS_WR_FLASH(4,0);
 }
 
 void SW_ON_V_A_EN()
@@ -254,11 +264,11 @@
 
 void SW_RST_PL_BEE()
 {
-    PYLD_DFF = 0;
+    SW_OFF_PL_BEE();
     wait_ms(10);
-    PYLD_DFF = 1;
-    PL_STATUS = DEVICE_POWERED;
-    FCTN_CDMS_WR_FLASH(3,DEVICE_POWERED);
+    SW_ON_PL_BEE();
+    //PL_STATUS = DEVICE_POWERED;
+    //FCTN_CDMS_WR_FLASH(16,DEVICE_POWERED);
 }
 
 void RST_BAE()
@@ -302,6 +312,10 @@
 void RST_HK_COUNTER()
 {
     firstCount = true;
+    CDMS_HK_MAIN_COUNTER = 0;
+    PL_MAIN_COUNTER = 0;
+    PL_RCV_SC_DATA_COUNTER = 0;
+    COMRX_RESET_COUNTER = 0;
 }
 
 int8_t CDMS_RD_SD_HK(uint8_t *sd_statusbits)
@@ -319,36 +333,50 @@
     return p;
 }
 
-void CDMS_CALIB_RTC(uint64_t time)
+void CDMS_CALIB_RTC(uint32_t time)
 {
+    gPC.printf("\n\r Hello : %u",time);
     SPI_mutex.lock();
     gCS_RTC=1;
     spi.format(8,0);
     spi.frequency(1000000);
  
+    gPC.printf("\n\rseconds: %u",((((((uint8_t)(time) & 0x3F)/10)<<4)+(((uint8_t)(time) & 0x3F)%10)) & 0x7F));
+    gCS_RTC=0;
+    spi.write(0x81);
+    spi.write((((((uint8_t)(time) & 0x3F)/10)<<4)+(((uint8_t)(time) & 0x3F)%10)) & 0x7F);//set seconds plus stop bit = 0
+    gCS_RTC=1;
+     
+    gPC.printf("\n\rminutes: %u",(((((uint8_t)(time>>6) & 0x3F)/10)<<4)+(((uint8_t)(time>>6) & 0x3F)%10)));
     gCS_RTC=0;
     spi.write(0x82);
-    spi.write((uint8_t)time);//set minutes
+    spi.write(((((uint8_t)(time>>6) & 0x3F)/10)<<4)+(((uint8_t)(time>>6) & 0x3F)%10));//set minutes
     gCS_RTC=1;
  
+    gPC.printf("\n\rhours: %u",(((((uint8_t)(time>>12) & 0x1F)/10)<<4)+(((uint8_t)(time>>12) & 0x1F)%10)));
     gCS_RTC=0;
     spi.write(0x83); 
-    spi.write((uint8_t)(time>>8) & 0x3F); //set hours
+    spi.write(((((uint8_t)(time>>12) & 0x1F)/10)<<4)+(((uint8_t)(time>>12) & 0x1F)%10)); //set hours
     gCS_RTC=1;
     
+    gPC.printf("\n\rdate : %u",(((((uint8_t)(time>>17) & 0x1F)/10)<<4)+(((uint8_t)(time>>17) & 0x1F)%10)));
     gCS_RTC=0;
     spi.write(0x85); 
-    spi.write((uint8_t)(time>>16) & 0x3F); //set date
+    spi.write(((((uint8_t)(time>>17) & 0x1F)/10)<<4)+(((uint8_t)(time>>17) & 0x1F)%10)); //set date
     gCS_RTC=1;
     
+    gPC.printf("\n\rmonth : %u",(((((uint8_t)(time>>22) & 0x0F)/10)<<4)+(((uint8_t)(time>>22) & 0x0F)%10)));
     gCS_RTC=0;
     spi.write(0x86); 
-    spi.write((uint8_t)(time>>24) & 0x1F); //set month
+    spi.write(((((uint8_t)(time>>22) & 0x0F)/10)<<4)+(((uint8_t)(time>>22) & 0x0F)%10)); //set month
     gCS_RTC=1;
     
+    uint8_t temp = ((((uint8_t)(time>>26) & 0xFF)/10)<<4)+(((uint8_t)(time>>26) & 0xFF)%10);
+    gPC.printf("\n\rtemp : %u",((((uint8_t)(time>>26) & 0xFF)/10)<<4)+(((uint8_t)(time>>26) & 0xFF)%10));
+    uint8_t year = (temp == 0x00)?0x16:(temp == 0x01)?0x17:(temp == 0x02)?0x18:(temp == 0x03)?0x019:0x16;
     gCS_RTC=0;
     spi.write(0x87); 
-    spi.write((uint8_t)(time>>32)); //set year to 00(2000)
+    spi.write(year); //set year
     gCS_RTC=1;
     gPC.puts("\n\r rtc initalised \n");
     SPI_mutex.unlock();
@@ -375,6 +403,8 @@
     uint32_t time = FCTN_CDMS_RD_RTC() >> 7;             //Reading Time from RTC
     for(int i = 124; i<128; i++)
     CDMS_HEALTH_DATA[i] = time >> (127-i)*8;
+    CDMS_HEALTH_DATA[87] = (uint8_t)(RSSI_MAX*10);
+    CDMS_HEALTH_DATA[103] = (uint8_t)(COM_PA_TMP_IN_TX);
     for(int i=0;i<128;i++)
     {
         //tm_pointer->TM_string[i] = 0;
@@ -386,6 +416,7 @@
 void FCTN_CDMS_MAX_MIN(Base_tm *tm_pointer)
 { 
     tm_pointer->TM_string[4] = MAX_COM_ADF_TMP+40;
+    tm_pointer->TM_string[5] = (uint8_t)min_max_data.CDMS_temp_max;
     tm_pointer->TM_string[6] = (uint8_t)(MAX_RSSI_VOLTAGE*10);
     
     for(int i=0;i<16;i++)
@@ -395,6 +426,7 @@
     }
     
     tm_pointer->TM_string[23] = MIN_COM_ADF_TMP+40;
+    tm_pointer->TM_string[24] = (uint8_t)min_max_data.CDMS_temp_min;
     tm_pointer->TM_string[25] = (uint8_t)(MIN_RSSI_VOLTAGE*10);
     
     for(int i=0;i<16;i++)
@@ -403,8 +435,8 @@
         tm_pointer->TM_string[i+26] = (uint8_t)(min_max_data.temp_min[i]);
     }
 
-    tm_pointer->TM_string[42] = GPIO_STATUS>>8;
-    tm_pointer->TM_string[43] = (GPIO_STATUS<<3) & 0x00FF;
+    tm_pointer->TM_string[42] = GPIO_FAULTS>>8;
+    tm_pointer->TM_string[43] = (GPIO_FAULTS<<3) & 0x00FF;
     
     TIME_LATEST_RTC= FCTN_CDMS_RD_RTC() >> 7;
     for(int i = 0; i<4; i++)
--- a/Flash.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/Flash.h	Sat Feb 10 09:06:26 2018 +0000
@@ -13,7 +13,12 @@
 
 void FLASH_INI()
 {
-    INITIAL_FLASH[16] = 80000;
+    INITIAL_FLASH[3] = 80000;
+    INITIAL_FLASH[6] = 1;
+    INITIAL_FLASH[5] = 0;
+    INITIAL_FLASH[2] = 1;
+    INITIAL_FLASH[7] = 0;
+    INITIAL_FLASH[14] = 90;
     uint32_t read[32]; 
     for(int i=0;i<32;i++)
     {  
@@ -22,10 +27,15 @@
             
     if(read[0] == -1)
     {
+        gPC.printf("I happen only once right ???");
         for(int j=0;j<32;j++)
         {   
             FCTN_CDMS_WR_FLASH(j,INITIAL_FLASH[j]); 
         } 
+        for(int j=0;j<32;j++)
+        {   
+            read[j] = nativeflash[j];
+        }
     }
     else
     {
@@ -37,8 +47,10 @@
     EPS_V_A_EN_STATUS = read[0];
     BAE_STATUS = read[1]; 
     SD_STATUS = read[2];
-    PL_STATUS = read[3];
-    PL_EPS_LATCH_SW_EN = read[4];
+    //PL_STATUS = read[3];
+    SD_LIB_BLK_CURRENT = read[3];
+    PL_BEE_SW_STATUS = read[4];
+    //PL_EPS_LATCH_SW_EN = read[4];
     RTC_INIT_STATUS = read[5];
     CDMS_RTC_DISABLE = read[6];
     CDMS_RESET_COUNTER = read[7];
@@ -46,12 +58,13 @@
     COM_TC_BYTES_LIMIT = read[9];
     COM_RX_CURRENT_MAX = read[10];
     COM_RX_DISABLE_TIMEOUT = read[11];
-    COM_PA_TMP_HIGH = read[12];
+    COM_PA_TMP_HIGH = 85;//read[12];
     COM_PA_RECOVERY_TIMEOUT = read[13];
     COM_SESSION_TIMEOUT = read[14];
     COM_RSSI_MIN = read[15];
-    SD_LIB_BLK_CURRENT = read[16];
-    CDMS_RESET_COUNTER = read[17];
+    PL_STATUS = read[16];
+    //SD_LIB_BLK_CURRENT = read[16];
+    //CDMS_RESET_COUNTER = read[17];
 }
 
 /*Writing to the Flash*/
--- a/OBSRS.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/OBSRS.h	Sat Feb 10 09:06:26 2018 +0000
@@ -39,6 +39,7 @@
 bool enable_DS = false;
 bool half_buffer = 0;
 bool TM_sent = false;
+bool print_stuff = false;
 //bool session_over = gFLAGS & COM_SESSION_TIMEOUT_FLAG;
 
 uint8_t SID ;
@@ -116,10 +117,37 @@
         }
 }
 
+#define print_PA_temp {\
+    uint8_t pa_temp = 0;\
+    float pa_temp1 = 0;\
+    pa_temp1 = TempInput.read();\
+    pa_temp1 = pa_temp1 * 3.3;\
+    float resistance;\
+    resistance = 24300 * pa_temp1/(3.3 - pa_temp1);\
+    if(pa_temp1 < 1.378) {\
+        pa_temp1 = (3694/log(24.032242*resistance))-273;\
+    }\
+    else{\
+        pa_temp1 = (3365.4/log(7.60573*resistance))-273;\
+    }\
+    gPC.printf("\n%f\n",pa_temp1);\
+    if (pa_temp1 > COM_PA_TMP_HIGH){\
+        PA_HOT = 1;\
+        gPC.puts("PA is measured HOT\r\n");\
+    }\
+    else{\
+        PA_HOT = 0;\
+        /*gPC.puts("PA is measured not HOT\r\n");*/\
+    }\
+}
+
 #define check_PA_temp {\
-/* pending temperature check function*/\
 /*   return 0 or 1*/\
-    PA_HOT=0;\
+    SelectLinec0=0;\
+    SelectLinec1=0;\
+    SelectLinec2=0;\
+    SelectLinec3=1;\
+    wait_ms(0.1);\
 }
 
 
@@ -341,7 +369,7 @@
             new_session =false;\
             /*cout<<"call_sign_sent"<<" ";*/\
             T_frames_sent = T_frames_sent+2;\
-            check_PA_temp;\
+            print_PA_temp;\
             /*cout<<"pa_hot = "<<PA_HOT<<ENDL;*/\
             if(PA_HOT){\
                 enable_ackandcallsign = true;\
@@ -356,8 +384,30 @@
 }
 
 void send_tm_from_SD_card_fun(unsigned char * buffer_112,bool & last_buffer){
-/*    gPC.printf("\n\rsend tm sd called\r\n");*/
     for(counter_buffer_112=0;counter_buffer_112<112;counter_buffer_112++){
+        //if (print_stuff){
+//            if (restart_adf_flag){
+//                gPC.printf("restart_adf_flag");}
+//                if (enable_new_segment){
+//                gPC.printf("enable_new_segment");}
+//                if (enable_call_sign){
+//                gPC.printf("enable_call_sign");}
+//                if (enable_T_frame){
+//                gPC.printf("enable_T_frame");}
+//                if (adf_limit){
+//                gPC.printf("adf_limit");}
+//                if (enable_ackandcallsign){
+//                gPC.printf("enable_ackandcallsign");}
+//                if (last_buffer){
+//                gPC.printf("last_buffer");}
+//                if (enable_EoS){
+//                gPC.printf("enable_EoS");}
+//                if (enable_read_block && (!last_block)){
+//                gPC.printf("enable_read_block && (!last_block)");}
+//                if ((PA_HOT || (gFLAGS & COM_SESSION_TIMEOUT_FLAG) ||(ack_code != 0xA0))&&(!enable_ackandcallsign)&&(!last_buffer)){
+//                gPC.printf("failed");}
+//                print_stuff = false;
+//            }
         if(restart_adf_flag)
             buffer_112[counter_buffer_112] = 0;
         else if(enable_new_segment){                     // starting new segment
@@ -435,12 +485,15 @@
 //                                    cout<<"new segment"<<ENDL;
                                 }
                             }
+                                                        //print_stuff = true;
                             continue;
+                            
                         }
                         else{
-                            check_PA_temp;
+                            print_PA_temp;
 //                            cout<<"pa_hot = "<<PA_HOT<<" ";
                             if(PA_HOT){
+                                            gPC.printf("h1");
                                 enable_T_frame = false;
                                 enable_read_block = false;
                                 enable_ackandcallsign = true;
@@ -509,11 +562,11 @@
                 }
                 create_Ack_andcallsign = false;
                 Base_tm *ack_and_callsign = NULL;
-                Base_tm *ack_head = NULL;
+                //Base_tm *ack_head = NULL;
 //                get_call_sign(ack_and_callsign);
                 get_ack_l234(ack_and_callsign);
                 ack_head = ack_and_callsign;
-                get_ack_l1(ack_and_callsign->next_TM); 
+                get_ack_l1(ack_and_callsign->next_TM);
                 while(ack_and_callsign->next_TM != NULL){
                     ack_and_callsign = ack_and_callsign->next_TM;
                 }
@@ -645,6 +698,7 @@
 
 void execute_OBSRS_TC(Base_tc *tcp){
     current_obsrs_tc = tcp;
+    check_PA_temp;
     flags_init;
     sent_tmfrom_SDcard = true;
     diff_prev = 0;
--- a/ThreadsAndFunctions.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/ThreadsAndFunctions.h	Sat Feb 10 09:06:26 2018 +0000
@@ -45,11 +45,11 @@
       //  srp(gPAYLOAD_BUFFER);//skip one apcket when cdms resets
         //gPC.puts("exit_srp\r\n");
         //wait(2);/*timeout to be decided*/
-        if(dma)
+        if(dma)  //something wrong here
         {
      //       TIME_LATEST_I2C_SPEED = FCTN_CDMS_RD_RTC() >> 7;   
             PL_RCV_SC_DATA_COUNTER++;
-            gPAY_SPI->bulkRead_start();
+            gPAY_SPI->bulkRead_start();//something wrong here
          //     DMA_LED1 = !DMA_LED1; // added by samp
             //gPAY_SPI->bulkRead_pause();
             //gPAY_SPI->bulkRead_resume(&payload_isr_fun);
@@ -110,8 +110,8 @@
     RSSI_VOLTAGE = COMRX_RSSI_VOLTAGE.read() * 3.3;//to be checked
     RSSI_MAX = RSSI_VOLTAGE;
     COM_RSSI_SET = 1;
-  
     gFLAGS = gFLAGS | UART_INT_FLAG;
+    
     gCOM_MNG_TMTC_THREAD->signal_set(COM_MNG_TMTC_SIGNAL_UART_INT);
 }
 
@@ -193,15 +193,20 @@
     }
     while(true){
         Thread::signal_wait( COM_MNG_TMTC_SIGNAL_UART_INT );
+//        RSSI_MAX = 0;
         if( gFLAGS & UART_INT_FLAG ){
             gFLAGS = gFLAGS & (~UART_INT_FLAG);
+            //gPC.puts("in uart");
             if( !(gFLAGS & COM_SESSION_FLAG) ){
                 // DISABLE THREADS
+               // gPC.puts("1\r\n");
                 HK_counter->stop();
+                resume_pl_dma = 0;
+                PL_GPIO_2_STATUS = 1;//STOP SCIENCE TRANSFER &  CONTROL pin NOT STATUS
                 gPAY_SPI->bulkRead_pause();
-                 PL_GPIO_3_STATUS=1;
+                 PL_GPIO_3_STATUS=1;//problem with this
                 gFLAGS = gFLAGS | COM_SESSION_FLAG;
-                gSESSION_TIMEOUT.attach(&after_session, COM_SESSION_TIMEOUT);
+                gSESSION_TIMEOUT.attach(&after_session, 7*COM_SESSION_TIMEOUT);
                 gFLAGS = gFLAGS | COM_RX_FLAG;
                 gTOTAL_RAW_BYTES = 0;
                 PUT_RAW_BYTE;
@@ -214,29 +219,38 @@
             else if( gFLAGS & COM_RX_FLAG ){
                 //gLEDR = 0;
                 //gLEDG = 1;
+                                //gPC.puts("2\r\n");
+
                 PUT_RAW_BYTE;
             }
             else{
                 //gLEDR = 1;
                 //gLEDG = 0;
+                               // gPC.puts("3\r\n");
+
                 gFLAGS = gFLAGS | COM_RX_FLAG;
                 PUT_RAW_BYTE;
             }
             if(gTOTAL_RAW_BYTES > BYTE_OVERFLOW_CONSTANT){
+                                //gPC.puts("4\r\n");
+
                 RX1M.attach(NULL);
                 gCOM_RX_DISABLE.attach(&after_com_disable, COM_RX_DISABLE_TIMEOUT);
                 after_receive();
             }
         }
         else if( gFLAGS & NEW_TC_RECEIVED ){
-            gPC.puts("\n\n\n\r\rNEW TC RECEIVED\r\n");
+            gFLAGS = gFLAGS & (~NEW_TC_RECEIVED);
+            // DISABLE THE RX1M INTERRUPT
+            RX1M.attach(NULL);
+            gPC.printf("RSSI  =  %f",RSSI_MAX);
             /*Thread::wait(2000);*/
-            gFLAGS = gFLAGS & (~NEW_TC_RECEIVED);
             gFLAGS = gFLAGS | COM_MNG_TMTC_RUNNING_FLAG;
 
-            // DISABLE THE RX1M INTERRUPT
-            RX1M.attach(NULL);
             
+            //gPC.printf("\n flags = %u \n", gFLAGS);
+            gPC.puts("\n\n\n\r\rNEW TC RECEIVED\r\n");
+            //gPC.printf("\n %u \n", gFLAGS);
             // VERIFY CRC, REPEATED PSC AND UPDATE TOTAL_VALID_TC, INCORRECT SIZE TC, CRC FAIL TC
             while(gRX_COUNT < (RX_BUFFER_LENGTH)){
                 gRX_CURRENT_DATA_NODE->values[gRX_COUNT] = 0x00;
@@ -249,7 +263,7 @@
             //Mutex
             gMutex.lock();
             
-            if( gTOTAL_VALID_TC > 0 ){
+            if( gTOTAL_VALID_TC > 1 ){
                 gPC.printf("valid TC rx: %u\r\n", gTOTAL_VALID_TC);
                 //gTOTAL_VALID_TC = 1;
                 if( gTOTAL_VALID_TC < COM_MAX_TC_LIMIT ){
@@ -268,9 +282,9 @@
                             gFLAGS = gFLAGS | COM_SESSION_VALIDITY;
                             //sys_reset_cdms_timer->start(cdms_reset_timeout);
                             COM_POWER_ON_TX();
-                            //gPC.puts("P_com_hk !!\r\n");
+//                            gPC.puts("P_com_hk !!\r\n");
                             P_COM_HK;
-                            //gPC.puts("P_com_hk !!\r\n");
+//                            gPC.puts("P_com_hk !!\r\n");
                             if( (gFLAGS & COM_PA_HOT_FLAG) || ( gFLAGS & COM_PA_OC_FLAG ) ){
                                 gPC.puts("Pahot or paoc !!\r\n");
                                 COM_DATA_fromuC_ENAIN = 0;
@@ -339,8 +353,13 @@
                         COM_DATA_fromuC_ENAIN = 0;
                         COM_DATA_fromISO_ENAOUT=0;
                         COM_TX_CNTRL = 0;
+                        COM_POWER_OFF_TX;
+                        //reset_all;
                         RX1M.attach(&rx_read, Serial::RxIrq);
                         gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);
+                        //resume_pl_dma = 1;
+                        //HK_counter->start(10000);
+                        
                     }
                 }
                 else{
@@ -414,10 +433,9 @@
         if(resume_pl_dma)
         {
             
-            
-            resume_pl_dma = 0;
+            PL_GPIO_2_STATUS=0;
+           // resume_pl_dma = 0;
         }
-        //gPC.puts("dma started");
         gPAY_SPI->bulkRead_resume(&payload_isr_fun);
         gMutex.unlock();
         
--- a/adf.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/adf.h	Sat Feb 10 09:06:26 2018 +0000
@@ -34,6 +34,8 @@
 uint8_t firstbyte;
 uint8_t secondbyte;
 unsigned char buffer_112[112];
+int loop_conter = 0;
+int loop_cntr = 0;
 //ADF temperature reading
 #define temp_correction_value 0
 
@@ -460,8 +462,8 @@
     /*SPI_mutex.unlock();*/\
     if(data_err==false){\
     buffer_state = !buffer_state;\
+        data_err_cnt++;\
     }\
-    data_err_cnt++;\
     if(last_buffer){\
         finish_write_data = true;\
            /*gPC.puts("adf_off\r\n");*/\
@@ -489,8 +491,9 @@
                 T.reset();\
                 T.start();\
                 write_data;\
-                if(sent_tmfrom_SDcard)\
+                if(sent_tmfrom_SDcard){\
                 send_tm_from_SD_card_fun(buffer_112,last_buffer);\
+                }\
                 else snd_tm.transmit_data(buffer_112,&last_buffer);\
             }\
         } else {\
@@ -623,6 +626,8 @@
                             if(cmd_err_flag){\
                                 reset_flag=1;\
                                 gPC.printf("CMD ERR\r\n");\
+                                loop_cntr++;\
+                                if (loop_cntr > 30){loop_cntr = 0;break;}\
                             }\
                             else{\
                                 reset_flag=0;\
@@ -669,9 +674,11 @@
 void transmit_adf(){
     configure =true;
     
+    
 while(retransmit||configure){
     configure =0;
     retransmit=0;
+    loop_conter = 0;//to prevent from going into infinite while loop
 while(power_reset_count<POWER_RESET_THRS){     
     restart_adf_flag=false;
     bool tx_err=false;
@@ -682,7 +689,8 @@
         spi.write(0x1B);
         spi.write(0x07);
    //     spi.write(0x35);
-    spi.write(0x36);
+   // spi.write(0x36);
+    spi.write(0x38);  //PA Output = 30.8
         gCS_ADF=1;
     CMD(CMD_PHY_TX);
     wait_us(2000);
@@ -703,6 +711,8 @@
         break;
     }
     gPC.puts("after while looprn");
+    loop_conter++;
+    if (loop_conter > 2){break;gPC.printf("\nERROR: TM DROPPED");}//to prevent from going into infinite while loop
     
 }
 if(restart_adf_flag){
@@ -710,6 +720,14 @@
         restart_adf_flag=false;
         }
 }
-
+if(ack_head != NULL){
+while(ack_head != NULL){
+                           
+                           Base_tm *temp_pointer = ack_head->next_TM;
+                           delete ack_head;
+//                           gPC.printf("deleting stuff\n\r");
+                           ack_head = temp_pointer;
+                        }                        
+                        }
 }
 #endif
\ No newline at end of file
--- a/cdms_rtc.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/cdms_rtc.h	Sat Feb 10 09:06:26 2018 +0000
@@ -76,7 +76,7 @@
     
     gCS_RTC=0;
     spi.write(0x87); 
-    spi.write(0x00); //set year to 00(2000)
+    spi.write(0x16); //set year to 00(2000)
     gCS_RTC=1;
     gPC.puts("\n\r rtc initalised \n");
     RTC_INIT_STATUS = 1;
@@ -86,7 +86,7 @@
  
 uint64_t FCTN_CDMS_RD_RTC()
 {   
-    if(EN_RTC == 0)
+    if(EN_RTC == 0)  
     return 0;
     SPI_mutex.lock(); 
     uint8_t response;
@@ -140,19 +140,19 @@
     
     gCS_RTC=1;
     gCS_RTC=0;
-    response =spi.write(0x00);
+    spi.write(0x07);
+    response = spi.write(0x01);
     uint8_t year = ((response&0xF0)>>4)*10+(response&0x0F)*1;
     year = (year == 16)?0x00:(year == 17)?0x01:(year == 18)?0x02:(year == 19)?0x03:0x00;
     gCS_RTC=1;
     
-    uint8_t Time_stamp[8] = {year, month, date, day, hours, minutes, seconds, centiseconds};
     
     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)(date&0x1F))<<24);
+    time = time|(((uint64_t)(month&0x0F))<<29);
     time = time|(((uint64_t)(year&0x03))<<33);
     time = (time&0x00000007FFFFFFFF);
     SPI_mutex.unlock();
--- a/cdms_sd.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/cdms_sd.h	Sat Feb 10 09:06:26 2018 +0000
@@ -22,10 +22,10 @@
 
 uint32_t SD_SCP_FIRST=10001;
 uint32_t SD_SCP_LAST=20000;
-uint32_t SD_SFF_AT_FIRST=20001;
-uint32_t SD_SFF_AT_LAST = 30000;
-uint32_t SD_SFF_BT_FIRST =30001;
-uint32_t SD_SFF_BT_LAST=40000;
+uint32_t SD_SFF_AT_FIRST=30001;
+uint32_t SD_SFF_AT_LAST = 40000;
+uint32_t SD_SFF_BT_FIRST =20001;
+uint32_t SD_SFF_BT_LAST=30000;
 uint32_t SD_HK_ARCH_FIRST=40001;
 uint32_t SD_HK_ARCH_LAST= 50000;
 uint32_t LOG_FIRST =50001;
@@ -133,7 +133,7 @@
     if(SD_MNG_SECT != SD_LIB_BLK_CURRENT)
     {
         SD_LIB_BLK_CURRENT = SD_MNG_SECT;
-        FCTN_CDMS_WR_FLASH(16,SD_LIB_BLK_CURRENT);      
+        FCTN_CDMS_WR_FLASH(3,SD_LIB_BLK_CURRENT);      
     }
     SD_LIB_WRITES = SD_LIB_WRITES%(int)0xFFFF;
     disk_write(buffer,SD_MNG_SECT);
@@ -327,7 +327,7 @@
     }
      if(sid==0x05)
     {
-        block_number= LOG_FIRST +(fsc%(LOG_FIRST - LOG_FIRST +1))-1;
+        block_number= LOG_FIRST +(fsc%(LOG_LAST - LOG_FIRST +1))-1;
         //block_number= LOG_FIRST +fsc;
         result= disk_write(buffer,block_number);
         if(result == 0)
@@ -385,7 +385,7 @@
         if(!(FSC_OLD[5]<=fsc && fsc<=FSC_CURRENT[5])){
             return 0x86;
         }
-        block_number= LOG_FIRST +(fsc%(LOG_FIRST - LOG_FIRST +1))-1;
+        block_number= LOG_FIRST +(fsc%(LOG_LAST - LOG_FIRST +1))-1;
         result= disk_read(buffer,block_number);
     }
     else
@@ -668,7 +668,7 @@
     int r = write(buffer, 512);
     if(r == 0 ){
         temp = FCTN_CDMS_RD_RTC();
-        TIME_LATEST_SD_WR = temp >> 7; //corrected by samp:TIME_LATEST_SD_WRD = temp >> 7;
+        TIME_LATEST_SD_WR = temp >> 7; 
     }
     return  r;
 }
--- a/common_functions.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/common_functions.h	Sat Feb 10 09:06:26 2018 +0000
@@ -167,8 +167,8 @@
     pa_temp1 = TempInput.read();\
     pa_temp1 = pa_temp1 * 3.3;\
     float resistance;\
-    resistance = 24000 * pa_temp1/(3.3 - pa_temp1);\
-    if(pa_temp1 > 1.47) {\
+    resistance = 24300 * pa_temp1/(3.3 - pa_temp1);\
+    if(pa_temp1 < 1.378) {\
         pa_temp1 = (3694/log(24.032242*resistance))-273;\
     }\
     else{\
@@ -185,10 +185,8 @@
     gPC.printf("PA V  = %f r\n",pa_volt);\
     COM_PA_IC_TMP = quantiz(tstart_thermistor,tstep_thermistor,pa_temp1);\
     COM_PA_TMP_IN_TX = COM_PA_IC_TMP;\
-    /*comment this ater*/\
-     COM_PA_IC_TMP = 0;\
     /*COM_PA_TMP_HIGH to be found*/\
-    if (COM_PA_IC_TMP > COM_PA_TMP_HIGH){\
+    if (pa_temp1 > COM_PA_TMP_HIGH){\
         returnHere = 0xFF;\
         gPC.puts("PA is measured HOT\r\n");\
     }\
--- a/i2c.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/i2c.h	Sat Feb 10 09:06:26 2018 +0000
@@ -67,7 +67,7 @@
 }
 bool FCTN_I2C_WRITE_PL(char *data2,uint8_t tc_len2)       // Returns 0 for success
 {
-    write_ack = master.write(addr_pl|0x00,data2,tc_len2);//address to be defined in payload    
+    write_ack = master.write(addr_pl|0x00,data2,tc_len2+1);//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;
--- a/main.cpp	Tue Mar 28 08:50:07 2017 +0000
+++ b/main.cpp	Sat Feb 10 09:06:26 2018 +0000
@@ -71,6 +71,10 @@
     // COMMON SPI
     spi.format(8,0);
     spi.frequency(500000);
+    FLASH_INI();        
+    if ((BAE_STATUS & 0x03) == 0x03)
+    {SW_OFF_BAE();}
+    uint8_t sd_stat_temp = SD_STATUS & 0x03;                        //Joel
     SD_CARD_fromuC_ENA3 = 0; 
     SD_CARD_fromISO_ENA4 = 0;
     SD_SW_EN_DS = 0; 
@@ -78,32 +82,49 @@
     SD_CARD_fromuC_ENA3 = 1; 
     SD_CARD_fromISO_ENA4 = 1;
     SD_SW_EN_DS = 1;
-    
+        
     wait(1);
     // SD CARD
     cs_sd = 1;
     gCS_RTC = 1;
     gCS_ADF = 1;
     
-  //  CDMS_RTC_DISABLE = 1;
-    
-    FCTN_CDMS_INIT_RTC();/* rtc initialization*/
+    //CDMS_RTC_DISABLE = 1;
+    if((CDMS_RTC_DISABLE & 0x01) == 1)
+    {
+      CDMS_RTC_ON();
+    }
+    if((RTC_INIT_STATUS & 0x01) != 1)                  //Joel
+     {
+        FCTN_CDMS_INIT_RTC();/* rtc initialization*/
+     }
     FCTN_CDMS_SD_INIT();/* sd card initialization*/
+    if(sd_stat_temp ==  DEVICE_DISABLED)
+    {
+        SD_STATUS = sd_stat_temp;
+    //    SD_STATUS = 0x03;
+        SD_SW_EN_DS = 0;
+    }
+    else if(sd_stat_temp == DEVICE_OC_FAULT)
+    {
+        SD_STATUS = sd_stat_temp;
+    }
    // INIT_EEPROM(); // added by samp
-
-    CDMS_RESET_COUNTER++;
-    FCTN_CDMS_WR_FLASH(17,CDMS_RESET_COUNTER);
+    CDMS_RESET_COUNTER+=1;
+    FCTN_CDMS_WR_FLASH(7,CDMS_RESET_COUNTER);
+    gPC.printf("\n\r%d\n",CDMS_RESET_COUNTER);
     TIME_LATEST_CDSMS_RESET = FCTN_CDMS_RD_RTC() >> 7;
     FCTN_CDMS_WR_FLASH(8,TIME_LATEST_CDSMS_RESET);
     
    // gPC.printf("%d \n", READ_FROM_EERPOM(8));
-    uint8_t test[512];
+    //uint8_t test[512];
   
-    for(int i =0; i<512; i++)
-        test[i] = 0;
-    disk_write(test,80000);
-   
-    //SD_MNG_SECT = SD_LIB_BLK_CURRENT;
+    //for(int i =0; i<512; i++)
+    //    test[i] = 0;
+    //disk_write(test,80000);
+
+    SD_MNG_SECT = SD_LIB_BLK_CURRENT;
+
     FCTN_SD_MNGR();
 
     #if DEBUG