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:
Sun Apr 03 15:35:53 2022 +0000
Parent:
356:197c93dc2012
Commit message:
First commit test

Changed in this revision

.hgignore Show annotated file Show diff for this revision Revisions of this file
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_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
RESET_functions.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
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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Sun Apr 03 15:35:53 2022 +0000
@@ -0,0 +1,10 @@
+^BUILD$
+^compile_commands.json$
+^.mbed$
+^.clangd$
+^.cache$
+^mbed-rtos$
+^eeprom$
+^SimpleDMA$
+^FreescaleIAP$
+^mbed$
--- a/CDMS_HK.h	Sat Feb 10 09:06:26 2018 +0000
+++ b/CDMS_HK.h	Sun Apr 03 15:35:53 2022 +0000
@@ -12,6 +12,7 @@
 void COLLECT_CDMS_RAM();
 void IS_BAE_IN_DISTRESS();
 uint8_t SAVE_BAE(uint8_t);
+void FCTN_CDMS_P_MGMT(uint8_t);
 
 extern uint8_t beacon_array[134];
 //extern RSSI_MAX;
@@ -45,7 +46,8 @@
 uint8_t BAE_HEALTH_FINAL[512] = {0};
 unsigned char BAE_HK_FRAME[134] = {0};
 
-
+//Software Timer
+void int_acq1() { expired=1; }
 
 void FCTN_CDMS_HK_MAIN(void const *args)
 {
@@ -73,7 +75,10 @@
           
         //gPC.printf("\n\r%d\n",CDMS_WR_SD_FAULT_COUNTER);
         if(EN_CDMS_HK == 0x00)
+        {
+            gMutex.unlock();
             continue;
+        }
         CDMS_HK_MAIN_STATUS = 0x01;
         CDMS_HK_MAIN_COUNTER++;
         CDMS_I2C_ERR_BAE_CURRENT = CDMS_I2C_ERR_BAE_COUNTER;
@@ -82,6 +87,7 @@
         if(!COM_RSSI_SET)
         {
             RSSI_VOLTAGE = COMRX_RSSI_VOLTAGE.read() * 3.3;//to be checked
+            gPC.printf("RSSI= %f",RSSI_VOLTAGE);
             gPC.printf("RSSI_MAX = %f",RSSI_MAX);
         }
         else {
@@ -105,7 +111,7 @@
             CDMS_quant[i+4]= (uint8_t)quant_data.temp_quant[i];
         }
         minMaxHkData();
-
+         gPC.printf("\n\r %d",CDMS_quant[0]);
         CDMS_HEALTH_DATA[1] = GPIO_STATUS;            //Reading GPIO Pins
         CDMS_HEALTH_DATA[0] = GPIO_STATUS >> 8;
         COLLECT_CDMS_RAM();
@@ -246,20 +252,23 @@
                 
         }
         // gPC.printf("Completed BAE HK\n");
-
+        POWER_MODE = BAE_HK[40];                                 // Extract Power Mode from BAE_HK
+        gPC.printf("POWER MODE is %d\n", POWER_MODE);
+        /*----------------------------------Power Management------------------------------------*/
+        FCTN_CDMS_P_MGMT(POWER_MODE);
         /*----------------------------------Beacon message--------------------------------------*/
 
 
         // Add HK bits
         beacon_array[0] = 0x00;
-        beacon_array[1] = time >> 32;
+        beacon_array[1] = TC_count;
         beacon_array[2] = time >> 24;
         beacon_array[3] = time >> 16;
         beacon_array[4] = time >> 8;
         beacon_array[5] = time;
         beacon_array[6] = SD_FAULTCOUNT >> 8;
         beacon_array[7] = SD_FAULTCOUNT;
-        beacon_array[8] = RTC_FAULTCOUNT >> 8;
+        beacon_array[8] = CDMS_RESET_COUNTER;
         beacon_array[9] = RTC_FAULTCOUNT;
         if(!COM_OC_SET)
         beacon_array[10] = (((SD_STATUS == DEVICE_DISABLED || SD_STATUS == DEVICE_OC_FAULT)?1:0)<<7)|(RTC_STATUS <<6)|(COM_RX_STATUS<<3)|(0<<2)|(COMRX_OC_FAULT<<1)|(COM_TX_OC_FAULT);
@@ -268,7 +277,7 @@
             beacon_array[10] = (((SD_STATUS == DEVICE_DISABLED || SD_STATUS == DEVICE_OC_FAULT)?1:0)<<7)|(RTC_STATUS <<6)|(COM_RX_STATUS<<3)|(0<<2)|(COMRX_OC_FAULT<<1)|(com_oc_sw_status);
             COM_OC_SET = 0;
         }
-        beacon_array[11] = (COM_RX_CNTRL <<7)|(COM_TX_CNTRL<<6);
+        beacon_array[11] = (COM_RX_CNTRL <<7)|(COM_TX_CNTRL<<6)|(COM_PA_IC_TMP >> 2);
         beacon_array[12] = CDMS_HK_MAIN_COUNTER >>8;
         beacon_array[13] = CDMS_HK_MAIN_COUNTER;
         beacon_array[14] = PL_MAIN_COUNTER >>8;
@@ -289,14 +298,14 @@
         y = FCTN_I2C_WRITE((char *)beacon_array,135); //134
         if(y == 0) {
             TIME_LATEST_I2C_BAE = FCTN_CDMS_RD_RTC() >> 7;
-            //gPC.printf("long Bcn sent\n\r");
+            gPC.printf("long Bcn sent\n\r");
         } 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");
+        gPC.printf("\n\rreleasing mutex for HK_MAIN \n\r");
       SCI_LED1 =0;
        gMutex.unlock();
     }
@@ -440,10 +449,23 @@
 
     //COMRX_OC_FAULT //$
     GPIO_STATUS=(COMRX_OC_FAULT)?(GPIO_STATUS)|((uint16_t)(0x1<<11)):(GPIO_STATUS)&(~((uint16_t)(0x1<<11)));
+    
     // COMTX_OC_FAULT //$
     GPIO_STATUS=(COM_TX_OC_FAULT)?(GPIO_STATUS)|((uint16_t)(0x1<<10)):(GPIO_STATUS)&(~((uint16_t)(0x1<<10)));
     // CDMS_SD_OC_FAULT
     GPIO_STATUS=(SD_OC_FAULT)?(GPIO_STATUS)|((uint16_t)(0x1<<9)):(GPIO_STATUS)&(~((uint16_t)(0x1<<9)));
+    if(SD_OC_FAULT == 0) {
+        gPC.printf("SD CARD DOWN");
+    }
+    if(COMRX_OC_FAULT == 0) {
+        gPC.printf("COM RX DOWN");
+    }
+    if(COM_TX_OC_FAULT == 0) {
+        gPC.printf("COM TX DOWN");
+    }   
+    if(BAE_OC_FAULT == 0) {
+        gPC.printf("BAE DOWN");
+    }   
     //BAE_OC_FAULT //$
     GPIO_STATUS=(BAE_OC_FAULT)?(GPIO_STATUS)|((uint16_t)(0x1<<8)):(GPIO_STATUS)&(~((uint16_t)(0x1<<8)));
 
@@ -531,13 +553,16 @@
             SD_FAULTCOUNT++;
             SD_STATUS = (SD_FAULTCOUNT == 3) ? DEVICE_DISABLED :DEVICE_OC_FAULT;
             if(SD_FAULTCOUNT == 3) {
-                FCTN_CDMS_WR_FLASH(2,DEVICE_DISABLED);
+                //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);
+            //if(SD_STATUS != DEVICE_POWERED)
+                //FCTN_CDMS_WR_FLASH(2,DEVICE_POWERED);
+            WRITE_TO_EEPROM(2,DEVICE_POWERED);
+            wait_us(1);
             SD_FAULTCOUNT = 0;
         }
     }
@@ -558,13 +583,15 @@
             BAE_FAULTCOUNT++;
             BAE_STATUS = (BAE_FAULTCOUNT == 3)?DEVICE_DISABLED:DEVICE_OC_FAULT;
             if(BAE_FAULTCOUNT == 3) {
-                FCTN_CDMS_WR_FLASH(1,DEVICE_DISABLED);
+                //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);
+            //FCTN_CDMS_WR_FLASH(1,DEVICE_POWERED);
+            WRITE_TO_EEPROM(1,DEVICE_POWERED);
+            wait_us(1);
             BAE_FAULTCOUNT = 0;
         }
     }
@@ -595,13 +622,14 @@
             PL_FAULTCOUNT++;
             PL_BEE_SW_STATUS = (PL_FAULTCOUNT == 3)?DEVICE_DISABLED:DEVICE_OC_FAULT;
             if(PL_FAULTCOUNT == 3) {
-               FCTN_CDMS_WR_FLASH(4,DEVICE_DISABLED);
+               //FCTN_CDMS_WR_FLASH(4,DEVICE_DISABLED);
+               WRITE_TO_EEPROM(4,DEVICE_DISABLED);
                 gPC.printf("Declaring PL_BEE permanantly Disabled");
             }
         } else {
             PL_BEE_SW_STATUS = DEVICE_POWERED;
-            if(SD_STATUS != DEVICE_POWERED)
-            FCTN_CDMS_WR_FLASH(4,DEVICE_POWERED);
+            //FCTN_CDMS_WR_FLASH(4,DEVICE_POWERED);
+            WRITE_TO_EEPROM(4,DEVICE_POWERED);
             PL_FAULTCOUNT = 0;
         }
     }
@@ -686,18 +714,18 @@
             {
                 CDMS_I2C_ERR_HK_LATEST = CDMS_HK_MAIN_COUNTER;
                 KILL_BAE_COUNTER++;
-                gPC.printf("\n Takin aim, Boss");
+                gPC.printf("\r Takin aim, Boss\n\r");
             }
             else
             {
                 CDMS_I2C_ERR_HK_LATEST = CDMS_HK_MAIN_COUNTER;
                 KILL_BAE_COUNTER = 1;
-                gPC.printf("\n Target lost");
+                gPC.printf("\r Target lost\n\r");
             } 
             if (KILL_BAE_COUNTER >= 3)
             {
                 GIVE_UP_ON_BAE = SAVE_BAE(KILL_BAE_COUNTER);
-                gPC.printf("\n Kill Confirmed");
+                gPC.printf("\r Kill Confirmed\n\r");
             }
         }
 }
@@ -709,8 +737,121 @@
     BAE_SW_EN_DS = 1;
     
     BAE_STATUS = DEVICE_POWERED;
-    FCTN_CDMS_WR_FLASH(1,DEVICE_POWERED);
+    //FCTN_CDMS_WR_FLASH(1,DEVICE_POWERED);
+    WRITE_TO_EEPROM(1,DEVICE_POWERED);
     if (counter == 6)
     return 1;
     return 0;
-}
\ No newline at end of file
+}
+
+void FCTN_CDMS_P_MGMT(uint8_t power_mode)
+{
+    if(EN_P_MGMT == 0x00)   //ENABLE POWER MANAGEMENT
+    return;
+    switch(power_mode)
+    {
+        case 3:
+        {
+            if(EN_PL == 0)                                   //Enabling PMS thread if disabled
+                EN_PL = 1;
+            break;
+        }
+        
+        case 2:
+        {
+            EN_PL = 0;
+            if(PL_STATE == 0x01)
+            {
+                gPC.printf("\r Already in  Standby\n");
+                break;
+            }    
+            gPC.printf("\n\rCommanding PL_BEE to go to Standby State");  
+            if(PL_BEE_SW_STATUS == 0x03) 
+            {
+                gPC.printf("\n\r Switched Off chilling out");
+                break;
+            }
+            else
+            {
+                pl_next_index = -1;
+                Base_tm *tm_ptr_standby;
+                SET_PL_BEE_STANDBY(tm_ptr_standby);
+                if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
+                {
+                    pl_main_flag = pl_main_flag & (~PL_DISABLED);
+                    pl_main_flag |= PL_STANDBY; 
+                    PL_STATUS = 0x01;
+                    PL_STATE = 0x01;
+                    WRITE_TO_EEPROM(16,(uint32_t)PL_STATE);
+                }        
+                break;   
+            }
+        }
+        
+        case 1:
+        {
+            EN_PL = 0;
+            if(PL_BEE_SW_STATUS != 0x03) 
+            {
+                gPC.printf("\n\rCommanding PL_BEE to go to Standby State");
+                pl_next_index = -1;
+                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);
+            pl_main_flag |= PL_OFF;
+            PL_STATUS = 0x00;
+            PL_STATE = 0x00;
+            WRITE_TO_EEPROM(16,(uint32_t)PL_STATE);
+            break;
+        }
+        
+        default:
+        {
+            gPC.printf("Non-existant Power Mode (%d) BAE's lost it\n\r", power_mode);
+        }    
+    }
+}
+/*
+void SYS_RESET(uint_8 ack_pin){
+    if(ack_pin == 1)
+    {
+                    //ACKNOWLEDGE SYS RST
+                    ack_pin = 0;
+                    
+                    //DISABLE THE THREADS
+                    EN_PL = 0;
+                    EN_CDMS_HK = 0;
+                    
+                    //SWITCH OFF BAE
+                    SW_OFF_BAE();
+                    
+                    //PAYLOAD IN STANDBY MODE
+                    Base_tm *tm_ptr_standby;
+                    SET_PL_BEE_STANDBY(tm_ptr_standby);
+                    if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
+                    {
+                        pl_main_flag = pl_main_flag & (~PL_DISABLED);
+                        pl_main_flag |= PL_STANDBY; 
+                        PL_STATUS = 0x01;
+                    }        
+                    pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);   
+                    
+                    //SWITCH OFF SD CARD
+                    CDMS_SD_SW_OFF();   
+                    
+                    //TURN ON
+                    WRITE_TO_EEPROM(2,DEVICE_POWERED);
+                    WRITE_TO_EEPROM(1,DEVICE_POWERED);      
+                    
+    }
+                    
+                    
+
+}
+*/
\ No newline at end of file
--- a/CDMS_PL.h	Sat Feb 10 09:06:26 2018 +0000
+++ b/CDMS_PL.h	Sun Apr 03 15:35:53 2022 +0000
@@ -1,7 +1,6 @@
 int pl_next_index=-1;   
 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};
 uint32_t pl_time;
 uint32_t TIME_LATEST_PL=0;
