Team Fox / Mbed 2 deprecated BAE_QM_MAR9

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of workinQM_5thJan_azad by Team Fox

Revision:
33:76f2b8735501
Parent:
27:61c856be467e
Child:
34:1b41c34b12ea
Child:
39:670133e7ffd8
--- a/main.cpp	Sat Jul 02 14:06:34 2016 +0000
+++ b/main.cpp	Mon Jul 04 04:29:59 2016 +0000
@@ -10,6 +10,21 @@
 #define batt_heat_low 20
 #define print 1
 
+#define DISABLE_WDOG 0;
+
+void kick_WDOG()
+{
+    SIM->SRVCOP=0x55;
+    SIM->SRVCOP=0xAA;
+    printf("\n\rkicked");
+}
+
+void init_WDOG()
+{
+    SIM->COPC=0x0C;  
+    kick_WDOG();
+}
+
 DigitalOut time_wdog(PIN68);// for determining the time between code
 
 //**********************************************GLOBAL RTOS TIMER*********************************************************//
@@ -21,7 +36,20 @@
 RtosTimer *HTR_CYCLE=NULL;
 RtosTimer *HTR_DLY=NULL;
 
-extern DigitalOut EN_BTRY_HT;
+//UPTIME  TIMERS
+Timer BAE_uptime;
+Timer I2C_last;
+
+void RETURN_UPTIME(float time, uint8_t *day,uint8_t *hour,uint8_t *min)
+{
+    *day = time/(3600*24);
+    time = (int)time % (3600*24);
+    *hour = time/(3600);
+    time = (int)time % (3600);
+    *min = time/60;    
+}
+
+extern DigitalOut BTRY_HTR_ENABLE;
 
 uint8_t HTR_CYCLE_COUNTS=0;         //Count of heater cycles
 uint8_t HTR_CYCLE_START_DLY=0;      //EPS_HTR_DLY_TIMER timer duration in minutes
