Latest FM Code with EEPROM

Dependencies:   FreescaleIAP SimpleDMA eeprom mbed-rtos mbed

Fork of CDMS_QM_03MAR2017_Flash_with_obsrs by Team Fox

Files at this revision

API Documentation at this revision

Comitter:
samp1234
Date:
Sat Feb 24 10:28:56 2018 +0000
Parent:
355:a5e72222ff26
Commit message:
EEPROM FM

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
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
EEPROM.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 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
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r a5e72222ff26 -r 4484bf39526f CDMS_HK.h
--- a/CDMS_HK.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/CDMS_HK.h	Sat Feb 24 10:28:56 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;
@@ -48,7 +50,6 @@
 void FCTN_CDMS_HK_MAIN(void const *args)
 {
     uint8_t sd_stat = 0;
-    uint8_t hk_count=0;
     while(1) {
         gPC.printf("waiting for HK sig\n\r");
         gHK_THREAD->signal_wait(HK_SIGNAL);
@@ -71,11 +72,15 @@
           
         //gPC.printf("\n\r%d\n",CDMS_WR_SD_FAULT_COUNTER);
         if(EN_CDMS_HK == 0x00)
+        {
+            gMutex.unlock();  //unlocked mutex
             continue;
+        }
         CDMS_HK_MAIN_STATUS = 0x01;
         CDMS_HK_MAIN_COUNTER++;
+        CDMS_I2C_ERR_BAE_CURRENT = CDMS_I2C_ERR_BAE_COUNTER;
+        
 
-       
         FCTN_CDMS_HK();         //collects temperatures
        
         if(!COM_RSSI_SET)
@@ -109,9 +114,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 +190,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 +292,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();
@@ -333,6 +343,7 @@
 
         min_max_data.CDMS_temp_min = saveMin(min_max_data.CDMS_temp_min,quant_data.CDMS_temp_quant);
         min_max_data.CDMS_temp_max = saveMax(min_max_data.CDMS_temp_max,quant_data.CDMS_temp_quant);
+        GPIO_FAULTS = 0;
     }
     firstCount=false;
 }
@@ -446,6 +457,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()
@@ -507,13 +522,13 @@
             SD_FAULTCOUNT++;
             SD_STATUS = (SD_FAULTCOUNT == 3) ? DEVICE_DISABLED :DEVICE_OC_FAULT;
             if(SD_FAULTCOUNT == 3) {
-                FCTN_CDMS_WR_FLASH(2,DEVICE_DISABLED);
+                WRITE_TO_EEPROM(2,DEVICE_DISABLED);
                 gPC.printf("Declaring SD card permanantly Disabled");
             }
         } else {
             SD_STATUS = DEVICE_POWERED;
             if(SD_STATUS != DEVICE_POWERED)
-                FCTN_CDMS_WR_FLASH(2,DEVICE_POWERED);
+                WRITE_TO_EEPROM(2,DEVICE_POWERED);
             SD_FAULTCOUNT = 0;
         }
     }
@@ -534,13 +549,13 @@
             BAE_FAULTCOUNT++;
             BAE_STATUS = (BAE_FAULTCOUNT == 3)?DEVICE_DISABLED:DEVICE_OC_FAULT;
             if(BAE_FAULTCOUNT == 3) {
-                FCTN_CDMS_WR_FLASH(1,DEVICE_DISABLED);
+                WRITE_TO_EEPROM(1,DEVICE_DISABLED);
                 gPC.printf("Declaring BAE permanantly Disabled");
             }
         } else {
             BAE_STATUS = DEVICE_POWERED;
             if(SD_STATUS != DEVICE_POWERED);
-            FCTN_CDMS_WR_FLASH(1,DEVICE_POWERED);
+            WRITE_TO_EEPROM(1,DEVICE_POWERED);
             BAE_FAULTCOUNT = 0;
         }
     }
@@ -548,8 +563,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 +584,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);
+               WRITE_TO_EEPROM(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)
+            WRITE_TO_EEPROM(4,DEVICE_POWERED);
             PL_FAULTCOUNT = 0;
         }
     }
@@ -662,4 +667,38 @@
         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++;
+            }
+            else
+            {
+                CDMS_I2C_ERR_HK_LATEST = CDMS_HK_MAIN_COUNTER;
+                KILL_BAE_COUNTER = 1;
+            } 
+            if (KILL_BAE_COUNTER >= 3)
+            {
+                GIVE_UP_ON_BAE = SAVE_BAE(KILL_BAE_COUNTER);
+            }
+        }
+}
+
+uint8_t SAVE_BAE(uint8_t counter)
+{
+    BAE_SW_EN_DS = 0;
+    wait(5);
+    BAE_SW_EN_DS = 1;
+    
+    BAE_STATUS = DEVICE_POWERED;
+    WRITE_TO_EEPROM(1,DEVICE_POWERED);
+    if (counter == 6)
+    return 1;
+    return 0;
 }
\ No newline at end of file
diff -r a5e72222ff26 -r 4484bf39526f CDMS_PL.h
--- a/CDMS_PL.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/CDMS_PL.h	Sat Feb 24 10:28:56 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};
@@ -139,6 +139,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);\
+    if(EN_PL == 0)\
+    VERIFY_TM_NON_PMS(tm_ptr_science);\
+    else\
     VERIFY_TM(tm_ptr_science);\
     delete pl_tc_science;\
     Base_tm *temp;\
@@ -172,9 +175,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)
 {
@@ -269,6 +272,62 @@
         }
     }    
 }
+void VERIFY_TM_NON_PMS(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 non PMS");
+        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==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);
+            }
+            else
+            {
+                gPC.printf("\n\rACK_CODE failure (0x%02X)",temp);
+                PL_RCV_SC_STATUS = 2;
+            }
+        }
+        else
+        {
+            gPC.printf("\n\rShort_TM CRC failed");
+            PL_RCV_SC_STATUS = 3;
+        }
+    }
+    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==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);
+            }
+            else
+            {
+                gPC.printf("\n\rACK_CODE failure (0x%02X)",temp);
+                PL_RCV_SC_STATUS = 2;
+            }   
+        }
+        else
+        {
+            gPC.printf("\n\rLong_TM CRC failed");  
+            PL_RCV_SC_STATUS = 3;
+        }
+    }    
+}
 void test1(uint8_t t)
 {
     //Output should be PL_STATUS = PL_DISABLED
@@ -318,16 +377,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;
@@ -358,6 +421,8 @@
                 break;
             }
         }
+        if(((pl_block[pl_next_index+1] & 0x3FE00000) >> 21)!=0)
+        {pl_next_index += 1;}
     }
     if((pl_next_index==-1) || pl_time==0)           //RTC read failed or Future block not found
     {       
@@ -385,67 +450,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;
+                WRITE_TO_EEPROM(4,DEVICE_DISABLED);
                 PL_STATUS=0;    
             }           
             pl_main_flag  = pl_main_flag & (~PL_DISABLED);