@@ -200,8 +199,13 @@
                 pl_main_flag  = pl_main_flag & (~PL_DISABLED);
                 pl_main_flag |= PL_SUCCESS_I2C;
                 PL_STATUS = 0x04;
-                pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
-                pl_block[pl_next_index-1] |= EXECUTED;
+                if(pl_next_index >= 1)
+                {
+                    pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
+                    pl_block[pl_next_index-1] |= EXECUTED;
+                }
+                if(pl_next_index >=2)
+                    UPDATE_PMS_ENTRY((pl_next_index-2), pl_block[pl_next_index-2]);     //1to2 , updating the previous status to EEPROM
             }
             else
             {
@@ -210,8 +214,13 @@
                 pl_main_flag  = pl_main_flag & (~PL_DISABLED);
                 pl_main_flag |= PL_ERR_I2C;
                 PL_STATUS = 0x05;
-                pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
-                pl_block[pl_next_index-1] |= RETRY;   
+                if(pl_next_index >= 1)
+                {
+                    pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
+                    pl_block[pl_next_index-1] |= RETRY;   
+                }
+                if(pl_next_index >=2)
+                    UPDATE_PMS_ENTRY((pl_next_index-2), pl_block[pl_next_index-2]);     //1to2 , updating the previous status to EEPROM
             }
         }
         else
@@ -220,8 +229,13 @@
             pl_main_flag  = pl_main_flag & (~PL_DISABLED);
             pl_main_flag |= PL_ERR_I2C;
             PL_STATUS = 0x05;
-            pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
-            pl_block[pl_next_index-1] |= RETRY;
+            if(pl_next_index >= 1)
+            {
+                pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
+                pl_block[pl_next_index-1] |= RETRY;
+            }
+            if(pl_next_index >=2)
+                UPDATE_PMS_ENTRY((pl_next_index-2), pl_block[pl_next_index-2]);     //1to2 , updating the previous status to EEPROM
         }
     }
     else if(GETshort_or_long_tm(tm_ptr)==0)                  //LONG TM
@@ -245,8 +259,13 @@
                 pl_main_flag  = pl_main_flag & (~PL_DISABLED);
                 pl_main_flag |= PL_SUCCESS_I2C;
                 PL_STATUS = 0x04;
-                pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
-                pl_block[pl_next_index-1] |= EXECUTED;
+                if(pl_next_index >= 1)
+                {
+                    pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
+                    pl_block[pl_next_index-1] |= EXECUTED;
+                }
+                if(pl_next_index >=2)
+                    UPDATE_PMS_ENTRY((pl_next_index-2), pl_block[pl_next_index-2]);     //1to2 , updating the previous status to EEPROM
             }
             else
             {
@@ -254,8 +273,13 @@
                 pl_main_flag  = pl_main_flag & (~PL_DISABLED);
                 pl_main_flag |= PL_ERR_I2C;
                 PL_STATUS = 0x05;
-                pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
-                pl_block[pl_next_index-1] |= RETRY;   
+                if(pl_next_index >= 1)
+                {
+                    pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
+                    pl_block[pl_next_index-1] |= RETRY;  
+                }
+                if(pl_next_index >=2)
+                    UPDATE_PMS_ENTRY((pl_next_index-2), pl_block[pl_next_index-2]);     //1to2 , updating the previous status to EEPROM
             }   
         }
         else
@@ -263,9 +287,14 @@
             gPC.printf("\n\rLong_TM CRC failed");
             pl_main_flag  = pl_main_flag & (~PL_DISABLED);
             pl_main_flag |= PL_ERR_I2C;
-            PL_STATUS = 0x05;     
-            pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
-            pl_block[pl_next_index-1] |= RETRY;   
+            PL_STATUS = 0x05;
+            if(pl_next_index >= 1)     
+            {
+                pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
+                pl_block[pl_next_index-1] |= RETRY;   
+            }
+            if(pl_next_index >=2)
+                UPDATE_PMS_ENTRY((pl_next_index-2), pl_block[pl_next_index-2]);     //1to2 , updating the previous status to EEPROM
         }
     }    
 }
@@ -303,6 +332,83 @@
         schedule1[0] = {0,0,0,0,0,0,0,0};
     }
 }*/
+void SYS_RESET(){
+                    
+                    gPC.printf("\n\r Going into system reset\n");
+                    //DISABLE THE THREADS
+                    EN_PL = 0;
+                    EN_CDMS_HK = 0;
+                    EN_RCV_SC = 0;
+                    
+                    //SWITCH OFF BAE
+                    BAE_SW_EN_DS = 0;
+                    BAE_STATUS = DEVICE_DISABLED;
+                    WRITE_TO_EEPROM(1,DEVICE_DISABLED);
+                    gPC.printf("sw off bae\n");
+                    
+                    //PAYLOAD IN STANDBY MODE
+                    Base_tm *tm_ptr_standby;
+                    SET_PL_BEE_STANDBY(tm_ptr_standby);
+                    if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
+                    {
+                        pl_main_flag = pl_main_flag & (~PL_DISABLED);
+                        pl_main_flag |= PL_STANDBY; 
+                        PL_STATUS = 0x01;
+                        PL_STATE = 0x01;
+                    }        
+                    pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);   
+                    
+                    //SWITCH OFF SD CARD
+                    SD_SW_EN_DS = 0;
+                    SD_STATUS = DEVICE_DISABLED;
+                    WRITE_TO_EEPROM(2,DEVICE_DISABLED);
+                    gPC.printf("sw off sd\n");   
+                    
+                    //TURN ON
+                    WRITE_TO_EEPROM(2,DEVICE_POWERED);
+                    WRITE_TO_EEPROM(1,DEVICE_POWERED);    
+                    
+                    //UPDATE SYS_RESET TIME  
+                    TIME_LATEST_CDMS_EPS_RST_BACKUP = TIME_LATEST_CDMS_EPS_RST;
+                    WRITE_TO_EEPROM(21,(uint32_t)TIME_LATEST_CDMS_EPS_RST_BACKUP);
+                    WRITE_TO_EEPROM(20,TIME_LATEST_CDMS_EPS_RST_BACKUP>>32); 
+                    
+                    TIME_LATEST_CDMS_EPS_RST = FCTN_CDMS_RD_RTC();
+                    //Latest EPS_RST time stored in EEPROM
+                    WRITE_TO_EEPROM(19,(uint32_t)TIME_LATEST_CDMS_EPS_RST);
+                    WRITE_TO_EEPROM(18,TIME_LATEST_CDMS_EPS_RST>>32);                     
+                    
+
+}
+
+void BACK_NORMAL()
+{
+    //ENABLE THE THREADS
+    EN_PL = 1;
+    EN_CDMS_HK = 1;
+    EN_RCV_SC = 1;
+                    
+    //SWITCH ON BAE
+    BAE_SW_EN_DS = 1;
+    BAE_STATUS = DEVICE_POWERED;
+    WRITE_TO_EEPROM(1,DEVICE_POWERED);
+    gPC.printf("sw on bae\n");
+                    
+    //SWITCH ON SD CARD
+    SD_SW_EN_DS = 1;
+    SD_STATUS = DEVICE_POWERED;
+    WRITE_TO_EEPROM(2,DEVICE_POWERED);
+    gPC.printf("sw on sd\n");  
+                    
+    //UPDATE SYS_RESET TIME  
+    TIME_LATEST_CDMS_EPS_RST = TIME_LATEST_CDMS_EPS_RST_BACKUP ;
+    //Latest EPS_RST time stored in EEPROM
+    WRITE_TO_EEPROM(19,(uint32_t)TIME_LATEST_CDMS_EPS_RST);
+    WRITE_TO_EEPROM(18,TIME_LATEST_CDMS_EPS_RST>>32); 
+    
+}
+
+
 void FCTN_CDMS_PL_MAIN(void const *args)
 {
     if(EN_PL == 0x00)
@@ -337,6 +443,7 @@
     uint64_t temp_time;
     temp_time=FCTN_CDMS_RD_RTC();
     pl_time = (uint32_t)((temp_time>>7) & 0x000000000FFFFFFF);
+    gPC.printf("\n\r %u", pl_time);
 
     //call test3() here
     //test3(1);
@@ -349,6 +456,7 @@
     gPC.printf("\n\rHours :%d",((pl_time & 0x0001F000)>>12));
     gPC.printf("\tMin :%d",((pl_time & 0x00000FC0)>>6));
     gPC.printf("\t\tSec :%d",(pl_time & 0x0000003F));*/
+    pl_next_index = -1;
     if(pl_time!=0)                             //RTC read successful 
     {
         // run the loop until end of schedule is reached or month and day are both zeros or future block is found
@@ -371,7 +479,19 @@
         {
             pl_main_flag  = pl_main_flag & (~STATE_SCIENCE);
             pl_main_flag |= STATE_HIBERNATE;
-            PL_STATE = 0x02;
+            gPC.printf("\n\rCommanding PL_BEE to go to Hibernate State");  
+            Base_tm *tm_ptr_hibernate;
+            SET_PL_BEE_HIBERNATE(tm_ptr_hibernate);
+            if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
+            {
+                pl_main_flag = pl_main_flag & (~PL_DISABLED);
+                pl_main_flag |= PL_HIBERNATE;
+                PL_STATUS = 0x02;
+                PL_STATE = 0x02;
+                WRITE_TO_EEPROM(16,(uint32_t)PL_STATE);
+            }
+            pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);               //Clearing PL_MAIN_STATUS
+            PL_MAIN_status = 0;
         } 
         else
         {                        
@@ -405,25 +525,21 @@
             {            
                 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;
+                pl_main_flag  = pl_main_flag & (~STATE_SCIENCE);
+                pl_main_flag |= STATE_SCIENCE;
             }
         }
         else if((pl_block[pl_next_index-1] & UNEXECUTED)==1)
@@ -455,14 +571,18 @@
                 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;    
+                WRITE_TO_EEPROM(4,DEVICE_DISABLED);
+                PL_STATUS = 0;    
             }           
             pl_main_flag  = pl_main_flag & (~PL_DISABLED);
             pl_main_flag |= PL_OFF;
             PL_STATUS = 0x00;
+            PL_STATE = 0x00;
+            WRITE_TO_EEPROM(16,(uint32_t)PL_STATE);
             pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
-            pl_block[pl_next_index-1] |= EXECUTED;                              
+            pl_block[pl_next_index-1] |= EXECUTED;
+            if(pl_next_index >=2)
+                UPDATE_PMS_ENTRY((pl_next_index-2), pl_block[pl_next_index-2]);     //1to2 , updating the previous status to EEPROM                
             pl_main_flag = pl_main_flag & (~PL_MAIN_STATUS);                    //Clearing PL_MAIN_STATUS
             PL_MAIN_status = 0;
             print_processed_block(pl_next_index-1);
@@ -477,7 +597,8 @@
                 gPC.printf("\n\rPowering on PL_BEE");
                 SET_PL_BEE_ON;
                 PL_BEE_SW_STATUS = 0x01;
-                FCTN_CDMS_WR_FLASH(4,DEVICE_POWERED);
+                WRITE_TO_EEPROM(4,DEVICE_POWERED);
+                // PL_STATE = 0x01                                          //uncomment - Joel after finding out default startup state of PYLD
             }
             gPC.printf("\n\rCommanding PL_BEE to go to Standby State");  
             Base_tm *tm_ptr_standby;
@@ -487,6 +608,8 @@
                 pl_main_flag = pl_main_flag & (~PL_DISABLED);
                 pl_main_flag |= PL_STANDBY; 
                 PL_STATUS = 0x01;
+                PL_STATE = 0x01;
+                WRITE_TO_EEPROM(16,(uint32_t)PL_STATE);
             }        
             pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);               //Clearing PL_MAIN_STATUS 
             PL_MAIN_status = 0;
