init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Revision:
37:505ef618f06c
Parent:
36:8e359069192b
Child:
38:476a9b5629a1
--- a/main.cpp	Tue Jan 15 11:19:41 2019 +0000
+++ b/main.cpp	Wed Jan 16 21:27:28 2019 +0000
@@ -3,7 +3,9 @@
 //------------------------------------------------------------------------------
 //DEFINES
 //------------------------------------------------------------------------------ 
-#define FW_VERSION 2
+#define FW_VERSION          2
+#define SKU                 "GPSPLUS"
+#define HW_MAJORREVISION    "001"
 
 //MODES
 #define USE_NRF_TEMP_SENSOR                 1
@@ -24,7 +26,7 @@
 #define DEFAULT_BEACON_INTERVAL_SECONDS     10
 
 //------------------------------------------------------------------------------
-//FUNCTION PROTOTYPES
+//FUNCTION PROTOTYPES - NEED TO ADD ALL OF THE MISSING ONES
 //------------------------------------------------------------------------------ 
 void mainStateEngine(void);
 void selftest(void);
@@ -109,12 +111,12 @@
 //PERIPHERALS
 //------------------------------------------------------------------------------ 
 //BLE myble;
-WatchdogTimer watchdog(300.0); //Do not set to less than 4500ms or can cause issues with softdevice
+WatchdogTimer watchdog; //Do not set to less than 4500ms or can cause issues with softdevice
 void watchdogKick() {watchdog.kick();}
 SI7060 si7060(PN_I2C_SDA, PN_I2C_SCL);
 LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK);
 Modem modem(PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
-LowPowerTicker RTCticker; //no impact on power consumption
+LowPowerTicker RTCticker;
 
 //------------------------------------------------------------------------------
 //THREAD SEMAPHORES
@@ -202,14 +204,12 @@
 }
 float getTemperature() {
     float temperature;
-
     if (USE_NRF_TEMP_SENSOR) {
         //INTERNAL NRF52 TEMP SENSOR
         temperature = nrfTemperature();
     } else {
         temperature = si7060.getTemperature(); //currently disabled because its causing a high current 450ua sleep, most likely due to sensor not sleeping correctly, or i2c sleep issue
     }
-    
     return temperature;  
 }
 void addToExceptionString(char* value) {
@@ -276,7 +276,6 @@
     GLOBAL_accel_healthy = false;
     GLOBAL_motionStopFlagTriggered = false;
     memset(GLOBAL_exceptionString,0x00,sizeof(GLOBAL_exceptionString));
-    if (DEBUG_ON) {memset(GLOBAL_debug_buffer,0x00,sizeof(GLOBAL_debug_buffer));}
 }
 void setDefaults() {
     //IDENTITY 
@@ -480,6 +479,16 @@
             RET_haveSettings = true;
             GLOBAL_needToConfigureLis3dh = true;
             RET_SettingsGotAt = RET_RTCunixtime;
+            
+            //check for firmware update
+            if (RET_setting_firmware != FW_VERSION) {
+                read_app_data_from_flash(&app_data);
+                app_data.target_firmware_version = RET_setting_firmware;
+                write_app_data_to_flash(&app_data);
+                ThisThread::wait(500);
+                system_reset();
+            }
+            
             return true;
         } else {
             if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "CRITICAL FAILS:%d",critical_fail_count);debug_exe();}
@@ -723,12 +732,8 @@
                     break;
             }
             if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { run_location_tx = true; }
-            if (run_location_tx) { DEBUG("INTERVAL LOC TX...\n",false); event_location_tx(); }
+            if (run_location_tx) { event_location_tx(); }
             
-            //if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
-            //Location Failsafe timer catchall
-            //if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { run_location_tx = true; }
-            //if (run_location_tx) { DEBUG("INTERVAL LOC TX...\n"); event_location_tx(); }
             
             //ACTIVITY EVENT
             bool run_activity_tx = false;
@@ -784,8 +789,8 @@
         {
             DEBUG("STATE:BUTTONHOLD\n",false);
             if (RET_state_prev == STATE_NORMAL) {
+                RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (72*3600)); //72hrs
                 setState(STATE_DORMANT);
-                RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (48*3600)); //48hrs
                 DEBUG("TURNING OFF\n",false);
                 DEBUG("STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);
                 LED1on(5000);
@@ -815,6 +820,7 @@
     LED1on(500);
     
     //INIT
+    watchdog.configure(300.0);
     LED1off();
     RTCticker.attach(&RTCtick, 1.0);
     button.fall(&buttonPress);
@@ -862,12 +868,15 @@
         watchdogKick(); //only need this if we're in while loop
         
         //INIT
-        resetGlobals(); // could move this into state engine switches to save actions on each loop
+        resetGlobals(); // could move this into state switches to save actions on each loop
         GLOBAL_wakeTime = RET_RTCunixtime;
         
         //MAIN LOGIC
-        DEBUG("STATE:%d, HAVESETTINGS:%d, MOTION: %d, RTC:%u, BOOTAT:%u, LOC:%u, LOCFS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_motionState, RET_RTCunixtime,RET_SetupRunAt,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);
-        DEBUG("ACTIVITY:%s\n",RET_activityData);
+        //DEBUG("STATE:%d, HAVESETTINGS:%d, MOTION: %d, RTC:%u, BOOTAT:%u, LOC:%u, LOCFS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_motionState, RET_RTCunixtime,RET_SetupRunAt,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);
+        //DEBUG("ACTIVITY:%s\n",RET_activityData);
+        if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "STATE:%d, HAVESETTINGS:%d, MOTION: %d, RTC:%u, BOOTAT:%u, LOC:%u, LOCFS:%u, ACT:%u \n", RET_state, RET_haveSettings, RET_motionState, RET_RTCunixtime,RET_SetupRunAt,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);debug_exe();}
+        if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ACTIVITY:%s\n",RET_activityData);debug_exe();}
+
         mainStateEngine();
                 
         //LOG FIRST RUN - BOOTLOADER COMMS