@@ -462,11 +531,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;
+                WRITE_TO_EEPROM(4,DEVICE_POWERED);
             }
             gPC.printf("\n\rCommanding PL_BEE to go to Standby State");  
             Base_tm *tm_ptr_standby;
@@ -489,11 +559,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;
+                    WRITE_TO_EEPROM(4,DEVICE_POWERED);
                 }
                 gPC.printf("\n\rCommanding PL_BEE to go to Hibernate State");  
                 Base_tm *tm_ptr_hibernate;
@@ -509,11 +580,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;
+                    WRITE_TO_EEPROM(4,DEVICE_POWERED);
                 }
                 gPC.printf("\n\rCommanding PL_BEE to go to Standby State");
                 Base_tm *tm_ptr_standby;
@@ -544,11 +616,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;
+                    WRITE_TO_EEPROM(4,DEVICE_POWERED);
                 }
                 gPC.printf("\n\rCommanding PL_BEE to go to Science State");  
                 Base_tm *tm_ptr_science;
@@ -572,11 +645,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;
+                        WRITE_TO_EEPROM(4,DEVICE_POWERED);
                     }
                     gPC.printf("\n\rCommanding PL_BEE to go to Hibernate State");  
                     Base_tm *tm_ptr_hibernate;
@@ -592,11 +666,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;
+                        WRITE_TO_EEPROM(4,DEVICE_POWERED);
                     }
                     gPC.printf("\n\rCommanding PL_BEE to go to Standby State");  
                     Base_tm *tm_ptr_standby;
@@ -636,5 +711,6 @@
             print_exit(temp);
             return;
         }
+    }
     }    
 }
\ No newline at end of file
diff -r a5e72222ff26 -r 4484bf39526f COM_MNG_TMTC.h
--- a/COM_MNG_TMTC.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/COM_MNG_TMTC.h	Sat Feb 24 10:28:56 2018 +0000
@@ -10,7 +10,6 @@
 //added RLY_TMTC function
 //added included related files
 
-//#include "Flash.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
@@ -18,7 +17,8 @@
 #define COM_MNG_TMTC_H
 
 #include "dmaSPIslave.h"
-
+//#include "FMS_all.h"//remove these
+//#include "Compression.h"//remove these
 #define RESET_CDMS NVIC_SystemReset()
 
 
@@ -446,6 +446,8 @@
                             FCTN_CDMS_RD_L_RAM(tm_ptr);
                             if(mid == 0x0001)
                             FCTN_CDMS_MAX_MIN(tm_ptr);
+                            if(mid == 0x0002)
+                            FCTN_SCP_CALIB_A(tm_ptr, mid);
                             
                             crc16 = crc16_gen(tm_ptr->TM_string,132);
                             tm_ptr->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
@@ -461,16 +463,16 @@
                             Base_tm *tm_ptr_short = new Short_tm;
                             if(mid == 0x0101 || mid==0x0102 || mid == 0x0103 || mid == 0x0100)                                  //allowable MID values?
                             {
-                                
-                                ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
+                                //FCTN_CDMS_WR_S_FLASH(mid,(uint32_t)(tc_ptr->TC_string[5]<<24)+(uint32_t)(tc_ptr->TC_string[6]<<16)+(uint32_t)(tc_ptr->TC_string[7]<<8)+(uint32_t)(tc_ptr->TC_string[8]));                                                     
+                                ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));  
                                 return tm_ptr_short;
                             }                                                        