@@ -497,146 +620,60 @@
         }
         case STATE_HIBERNATE: 
         {
-            gPC.printf("\n\rEntered PL_HIBERNATE case");
-            if(POWER_LEVEL==2 || POWER_LEVEL==3 || POWER_LEVEL==0)
-            {                
-                if(PL_BEE_SW_STATUS == 0x03) 
-                {
-                    gPC.printf("Powering on PL_BEE\r\n");                    
-                    SET_PL_BEE_ON;
-                    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;
-                SET_PL_BEE_HIBERNATE(tm_ptr_hibernate);
-                if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
-                {
-                    pl_main_flag = pl_main_flag & (~PL_DISABLED);
-                    pl_main_flag |= PL_HIBERNATE;
-                    PL_STATUS = 0x02;
-                }
-            }
-            else 
+            gPC.printf("\n\rEntered PL_HIBERNATE case");                
+            if(PL_BEE_SW_STATUS == 0x03) 
             {
-                pl_main_flag |= PL_LOW_POWER;
-                PL_LOW_power = 1;
-                if(PL_BEE_SW_STATUS == 0x03) 
-                {
-                    gPC.printf("\n\rPowering on PL_BEE");
-                    SET_PL_BEE_ON;
-                    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;
-                SET_PL_BEE_STANDBY(tm_ptr_standby);
-                if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
-                {
-                    pl_main_flag = pl_main_flag & (~PL_DISABLED);
-                    pl_main_flag |= PL_STANDBY; 
-                    PL_STATUS = 0x01;
-                }        
-                pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);               //Clearing PL_MAIN_STATUS 
-                PL_MAIN_status = 0;
-                print_processed_block(pl_next_index-1);
-                print_exit(temp);
-                return;
-                                                                                //////DELETE THE TM AND TC LATER
-            }            
+                gPC.printf("Powering on PL_BEE\r\n");                    
+                SET_PL_BEE_ON;
+                PL_BEE_SW_STATUS = 0x01;
+                WRITE_TO_EEPROM(4,DEVICE_POWERED);
+                // PL_STATE = 0x01                                          //uncomment - Joel after finding out default startup state of PYLD
+            }
+            gPC.printf("\n\rCommanding PL_BEE to go to Hibernate State");  
+            Base_tm *tm_ptr_hibernate;
+            SET_PL_BEE_HIBERNATE(tm_ptr_hibernate);
+            if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
+            {
+                pl_main_flag = pl_main_flag & (~PL_DISABLED);
+                pl_main_flag |= PL_HIBERNATE;
+                PL_STATUS = 0x02;
+                PL_STATE = 0x02;
+                WRITE_TO_EEPROM(16,(uint32_t)PL_STATE);
+            }
             pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);               //Clearing PL_MAIN_STATUS
             PL_MAIN_status = 0;
             print_processed_block(pl_next_index-1);
             print_exit(temp);
             return;
-                                                                            //////DELETE THE TM LATER
         }
         case STATE_SCIENCE:
         {
             gPC.printf("\n\rEntered PL_SCIENCE case");
-            if(POWER_LEVEL==3 || POWER_LEVEL==0)                            //POWER_LEVEL = 0 = NA
+            if(PL_BEE_SW_STATUS == 0x03)
             {
-                gPC.printf("\n\rPOWER_LEVEL = 3 or NA");
-                if(PL_BEE_SW_STATUS == 0x03)
-                {
-                    gPC.printf("\n\rPowering on PL_BEE");
-                    SET_PL_BEE_ON;
-                    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;
-                SET_PL_BEE_SCIENCE(tm_ptr_science);
-                if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
-                {
-                    pl_main_flag = pl_main_flag & (~PL_DISABLED);
-                    pl_main_flag |= PL_SCIENCE; 
-                    PL_STATUS = 0x03;
-                }
-                pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);               //Clearing PL_MAIN_STATUS
-                PL_MAIN_status = 0;
-                print_processed_block(pl_next_index-1);
-                print_exit(temp);
-                return;
+                gPC.printf("\n\rPowering on PL_BEE");
+                SET_PL_BEE_ON;
+                PL_BEE_SW_STATUS = 1;
+                //FCTN_CDMS_WR_FLASH(4,DEVICE_POWERED);
+                WRITE_TO_EEPROM(4,DEVICE_POWERED);
+                // PL_STATE = 0x01                                          //uncomment - Joel after finding out default startup state of PYLD
             }
-            else
-            {   
-                gPC.printf("\n\rPower level = 2,3 or NA");
-                pl_main_flag |= PL_LOW_POWER;
-                PL_LOW_power = 1;
-                if(POWER_LEVEL==2 || POWER_LEVEL==3 || POWER_LEVEL==0)
-                {                
-                    if(PL_BEE_SW_STATUS == 0x03) 
-                    {
-                        gPC.printf("\n\rPowering on PL_BEE");
-                        SET_PL_BEE_ON;
-                        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;
-                    SET_PL_BEE_HIBERNATE(tm_ptr_hibernate);
-                    if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
-                    {
-                        pl_main_flag = pl_main_flag & (~PL_DISABLED);
-                        pl_main_flag |= PL_HIBERNATE; 
-                        PL_STATUS = 0x02;
-                    }
-                }
-                else 
-                {
-                    pl_main_flag |= PL_LOW_POWER;
-                    PL_LOW_power = 1;
-                    if(PL_BEE_SW_STATUS == 0x03) 
-                    {
-                        gPC.printf("\n\rPowering on PL_BEE");
-                        SET_PL_BEE_ON;
-                        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;
-                    SET_PL_BEE_STANDBY(tm_ptr_standby);
-                    if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
-                    {
-                        pl_main_flag = pl_main_flag & (~PL_DISABLED);
-                        pl_main_flag |= PL_STANDBY; 
-                        PL_STATUS = 0x01;
-                    }        
-                    pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);               //Clearing PL_MAIN_STATUS 
-                    PL_MAIN_status = 0;
-                    print_processed_block(pl_next_index-1);
-                    print_exit(temp);
-                    return;                                               
-                                                                                    //////DELETE THE TM AND TC LATER
-                }            
-                pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);               //Clearing PL_MAIN_STATUS
-                PL_MAIN_status = 0;
-                print_processed_block(pl_next_index-1);
-                print_exit(temp);
-                return;
-                                                                                //////DELETE THE TM LATER
+            gPC.printf("\n\rCommanding PL_BEE to go to Science State");  
+            Base_tm *tm_ptr_science;
+            SET_PL_BEE_SCIENCE(tm_ptr_science);
+            if((pl_main_flag & PL_DISABLED)==PL_SUCCESS_I2C)
+            {
+                pl_main_flag = pl_main_flag & (~PL_DISABLED);
+                pl_main_flag |= PL_SCIENCE; 
+                PL_STATUS = 0x03;
+                PL_STATE = 0x03;
+                WRITE_TO_EEPROM(16,(uint32_t)PL_STATE);
             }
+            pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);               //Clearing PL_MAIN_STATUS
+            PL_MAIN_status = 0;
+            print_processed_block(pl_next_index-1);
+            print_exit(temp);
+            return;
         }
         default:
         {
@@ -646,6 +683,8 @@
             PL_STATUS = 0x06;
             pl_block[pl_next_index-1] &= (~UNEXECUTED);                         //changing exec_status                    
             pl_block[pl_next_index-1] |= EXECUTED;
+            if(pl_next_index >=2)
+                UPDATE_PMS_ENTRY((pl_next_index-2), pl_block[pl_next_index-2]);     //1to2 , updating the previous status to EEPROM
             pl_main_flag  = pl_main_flag & (~PL_MAIN_STATUS);               //Clearing PL_MAIN_STATUS
             PL_MAIN_status = 0;
             print_processed_block(pl_next_index-1);
@@ -653,5 +692,35 @@
             return;
         }
     }
-    }    
+    }  
+    RESET_WATCHDOG(); 
+    //CHECK FOR SYS POWER RESET
+    sys_pwr_rst.input();
+    int sys_val = sys_pwr_rst.read();
+    gPC.printf("\rsys_reset_val = %d\n",sys_val);
+    if(sys_val == 1)
+    {
+        //ACKNOWLEDGE SYS RST
+        sys_pwr_rst.output();
+        sys_pwr_rst = 0;
+        gPC.printf("\rsys_reset_val = %d\n",sys_pwr_rst.read());
+            
+        SYS_RESET();
+        while(1){
+            wait_ms(10000);
+            //RESET_WATCHDOG();                 //uncomment in the FM SBC with watchdog enabled
+            uint64_t cur_rtc = FCTN_CDMS_RD_RTC();
+            uint32_t T_check1 = ((((cur_rtc - TIME_LATEST_CDMS_EPS_RST) >> 7)& 0x00000FC0)>>6);
+            //gPC.printf("\r%d\n",T_check1 );
+            //gPC.printf("\r%d\n",TIME_LATEST_CDMS_EPS_RST);
+            //gPC.printf("\r%d\n",cur_rtc);
+            if(T_check1 >=  5*T_OFF_RST)
+            {
+                break;
+            }            
+        }
+        BACK_NORMAL();                    
+    }
+    gPC.printf("\rTIME_LATEST_CDMS_EPS_RST = %d\n",TIME_LATEST_CDMS_EPS_RST);
+        
 }
\ No newline at end of file
--- a/COM_MNG_TMTC.h	Sat Feb 10 09:06:26 2018 +0000
+++ b/COM_MNG_TMTC.h	Sun Apr 03 15:35:53 2022 +0000
@@ -104,7 +104,26 @@
     }\
 }
 
+void mcpy(uint32_t *a2,uint8_t *a1,int n){
+    
+    *a2=0;
+    for (int i=0;i<n;i++){
+        *(a2) += *(a1+i);
+        if(i<n-1){
+        *(a2)=(*(a2))<<8;
+        //printf("%d\n",*(a2));
+        }
+    }
+    
+}
 
+/*void rmcpy(uint8_t *a1,uint32_t *a2,int n){
+    
+    for (int i=0;i<n;i++){
+        *(a1+i) = (*(a2))>>((n*8)-(8*(i+1)));
+    }
+    
+}*/
 
 void after_cooling_pa(){
     gCOM_MNG_TMTC_THREAD->signal_set(COM_MNG_TMTC_SIGNAL_UART_INT);
@@ -432,7 +451,7 @@
                         {
                             //gPC.printf("sub_service:Read LONG_RAM_Memory\r\n");
                             mid=((uint16_t)(tc_ptr->TC_string[3]<<4))+((uint16_t)(tc_ptr->TC_string[4]));                                                       
-                            if(mid!=0x0000 && mid!=0x0001)
+                            if(mid < 0x0000 && mid > 0x000A)
                             {
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0x02,GETpacket_seq_count(tc_ptr));
@@ -448,6 +467,24 @@
                             FCTN_CDMS_RD_L_RAM(tm_ptr);
                             if(mid == 0x0001)
                             FCTN_CDMS_MAX_MIN(tm_ptr);
+                            if(mid == 0x0002)
+                            FCTN_CDMS_SCP_M0a(tm_ptr);
+                            if(mid == 0x0003)
+                            FCTN_CDMS_SCP_M0b(tm_ptr);
+                            if(mid == 0x0004)
+                            FCTN_CDMS_SCP_M1a(tm_ptr);
+                            if(mid == 0x0005)
+                            FCTN_CDMS_SCP_M1b(tm_ptr);
+                            if(mid == 0x0006)
+                            FCTN_CDMS_SCP_SFP_SUM(tm_ptr);
+                            if(mid == 0x0007)
+                            FCTN_CDMS_SFP_M0a(tm_ptr);
+                            if(mid == 0x0008)
+                            FCTN_CDMS_SFP_M0b(tm_ptr);
+                            if(mid == 0x0009)
+                            FCTN_CDMS_SFP_M1a(tm_ptr);
+                            if(mid == 0x000A)
+                            FCTN_CDMS_SFP_M1b(tm_ptr);
                             
                             crc16 = crc16_gen(tm_ptr->TM_string,132);
                             tm_ptr->TM_string[132] = (uint8_t)((crc16&0xFF00)>>8);
@@ -457,18 +494,18 @@
                         }                           
                         case 0x5:                           // change the cases appropriately
                         {
-                            //gPC.printf("WRITE ON SHORT_FLASH_MEMORY\r\n");                          
+                            gPC.printf("WRITE ON SHORT_EEPROM_MEMORY\r\n");                          
                             mid=(uint16_t)(tc_ptr->TC_string[3]<<4)+(uint16_t)(tc_ptr->TC_string[4]);
                             //32 bits at a time           
                             Base_tm *tm_ptr_short = new Short_tm;
-                            if(mid == 0x0101 || mid==0x0102 || mid == 0x0103 || mid == 0x0100)                                  //allowable MID values?
+                            if(mid == 0x0101 || mid==0x0102 || mid == 0x0103 || mid == 0x0100 || mid == 0x0104)                                  //allowable MID values?
                             {
                                 
                                 ACK_L234(tm_ptr_short,0x02,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]));                                                     
+                            FCTN_CDMS_WR_S_EEPROM(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));              
 