@@ -42,7 +70,7 @@
 
 void FCTN_EPS_HTR_CYCLE(void const *arg)
 {
-    EN_BTRY_HT = 1;//assuming its active high check
+    BTRY_HTR_ENABLE = 1;//assuming its active high check
     //pc.printf("\n\r on kar diya");
     //pc.printf("\n\r FCTN_EPS_HTR_CYCLE");
     //tim.reset();
@@ -53,7 +81,7 @@
 
 void FCTN_EPS_HTR_DLY(void const* arg)
 {
-    EN_BTRY_HT = 1;
+    BTRY_HTR_ENABLE = 1;
     HTR_CYCLE_COUNTER = 0;
     HTR_OFF->start(((int)HTR_ON_DURATION*60*1000));
     //pc.printf("\n\r on kar diya");
@@ -64,7 +92,7 @@
 
 void FCTN_EPS_HTR_OFF(void const *arg)
 {
-    EN_BTRY_HT = 0;
+    BTRY_HTR_ENABLE = 0;
     //tim.stop();
     //pc.printf("\n\r off kar diya");
     //pc.printf("\n\r the timer value is %f",tim.read());
@@ -80,11 +108,12 @@
 //====================================================================================================
 
 //fctn to pass the data to bcn long type
-uint8_t CDMS_HK_data[134]; 
+//uint8_t CDMS_HK_data[134]; now implemented as LONG_HK_data[0][134];
+uint8_t LONG_HK_data[2][134];
 void FCTN_CDMS_HK_TC(uint8_t tc[])
 {
     for(int i=0;i<134;i++)
-        CDMS_HK_data[i] = tc[i];    
+        LONG_HK_data[0][i] = tc[i];    
 }
 
 uint8_t BAE_STANDBY=0x00;// as Bcn Acs Eps last three bits BAE order , 1 refer to switch off.
@@ -127,7 +156,7 @@
 uint8_t ACS_DETUMBLING_ALGO_TYPE = 0;
 uint8_t ACS_ATS_ENABLE = 1;
 uint8_t ACS_DATA_ACQ_ENABLE = 1;
-uint8_t ACS_STATE = 7;
+uint8_t ACS_STATE = 8;
 
 extern uint16_t ACS_MM_X_COMSN;
 extern uint16_t ACS_MM_Y_COMSN;
@@ -143,7 +172,7 @@
 extern void FCTN_BCN_FEN(void const *args);
 extern void LONG_BCN_DATA(uint8_t* );
 extern uint16_t gencrc16_for_me();//for calculating the crc of baehk data before sendingkk as including crc causes problem
-extern DigitalOut EN_BTRY_HT;
+extern DigitalOut BTRY_HTR_ENABLE;
 
 
 Timeout timeout_bcn;
@@ -154,8 +183,9 @@
 uint8_t EPS_INIT_STATUS = 0;
 uint8_t EPS_BATTERY_GAUGE_STATUS = 0;
 uint8_t EPS_MAIN_STATUS = 0;
-uint8_t EPS_BATTERY_TEMP_STATUS = 0;
+uint8_t EPS_BTRY_TMP_STATUS = 0;/// to be diss
 uint8_t EPS_STATUS = 7; //invalid status
+uint8_t EPS_BTRY_HTR = 0;
 
 uint8_t EPS_BTRY_HTR_AUTO = 1;
 extern uint8_t EPS_SOC_LEVEL_12;
@@ -163,6 +193,10 @@
 extern uint8_t EPS_BAT_TEMP_LOW;
 extern uint8_t EPS_BAT_TEMP_HIGH;
 extern uint8_t EPS_BAT_TEMP_DEFAULT;
+float EPS_BTRY_TMP_AVG;
+extern DigitalOut BTRY_HTR_ENABLE;
+
+//extern void FCTN_BATTERYGAUGE_MAIN(float*Battery_parameters);
 
 
 //**********************GLOBAL DECLARATIONS********************************//
@@ -177,7 +211,8 @@
 
 //BAE                                           new hk structure- everything has to changed based on this
 char BAE_chardata[74];
-uint8_t BAE_HK_data[134];  
+// uint8_t LONG_HK_data[2][134];
+//uint8_t BAE_HK_data[134];  now known as LONG_HK_data[1][i];  
 uint8_t BAE_RESET_COUNTER;// INITIALIZATION or do you want to initialize evertime the main is reset? or is it same     
 uint16_t BAE_I2C_COUNTER = 0;//change/apply
 //uint8_t BAE_STANDBY=0x00;// as Bcn Acs Eps last three bits BAE order , 1 refer to switch off.
@@ -598,61 +633,81 @@
 
 //***************************************************EPS THREAD***********************************************//
 
+
 void F_EPS()
 {
-    //time_wdog = 1; 
-    t_eps.start();
-    EPS_MAIN_STATUS = 1; // Set EPS main status
-    FLAG();
-    EPS_MAIN_COUNTER+=1;
-    //if print
-        pc.printf("\n\rEntered EPS   %f\n",t_start.read());
-    //#endif
-    
-    
-    FCTN_BATT_TEMP_SENSOR_MAIN(actual_data.Batt_temp_actual);
-    //#if print
-        pc.printf("\n\r Battery temperature %f %f" ,actual_data.Batt_temp_actual[0], actual_data.Batt_temp_actual[1]);
-    //#endif
-    EPS_BATTERY_TEMP_STATUS = 1;          //set EPS_BATTERY_TEMP_STATUS
-    if(EPS_BTRY_HTR_AUTO == 1)
+      float eps_btry_temp;
+      pc.printf("\n\rEntered EPS   %f\n\r",t_start.read());
+      EPS_MAIN_STATUS = 1; // Set EPS main status
+      EPS_MAIN_COUNTER++;
+      FCTN_BATT_TEMP_SENSOR_MAIN(actual_data.Batt_temp_actual);
+      pc.printf("Battery temperature %f %f\n\r" ,actual_data.Batt_temp_actual[0], actual_data.Batt_temp_actual[1]);
+      EPS_BTRY_TMP_AVG = ( actual_data.Batt_temp_actual[0] + actual_data.Batt_temp_actual[1] )/2.0;
+      if(abs(actual_data.Batt_temp_actual[0] - actual_data.Batt_temp_actual[1]) > 10)
+      {
+          EPS_BTRY_TMP_STATUS = 0;          //clear EPS_BTRY_TMP_STATUS
+          eps_btry_temp = (float) EPS_BAT_TEMP_DEFAULT;
+      }
+      else   
+      {
+          EPS_BTRY_TMP_STATUS = 1;          //set EPS_BTRY_TMP_STATUS
+          eps_btry_temp = EPS_BTRY_TMP_AVG;
+      }
+         
+      if(EPS_BTRY_HTR_AUTO == 0)
+          EPS_STATUS = 1;                   //EPS_BATTERY_HEATER_DISABLED
+      else if( EPS_BTRY_TMP_STATUS == 0 )
+      {
+          BTRY_HTR_ENABLE = 0;
+          EPS_BTRY_HTR = 0;
+          EPS_STATUS = 0;                   //Set EPS_ERR_BATTERY_TEMP
+      }
+      else
+      {
+            if( EPS_BTRY_HTR == 1 && eps_btry_temp >= EPS_BAT_TEMP_HIGH )
+            {
+              BTRY_HTR_ENABLE = 0;
+              EPS_BTRY_HTR = 0;
+              EPS_STATUS = 4;               //Set EPS_BATTERY_HEATER_OFF
+            }
+            else if( EPS_BTRY_HTR == 0 && eps_btry_temp < EPS_BAT_TEMP_LOW )
+            {
+              BTRY_HTR_ENABLE = 1;
+              EPS_BTRY_HTR = 1;
+              EPS_STATUS = 5;               //Set EPS_BATTERY_HEATER_ON
+            }
+      }  
+ 
+        unsigned short statusreg = read(REG_STATUS);
+        if( EPS_BATTERY_GAUGE_STATUS == 0 ) reset();
+        if( read(REG_STATUS)  & 0x0100 == 0x0100 )     //checking if Reset Indicator bit is set
         {
-            if((actual_data.Batt_temp_actual[0] < batt_heat_low) && (actual_data.Batt_temp_actual[1] < batt_heat_low)) // to confirm
-                {
-                    EN_BTRY_HT = 1;    //turn on battery heater //earlier named as batt_heat
-                }
-            else
-                {
-                    EN_BTRY_HT = 0;     //turn off battery heater
-                }
-        } 
-    else if(EPS_BTRY_HTR_AUTO == 0)
-        {
-            EPS_STATUS = 1;//EPS_STATUS = EPS_BATTERY_HEATER_DISABLED
-        }
-    FCTN_BATTERYGAUGE_MAIN(actual_data.Batt_gauge_actual);
-    if (actual_data.Batt_gauge_actual[1] == 200)   //data not received
-        {
-            actual_data.power_mode = 1;
-            EPS_BATTERY_GAUGE_STATUS = 0;           //clear EPS_BATTERY_GAUGE_STATUS
-        }
-    else
-        {
-            FCTN_EPS_POWERMODE(actual_data.Batt_gauge_actual[1]);            //updating power level 
-            EPS_BATTERY_GAUGE_STATUS = 1;           //set EPS_BATTERY_GAUGE_STATUS
-        }
-    FCTN_HK_MAIN();
-    FCTN_APPEND_HKDATA();
-    minMaxHkData();
-    EPS_MAIN_STATUS = 0; // clear EPS main status
-    t_eps.stop();
-    #if print
-        printf("\n\r the time for eps is %f",(float)t_eps.read_ms()); 
-    #endif
-    t_eps.reset();
-
+            printf("REG_STATUS = %d\r\n",read(REG_STATUS));    
+            FCTN_BATTERYGAUGE_INIT();
+        }    
+        int BTG_MAIN_FLAG = FCTN_BATTERYGAUGE_MAIN(actual_data.Batt_gauge_actual, eps_btry_temp);
+        if( BTG_MAIN_FLAG == 0 )                      //Data not received
+            {
+              actual_data.power_mode = 1;
+              EPS_BATTERY_GAUGE_STATUS = 0;           //clear EPS_BATTERY_GAUGE_STATUS 
+            }
+        else
+            {
+              FCTN_EPS_POWERMODE(actual_data.Batt_gauge_actual[1]);            //updating power level 
+              EPS_BATTERY_GAUGE_STATUS = 1;           //set EPS_BATTERY_GAUGE_STATUS
+            }
+     
+        FCTN_HK_MAIN();
+       // printf("\n\r here");
+        FCTN_EPS_HANDLE_HW_FAULTS();
+        FCTN_EPS_HANDLE_CDMS_FAULT();
+        FCTN_APPEND_HKDATA();
+        minMaxHkData();
+        //printf("\n\r here");  
+        EPS_MAIN_STATUS = 0; // clear EPS main status 
+ 
 }
-
+ 
 
 //**************************************************BCN THREAD*******************************************************************//
 
@@ -673,6 +728,8 @@
             //wait_us(200);// se if it can be changed//@Lakshya:It can't be changed :)
             wait_us(300);
             BAE_MNG_I2C_STATUS =1 ;
+            I2C_last.reset();
+            I2C_last.start();
             FLAG();
             //earlier BAE_I2CRX_STATUS=1;
             #if print
@@ -701,7 +758,7 @@
             else if( slave.receive() == 1)                                     // slave writes to master
                 {
                     BAE_I2C_COUNTER++;                                                      //////////edited
-                    if(data_send_flag = 'h') //to be renamed as BAE_I2C_STATUS
+                    if(data_send_flag == 'h') //to be renamed as BAE_I2C_STATUS
                         {
                             irpt_2_mstr =1;                                     //wait till cdms code is changed
                             FCTN_APPEND_HKDATA();
@@ -898,7 +955,7 @@
         printf("\n\r in scheduler");
     #endif
    /*if keeping thish many cases creates a problem then make 3 seperate flagvariable i.e bae_standby_acs so on that will make it easy.!!!*/
-    if(schedcount == 7)                         //to reset the counter
+    if(schedcount == 13)                         //to reset the counter
         schedcount = 1;
     if(schedcount%1==0)
         {
@@ -910,7 +967,7 @@
                 }
     
         }
-    if(schedcount%2==0)
+    if(schedcount%4==0)
         {   
             if( BAE_STANDBY!=0x01 && BAE_STANDBY!=0x03 && BAE_STANDBY!=0x05 && BAE_STANDBY!=0x07)
                 {
@@ -919,7 +976,7 @@
                 }
             //time_wdog = 0;
         }
-    if(schedcount%3==0)
+    if(schedcount%6==0)
         {
             if(BAE_STANDBY!=0x04 && BAE_STANDBY!=0x05 && BAE_STANDBY!=0x06 && BAE_STANDBY!=0x07)
             //  time_wdog = 0;
@@ -1028,9 +1085,9 @@
     else if(EPS_BATTERY_GAUGE_STATUS==0)          // Clear
         BAE_STATUS &= 0xFFFDFFFF;
 
-    if (EPS_BATTERY_TEMP_STATUS==1)             // Set EPS_BATTERY_TEMP_STATUS
+    if (EPS_BTRY_TMP_STATUS==1)             // Set EPS_BATTERY_TEMP_STATUS
         BAE_STATUS |= 0x00080000;
-    else if(EPS_BATTERY_TEMP_STATUS==0)       // Clear
+    else if(EPS_BTRY_TMP_STATUS==0)       // Clear
         BAE_STATUS &= 0xFFF7FFFF;
 
     if (EPS_STATUS==0)
@@ -1103,6 +1160,8 @@
 
 int main()
 {
+    //BAE_uptime.reset();
+    BAE_uptime.start();
     //time_wdog = 1;
     pc.printf("\n\r BAE Activated. Testing Version 1.2 \n");
     //FLASH_INI();
@@ -1119,7 +1178,7 @@
     irpt_4m_mstr.rise(&FCTN_I2C_ISR);
     
     RtosTimer t_sc_timer(T_SC,osTimerPeriodic);               // Initiating the scheduler thread
-    t_sc_timer.start(10000);
+    t_sc_timer.start(5000);
     t_start.start();
     #if print
         pc.printf("\n\rStarted scheduler %f\n\r",t_start.read());