-
-                            //FCTN_CDMS_WR_S_FLASH(mid,(uint32_t)(tc_ptr->TC_string[5]<<24)+(uint32_t)(tc_ptr->TC_string[6]<<16)+(uint32_t)(tc_ptr->TC_string[7]<<8)+(uint32_t)(tc_ptr->TC_string[8]));                                                     
-
-                            ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));              
-
-                            return tm_ptr_short;
+                            else
+                            {
+                                ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
+                                return tm_ptr_short;
+                            }
+                            
                         }
                         case 0x6:   //service subtye = WR_L_FLASH
                         {
@@ -486,9 +488,7 @@
                                 for(uint8_t z=24;z<32;z++)
                                     flasharray_thres[z] = scp_threshold_m0[z+8];
                                 
-   //                            FCTN_CDMS_WR_S_FLASH(mid,flasharray_thres);
-                                erase_sector(strt_add_thres);
-                                program_flash(strt_add_thres, (char*)flasharray_thres,32);
+                                THRES_WRITE_TO_EEPROM(mid,flasharray_thres);
      
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
@@ -501,9 +501,7 @@
                                 for(uint8_t z=0;z<12;z++)
                                     flasharray_thres[z] = scp_threshold_m0[z+40];
                                 
-  //                              FCTN_CDMS_WR_S_FLASH(mid,flasharray_thres);
-                                erase_sector(strt_add_thres+SECTOR_SIZE);
-                                program_flash(strt_add_thres+SECTOR_SIZE, (char*)flasharray_thres,32);
+                                THRES_WRITE_TO_EEPROM(mid,flasharray_thres);
                                 
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
@@ -516,9 +514,7 @@
                                 for(uint8_t z=0;z<32;z++)
                                     flasharray_thres[z] = scp_threshold_m1[z];
                                 
-  //                              THRES_WRITE_TO_EEPROM(mid,flasharray_thres);
-                                 erase_sector(strt_add_thres+(2*SECTOR_SIZE));
-                                program_flash(strt_add_thres+(2*SECTOR_SIZE), (char*)flasharray_thres,32);
+                                THRES_WRITE_TO_EEPROM(mid,flasharray_thres);
                                 
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
@@ -531,9 +527,7 @@
                                 for(uint8_t z=0;z<20;z++)
                                     flasharray_thres[z] = scp_threshold_m1[z+32];
                                 
- //                               THRES_WRITE_TO_EEPROM(mid,flasharray_thres);
-                                erase_sector(strt_add_thres+(3*SECTOR_SIZE));
-                                program_flash(strt_add_thres+(3*SECTOR_SIZE), (char*)flasharray_thres,32);
+                                THRES_WRITE_TO_EEPROM(mid,flasharray_thres);
                                 
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
@@ -560,10 +554,7 @@
                                 flasharray_thres[10] = scp_sfp_threshold_m0_2[1];
                                 flasharray_thres[11] = scp_sfp_threshold_m0_2[2];
                                 
-  //                              THRES_WRITE_TO_EEPROM(mid,flasharray_thres);
-    
-                                erase_sector(strt_add_thres+(4*SECTOR_SIZE));
-                                program_flash(strt_add_thres+(4*SECTOR_SIZE), (char*)flasharray_thres,32);
+                                THRES_WRITE_TO_EEPROM(mid,flasharray_thres);
                                 
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
@@ -580,10 +571,8 @@
                                 for(uint8_t z=24;z<32;z++)
                                     flasharray_thres[z] = sfp_threshold_m0[z+8];
                                 
-  //                              THRES_WRITE_TO_EEPROM(mid,flasharray_thres);
-                                erase_sector(strt_add_thres+(5*SECTOR_SIZE));
-                                program_flash(strt_add_thres+(5*SECTOR_SIZE), (char*)flasharray_thres,32);
-                                
+                                THRES_WRITE_TO_EEPROM(mid,flasharray_thres);
+                             
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
@@ -595,9 +584,8 @@
                                 for(uint8_t z=0;z<12;z++)
                                     flasharray_thres[z] = sfp_threshold_m0[z+40];
                                 
- //                               THRES_WRITE_TO_EEPROM(mid,flasharray_thres);
-                                        erase_sector(strt_add_thres+(6*SECTOR_SIZE));
-                                program_flash(strt_add_thres+(6*SECTOR_SIZE), (char*)flasharray_thres,32);                             
+                                THRES_WRITE_TO_EEPROM(mid,flasharray_thres);
+                           
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
@@ -609,10 +597,7 @@
                                 for(uint8_t z=0;z<32;z++)
                                     flasharray_thres[z] = sfp_threshold_m1[z];
                                 
- //                               THRES_WRITE_TO_EEPROM(mid,flasharray_thres);
- 
-                                erase_sector(strt_add_thres+(7*SECTOR_SIZE));
-                                program_flash(strt_add_thres+(7*SECTOR_SIZE), (char*)flasharray_thres,32);
+                                THRES_WRITE_TO_EEPROM(mid,flasharray_thres);
                                 
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
@@ -625,9 +610,7 @@
                                 for(uint8_t z=0;z<20;z++)
                                     flasharray_thres[z] = sfp_threshold_m1[z+32];
                                 
-//                                THRES_WRITE_TO_EEPROM(mid,flasharray_thres);
-                                erase_sector(strt_add_thres+(8*SECTOR_SIZE));
-                                program_flash(strt_add_thres+(8*SECTOR_SIZE), (char*)flasharray_thres,32);
+                                THRES_WRITE_TO_EEPROM(mid,flasharray_thres);
                                 
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
@@ -651,7 +634,7 @@
                         if(GETpid(tc_ptr)==0x01)
                         {
                             Base_tm *tm_ptr_short = new Short_tm;
-                            if(CDMS_STANDBY==1)
+                            if(EN_CDMS_HK == 0 && EN_PL == 0 && EN_RCV_SC == 0)
                             {   
                                 //gPC.printf("TC_PL_INIT\r\n");                         // call PWR_SWCH_ON function
                                 
@@ -660,7 +643,7 @@
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }                
-                            else if(CDMS_STANDBY==0)
+                            else
                             {
                                 ACK_L234(tm_ptr_short,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
                                 return tm_ptr_short;
@@ -669,16 +652,17 @@
                         else if(GETpid(tc_ptr)==0x02)
                         {
                             Base_tm *tm_ptr_short = new Short_tm;
-                            if(CDMS_STANDBY==1)
+                            if(EN_CDMS_HK == 0 && EN_PL == 0 && EN_RCV_SC == 0)
                             {   
                                 //gPC.printf("TC_PL_MAIN\r\n");                         // call PWR_SWCH_ON function
-                                
+                                EN_PL = 1;
                                 P_PL_MAIN();
-
+                                EN_PL = 0;
+                                
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }                
-                            else if(CDMS_STANDBY==0)
+                            else 
                             {
                                 ACK_L234(tm_ptr_short,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
                                 return tm_ptr_short;
@@ -687,7 +671,7 @@
                         else if(GETpid(tc_ptr)==0x03)
                         {                            
                             Base_tm *tm_ptr_short = new Short_tm;
-                            if(CDMS_STANDBY==1)
+                            if(EN_CDMS_HK == 0 && EN_PL == 0 && EN_RCV_SC == 0)
                             {   
                                 //gPC.printf("PL_COM_INIT\r\n");                        // call PWR_SWCH_ON function
                                 
@@ -696,7 +680,7 @@
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }                
-                            else if(CDMS_STANDBY==0)
+                            else
                             {
                                 ACK_L234(tm_ptr_short,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
                                 return tm_ptr_short;
@@ -705,34 +689,59 @@
                         else if(GETpid(tc_ptr)==0x04)
                         {                            
                             Base_tm *tm_ptr_short = new Short_tm;
-                            if(CDMS_STANDBY==1)
+                            if(EN_CDMS_HK == 0 && EN_PL == 0 && EN_RCV_SC == 0)
                             {   
                                 //gPC.printf("TC_P_CDMS_HK_MAIN\r\n"); // call PWR_SWCH_ON function
+                                EN_CDMS_HK = 1;
+                                hk_count = 0;
+                                gHK_THREAD->signal_set(HK_SIGNAL);
+                                wait(1);
+                                EN_CDMS_HK = 0;
                                 
-                                //gHK_THREAD->signal_set(HK_SIGNAL);
-
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }                
-                            else if(CDMS_STANDBY==0)
+                            else
                             {
                                 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)
-                        {                            
+                        {   gPC.printf("...............0x%02X",GETpid(tc_ptr));                         
                             Base_tm *tm_ptr_short = new Short_tm;
-                            if(CDMS_STANDBY==1)
+                            if(EN_CDMS_HK == 0 && EN_PL == 0 && EN_RCV_SC == 0)
                             {   
+                                PL_RCV_SC_STATUS = 1;
                                 //gPC.printf("TC_SW_ON_SD\r\n");                        // call PWR_SWCH_ON function
-                                
-                                P_PL_RCV_SC_DATA();
-
-                                ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
-                                return tm_ptr_short;
+                                if(PL_STATE != 0x03)
+                                {   
+                                    Base_tm *tm_ptr_science;
+                                    SET_PL_BEE_SCIENCE(tm_ptr_science);
+                                }
+                                if(PL_RCV_SC_STATUS == 1)
+                                {
+                                    EN_RCV_SC = 1;
+                                    P_PL_RCV_SC_DATA();
+                                    EN_RCV_SC = 0;
+                                    PL_STATE = 0x03;
+                                    ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
+                                    return tm_ptr_short;
+                                }
+                                else if(PL_RCV_SC_STATUS == 2)
+                                {
+                                    gPC.printf("Not in Sci Ack failure\r\n");
+                                    ACK_L234(tm_ptr_short,0x8B,GETpacket_seq_count(tc_ptr));
+                                    return tm_ptr_short;
+                                }
+                                else if(PL_RCV_SC_STATUS == 3)
+                                {
+                                    gPC.printf("Not in Sci CRC failure\r\n");
+                                    ACK_L234(tm_ptr_short,0x8C,GETpacket_seq_count(tc_ptr));
+                                    return tm_ptr_short;
+                                }
                             }                
-                            else if(CDMS_STANDBY==0)
+                            else
                             {
                                 ACK_L234(tm_ptr_short,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
                                 return tm_ptr_short;
@@ -741,7 +750,7 @@
                         else if(GETpid(tc_ptr)==0x06)
                         {                            
                             Base_tm *tm_ptr_short = new Short_tm;
-                            if(CDMS_STANDBY==1)
+                            if(EN_CDMS_HK == 0 && EN_PL == 0 && EN_RCV_SC == 0)
                             {   
                                 //gPC.printf("TC_SW_ON_RTC\r\n"); // call PWR_SWCH_ON function
                                 gPC.printf("SD _INIT\r\n");
@@ -750,7 +759,7 @@
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
                             }                
-                            else if(CDMS_STANDBY==0)
+                            else
                             {
                                 ACK_L234(tm_ptr_short,0x87,GETpacket_seq_count(tc_ptr));                        //TC cannot be processed now
                                 return tm_ptr_short;
@@ -927,9 +936,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 +958,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 +1012,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 +1174,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)
@@ -1430,7 +1443,7 @@
         /*Enable threads*/\
         gPAY_SPI->bulkRead_resume(&payload_isr_fun);\
         HK_counter->start(10000);\
-        gSESSION_TIMEOUT.detach();\
+        /*.detach();*/\
     }\
 }
 
@@ -1612,8 +1625,12 @@
                             }\
                             /*SEND call sign, ACK_L1, NON OBSRS TM TO GS*/\
                             snd_tm.head_pointer(tm_ptr_head);\
+                            sent_tmfrom_SDcard = false;\
                             gPC.puts("enter_adf\r\n");\
+                            COM_RST.reset();\
+                            COM_RST.start();\
                             transmit_adf();\
+                            COM_RST.stop();\
                             gPC.puts("exit_adf\r\n");\
                             /*DELETE THE TM AFTER USE*/\
                             tm_ptr = tm_ptr_head;\
@@ -1637,7 +1654,10 @@
                             /*EXECUTION OF SD-CARD DATA SENDING (OBSRS)*/\
                             execute_OBSRS_TC(current_TC);\
                             gPC.puts("en_adf");\
+                            COM_RST.reset();\
+                            COM_RST.start();\
                             transmit_adf();\
+                            COM_RST.stop();\
                             gPC.puts("ex_adf");\
                         }\
                     }\
@@ -1683,13 +1703,14 @@
             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*/\
         resume_pl_dma = 1;\
         /*gPAY_SPI->bulkRead_resume(&payload_isr_fun)*/;\
         HK_counter->start(10000);\
-        gSESSION_TIMEOUT.detach();\
+        /*.detach();*/\
         gFLAGS = gFLAGS & (~COM_MNG_TMTC_RUNNING_FLAG);\
         gFLAGS = gFLAGS & (~COM_SESSION_FLAG);\
     }\
diff -r a5e72222ff26 -r 4484bf39526f COM_POWER_ON_TX.h
--- a/COM_POWER_ON_TX.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/COM_POWER_ON_TX.h	Sat Feb 24 10:28:56 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,12 @@
         }
         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();*/
+        sent_tmfrom_SDcard = false;
+        snd_tm.head_pointer(power_on_tm_head);
+        COM_RST.reset();//com thread saviour
+        COM_RST.start();
+        transmit_adf();
+        COM_RST.stop();
         uint8_t transmissionPass = 0xFF;
         /*PENDING: get acknowledgement of transmission*/
         /*deleting the telemetry*/
diff -r a5e72222ff26 -r 4484bf39526f COM_RCV_TC.h
--- a/COM_RCV_TC.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/COM_RCV_TC.h	Sat Feb 24 10:28:56 2018 +0000
@@ -125,6 +125,10 @@
     //gPC.puts("received data : \n");
     
     while( data_node != NULL ){
+        if(COM_RST.read()>30){
+        gPC.printf("STUCK IN VALIDATION--DROPPING TC\R\N");
+        break;
+        }
         for(int iBuf = 0 ; iBuf < RX_BUFFER_LENGTH ; ++iBuf ){
             uint8_t test_this = data_node->values[iBuf];
             //gPC.printf("0x%02X ",test_this);
diff -r a5e72222ff26 -r 4484bf39526f Compression.h
--- a/Compression.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/Compression.h	Sat Feb 24 10:28:56 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
diff -r a5e72222ff26 -r 4484bf39526f DefinitionsAndGlobals.h
--- a/DefinitionsAndGlobals.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/DefinitionsAndGlobals.h	Sat Feb 24 10:28:56 2018 +0000
@@ -2,6 +2,7 @@
 // **************DEFINITIONS*********************
 
 #define dma 1
+Timer COM_RST;//to resume other threads if COM gets stuck
 
 // COM_RX
     #define RX_TIMEOUT_LIMIT 0.5
@@ -17,8 +18,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
@@ -39,13 +40,15 @@
     #define SBC 1
     
   //  #if SBC
-    DigitalOut CDMS_I2C_GPIO(PIN67,1);
-    DigitalIn BAE_I2C_GPIO(PIN1);
+  //  DigitalOut CDMS_I2C_GPIO(PIN67,1);
+  //  DigitalIn BAE_I2C_GPIO(PIN1);
+     uint32_t pdir_ss1;
+     uint8_t i2c_count_cdms_bae;
   //  #endif
     
   //  #if !SBC
-  //  DigitalIn BAE_I2C_GPIO(PIN67);
-  //  DigitalOut CDMS_I2C_GPIO(PIN39,0);    
+    DigitalIn BAE_I2C_GPIO(PIN67);
+    DigitalOut CDMS_I2C_GPIO(PIN39,0);    
 // #endif
 
 // TC LIST
@@ -55,22 +58,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
@@ -142,6 +145,7 @@
     #define PAY_SPI_CLK PTE17
     #define PAY_SPI_CS PTE16
     #define SCIENCE_SIGNAL 0x04
+    #define RCVED_SCI_DATA_SIGNAL 0x06 //FOR P_PL_RCV_SC_DATA
 
 //BEACON
     #define BCN_APID_SOURCE 0x41
@@ -155,10 +159,10 @@
     #define BAE_RESET_PID 0x33
 
 //RF RELAY
-  //  #define RF_RELAY_CNTRL_TX PTA12
-  //  #define RF_RELAY_CNTRL_BCN PTA7 
-  #define RF_RELAY_CNTRL_TX PTB7
-    #define RF_RELAY_CNTRL_BCN PTB17 
+    #define RF_RELAY_CNTRL_TX PTA12
+    #define RF_RELAY_CNTRL_BCN PTA7 
+  //#define RF_RELAY_CNTRL_TX PTB7
+    //#define RF_RELAY_CNTRL_BCN PTB17 
     
 //CDMS HK
 #define HK_SIGNAL 0x05
@@ -210,7 +214,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);
 
@@ -253,10 +257,10 @@
 RtosTimer *HK_counter;
 
 // 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};
 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 +297,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,12 +349,17 @@
 //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;
 uint16_t CDMS_WR_SD_FAULT_COUNTER = 0;
 uint16_t SD_LIB_WRITES = 0;
+uint8_t hk_count=0;
 
 //CDMS RAM parameters
 uint8_t PL_INIT_STATUS = 0;
@@ -378,9 +387,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 +399,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;  //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 +413,6 @@
 uint8_t COM_OC_SET = 0;
 uint8_t COM_RSSI_SET = 0;
 uint8_t resume_pl_dma = 0;
-
+bool PL_SC_TEST = false;    //P_PL_RCV_SC_DATA used as a flag for signal
+uint8_t PL_RCV_SC_STATUS;
+uint8_t TEST = 0;
\ No newline at end of file
diff -r a5e72222ff26 -r 4484bf39526f EEPROM.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EEPROM.h	Sat Feb 24 10:28:56 2018 +0000
@@ -0,0 +1,79 @@
+#ifndef EEPROM_H
+#define EEPROM_H
+
+#define ee_scl PIN86
+#define ee_sda PIN87
+#define check 10
+
+EEPROM e2prom(ee_sda, ee_scl, 0, EEPROM::T24C512);
+int32_t INITIAL_EEPROM[32] = {1,1,1,1,1,0,0,0,0,35001,0xff,300,85,20,20,3,80000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+uint32_t flasharray_thres[32];
+
+void WRITE_TO_EEPROM(uint32_t address, int32_t data_block)
+{
+    e2prom.write(address*4+10,data_block);
+}
+
+void THRES_WRITE_TO_EEPROM(uint16_t mid, uint32_t* thres_data)
+{
+    int8_t data[128];
+    memcpy(data,&thres_data,128);
+    
+    if(mid <11 & mid >1)
+    {
+        e2prom.write((uint32_t)(mid-1)*128,data,128);
+    }    
+}
+
+void THRES_READ_FROM_EEPROM(uint16_t mid, uint32_t* thres_data)
+{
+    int8_t data[128];
+    if(mid <11 & mid >1)
+    {
+        e2prom.read((uint32_t)(mid-1)*128,data,128);
+        memcpy(&thres_data,data,128);
+    }    
+}
+
+uint32_t READ_FROM_EERPOM(uint32_t address)
+{
+    int32_t data;
+    e2prom.read(address*4+10, data);
+    return (uint32_t)data;
+}
+
+void INIT_EEPROM()
+{
+    int32_t test_buffer;
+    e2prom.ready();
+    e2prom.read(2,test_buffer);
+    if(test_buffer == 0)
+    {
+        for(int i=0;i<32;i++)
+            WRITE_TO_EEPROM( i, INITIAL_EEPROM[i]);
+        
+        e2prom.write(2,1);
+        gPC.printf("starting flash");
+    }
+    
+    gPC.printf("I happen only once right??\n");
+    EPS_V_A_EN_STATUS         = READ_FROM_EERPOM(0);
+    BAE_STATUS                = READ_FROM_EERPOM(1);
+    SD_STATUS                 = READ_FROM_EERPOM(2);
+    PL_STATUS                 = READ_FROM_EERPOM(3);
+    PL_BEE_SW_STATUS          = READ_FROM_EERPOM(4);
+    RTC_INIT_STATUS           = READ_FROM_EERPOM(5);
+    CDMS_RTC_DISABLE          = READ_FROM_EERPOM(6);
+    CDMS_RESET_COUNTER        = READ_FROM_EERPOM(7);
+    TIME_LATEST_CDSMS_RESET   = READ_FROM_EERPOM(8);
+    COM_TC_BYTES_LIMIT        = READ_FROM_EERPOM(9);
+    COM_RX_CURRENT_MAX        = READ_FROM_EERPOM(10);
+    COM_RX_DISABLE_TIMEOUT    = READ_FROM_EERPOM(11);
+    COM_PA_TMP_HIGH           = READ_FROM_EERPOM(12);
+    COM_PA_RECOVERY_TIMEOUT   = READ_FROM_EERPOM(13);
+    COM_SESSION_TIMEOUT       = READ_FROM_EERPOM(14);
+    COM_RSSI_MIN              = READ_FROM_EERPOM(15);
+    SD_LIB_BLK_CURRENT        = READ_FROM_EERPOM(16);
+}
+
+#endif
\ No newline at end of file
diff -r a5e72222ff26 -r 4484bf39526f FMS_all.h
--- a/FMS_all.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/FMS_all.h	Sat Feb 24 10:28:56 2018 +0000
@@ -1,16 +1,20 @@
 #ifndef FMS_ALL_INCLUDED
 #define FMS_ALL_INCLUDED
-
+//#include "ThreadsAndFunctions.h"
 // Includes MMS RAM functions also
 
 /*===================================================FMS Functions=================================================*/
-
-
+//==========for P_PL_RCV_SC_DATA==========// 
+dmaSPISlave *gPAY_SPI;
+void payload_isr_fun(){
+    gSCIENCE_THREAD->signal_set(SCIENCE_SIGNAL);
+  
+}
+//=========================================//
 //Run processes
 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();
 
@@ -51,7 +55,8 @@
 
 void P_PL_INIT()
 {
-    
+    gPC.printf("No such TC, here have some motivation instead\r\n");
+    gPC.printf("This satellite will work , you just have to believe\r\n");
 }
 
 void P_PL_MAIN()
@@ -64,14 +69,19 @@
     P_COM_INIT;
 }
 
-void P_CDMS_HK_MAIN()
-{
-    FCTN_CDMS_HK_MAIN((void *)NULL);
-}
+void P_PL_RCV_SC_DATA()
+{   gPC.printf("\r\nFM TC....Testing Payload science mode\r\n");
+    PL_SC_TEST = true;
+    PL_GPIO_2_STATUS=0;
+    gPAY_SPI->bulkRead_resume(&payload_isr_fun);//runs science thread while com is waiting
+    gMutex.unlock();
+    //gSCIENCE_THREAD->signal_set(SCIENCE_SIGNAL);
+    Thread::signal_wait( RCVED_SCI_DATA_SIGNAL );
+    //gPC.printf("\r\nit works!!!\r\n");
+    PL_GPIO_2_STATUS = 1;
+    gPAY_SPI->bulkRead_pause();
 
-void P_PL_RCV_SC_DATA()
-{
-    
+    gMutex.lock();
 }
 
 void P_CDMS_INIT_SD()
@@ -81,22 +91,21 @@
 
 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);
+    WRITE_TO_EEPROM(2,DEVICE_POWERED);
     gPC.printf("sw on sd\n");
 }
 
 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;
-   FCTN_CDMS_WR_FLASH(2,DEVICE_DISABLED);
+    SD_STATUS = DEVICE_DISABLED;
+    WRITE_TO_EEPROM(2,DEVICE_DISABLED);
     gPC.printf("sw off sd\n");
 }
 
@@ -106,16 +115,22 @@
     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);
+    WRITE_TO_EEPROM(6,DEVICE_POWERED);
   //  gPC.printf("0x%08X \n", READ_FROM_EERPOM(6));
     SPI_mutex.unlock();
     gPC.printf("sw on rtc\n");
@@ -126,17 +141,22 @@
     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;
-   CDMS_RTC_DISABLE = 0;
-    FCTN_CDMS_WR_FLASH(6,0x00);
+   //EN_RTC  = 0x00;
+    CDMS_RTC_DISABLE = 0;
+    WRITE_TO_EEPROM(6,0x00);
     SPI_mutex.unlock();
     gPC.printf("sw off rtc\n");
 }
@@ -145,16 +165,15 @@
 {
     BAE_SW_EN_DS = 1;
     BAE_STATUS = DEVICE_POWERED;
-    FCTN_CDMS_WR_FLASH(1,DEVICE_POWERED);
+    WRITE_TO_EEPROM(1,DEVICE_POWERED);
     gPC.printf("sw on bae\n");
 }
 
 void SW_OFF_BAE()
 {
     BAE_SW_EN_DS = 0;
- //   BAE_STATUS = DEVICE_DISABLED;
-     BAE_STATUS = 0;
-     FCTN_CDMS_WR_FLASH(1,DEVICE_DISABLED);
+    BAE_STATUS = DEVICE_DISABLED;
+    WRITE_TO_EEPROM(1,DEVICE_DISABLED);
     gPC.printf("sw off bae\n");
 }
 
@@ -167,8 +186,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;
+    WRITE_TO_EEPROM(4,DEVICE_POWERED);
 }
 
 void SW_OFF_PL_BEE()
@@ -180,9 +199,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;
+    WRITE_TO_EEPROM(4,DEVICE_DISABLED);
 }
 
 void SW_ON_PL_EPS()
@@ -195,8 +213,6 @@
     PYLD_EPS_DFF_PWR_CLK = 0;
     wait_us(1);
     PL_EPS_LATCH_SW_EN = 1;
-    
-    FCTN_CDMS_WR_FLASH(4,1);
 }
 
 void SW_OFF_PL_EPS()
@@ -209,22 +225,21 @@
     PYLD_EPS_DFF_PWR_CLK = 0;
     wait_us(1);
     PL_EPS_LATCH_SW_EN = 0;
-    
-    FCTN_CDMS_WR_FLASH(4,0);
 }
 
 void SW_ON_V_A_EN()
 {
     V_A_EN = 1;
     EPS_V_A_EN_STATUS = 1;
-   FCTN_CDMS_WR_FLASH(0,1);
+    WRITE_TO_EEPROM(0,1);
+   
 }
 
 void SW_OFF_V_A_EN()
 {
     V_A_EN = 0;
     EPS_V_A_EN_STATUS = 0;
-   FCTN_CDMS_WR_FLASH(0,0);
+    WRITE_TO_EEPROM(0,0);
 }
 
 void RST_SD()
@@ -237,7 +252,7 @@
     SD_CARD_fromISO_ENA4 = 1;
     SD_SW_EN_DS = 1;
     SD_STATUS = DEVICE_POWERED;
-    FCTN_CDMS_WR_FLASH(2,DEVICE_POWERED);
+    WRITE_TO_EEPROM(2,DEVICE_POWERED);
     gPC.printf("rst sd\n");
 }
 
@@ -248,17 +263,15 @@
    BAE_SW_EN_DS = 1;
    
     BAE_STATUS = DEVICE_POWERED;
-    FCTN_CDMS_WR_FLASH(1,DEVICE_POWERED);
+    WRITE_TO_EEPROM(1,DEVICE_POWERED);
     gPC.printf("rst bae\n");
 }
 
 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();
 }
 
 void RST_BAE()