@@ -480,18 +517,16 @@
                             if(mid == 0x0002)
                             {
                                 for(uint8_t z=0;z<24;z++)
-                                    scp_threshold_m0[z] = tc_ptr->TC_string[z+5];
-                                for(uint8_t z=24;z<32;z++)
-                                    scp_threshold_m0[z+8] = tc_ptr->TC_string[z+5];                            
+                                    mcpy(scp_threshold_m0+z,tc_ptr->TC_string+4*z+5,4);
+                                for(uint8_t z=24;z<32;z++) 
+                                    mcpy(scp_threshold_m0+z+8,tc_ptr->TC_string+4*z+5,4);                           
                                 for(uint8_t z=0;z<24;z++)
                                     flasharray_thres[z] = scp_threshold_m0[z];
                                 for(uint8_t z=24;z<32;z++)
                                     flasharray_thres[z] = scp_threshold_m0[z+8];
+
+                                THRES_WRITE_TO_EEPROM(mid,flasharray_thres,1);
                                 
-   //                            FCTN_CDMS_WR_S_FLASH(mid,flasharray_thres);
-                                erase_sector(strt_add_thres);
-                                program_flash(strt_add_thres, (char*)flasharray_thres,32);
-     
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
@@ -499,13 +534,11 @@
                             else if(mid == 0x0003)
                             {
                                 for(uint8_t z=0;z<12;z++)
-                                    scp_threshold_m0[z+40] = tc_ptr->TC_string[z+5];
+                                    mcpy(scp_threshold_m0+z+40,tc_ptr->TC_string+4*z+5,4);
                                 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,2);
                                 
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
@@ -514,13 +547,11 @@
                             else if(mid == 0x0004)
                             {
                                 for(uint8_t z=0;z<32;z++)
-                                    scp_threshold_m1[z] = tc_ptr->TC_string[z+5];
+                                    mcpy(scp_threshold_m1+z,tc_ptr->TC_string+4*z+5,4);
                                 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,1);
                                 
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
@@ -529,13 +560,11 @@
                             else if(mid == 0x0005)
                             {
                                 for(uint8_t z=0;z<20;z++)
-                                    scp_threshold_m1[z+32] = tc_ptr->TC_string[z+5];
+                                    mcpy(scp_threshold_m1+z+32,tc_ptr->TC_string+4*z+5,4);
                                 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,3);
                                 
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
@@ -543,15 +572,16 @@
                             }
                             else if(mid == 0x0006)
                             {
-                                scp_threshold_m0_1[7] = tc_ptr->TC_string[5];
-                                scp_threshold_m0_1[5] = tc_ptr->TC_string[6];
-                                scp_threshold_m0_1[8] = tc_ptr->TC_string[7];
-                                scp_threshold_m0_1[6] = tc_ptr->TC_string[8];
+                                mcpy(scp_threshold_m0_1+7,tc_ptr->TC_string+4*0+5,4);
+                                mcpy(scp_threshold_m0_1+5,tc_ptr->TC_string+4*1+5,4);
+                                mcpy(scp_threshold_m0_1+8,tc_ptr->TC_string+4*2+5,4);
+                                mcpy(scp_threshold_m0_1+6,tc_ptr->TC_string+4*3+5,4);
                                 for(uint8_t z=0;z<5;z++)
-                                    scp_threshold_m0_1[z] = tc_ptr->TC_string[z+9];
-                                scp_sfp_threshold_m0_2[0] = tc_ptr->TC_string[14];
-                                scp_sfp_threshold_m0_2[1] = tc_ptr->TC_string[15];
-                                scp_sfp_threshold_m0_2[2] = tc_ptr->TC_string[16];
+                                    mcpy(scp_threshold_m0_1+z,tc_ptr->TC_string+4*(z+4)+5,4);
+                                mcpy(scp_sfp_threshold_m0_2+0,tc_ptr->TC_string+4*9+5,4);
+                                mcpy(scp_sfp_threshold_m0_2+1,tc_ptr->TC_string+4*10+5,4);
+                                mcpy(scp_sfp_threshold_m0_2+2,tc_ptr->TC_string+4*11+5,4);
+                                
                                 flasharray_thres[0] = scp_threshold_m0_1[7];
                                 flasharray_thres[1] = scp_threshold_m0_1[5];
                                 flasharray_thres[2] = scp_threshold_m0_1[8];
@@ -561,11 +591,8 @@
                                 flasharray_thres[9]  = scp_sfp_threshold_m0_2[0];
                                 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,2);
                                 
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
@@ -574,17 +601,15 @@
                             else if(mid == 0x0007)
                             {
                                 for(uint8_t z=0;z<24;z++)
-                                    sfp_threshold_m0[z] = tc_ptr->TC_string[z+5];
+                                    mcpy(sfp_threshold_m0+z,tc_ptr->TC_string+4*z+5,4);
                                 for(uint8_t z=24;z<32;z++)
-                                    sfp_threshold_m0[z+8] = tc_ptr->TC_string[z+5];
+                                    mcpy(sfp_threshold_m0+z+8,tc_ptr->TC_string+4*z+5,4);
                                 for(uint8_t z=0;z<24;z++)
                                     flasharray_thres[z] = sfp_threshold_m0[z];
                                 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,1);
                                 
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
@@ -593,13 +618,12 @@
                             else if(mid == 0x0008)
                             {
                                 for(uint8_t z=0;z<12;z++)
-                                    sfp_threshold_m0[z+40] = tc_ptr->TC_string[z+5];
+                                    mcpy(sfp_threshold_m0+z+40,tc_ptr->TC_string+4*z+5,4);
                                 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,2);
+                                                          
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
@@ -607,15 +631,12 @@
                             else if(mid == 0x0009)
                             {
                                 for(uint8_t z=0;z<32;z++)
-                                    sfp_threshold_m1[z] = tc_ptr->TC_string[z+5];
+                                    mcpy(sfp_threshold_m1+z,tc_ptr->TC_string+4*z+5,4);
                                 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,1);
+
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
@@ -623,14 +644,12 @@
                             else if(mid == 0x000A)
                             {
                                 for(uint8_t z=0;z<20;z++)
-                                    sfp_threshold_m1[z+32] = tc_ptr->TC_string[z+5];
+                                    mcpy(sfp_threshold_m1+z+32,tc_ptr->TC_string+4*z+5,4);
                                 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,3);
+
                                 Base_tm *tm_ptr_short = new Short_tm;
                                 ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                                 return tm_ptr_short;
@@ -913,6 +932,35 @@
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
                             return tm_ptr_short;                            
                         }
+                        else if(GETpid(tc_ptr)==0x3A)
+                        {
+                            //REQUEST FOR SYS_RESET
+                            sys_pwr_rst.output();
+                            sys_pwr_rst = 1;
+                            
+                            //PREPARE FOR SYS_RESET
+                            SYS_RESET();
+                            
+                            while(1){
+                                wait_ms(10000);
+                                RESET_WATCHDOG();                 //uncomment in the FM SBC with watchdog enabled
+                                uint64_t cur_rtc_tc = FCTN_CDMS_RD_RTC();
+                                uint32_t T_check1_tc = ((((cur_rtc_tc - TIME_LATEST_CDMS_EPS_RST) >> 7)& 0x00000FC0)>>6);
+                                gPC.printf("\r%d\n",T_check1_tc );
+                                //gPC.printf("\r%d\n",TIME_LATEST_CDMS_EPS_RST);
+                                gPC.printf("\r%d\n",cur_rtc_tc);
+                                if(T_check1_tc >=  5*T_OFF_RST)
+                                {
+                                    break;
+                                }            
+                            }
+                            BACK_NORMAL(); 
+                            
+                            Base_tm *tm_ptr_short = new Short_tm;
+                            ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
+                            return tm_ptr_short;
+                         
+                        }
                          else if(GETpid(tc_ptr)==0x3b)
                         {                            
                             //gPC.printf("TC_RST_BAE\r\n"); // call PWR_SWCH_ON function
@@ -926,13 +974,14 @@
                         else if(GETpid(tc_ptr)==0x40)
                         {                         
                             //gPC.printf("RESET_HK_COUNTER\r\n"); // call PWR_SWCH_ON function
-                            
                             if(tc_ptr->TC_string[4] == 0x00 || tc_ptr->TC_string[4] == 0x01)
                             EN_PL = tc_ptr->TC_string[4];
                             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];
                             if(tc_ptr->TC_string[6] == 0x00 || tc_ptr->TC_string[6] == 0x01)
                             EN_RCV_SC = tc_ptr->TC_string[6];
+                            if(tc_ptr->TC_string[7] == 0x00 || tc_ptr->TC_string[7] == 0x01)
+                            EN_P_MGMT = tc_ptr->TC_string[7];
                             
                             Base_tm *tm_ptr_short = new Short_tm;
                             ACK_L234(tm_ptr_short,0xA0,GETpacket_seq_count(tc_ptr));
@@ -1044,7 +1093,8 @@
                             pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[5+j]))<<8);
                             pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[6+j])));
                             j+=4;
-                        }                        
+                        }
+                        //WRITE_PMS_TO_EEPROM(0x0001, pl_block);                   //Uncomment and test after ensuring PMS TCs work without the EEPROM part in it     
                         return tm_ptr;
                     }                    
                     else if(GETservice_subtype(tc_ptr)==0x2)
@@ -1060,6 +1110,7 @@
                             pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[5+j]))<<8);
                             pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[6+j])));
                         }
+                        //WRITE_PMS_TO_EEPROM(0x0002, pl_block + (32));                   //Uncomment and test after ensuring PMS TCs work without the EEPROM part in it     
                         return tm_ptr;
                     }
                     else if(GETservice_subtype(tc_ptr)==0x3)
@@ -1075,6 +1126,7 @@
                             pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[5+j]))<<8);
                             pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[6+j])));
                         }
+                        //WRITE_PMS_TO_EEPROM(0x0003, pl_block + (64));                   //Uncomment and test after ensuring PMS TCs work without the EEPROM part in it     
                         return tm_ptr;
                     }
                     else if(GETservice_subtype(tc_ptr)==0x4)
@@ -1090,6 +1142,7 @@
                             pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[5+j]))<<8);
                             pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[6+j])));
                         }
+                        //WRITE_PMS_TO_EEPROM(0x0004, pl_block + (96));                   //Uncomment and test after ensuring PMS TCs work without the EEPROM part in it     
                         return tm_ptr;
                     }
                     else if(GETservice_subtype(tc_ptr)==0x5)
@@ -1105,6 +1158,7 @@
                             pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[5+j]))<<8);
                             pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[6+j])));
                         }
+                        //WRITE_PMS_TO_EEPROM(0x0005, pl_block + (128));                   //Uncomment and test after ensuring PMS TCs work without the EEPROM part in it     
                         return tm_ptr;    
                     }
                     else if(GETservice_subtype(tc_ptr)==0x6)
@@ -1120,6 +1174,7 @@
                             pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[5+j]))<<8);
                             pl_block[i] |= (((uint32_t)(tc_ptr->TC_string[6+j])));
                         }
+                        //WRITE_PMS_TO_EEPROM(0x0006, pl_block + (160));                   //Uncomment and test after ensuring PMS TCs work without the EEPROM part in it     
                         return tm_ptr;
                     }
                     else if(GETservice_subtype(tc_ptr)==0xF)
--- a/COM_RCV_TC.h	Sat Feb 10 09:06:26 2018 +0000
+++ b/COM_RCV_TC.h	Sun Apr 03 15:35:53 2022 +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;\
     }\
--- a/Compression.h	Sat Feb 10 09:06:26 2018 +0000
+++ b/Compression.h	Sun Apr 03 15:35:53 2022 +0000
@@ -119,14 +119,8 @@
 //--------------------------------------------------------beacon
 
 //variable declarations
-uint8_t srp_mode , at , pzf , ezf ,sfp[71] ,scp[56],beacon_at; //tells which mode is calibrated or plot 0 for calibrated and 1 for scatterered, below threshold , proton zero flux, electron zero flux.
+uint8_t srp_mode , at , pzf , ezf ,sfp[71] ,scp[56],beacon_at; //tells which mode is calibrated or plot 0 for calibrated and 1 for scatter plot, below threshold or above threshold, proton zero flux, electron zero flux, science_fine_packet, science_coarse_packet
 uint32_t sfp_bin[52] , scp_bin[52];         //storing the bin values.
-uint32_t sfp_threshold_m0[52]  = {124,124,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,4032,252,252,252,252,252,4032,8128,8128,8128,8128,8128,8128,8128,8128,960,960,960,960,960,960,960,960,960,960,960,960,4032,4032,4032,4032,8128,8128,8128,8128};
-uint32_t scp_threshold_m0[52]  = {4032,4032,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,114688,8128,4032,4032,4032,4032,114688,245760,245760,245760,245760,245760,245760,245760,245760,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,114688,114688,114688,114688,245760,245760,245760,245760};
-uint32_t scp_threshold_m0_1[9] = {114688,245760,245760,245760,245760,114688,114688,65472,65472} ; //for callibrated mode with data conservation
-uint32_t scp_sfp_threshold_m0_2[3] = {245760,65472,65472};
-uint32_t sfp_threshold_m1[52]  = {1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,8128,8128,8128,8128};
-uint32_t scp_threshold_m1[52]  = {114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,245760,245760,245760,245760};
 uint8_t frames[3][134] = {0};
 uint8_t position_tm_frame[3] = {8,11,5} , position_tm_starting[3] = {8,11,5}; //{sc,sf-bt,sf-at}
 uint8_t id;   //sf = 0,sc-at = 1,sc-bt = 2;