@@ -302,6 +315,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 +336,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();
@@ -383,6 +414,8 @@
     
 }
 
+void FCTN_CDMS_MAX_MIN(Base_tm *);                      //idk Joel
+
 void FCTN_CDMS_MAX_MIN(Base_tm *tm_pointer)
 { 
     tm_pointer->TM_string[4] = MAX_COM_ADF_TMP+40;
@@ -403,12 +436,26 @@
         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++)
         tm_pointer->TM_string[44+i] = TIME_LATEST_RTC >> (3-i)*8; 
 }
 
+void FCTN_SCP_CALIB_A(Base_tm *, uint16_t);
+
+void FCTN_SCP_CALIB_A(Base_tm *tm_pointer, uint16_t mid)
+{
+    uint32_t temp_e2prom[32] = {0};
+//    THRES_READ_FROM_EEPROM(mid, temp_e2prom);
+    for(uint8_t i = 0; i < 32; i++)
+    {
+        tm_pointer->TM_string[(4*i)+4] = (temp_e2prom[i]>>24);
+        tm_pointer->TM_string[(4*i)+5] = (temp_e2prom[i]>>16);
+        tm_pointer->TM_string[(4*i)+6] = (temp_e2prom[i]>>8);
+        tm_pointer->TM_string[(4*i)+7] = temp_e2prom[i];
+    }
+}
 #endif
\ No newline at end of file
diff -r a5e72222ff26 -r 4484bf39526f Flash.h
--- a/Flash.h	Tue Mar 28 08:50:07 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,113 +0,0 @@
-#include "FreescaleIAP.h"
-
-int strt_add_thres = flash_size() - (11*SECTOR_SIZE);
-uint32_t flasharray_thres[32] = {0};
-
-void FCTN_CDMS_WR_FLASH(uint16_t , uint32_t);
-int strt_add = flash_size() - (2*SECTOR_SIZE);
-uint32_t flasharray[32];    //256+(3*1024)
-uint32_t INITIAL_FLASH[32];
-
-/*corrected*/
-int *nativeflash = (int*)strt_add;
-
-void FLASH_INI()
-{
-    INITIAL_FLASH[16] = 80000;
-    uint32_t read[32]; 
-    for(int i=0;i<32;i++)
-    {  
-        read[i] = nativeflash[i];
-    }
-            
-    if(read[0] == -1)
-    {
-        for(int j=0;j<32;j++)
-        {   
-            FCTN_CDMS_WR_FLASH(j,INITIAL_FLASH[j]); 
-        } 
-    }
-    else
-    {
-        for(int j=0;j<32;j++)
-        {   
-            read[j] = nativeflash[j];
-        }
-    }
-    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];
-    RTC_INIT_STATUS = read[5];
-    CDMS_RTC_DISABLE = read[6];
-    CDMS_RESET_COUNTER = read[7];
-    TIME_LATEST_CDSMS_RESET = read[8];
-    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_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];
-}
-
-/*Writing to the Flash*/
-void FCTN_CDMS_WR_FLASH(uint16_t j,uint32_t datablock)  //j-position to write address  ; fdata - flash data to be written
-{
-    for(int i=0;i<17;i++)
-    {
-        flasharray[i]=nativeflash[i];
-    }
-    flasharray[j]=datablock;
-    erase_sector(strt_add);
-    program_flash(strt_add, (char*)flasharray,32);
-}
-/*End*/
-
-/*===================================================MMS Functions=================================================*/
-
-void FCTN_CDMS_WR_S_FLASH(uint16_t mid,uint32_t datablock);
-
-void FCTN_CDMS_WR_S_FLASH(uint16_t mid,uint32_t datablock)
-{
-    if(mid == 0x0100)
-    {
-        FCTN_CDMS_WR_FLASH(11,(datablock>>16) & 0x000000FF);
-        FCTN_CDMS_WR_FLASH(13,(datablock>>8) & 0x000000FF);
-        FCTN_CDMS_WR_FLASH(14,datablock & 0x000000FF);
-    }
-    else if(mid == 0x0101)
-    {
-        FCTN_CDMS_WR_FLASH(12,(datablock>>16) & 0x000000FF);
-        FCTN_CDMS_WR_FLASH(10,(datablock>>8) & 0x000000FF);
-        FCTN_CDMS_WR_FLASH(15,datablock & 0x000000FF);
-    }
-    else if(mid == 0x0102)
-    {
-        FCTN_CDMS_WR_FLASH(9,datablock & 0x0000FFFF);
-    }
-    else if(mid == 0x0103)
-    {
-        FCTN_CDMS_WR_FLASH(6,datablock & 0x00000001);
-    } 
-}
-
-int verify_flash_sector(int mid,uint32_t* flasharray_thres)
-{
-    int add = strt_add_thres + ((mid-1)*SECTOR_SIZE);
-    int result = 0;
-    int* testflash = (int*)add;
-    for(int i=0;i<32;i++)
-    {  
-        if(testflash[i] == flasharray_thres[i])
-            continue;
-        else
-        {
-            return 1;
-        }
-    }
-    return 0;
-}
\ No newline at end of file
diff -r a5e72222ff26 -r 4484bf39526f OBSRS.h
--- a/OBSRS.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/OBSRS.h	Sat Feb 24 10:28:56 2018 +0000
@@ -117,9 +117,35 @@
 }
 
 #define check_PA_temp {\