--- a/DefinitionsAndGlobals.h	Sat Feb 10 09:06:26 2018 +0000
+++ b/DefinitionsAndGlobals.h	Sun Apr 03 15:35:53 2022 +0000
@@ -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 changed Mar 7 2018
+   // #define SPI_CS_RTC PIN60 // sbc with jumper v1.0
     
 // COM_TX
     #define COM_TX_CONFIG_LIMIT 3
@@ -39,15 +39,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
@@ -138,7 +138,7 @@
     #define OBOSC_SUB_RESET 0x07
     
 // PAYLOAD or SCIENCE
-    #define PAYLOAD_BUFFER_LENGTH 6723  // 6723
+    #define PAYLOAD_BUFFER_LENGTH 3363  // 6723
     #define PAY_SPI_MOSI PTE18
     #define PAY_SPI_MISO PTE19
     #define PAY_SPI_CLK PTE17
@@ -157,10 +157,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     //chenged Mar 07 2018
+    #define RF_RELAY_CNTRL_BCN PTA7 
+  //#define RF_RELAY_CNTRL_TX PTB7
+    //#define RF_RELAY_CNTRL_BCN PTB17 
     
 //CDMS HK
 #define HK_SIGNAL 0x05
@@ -171,6 +171,15 @@
 #define DEVICE_OC_FAULT 0x02
 #define DEVICE_DISABLED 0x03
 
+//CDMS HK SYS_RESET
+#define T_WAIT_RST 0x00
+#define T_OFF_RST 0x0A
+#define T_RST 0x02
+#define T_RETRY_RST 0x1E
+
+//CDMS TIME_INIT
+#define T_INI 0x02
+
 
     
 // ****************GLOBAL VARIABLES******************
@@ -245,6 +254,7 @@
 // PAYLOAD OR SCIENCE_THREAD
 Thread* gSCIENCE_THREAD = NULL;
 uint8_t gPAYLOAD_BUFFER[PAYLOAD_BUFFER_LENGTH] = {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,44,1,44,1,44,1,44,1,44,1,44,1,188,2,188,2,188,2,188,2,88,2,88,2,88,2,88,2,100,0,100,0,200,0,200,0,244,1,244,1,88,2,88,2,208,2,208,2,246,0,246,0,10,0,250,0,250,0,250,0,120,0,0,0,172,13,0,0,184,11,0,0,160,15,0,0};
+//uint8_t gPAYLOAD_BUFFER[PAYLOAD_BUFFER_LENGTH] = {0};
 RtosTimer *PL_wo_dma;
 
 // CALL SIGN TM
@@ -253,7 +263,11 @@
 //HK Thread
 Thread* gHK_THREAD = NULL;
 RtosTimer *HK_counter;
-
+/*
+Startup_timer
+Thread* gST_UP = NULL;
+RtosTimer *St_upTimer
+*/
 // CDMS HK
 uint8_t CDMS_STANDBY;
 uint8_t CDMS_HEALTH_DATA[128];
@@ -310,7 +324,7 @@
 //DigitalOut TRXY_EN (PIN84);//TRXY EN
 //DigitalOut TRZ_EN (PIN88);//TRZ EN
 DigitalOut V_A_EN (PIN99,1);//TRZ FLT (no INTR capability)
-DigitalIn sys_pwr_rst (PIN46);//PTD3
+DigitalInOut sys_pwr_rst (PIN46, PIN_INPUT, PullNone, 0);//PTA16
 
 DigitalOut PYLD_DFF (PIN73);//PYLD_SW_PWR_EN_DIS
 DigitalOut PYLD_DFF_CLK (PIN65);//PL_3V3_EN_CLK
@@ -344,6 +358,8 @@
 uint32_t TIME_LATEST_SD_WR;
 uint32_t TIME_LATEST_SD_RD;
 uint32_t TIME_LATEST_SPI_SPEED;
+uint64_t TIME_LATEST_CDMS_EPS_RST;
+uint64_t TIME_LATEST_CDMS_EPS_RST_BACKUP;
 
 //Counter's
 uint16_t CDMS_I2C_ERR_SPEED_COUNTER = 0;
@@ -374,13 +390,14 @@
 uint8_t EN_CDMS_HK = 1;
 uint8_t EN_PL = 1;
 uint8_t EN_RCV_SC = 1;
+uint8_t EN_P_MGMT = 0;
 uint8_t CDMS_INIT_STATUS;
 uint8_t CDMS_HK_STATUS;
 uint8_t COM_RX_STATUS;
 uint8_t CDMS_RTC_BL;
 
 
-//CDMS FLASH parameters
+//CDMS EEPROM parameters
 
 uint8_t EPS_V_A_EN_STATUS;
 //uint8_t BAE_SW_STATUS;
@@ -399,9 +416,18 @@
 uint8_t COM_PA_RECOVERY_TIMEOUT;
 uint8_t COM_SESSION_TIMEOUT = 240;  //confirm with Anirudh 
 uint8_t COM_RSSI_MIN;
+uint8_t TC_count;
 //uint16_t SD_LIB_BLK_CURRENT_1;
 //uint16_t SD_LIB_BLK_CURRENT_2;
+//COM threshold
 uint32_t SD_LIB_BLK_CURRENT;
+uint32_t sfp_threshold_m0[52]  = {124,124,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,4032,252,252,252,252,252,4032,8128,8128,8128,8128,8128,8128,8128,8128,960,960,960,960,960,960,960,960,960,960,960,960,4032,4032,4032,4032,8128,8128,8128,8128};
+uint32_t scp_threshold_m0[52]  = {4032,4032,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,114688,8128,4032,4032,4032,4032,114688,245760,245760,245760,245760,245760,245760,245760,245760,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,49152,114688,114688,114688,114688,245760,245760,245760,245760};
+uint32_t scp_threshold_m0_1[9] = {114688,245760,245760,245760,245760,114688,114688,65472,65472} ; //for callibrated mode with data conservation
+uint32_t scp_sfp_threshold_m0_2[3] = {245760,65472,65472};
+uint32_t sfp_threshold_m1[52]  = {1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,1984,8128,8128,8128,8128};
+uint32_t scp_threshold_m1[52]  = {114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,114688,245760,245760,245760,245760};
+
 
 // Ambigouties
 uint8_t EN_RTC = 0x01;
@@ -411,3 +437,9 @@
 uint8_t COM_OC_SET = 0;
 uint8_t COM_RSSI_SET = 0;
 uint8_t resume_pl_dma = 0;