-/* pending temperature check function*/\
 /*   return 0 or 1*/\
-    PA_HOT=0;\
+uint8_t pa_temp = 0;\
+    float pa_temp1 = 0;\
+    SelectLinec0=0;\
+    SelectLinec1=0;\
+    SelectLinec2=0;\
+    SelectLinec3=1;\
+    wait_ms(0.1);\
+    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) {\
+        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);*/\
+    COM_PA_IC_TMP = quantiz(tstart_thermistor,tstep_thermistor,pa_temp1);\
+    COM_PA_TMP_IN_TX = COM_PA_IC_TMP;\
+    if (COM_PA_IC_TMP > 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");*/\
+    }\
 }
 
 
@@ -358,6 +384,7 @@
 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++){
+        //gPC.printf("%d\r\n",counter_buffer_112);
         if(restart_adf_flag)
             buffer_112[counter_buffer_112] = 0;
         else if(enable_new_segment){                     // starting new segment
diff -r a5e72222ff26 -r 4484bf39526f ThreadsAndFunctions.h
--- a/ThreadsAndFunctions.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/ThreadsAndFunctions.h	Sat Feb 24 10:28:56 2018 +0000
@@ -1,18 +1,16 @@
 //Science Thread
 //SPI ISR
-
+#include "FMS_all.h"//this is to include the 'dmaSPISlave' and payload_isr_fun
 #include "COM_MNG_TMTC.h"
 #include "adf.h"
 #include "dmaSPIslave.h"
-
+//dmaSPISlave *gPAY_SPI;
+//void payload_isr_fun(){
+//    gSCIENCE_THREAD->signal_set(SCIENCE_SIGNAL);
+//  
+//}
 
 
-dmaSPISlave *gPAY_SPI;
-
-void payload_isr_fun(){
-    gSCIENCE_THREAD->signal_set(SCIENCE_SIGNAL);
-  
-}
 
 void payload_isr_fun_dma(void const *args ){
     gSCIENCE_THREAD->signal_set(SCIENCE_SIGNAL);
@@ -30,10 +28,14 @@
     while(true){
         gPC.printf("waiting for Sci sig\n\r");
         gSCIENCE_THREAD->signal_wait(SCIENCE_SIGNAL);
-         
         gPC.printf("checking mutex in Sci\n\r");
         gMutex.lock();
         
+        if (EN_RCV_SC == 0)
+        {
+            gMutex.unlock();
+            continue;
+        }
         gPC.printf("locked mutex,entered in Sci thread\n\r"); 
   
         DMA_LED1 = 1;
@@ -45,11 +47,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);
@@ -88,7 +90,11 @@
         gPC.printf("\r\nreleasing mutex for Sci\n\r");
         DMA_LED1 =0;
         //__enable_irq();
-        gMutex.unlock(); 
+        if(PL_SC_TEST){
+          PL_SC_TEST = false;
+          gCOM_MNG_TMTC_THREAD->signal_set(RCVED_SCI_DATA_SIGNAL);//used for P_PL_RCV_SC_DATA
+        }
+        gMutex.unlock();
     }
 }
 
@@ -106,12 +112,14 @@
 void rx_read(){
     gRX_CURRENT_DATA_NODE->values[gRX_COUNT] = RX1M.getc();
     //Checking RSSI
+    //gPC.printf("\nfck ths sht\n");
     if(!COM_RSSI_SET)
     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);
 }
 