+
+//For power mode MGMT
+uint8_t POWER_MODE;
+
+//For sleep 
+bool expired;
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EEPROM.h	Sun Apr 03 15:35:53 2022 +0000
@@ -0,0 +1,292 @@
+#ifndef EEPROM_H
+#define EEPROM_H
+
+//#include "pinconfig.h"
+//#include "eeprom.h"
+//#include "DefinitionsAndGlobals.h"
+#define ee_scl PIN86
+#define ee_sda PIN87
+#define check 10
+
+//EEPROM e2prom(ee_sda, ee_scl, 0, EEPROM::T24C512); // T24C1024 for 1Mbit EEPROM
+EEPROM e2prom(ee_sda, ee_scl, 0x0, EEPROM::T24C512);
+
+int32_t INITIAL_EEPROM[29] = {1,1,1,80000,1,0,0,0,0,1,1,20,85,20,30,3,24,1,0,0,0,0,0,0,0,0,0,0,0};
+//int32_t INITIAL_EEPROM[32] = {1,0,1,0,1,0,1,0,1,35001,0xff,300,85,20,20,3,0x80000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
+
+uint32_t flasharray_thres[32];
+uint32_t flasharray_thres_1[32];
+
+void I2C_EEPROM_busreset()
+{
+    gPC.printf("\n\rChecking");
+    PORTC->PCR[10] &= 0xfffffffb;    //Enabling high slew rates for SDA and SCL lines
+    PORTC->PCR[11] &= 0xfffffffb;    //Enabling high slew rates for SDA and SCL lines
+    I2C1->C1 &= 0x7f;               //Disabling I2C module
+    SIM->SCGC4 &= 0xffffff7f;       //Disabling clock to I2C module
+    SIM->SCGC4 |= 0x00000080;       //Enabling clock to I2C module
+    I2C1->C1 |= 0x80;               //Enabling I2C module
+    PORTC->PCR[10] |= 0x00000004;    //Disabling high slew rates for SDA and SCL lines
+    PORTC->PCR[11] |= 0x00000004;    //Disabling high slew rates for SDA and SCL lines
+    Thread::wait(1);                //Wait for all I2C registers to be updates to their their values
+}
+
+void mcpy(uint32_t *a2,int8_t *a1,int n){
+    
+    *a2=0;
+    for (int i=0;i<n;i++){
+        *(a2) += *(a1+i);
+        if(i<n-1){
+        *(a2)=(*(a2))<<8;
+        //printf("%d\n",*(a2));
+        }
+    }
+    
+}
+
+void rmcpy(uint8_t *a1,uint32_t *a2,int n){
+    
+    for (int i=0;i<n;i++){
+        *(a1+i) = (*(a2))>>((n*8)-(8*(i+1)));
+    }
+    
+}
+
+void WRITE_TO_EEPROM(uint32_t address, int32_t data_block)
+{
+    e2prom.write(address*4+10,data_block);
+    wait_us(1);
+}
+
+void THRES_WRITE_TO_EEPROM(uint16_t mid, uint32_t* thres_data,int val)
+{
+    int8_t data[128] = {0};
+    //memcpy(data,thres_data,128);
+    if(val == 1 )
+        memcpy(data,thres_data,128);
+    if(val == 2)
+        memcpy(data,thres_data,48);
+    if(val == 3)
+        memcpy(data,thres_data,80);
+    if(mid <11 & mid >1)
+    {
+        e2prom.write((uint32_t)(mid-1)*128,data,128);
+    }    
+}
+
+void WRITE_PMS_TO_EEPROM(uint16_t pmid, uint32_t* thres_data)
+{
+    int8_t data[128];
+    memcpy(data,thres_data,128);
+    
+    if(pmid <7 & pmid >0)
+    {
+        e2prom.write((uint32_t)(1280 + (pmid-1)*128),data,128);
+    }    
+}
+
+void UPDATE_PMS_ENTRY(uint32_t address, uint32_t data_block)
+{
+    e2prom.write((1280 + (address*4)),data_block);
+    wait_us(1);
+}
+
+uint32_t READ_FROM_EERPOM(uint32_t address)
+{
+    int32_t data;
+    e2prom.read(address*4+10, data);
+    wait_ms(10);
+    return (uint32_t)data;
+}
+
+void THRES_READ_FROM_EEPROM(uint16_t mid, uint32_t* data32,int val)
+{
+    int8_t data[128];
+    if(mid<11 & mid >1)
+    {
+    e2prom.read((uint32_t)(mid-1)*128,data,128);
+    }
+    if(val == 1 )
+        memcpy(data32,data,128);
+    if(val == 2)
+        memcpy(data32,data,48);
+    if(val == 3)
+        memcpy(data32,data,80);
+    /*for(int i=0;i<32;i++){
+        mcpy(data32+i,data+4*i,4);
+        }*/
+    wait_ms(10);
+}    
+
+void READ_PMS_FROM_EEPROM(uint16_t pmid, uint32_t* data32)
+{
+    int8_t data[128];
+    if(pmid<7 & pmid > 0)
+    {
+    e2prom.read((uint32_t)(1280 + (pmid-1)*128),data,128);
+    }
+    memcpy(data32,data,128);
+    /*for(int i=0;i<32;i++){
+        mcpy(data32+i,data+4*i,4);
+        }*/
+    wait_ms(10);
+}    
+    
+void INIT_EEPROM()
+{
+     // To be commented starts
+    int32_t test_buffer;
+    test_buffer = READ_FROM_EERPOM(17);
+    // test_buffer = 0;
+   // e2prom.ready();
+  //  e2prom.read(2,test_buffer);
+
+    if(test_buffer != 1)
+    {
+        if(test_buffer == 0)
+            {
+                for(int i=0;i<29;i++)
+                    WRITE_TO_EEPROM( i, INITIAL_EEPROM[i]);
+                WRITE_TO_EEPROM(17,0x01);   
+                gPC.printf("Initializing EEPROM - Day 0\n\r");
+            }
+        if(test_buffer == 2)
+            {
+                for(int i=0;i<29;i++)
+                    WRITE_TO_EEPROM( i, INITIAL_EEPROM[i]);
+                WRITE_TO_EEPROM( 5, 1);
+                WRITE_TO_EEPROM(17,0x01); 
+                gPC.printf("Initializing EEPROM - Reset EEPROM Paramters only\n\r");
+            }
+    }
+  
+    // To be commented ends*/
+    gPC.printf("Reading intial parametrs from EEPROM\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[3];
+    SD_LIB_BLK_CURRENT = READ_FROM_EERPOM(3);
+    PL_BEE_SW_STATUS = READ_FROM_EERPOM(4);
+    //PL_EPS_LATCH_SW_EN = read[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 = 85;//read[12];
+    COM_PA_RECOVERY_TIMEOUT = READ_FROM_EERPOM(13);
+    COM_SESSION_TIMEOUT = READ_FROM_EERPOM(14);
+    COM_RSSI_MIN = READ_FROM_EERPOM(15);
+    PL_STATE = READ_FROM_EERPOM(16);
+    
+    THRES_READ_FROM_EEPROM(0x0002,flasharray_thres,1);
+    for(uint8_t z=0;z<24;z++)
+         scp_threshold_m0[z] = flasharray_thres[z] ;
+    for(uint8_t z=24;z<32;z++)
+         scp_threshold_m0[z+8] = flasharray_thres[z] ;
+    
+    THRES_READ_FROM_EEPROM(0x0003,scp_threshold_m0+(40),2);
+    
+    THRES_READ_FROM_EEPROM(0x0004,scp_threshold_m1,1);
+    
+    THRES_READ_FROM_EEPROM(0x0005,scp_threshold_m1+(32),3);
+    
+    THRES_READ_FROM_EEPROM(0x0006,flasharray_thres,2); 
+    scp_threshold_m0_1[7] = flasharray_thres[0];
+    scp_threshold_m0_1[5] = flasharray_thres[1] ;
+    scp_threshold_m0_1[8] = flasharray_thres[2] ;
+    scp_threshold_m0_1[6] = flasharray_thres[3] ;
+    for(uint8_t z=0;z<5;z++)
+        scp_threshold_m0_1[z] = flasharray_thres[z+4] ;
+    scp_sfp_threshold_m0_2[0] = flasharray_thres[9]  ;
+    scp_sfp_threshold_m0_2[1] = flasharray_thres[10] ;
+    scp_sfp_threshold_m0_2[2] = flasharray_thres[11] ;
+    
+     THRES_READ_FROM_EEPROM(0x0007,flasharray_thres,1);
+    for(uint8_t z=0;z<24;z++)
+         sfp_threshold_m0[z] = flasharray_thres[z] ;
+    for(uint8_t z=24;z<32;z++)
+         sfp_threshold_m0[z+8] = flasharray_thres[z] ;
+    
+    THRES_READ_FROM_EEPROM(0x0008,sfp_threshold_m0+(40),2);
+    
+    THRES_READ_FROM_EEPROM(0x0009,sfp_threshold_m1,1);
+    
+    THRES_READ_FROM_EEPROM(0x000A,sfp_threshold_m1+(32),3);
+    
+    /*READ_PMS_FROM_EEPROM(0x0001, pl_block);
+    
+    READ_PMS_FROM_EEPROM(0x0002, pl_block + (32));
+    
+    READ_PMS_FROM_EEPROM(0x0003, pl_block + (64));              //Uncomment and test this after all other PMS functionality has been verified
+    
+    READ_PMS_FROM_EEPROM(0x0004, pl_block + (96));
+    
+    READ_PMS_FROM_EEPROM(0x0005, pl_block + (128));
+    
+    READ_PMS_FROM_EEPROM(0x0006, pl_block + (160));*/
+    
+    
+    gPC.printf("0 = \r%d\n\r", READ_FROM_EERPOM(0));
+    gPC.printf("1 = %d\n\r", READ_FROM_EERPOM(1));
+    gPC.printf("2 = %d\n\r", READ_FROM_EERPOM(2));
+    gPC.printf("3 = %d\n\r", READ_FROM_EERPOM(3));
+    gPC.printf("4 = %d\n\r", READ_FROM_EERPOM(4));
+    gPC.printf("5 = %d\n\r", READ_FROM_EERPOM(5));
+    gPC.printf("6= %d\n\r", READ_FROM_EERPOM(6));
+    gPC.printf("7= %d\n\r", READ_FROM_EERPOM(7));
+    gPC.printf("8= %d\n\r", READ_FROM_EERPOM(8));
+    gPC.printf("9= %d\n\r", READ_FROM_EERPOM(9));
+    gPC.printf("10= %d\n\r", READ_FROM_EERPOM(10));
+    gPC.printf("11= %d\n\r", READ_FROM_EERPOM(11));
+    gPC.printf("12= %d\n\r", READ_FROM_EERPOM(12));
+    gPC.printf("13= %d\n\r", READ_FROM_EERPOM(13));
+    gPC.printf("14= %d\n\r", READ_FROM_EERPOM(14));
+    gPC.printf("15= %d\n\r", READ_FROM_EERPOM(15));
+    gPC.printf("16 = %u\n\r", READ_FROM_EERPOM(16));
+    gPC.printf("test buffer 17 = %u\n\r", READ_FROM_EERPOM(17));
+    
+ 
+   // SD_LIB_BLK_CURRENT        = READ_FROM_EERPOM(16); 
+    // added by samp
+ 
+    
+}
+
+/*===================================================MMS Functions=================================================*/
+
+void FCTN_CDMS_WR_S_EEPROM(uint16_t mid,uint32_t datablock);
+
+void FCTN_CDMS_WR_S_EEPROM(uint16_t mid,uint32_t datablock)
+{
+    if(mid == 0x0100)
+    {
+        WRITE_TO_EEPROM(11,(datablock>>16) & 0x000000FF);
+        WRITE_TO_EEPROM(13,(datablock>>8) & 0x000000FF);
+        WRITE_TO_EEPROM(14,datablock & 0x000000FF);
+    }
+    else if(mid == 0x0101)
+    {
+        WRITE_TO_EEPROM(12,(datablock>>16) & 0x000000FF);
+        WRITE_TO_EEPROM(10,(datablock>>8) & 0x000000FF);
+        WRITE_TO_EEPROM(15,datablock & 0x000000FF);
+    }
+    else if(mid == 0x0102)
+    {
+        WRITE_TO_EEPROM(9,datablock & 0x0000FFFF);
+    }
+    else if(mid == 0x0103)
+    {
+        WRITE_TO_EEPROM(6,datablock & 0x00000001);
+    }
+    else if(mid == 0x0104)
+    {
+        WRITE_TO_EEPROM(17,datablock & 0x00000001);
+    } 
+}
+
+#endif
\ No newline at end of file
--- a/FMS_all.h	Sat Feb 10 09:06:26 2018 +0000
+++ b/FMS_all.h	Sun Apr 03 15:35:53 2022 +0000
@@ -35,8 +35,6 @@
 void RST_BAE();
 void RST_PL_BEE();
 void CDMS_RESET();
-void SYS_PWR_RESET();  // Have to be decided with EPS team.
-void sys_pwr_reset(void const *args);
 void EPS_V_A_EN();
 void EPS_V_C_EN();
 
@@ -45,10 +43,6 @@
 void CDMS_CALIB_RTC(uint8_t *);
 void TOTAL_RESET_WITH_CDMS();
 
-void sys_pwr_reset(void const *args){
-    SYS_PWR_RESET();
-}
-
 void P_PL_INIT()
 {
     
@@ -81,11 +75,13 @@
 
 void CDMS_SD_SW_ON()
 {
+    wait_ms(100);
    // 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);
+    //FCTN_CDMS_WR_FLASH(2,DEVICE_POWERED);
     gPC.printf("sw on sd\n");
 }
 
@@ -96,12 +92,16 @@
     SD_SW_EN_DS = 0;
     SD_STATUS = DEVICE_DISABLED;
  //  SD_STATUS = 0;
-   FCTN_CDMS_WR_FLASH(2,DEVICE_DISABLED);
+   //FCTN_CDMS_WR_FLASH(2,DEVICE_DISABLED);
+    WRITE_TO_EEPROM(2,DEVICE_DISABLED);
+    wait_ms(100);
     gPC.printf("sw off sd\n");
 }
 
 void CDMS_RTC_ON()
 {
+    WRITE_TO_EEPROM(6,DEVICE_POWERED);
+    wait_ms(100);
     //FCTN_CDMS_INIT_RTC();
     SPI_mutex.lock();
     gCS_RTC=1;
@@ -121,7 +121,7 @@
 
      //EN_RTC = 0x01;
     CDMS_RTC_DISABLE =1;
-    FCTN_CDMS_WR_FLASH(6,DEVICE_POWERED);
+    //FCTN_CDMS_WR_FLASH(6,DEVICE_POWERED);
   //  gPC.printf("0x%08X \n", READ_FROM_EERPOM(6));
     SPI_mutex.unlock();
     gPC.printf("sw on rtc\n");
@@ -147,7 +147,9 @@
    
    //EN_RTC  = 0x00;
    CDMS_RTC_DISABLE = 0;
-    FCTN_CDMS_WR_FLASH(6,0x00);
+    //FCTN_CDMS_WR_FLASH(6,0x00);
+    WRITE_TO_EEPROM(6,0x00);
+    wait_ms(100);
     SPI_mutex.unlock();
     gPC.printf("sw off rtc\n");
 }
@@ -156,7 +158,9 @@
 {
     BAE_SW_EN_DS = 1;
     BAE_STATUS = DEVICE_POWERED;
-    FCTN_CDMS_WR_FLASH(1,DEVICE_POWERED);
+    //FCTN_CDMS_WR_FLASH(1,DEVICE_POWERED);
+    WRITE_TO_EEPROM(1,DEVICE_POWERED);
+    wait_ms(100);
     gPC.printf("sw on bae\n");
 }
 
@@ -165,7 +169,9 @@
     BAE_SW_EN_DS = 0;
     BAE_STATUS = DEVICE_DISABLED;
     // BAE_STATUS = 0;
-     FCTN_CDMS_WR_FLASH(1,DEVICE_DISABLED);
+     //FCTN_CDMS_WR_FLASH(1,DEVICE_DISABLED);
+    WRITE_TO_EEPROM(1,DEVICE_DISABLED);
+    wait_ms(100);
     gPC.printf("sw off bae\n");
 }
 
@@ -179,7 +185,10 @@
     PYLD_DFF_CLK = 0;
     wait_us(1);
     PL_BEE_SW_STATUS = DEVICE_POWERED;
-    FCTN_CDMS_WR_FLASH(4,DEVICE_POWERED);
+    //PL_STATE = 0x01;                                  //uncomment - JOel after figuring out PYLD startup state
+    //WRITE_TO_EEPROM(16,(int32_t)PL_STATE);
+    //FCTN_CDMS_WR_FLASH(4,DEVICE_POWERED);
+    WRITE_TO_EEPROM(4,DEVICE_POWERED);
 }
 
 void SW_OFF_PL_BEE()
@@ -192,7 +201,10 @@
     PYLD_DFF_CLK = 0;
     wait_us(1);
     PL_BEE_SW_STATUS = DEVICE_DISABLED;
-    FCTN_CDMS_WR_FLASH(4,DEVICE_DISABLED);
+    //FCTN_CDMS_WR_FLASH(4,DEVICE_DISABLED);
+    PL_STATE = 0x00;
+    WRITE_TO_EEPROM(16,(int32_t)PL_STATE);
+    WRITE_TO_EEPROM(4,DEVICE_DISABLED);
 }
 
 void SW_ON_PL_EPS()
@@ -227,14 +239,16 @@
 {
     V_A_EN = 1;
     EPS_V_A_EN_STATUS = 1;
-   FCTN_CDMS_WR_FLASH(0,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);
+   //FCTN_CDMS_WR_FLASH(0,0);
+   WRITE_TO_EEPROM(0,0);
 }
 
 void RST_SD()
@@ -247,18 +261,22 @@
     SD_CARD_fromISO_ENA4 = 1;
     SD_SW_EN_DS = 1;
     SD_STATUS = DEVICE_POWERED;
-    FCTN_CDMS_WR_FLASH(2,DEVICE_POWERED);
+    //FCTN_CDMS_WR_FLASH(2,DEVICE_POWERED);
+    WRITE_TO_EEPROM(2,DEVICE_POWERED);
+    wait_ms(100);
     gPC.printf("rst sd\n");
 }
 
 void SW_RST_BAE()
 {
-   BAE_SW_EN_DS = 0;
-   wait(5);
-   BAE_SW_EN_DS = 1;
+    BAE_SW_EN_DS = 0;
+    wait(5);
+    BAE_SW_EN_DS = 1;
    
     BAE_STATUS = DEVICE_POWERED;
-    FCTN_CDMS_WR_FLASH(1,DEVICE_POWERED);
+    //FCTN_CDMS_WR_FLASH(1,DEVICE_POWERED);
+    WRITE_TO_EEPROM(1,DEVICE_POWERED);
+    wait_ms(100);
     gPC.printf("rst bae\n");
 }
 
@@ -276,6 +294,9 @@
     RESET_TO_BAE = 0;
     wait_ms(10);
     RESET_TO_BAE = 1;
+    BAE_STATUS = DEVICE_POWERED;
+    
+    WRITE_TO_EEPROM(1,DEVICE_POWERED);
 }
 
 void RST_PL_BEE()