@@ -195,11 +203,15 @@
         Thread::signal_wait( COM_MNG_TMTC_SIGNAL_UART_INT );
         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();
-                gPAY_SPI->bulkRead_pause();
-                 PL_GPIO_3_STATUS=1;
+                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;//problem with this
                 gFLAGS = gFLAGS | COM_SESSION_FLAG;
                 gSESSION_TIMEOUT.attach(&after_session, COM_SESSION_TIMEOUT);
                 gFLAGS = gFLAGS | COM_RX_FLAG;
@@ -214,42 +226,50 @@
             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);
             /*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;
                 ++gRX_COUNT;
             }
             gRX_COUNT = 0;
+            COM_RST.reset();//com thread saviour
+            COM_RST.start();
             raw_data_to_tc();
+            COM_RST.stop();
             gFLAGS = gFLAGS & (~COM_RX_FLAG);
-            
             //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 ){
@@ -414,11 +434,11 @@
         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);
+        //gPAY_SPI->bulkRead_resume(&payload_isr_fun);
         gMutex.unlock();
         
     }
diff -r a5e72222ff26 -r 4484bf39526f adf.h
--- a/adf.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/adf.h	Sat Feb 24 10:28:56 2018 +0000
@@ -34,6 +34,7 @@
 uint8_t firstbyte;
 uint8_t secondbyte;
 unsigned char buffer_112[112];
+int loop_conter = 0;
 //ADF temperature reading
 #define temp_correction_value 0
 
@@ -407,6 +408,7 @@
 
 #define write_data {\
     counter++;\
+    /*gPC.printf("%d\r\n",counter);*/\
     if(counter==584){\
         finish_write_data=true;\
         restart_adf_flag=true;\
@@ -668,13 +670,19 @@
 bool configure = true;
 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;
+    if(sent_tmfrom_SDcard == false){
+        if(COM_RST>100){
+            gPC.printf("\r\nSTUCK IN LOOP EXITING\r\n ");
+            break;
+            }
+        }
     configure_adf;
     send_data;
     /*PA MCR level*/;
@@ -682,13 +690,20 @@
         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);
     T.start();
     while(loop_on){
-        wait_us(20);
+        if(sent_tmfrom_SDcard == false){
+        if(COM_RST>100){
+            gPC.printf("\r\nSTUCK IN LOOP EXITING\r\n ");
+            break;
+            }
+        }
+        wait_us(15);
         check;
         if(data_irq_err||tx_err){
             hw_reset_err_cnt++;
@@ -703,6 +718,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){
diff -r a5e72222ff26 -r 4484bf39526f cdms_rtc.h
--- a/cdms_rtc.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/cdms_rtc.h	Sat Feb 24 10:28:56 2018 +0000
@@ -76,17 +76,17 @@
     
     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;
-   FCTN_CDMS_WR_FLASH(5,1);
+    WRITE_TO_EEPROM(5,1);
     SPI_mutex.unlock();
 }
  
 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();
diff -r a5e72222ff26 -r 4484bf39526f cdms_sd.h
--- a/cdms_sd.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/cdms_sd.h	Sat Feb 24 10:28:56 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);      
+        WRITE_TO_EEPROM(16,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
diff -r a5e72222ff26 -r 4484bf39526f common_functions.h
--- a/common_functions.h	Tue Mar 28 08:50:07 2017 +0000
+++ b/common_functions.h	Sat Feb 24 10:28:56 2018 +0000
@@ -185,7 +185,7 @@
     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*/\
+    /*pending comment this later*/\
      COM_PA_IC_TMP = 0;\
     /*COM_PA_TMP_HIGH to be found*/\
     if (COM_PA_IC_TMP > COM_PA_TMP_HIGH){\
diff -r a5e72222ff26 -r 4484bf39526f main.cpp
--- a/main.cpp	Tue Mar 28 08:50:07 2017 +0000
+++ b/main.cpp	Sat Feb 24 10:28:56 2018 +0000
@@ -1,7 +1,7 @@
 // TESTING PUSH PULL IN MAIN CPP
 
 #include "mbed.h"
-//#include "eeprom.h"
+#include "eeprom.h"
 
 #define DEBUG 1
 
@@ -15,8 +15,7 @@
 #include "DefinitionsAndGlobals.h"
 #include "crc.h"
 #include "i2c.h"
-//#include "EEPROM.h"
-#include "Flash.h"
+#include "EEPROM.h"
 #include "COM_SND_TM_functions.h"
 #include "cdms_rtc.h"
 #include "COM_SND_TM.h"
@@ -47,7 +46,7 @@
     RF_SW_CNTRL_BCN = 0;
     PYLD_DFF = 1;
     
-    for(int i= 0; i< 10 ; i++)
+    for(int i= 0; i< 10 ; i++)                      //probably remove as PMS must turn on PYLD for FM
     {PYLD_DFF_CLK = !PYLD_DFF_CLK;
     wait_us(10);} 
     
@@ -61,16 +60,21 @@
     gRX_COUNT = 0;
     RX1M.attach(&rx_read, Serial::RxIrq);
     
-   gPC.baud(1200);//changed for bypassing COM
+    gPC.baud(1200);//changed for bypassing COM
    
     
     //Base_tm *tm_ptr = NULL;
     //SET_BCN_STANDBY(tm_ptr);
-    //gPC.puts("set Bcn off\r\n");
+    gPC.puts("set Bcn off\r\n");
     
     // COMMON SPI
     spi.format(8,0);
     spi.frequency(500000);
+   
+
+    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,23 +82,41 @@
     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) == 0)