@@ -290,11 +311,6 @@
     NVIC_SystemReset();
 }
 
-void SYS_PWR_RESET()  // Have to be decided with EPS team.
-{
-    //sys_pwr_rst = 1;
-}
-
 void EPS_V_A_EN()     // This is a reset function
 {
     V_A_EN = 0;
@@ -443,4 +459,82 @@
         tm_pointer->TM_string[44+i] = TIME_LATEST_RTC >> (3-i)*8; 
 }
 
+void FCTN_CDMS_SCP_M0a(Base_tm *tm_pointer)
+{
+    for(uint8_t i=0; i<24; i++)
+        rmcpy(tm_pointer->TM_string+4*i+4, scp_threshold_m0+i, 4);
+    for(uint8_t i=24; i<32; i++)
+        rmcpy(tm_pointer->TM_string+4*i+4, scp_threshold_m0+i+8, 4);
+        
+    for(uint8_t i=0; i<128; i++)
+        gPC.printf("\r %d\n",tm_pointer->TM_string[i]);
+    
+    
+}
+
+void FCTN_CDMS_SCP_M0b(Base_tm *tm_pointer)
+{
+    for(uint8_t i=0; i<12; i++)
+        rmcpy(tm_pointer->TM_string+4*i+4, scp_threshold_m0+i+40, 4);
+    
+}
+
+void FCTN_CDMS_SCP_M1a(Base_tm *tm_pointer)
+{
+    for(uint8_t i=0; i<32; i++)
+        rmcpy(tm_pointer->TM_string+4*i+4, scp_threshold_m1+i, 4);
+    
+}
+
+void FCTN_CDMS_SCP_M1b(Base_tm *tm_pointer)
+{
+    for(uint8_t i=0; i<20; i++)
+        rmcpy(tm_pointer->TM_string+4*i+4, scp_threshold_m1+i+32, 4);
+    
+}
+
+void FCTN_CDMS_SCP_SFP_SUM(Base_tm *tm_pointer)
+{
+    rmcpy(tm_pointer->TM_string+4*0+4, scp_threshold_m0_1+7, 4);
+    rmcpy(tm_pointer->TM_string+4*1+4, scp_threshold_m0_1+5, 4);
+    rmcpy(tm_pointer->TM_string+4*2+4, scp_threshold_m0_1+8, 4);
+    rmcpy(tm_pointer->TM_string+4*3+4, scp_threshold_m0_1+6, 4);
+    for(uint8_t i=0; i<5; i++)
+        rmcpy(tm_pointer->TM_string+4*(i+4)+4, scp_threshold_m0_1+i, 4);
+    rmcpy(tm_pointer->TM_string+4*9+4, scp_sfp_threshold_m0_2+0, 4);
+    rmcpy(tm_pointer->TM_string+4*10+4, scp_sfp_threshold_m0_2+1, 4);
+    rmcpy(tm_pointer->TM_string+4*11+4, scp_sfp_threshold_m0_2+2, 4);
+    
+}
+
+void FCTN_CDMS_SFP_M0a(Base_tm *tm_pointer)
+{
+    for(uint8_t i=0; i<24; i++)
+        rmcpy(tm_pointer->TM_string+4*i+4, sfp_threshold_m0+i, 4);
+    for(uint8_t i=24; i<32; i++)
+        rmcpy(tm_pointer->TM_string+4*i+4, sfp_threshold_m0+i+8, 4);
+    
+}
+
+void FCTN_CDMS_SFP_M0b(Base_tm *tm_pointer)
+{
+    for(uint8_t i=0; i<12; i++)
+        rmcpy(tm_pointer->TM_string+4*i+4, sfp_threshold_m0+i+40, 4);
+    
+}
+
+void FCTN_CDMS_SFP_M1a(Base_tm *tm_pointer)
+{
+    for(uint8_t i=0; i<32; i++)
+        rmcpy(tm_pointer->TM_string+4*i+4, sfp_threshold_m1+i, 4);
+    
+}
+
+void FCTN_CDMS_SFP_M1b(Base_tm *tm_pointer)
+{
+    for(uint8_t i=0; i<20; i++)
+        rmcpy(tm_pointer->TM_string+4*i+4, sfp_threshold_m1+i+32, 4);
+    
+}
+
 #endif
\ No newline at end of file
--- a/Flash.h	Sat Feb 10 09:06:26 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,126 +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[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++)
-    {  
-        read[i] = nativeflash[i];
-    }
-            
-    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
-    {
-        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];
-    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];
-    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 = 85;//read[12];
-    COM_PA_RECOVERY_TIMEOUT = read[13];
-    COM_SESSION_TIMEOUT = read[14];
-    COM_RSSI_MIN = read[15];
-    PL_STATUS = read[16];
-    //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
--- a/OBSRS.h	Sat Feb 10 09:06:26 2018 +0000
+++ b/OBSRS.h	Sun Apr 03 15:35:53 2022 +0000
@@ -1,5 +1,5 @@
 // problem in check pa and valid sid not sending anything in one for loop
-
+ 
 #ifndef OBSRS_H    // To make sure you don't declare the function more than once by including the header multiple times.
 #define OBSRS_H
 
@@ -130,7 +130,7 @@
     else{\
         pa_temp1 = (3365.4/log(7.60573*resistance))-273;\
     }\
-    gPC.printf("\n%f\n",pa_temp1);\
+    /*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");\
--- a/RESET_functions.h	Sat Feb 10 09:06:26 2018 +0000
+++ b/RESET_functions.h	Sun Apr 03 15:35:53 2022 +0000
@@ -1,6 +1,7 @@
 
 void RESET_COMRX();
 void RESET_RTC();
+void RESET_WATCHDOG();
 
 void RESET_COMRX()
 {
@@ -35,4 +36,15 @@
     gCS_RTC=0;
     spi.write(0x8F);
     spi.write(0x00);
+}
+
+void RESET_WATCHDOG(){
+    gPC.printf("\r WD\n");
+    int i=0;
+    WDTrigger = 1;
+    while(i<5){
+        i++;
+    }
+    WDTrigger = 0;
+    
 }
\ No newline at end of file
--- a/ThreadsAndFunctions.h	Sat Feb 10 09:06:26 2018 +0000
+++ b/ThreadsAndFunctions.h	Sun Apr 03 15:35:53 2022 +0000
@@ -22,34 +22,38 @@
    // gPAY_SPI->frequency(1000000);   //1000000
     gPAY_SPI->format(8,0); 
     gPC.printf("\n\rTest...........................");
-    gPAY_SPI->bulkRead_init(gPAYLOAD_BUFFER, PAYLOAD_BUFFER_LENGTH, &payload_isr_fun);
-    gPAY_SPI->bulkRead_start();
+    gPAY_SPI->bulkRead_init(gPAYLOAD_BUFFER, PAYLOAD_BUFFER_LENGTH, &payload_isr_fun);    //commented 2day
+    gPAY_SPI->bulkRead_start();       //commented 2day
     
     //AY_SPI->bulkRead_pause();
     //AY_SPI->bulkRead_resume(&payload_isr_fun);
     while(true){
         gPC.printf("waiting for Sci sig\n\r");
         gSCIENCE_THREAD->signal_wait(SCIENCE_SIGNAL);
-         
+        gPC.printf("got signal\n\r");
+        if(EN_RCV_SC == 0x00)
+            continue;
         gPC.printf("checking mutex in Sci\n\r");
         gMutex.lock();
+        gPC.printf("locked mutex,entered in Sci thread\n\r"); 
         
-        gPC.printf("locked mutex,entered in Sci thread\n\r"); 
-  
+        WRITE_TO_EEPROM(1,DEVICE_POWERED);
         DMA_LED1 = 1;
         PL_RCV_SC_DATA_STATUS = 1;
+        
         //gPC.puts("sig_set\r\n");
         //for(int i =0; i<PAYLOAD_BUFFER_LENGTH; i++)
         //if (gPAYLOAD_BUFFER[i] != 0x01) gPC.printf("%d = 0x%02X\n\r",i,gPAYLOAD_BUFFER[i]);  
         //gPAY_SPI->bulkRead_start();
-      //  srp(gPAYLOAD_BUFFER);//skip one apcket when cdms resets
+        //srp(gPAYLOAD_BUFFER);//skip one apcket when cdms resets
         //gPC.puts("exit_srp\r\n");
         //wait(2);/*timeout to be decided*/
         if(dma)  //something wrong here
         {
      //       TIME_LATEST_I2C_SPEED = FCTN_CDMS_RD_RTC() >> 7;   
             PL_RCV_SC_DATA_COUNTER++;
-            gPAY_SPI->bulkRead_start();//something wrong here
+            gPC.printf("dma activatedi\n\r");
+            gPAY_SPI->bulkRead_start();//something wrong here  //commented 2day
          //     DMA_LED1 = !DMA_LED1; // added by samp
             //gPAY_SPI->bulkRead_pause();
             //gPAY_SPI->bulkRead_resume(&payload_isr_fun);
@@ -57,8 +61,8 @@
        // DMA_LED1 = !DMA_LED1; // added by samp
        //__disable_irq();  
         srp(gPAYLOAD_BUFFER);//skip one apcket when cdms resets
-    
-      uint8_t sd_stat1 = 0;
+
+        uint8_t sd_stat1 = 0;
         if(BAE_HK_SD_DATA_READY==1)
                 {
                 for(int i=0; i<288; i++)
@@ -206,7 +210,7 @@
                 gPAY_SPI->bulkRead_pause();
                  PL_GPIO_3_STATUS=1;//problem with this
                 gFLAGS = gFLAGS | COM_SESSION_FLAG;
-                gSESSION_TIMEOUT.attach(&after_session, 7*COM_SESSION_TIMEOUT);
+                gSESSION_TIMEOUT.attach(&after_session, 14*COM_SESSION_TIMEOUT);
                 gFLAGS = gFLAGS | COM_RX_FLAG;
                 gTOTAL_RAW_BYTES = 0;
                 PUT_RAW_BYTE;
@@ -245,6 +249,7 @@
             RX1M.attach(NULL);
             gPC.printf("RSSI  =  %f",RSSI_MAX);
             /*Thread::wait(2000);*/
+            gFLAGS = gFLAGS & (~COM_RX_FLAG);//CHanged but not tested
             gFLAGS = gFLAGS | COM_MNG_TMTC_RUNNING_FLAG;
 
             
@@ -258,13 +263,14 @@
             }
             gRX_COUNT = 0;
             raw_data_to_tc();
-            gFLAGS = gFLAGS & (~COM_RX_FLAG);
+            //gFLAGS = gFLAGS & (~COM_RX_FLAG);
             
             //Mutex
             gMutex.lock();
             
             if( gTOTAL_VALID_TC > 1 ){
                 gPC.printf("valid TC rx: %u\r\n", gTOTAL_VALID_TC);
+                TC_count += gTOTAL_VALID_TC;
                 //gTOTAL_VALID_TC = 1;
                 if( gTOTAL_VALID_TC < COM_MAX_TC_LIMIT ){
                     // CHECK WEATHER TC LIST HAS MISSING TC OR WEATHER LAST FRAME BIT IS HIGH IN THE LAST PSC-TC
@@ -440,4 +446,9 @@
         gMutex.unlock();
         
     }
-}
\ No newline at end of file
+}
+/*
+void st_up_isr(void const *args){
+    gST_UP->signal_set(ST_UP_SIGNAL);\
+}
+*/
\ No newline at end of file
--- a/adf.h	Sat Feb 10 09:06:26 2018 +0000
+++ b/adf.h	Sun Apr 03 15:35:53 2022 +0000
@@ -11,7 +11,9 @@
 bool buffer_state;
 bool finish_write_data;
 uint8_t signal = 0x00;
-unsigned char bbram_buffer[66]={0x19,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x00,0xFB,0xC2,0x10,0xC0,0x00,0x30,0x31,0x07,0x00,0x01,0x00,/*PA Level Byte*/0x57/*PA Parameters*/,0x08/*GMSK_modulation*/,0x0B,0x37,0x00,0x00,0x40,0x0C,0x00,0x05,0x00,0x00,0x18,0x12,0x34,0x56,0x20,0x10,0xC4,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0xE0,0x00,0x10,0x04,0x00,0x00,0x00,0x00,0x00};
+//unsigned char bbram_buffer[66]={0x19,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x00,0xFB,0xC2,0x10,0xC0,0x00,0x30,0x31,0x07,0x00,0x01,0x00,/*PA Level Byte*/0x57/*PA Parameters*/,0x08/*GMSK_modulation*/,0x0B,0x37,0x00,0x00,0x40,0x0C,0x00,0x05,0x00,0x00,0x18,0x12,0x34,0x56,0x20,0x10,0xC4,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0xE0,0x00,0x10,0x04,0x00,0x00,0x00,0x00,0x00};
+unsigned char bbram_buffer[66]={0x19,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x00,0xF5,0xC2,0x10,0xC0,0x00,0x30,0x31,0x07,0x00,0x01,0x00,/*PA Level Byte*/0x57/*PA Parameters*/,0x08/*GMSK_modulation*/,0x0B,0x37,0x00,0x00,0x40,0x0C,0x00,0x05,0x00,0x00,0x18,0x12,0x34,0x56,0x20,0x10,0xC4,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0xE0,0x00,0x10,0x04,0x00,0x00,0x00,0x00,0x00};
+
 bool data_irq_err=0;
 bool data_err= false;
 unsigned char temp_byte=0x00;