+    {
+      EN_RTC = 0;
+    }
+    if((RTC_INIT_STATUS & 0x01) != 1)                  //Joel
+     {
+        FCTN_CDMS_INIT_RTC();/* rtc initialization*/
+     }
     FCTN_CDMS_SD_INIT();/* sd card initialization*/
-   // INIT_EEPROM(); // added by samp
+    INIT_EEPROM();
+    if(sd_stat_temp ==  DEVICE_DISABLED)
+    {
+        SD_STATUS = sd_stat_temp;
+        SD_SW_EN_DS = 0;
+    }
+    else if(sd_stat_temp == DEVICE_OC_FAULT)
+    {
+        SD_STATUS = sd_stat_temp;
+    }
 
-    CDMS_RESET_COUNTER++;
-    FCTN_CDMS_WR_FLASH(17,CDMS_RESET_COUNTER);
+    CDMS_RESET_COUNTER+=1;
+    WRITE_TO_EEPROM(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);
+    WRITE_TO_EEPROM(8,TIME_LATEST_CDSMS_RESET);
     
    // gPC.printf("%d \n", READ_FROM_EERPOM(8));
     uint8_t test[512];
@@ -102,8 +124,9 @@
     for(int i =0; i<512; i++)
         test[i] = 0;
     disk_write(test,80000);
-   
-    //SD_MNG_SECT = SD_LIB_BLK_CURRENT;
+
+    SD_MNG_SECT = SD_LIB_BLK_CURRENT;
+
     FCTN_SD_MNGR();
 
     #if DEBUG