@@ -254,12 +256,6 @@
 
                 gCS_ADF=0;
                 spi.write(0x3B);
-                spi.write(0x27);
-                spi.write(0xFF);
-                secondbyte = spi.write(0xFF);
-                gCS_ADF=1;
-                gCS_ADF=0;
-                spi.write(0x3B);
                 spi.write(0x28);
                 spi.write(0xFF);
                 firstbyte = spi.write(0xFF);
@@ -271,8 +267,10 @@
                 secondbyte = spi.write(0xFF);
                 gCS_ADF=1;
                 
-                COM_ADF_TMP = firstbyte & 0x03;
-                COM_ADF_TMP |= secondbyte & 0xfc;
+                COM_ADF_TMP = (firstbyte & 0xC0) >> 6;
+                //COM_ADF_TMP |= secondbyte & 0xfc;
+                secondbyte = (secondbyte & 0x3F) << 2;
+                COM_ADF_TMP = secondbyte | firstbyte;
                 COM_ADF_TMP = 0.9474*(COM_ADF_TMP - 28) +28.2;
                 
                 gPC.printf("\n\rCOM_ADF_TMP : %d \n",COM_ADF_TMP);
@@ -685,13 +683,13 @@
     configure_adf;
     send_data;
     /*PA MCR level*/;
-        gCS_ADF=0;
-        spi.write(0x1B);
-        spi.write(0x07);
+    gCS_ADF=0;
+    spi.write(0x1B);
+    spi.write(0x07);
    //     spi.write(0x35);
    // spi.write(0x36);
-    spi.write(0x38);  //PA Output = 30.8
-        gCS_ADF=1;
+    spi.write(0x34);  //PA Output = 30.8
+    gCS_ADF=1;
     CMD(CMD_PHY_TX);
     wait_us(2000);
     T.start();
--- a/cdms_rtc.h	Sat Feb 10 09:06:26 2018 +0000
+++ b/cdms_rtc.h	Sun Apr 03 15:35:53 2022 +0000
@@ -80,7 +80,8 @@
     gCS_RTC=1;
     gPC.puts("\n\r rtc initalised \n");
     RTC_INIT_STATUS = 1;
-   FCTN_CDMS_WR_FLASH(5,1);
+   //FCTN_CDMS_WR_FLASH(5,1);
+    WRITE_TO_EEPROM(5,1);
     SPI_mutex.unlock();
 }
  
--- a/cdms_sd.h	Sat Feb 10 09:06:26 2018 +0000
+++ b/cdms_sd.h	Sun Apr 03 15:35:53 2022 +0000
@@ -133,7 +133,8 @@
     if(SD_MNG_SECT != SD_LIB_BLK_CURRENT)
     {
         SD_LIB_BLK_CURRENT = SD_MNG_SECT;
-        FCTN_CDMS_WR_FLASH(3,SD_LIB_BLK_CURRENT);      
+        //FCTN_CDMS_WR_FLASH(3,SD_LIB_BLK_CURRENT);      
+        WRITE_TO_EEPROM(3,SD_LIB_BLK_CURRENT);
     }
     SD_LIB_WRITES = SD_LIB_WRITES%(int)0xFFFF;
     disk_write(buffer,SD_MNG_SECT);
--- a/i2c.h	Sat Feb 10 09:06:26 2018 +0000
+++ b/i2c.h	Sun Apr 03 15:35:53 2022 +0000
@@ -18,18 +18,19 @@
 
 void I2C_busreset()
 {
-    PORTE->PCR[1] &= 0xfffffffb;    //Enabling high slew rates for SDA and SCL lines
-    PORTE->PCR[0] &= 0xfffffffb;    //Enabling high slew rates for SDA and SCL lines
+    PORTE->PCR[24] &= 0xfffffffb;    //Enabling high slew rates for SDA and SCL lines
+    PORTE->PCR[25] &= 0xfffffffb;    //Enabling high slew rates for SDA and SCL lines
     I2C0->C1 &= 0x7f;               //Disabling I2C module
     SIM->SCGC4 &= 0xffffffbf;       //Disabling clock to I2C module
     SIM->SCGC4 |= 0x00000040;       //Enabling clock to I2C module
     I2C0->C1 |= 0x80;               //Enabling I2C module
-    PORTE->PCR[1] |= 0x00000004;    //Disabling high slew rates for SDA and SCL lines
-    PORTE->PCR[0] |= 0x00000004;    //Disabling high slew rates for SDA and SCL lines
+    PORTE->PCR[24] |= 0x00000004;    //Disabling high slew rates for SDA and SCL lines
+    PORTE->PCR[25] |= 0x00000004;    //Disabling high slew rates for SDA and SCL lines
     Thread::wait(1);                //Wait for all I2C registers to be updates to their their values
 }
 bool FCTN_I2C_READ_PL(char *data,int length)       // Returns 0 for success
 {    
+    master.frequency(400000);
     PL_I2C_GPIO = 1;
     read_ack = master.read(addr_pl|1,data,length);
     Thread::wait(1);        //as per tests Thread::wait not required on master side. But its safe to give 1ms 
@@ -67,7 +68,8 @@
 }
 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+1);//address to be defined in payload    
+    master.frequency(400000);
+    write_ack = master.write(addr_pl|0x00,data2,tc_len2);//address to be defined in payload    
     Thread::wait(1);    //As per the tests Thread::wait is not required on master side but its safe to give 1ms
     pdirw1=PTE->PDIR;
     uint8_t i2c_count = 0;
@@ -100,8 +102,9 @@
 bool FCTN_I2C_READ(char *data,int length)       // Returns 0 for success
 {   
  //   gPC.printf("i2C_rd\r\n");
+    master.frequency(400000);
     CDMS_I2C_GPIO = 1;
-    read_ack = master.read(addr_bae|1,data,length);
+    read_ack = master.read(addr_bae|1,data,length); 
     Thread::wait(1);        //as per tests Thread::wait not required on master side. But its safe to give 1ms 
     pdirr1=PTE->PDIR;
     uint8_t i2c_count = 0;    
@@ -123,11 +126,10 @@
             read_ack = 1;
         }
     }
-    else if (read_ack == 1)
+    if (read_ack == 1)
     {
         I2C_busreset();
-        CDMS_I2C_ERR_BAE_COUNTER++;
-        
+        CDMS_I2C_ERR_BAE_COUNTER++;     
     }
  //  gPC.printf("end_12c_RD_\r\n");
     CDMS_I2C_GPIO = 0;
@@ -138,8 +140,9 @@
 bool FCTN_I2C_WRITE(char *data,int tc_len2)       // Returns 0 for success
 {    
    //  gPC.printf("i2C_wr\r\n");
+    master.frequency(400000);
     CDMS_I2C_GPIO = 1;
-    write_ack = master.write(addr_bae|0x00,data,tc_len2);    
+    write_ack = master.write(addr_bae|0x00,data,tc_len2); 
     Thread::wait(1);   //As per the tests Thread::wait is not required on master side but its safe to give 1ms
     pdirw1=PTE->PDIR;
     uint8_t i2c_count = 0;
--- a/main.cpp	Sat Feb 10 09:06:26 2018 +0000
+++ b/main.cpp	Sun Apr 03 15:35:53 2022 +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,8 @@
 #include "DefinitionsAndGlobals.h"
 #include "crc.h"
 #include "i2c.h"
-//#include "EEPROM.h"
-#include "Flash.h"
+#include "EEPROM.h"
+//#include "Flash.h"
 #include "COM_SND_TM_functions.h"
 #include "cdms_rtc.h"
 #include "COM_SND_TM.h"
@@ -61,7 +61,7 @@
     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;
@@ -71,10 +71,13 @@
     // COMMON SPI
     spi.format(8,0);
     spi.frequency(500000);
-    FLASH_INI();        
+
+    //FLASH_INI();   
+    INIT_EEPROM();  //changed Mar 2018   
+    RESET_WATCHDOG();  
     if ((BAE_STATUS & 0x03) == 0x03)
     {SW_OFF_BAE();}
-    uint8_t sd_stat_temp = SD_STATUS & 0x03;                        //Joel
+    uint8_t sd_stat_temp = SD_STATUS & 0x03;                       
     SD_CARD_fromuC_ENA3 = 0; 
     SD_CARD_fromISO_ENA4 = 0;
     SD_SW_EN_DS = 0; 
@@ -89,16 +92,45 @@
     gCS_RTC = 1;
     gCS_ADF = 1;
     
-    //CDMS_RTC_DISABLE = 1;
+    FCTN_CDMS_SD_INIT();/* sd card initialization*/
+    
+    CDMS_RTC_DISABLE = 1;
+    //RTC_INIT_STATUS = test_buffer
     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*/
+        
+        //INIT_40mins
+                
+        /*BAE_SW_EN_DS = 0;   //DISABLE BAE
+        SD_SW_EN_DS = 0;    //DISABLE SD
+        uint64_t ini_time = FCTN_CDMS_RD_RTC();
+        uint32_t T_check_ini = (((ini_time >> 7)& 0x00000FC0)>>6);
+        uint32_t T_diff = T_check_ini; 
+        
+        expired=0;
+        Ticker Sftw_Timer; 
+        gPC.printf("\rGoing to start Ticker\n");
+        Sftw_Timer.attach(&int_acq1, 2500.0);
+        
+        while (T_check_ini - T_diff < T_INI)
+        {
+            RESET_WATCHDOG();
+            wait_ms(10000);
+            ini_time = FCTN_CDMS_RD_RTC();
+            T_check_ini = (((ini_time >> 7)& 0x00000FC0)>>6);
+            gPC.printf("ini_time %d",T_check_ini);
+            if (expired == 1)
+                break;
+        }
+        BAE_SW_EN_DS = 1;   //ENABLE BAE
+        SD_SW_EN_DS = 1;    //ENABLE SD*/
+    }
+    
     if(sd_stat_temp ==  DEVICE_DISABLED)
     {
         SD_STATUS = sd_stat_temp;
@@ -109,19 +141,27 @@
     {
         SD_STATUS = sd_stat_temp;
     }
-   // INIT_EEPROM(); // added by samp
+
     CDMS_RESET_COUNTER+=1;
-    FCTN_CDMS_WR_FLASH(7,CDMS_RESET_COUNTER);
+    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);
+    //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];
+    uint8_t test[512];
   
-    //for(int i =0; i<512; i++)
-    //    test[i] = 0;
-    //disk_write(test,80000);
+    for(int i =0; i<512; i++)
+        test[i] = 0;
+    test[2] = 0xF;
+    test[3] = 0xA0;
+    test[10] = 0xF;
+    test[11] = 0xA0;
+    test[18] = 0xF;
+    test[19] = 0xA0;
+
+    disk_write(test,80000);
 
     SD_MNG_SECT = SD_LIB_BLK_CURRENT;
 
@@ -145,8 +185,8 @@
     gCOM_MNG_TMTC_THREAD->set_priority(osPriorityRealtime);
     
     gHK_THREAD = new Thread(FCTN_CDMS_HK_MAIN);
-   // gHK_THREAD->set_priority(osPriorityHigh);
-     gHK_THREAD->set_priority(osPriorityNormal);
+    //gHK_THREAD->set_priority(osPriorityHigh);
+    gHK_THREAD->set_priority(osPriorityNormal);
     
     gSCIENCE_THREAD = new Thread(SCIENCE_FUN);
     gSCIENCE_THREAD->set_priority(osPriorityHigh);
@@ -158,18 +198,17 @@
     master.frequency(400000);
     
     HK_counter = new RtosTimer(hk_isr, osTimerPeriodic,(void * )NULL);
-    gHK_THREAD->signal_set(HK_SIGNAL);
-   HK_counter->stop();
-   wait_ms(0.1);
-   HK_counter->start(10000);
-  // HK_counter->start(10000);
-    
-    sys_reset_cdms_timer = new RtosTimer(sys_pwr_reset, osTimerPeriodic, (void * )NULL);
-    sys_reset_cdms_timer->start(cdms_reset_timeout);
-
+    //gHK_THREAD->signal_set(HK_SIGNAL);
+    HK_counter->stop();
+    wait_ms(0.1);
+    HK_counter->start(10000);
+//   HK_counter->start(10000);
+//
     //PL_wo_dma = new RtosTimer(payload_isr_fun_dma, osTimerPeriodic,(void * )NULL);
+    //gSCIENCE_THREAD->signal_set(SCIENCE_SIGNAL);
+    //PL_wo_dma->stop();
+    //wait_ms(0.1);
     //PL_wo_dma->start(6000);
-    //gSCIENCE_THREAD->signal_set(SCIENCE_SIGNAL);
     
     /*Timeout flipper;
     flipper.attach(BAE_TC,31);*/