init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Revision:
31:c84fc6d8eaa3
Parent:
29:059fc7324328
Child:
32:dff4858bdf37
--- a/main.cpp	Mon Dec 24 02:01:38 2018 +0000
+++ b/main.cpp	Tue Jan 01 15:44:24 2019 +0000
@@ -162,18 +162,17 @@
 bool memoryIntegrityCheck() {
     bool pass = true;
     //check memory is init correct
-    if (RET_memTest != 12345) {
-        pass = false;
-    }
-    //Check clocks match and are in range
-    if (RET_RTCunixtime != RET_RTCunixtime || RET_RTCunixtime < 1545412457) {
-        pass = false;
-    }
+    if (RET_memTest != 12345) { pass = false; DEBUG("Mem fail 1\n"); }
+    //Check clocks match
+    if (RET_RTCunixtime != RET_RTCunixtime_bu) { pass = false; DEBUG("Mem fail 2\n"); }
+    //Check clocks are in range (only if we have settings)
+    if (RET_haveSettings && RET_RTCunixtime < 1545412457) { pass = false; DEBUG("Mem fail 3\n"); }
     return pass;
 }
 void dumpSettings() {
     DEBUG("RET_memTest:%d \n",RET_memTest);
     DEBUG("RET_RTCunixtime:%u \n",RET_RTCunixtime);
+    DEBUG("RET_RTCunixtime_bu:%u \n",RET_RTCunixtime_bu);
     DEBUG("RET_setting_firmware:%d \n",RET_setting_firmware);
     DEBUG("RET_state:%d \n",RET_state);
     DEBUG("RET_setting_location_mode:%d \n",RET_setting_location_mode);
@@ -228,7 +227,7 @@
 void RTCtick() {
     //YOU MUST NOT CALL ANY OTHER FUNCTIONS OR DEBUG FROM INSIDE HERE!!! OR IT LOCKS UP THE DEVICE, just change vars
     RET_RTCunixtime += 1;
-    RET_RTCunixtime_bu += 1;
+    RET_RTCunixtime_bu = RET_RTCunixtime;
     GLOBAL_RTCunixtime = RET_RTCunixtime;
     
     //button logic - check for hold
@@ -439,7 +438,7 @@
         
         //FAILUREMODE need to be checking these against checksums
         char changed;
-        if(TEMP_b != 0) { RET_RTCunixtime = TEMP_b;                                changed = 'Y'; } else { changed = 'N'; critical_fail_count++; }; DEBUG("RET_RTCunixtime:%u..%c\n",RET_RTCunixtime,changed);
+        if(TEMP_b != 0) { RET_RTCunixtime = TEMP_b; RET_RTCunixtime_bu = TEMP_b;   changed = 'Y'; } else { changed = 'N'; critical_fail_count++; }; DEBUG("RET_RTCunixtime:%u..%c\n",RET_RTCunixtime,changed);
         if(TEMP_c != -1) { RET_setting_firmware = TEMP_c;                          changed = 'Y'; } else { changed = 'N'; RET_setting_firmware = -1;}; DEBUG("RET_setting_firmware:%d..%c\n",RET_setting_firmware,changed);
         if(TEMP_d != -1) { setState(TEMP_d);                                       changed = 'Y'; } else { changed = 'N'; critical_fail_count++; }; DEBUG("RET_state:%d..%c\n",RET_state,changed);
         if(TEMP_e != -1) { RET_setting_location_mode = TEMP_e;                     changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_mode:%d..%c\n",RET_setting_location_mode,changed);
@@ -531,10 +530,13 @@
             }
         } else {
             //FAILUREMODE modem failed to register on network
+            DEBUG("NET REGISTER FAIL");
             pass = false;
         }
     } else {
         //FAILUREMODE Modem failed to turn on  
+        DEBUG("MODEM ON FAIL");
+        LED1blink(200,50);
         pass = false;
     }
     
@@ -572,6 +574,8 @@
                 }
             }
         }
+    } else {
+        LED1blink(200,50);   
     }
     //RESETS
     RET_motionTriggeredInTXInterval = 0;
@@ -586,10 +590,8 @@
     Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
     if (modem.on()) {
         char locString[70];
-        if (RET_setting_location_accuracy > 0) { 
-            memcpy(locString, modem.getLocation(RET_setting_location_accuracy, RET_setting_location_timeout), 70);
-            //DEBUG("locString:%s-\n",locString);
-        }
+        memcpy(locString, modem.getLocation(RET_setting_location_accuracy, RET_setting_location_timeout), 70);
+        //DEBUG("locString:%s-\n",locString);
         //SEND DATA
         if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
             int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
@@ -605,6 +607,8 @@
                 }
             }
         }
+    } else {
+        LED1blink(200,50);   
     }
     //RESETS
     RET_motionTriggeredInTXInterval = 0;
@@ -629,6 +633,8 @@
                 if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; }
             }
         }
+    } else {
+        LED1blink(200,50);   
     }
     //RESETS
     RET_motionTriggeredInTXInterval = 0;
@@ -646,9 +652,10 @@
             if (setup()) {
                 // All good
             } else {
+                LED1blink(200,50);   
                 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + (24*3600)); //24hrs
                 setState(STATE_DORMANT);
-                DEBUG("SETSTATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);
+                DEBUG("SETUP FAILED:DORMANT until %u\n",RET_eventTime_wakeFromDormant);
             }
             break;
         case STATE_NORMAL :
@@ -684,7 +691,6 @@
             }
             if (strlen(RET_activityData) > 130) { run_activity_tx = true; }
             if (run_activity_tx) { DEBUG("ACTIVITY TX...\n"); event_activity_tx();}
-            
             break;
         case STATE_DORMANT :
             if (RET_RTCunixtime >= RET_eventTime_wakeFromDormant) {
@@ -693,7 +699,6 @@
                 } else {
                     setState(STATE_SETUP); 
                 }
-                
                 DEBUG("WAKING UP FROM DORMANT\n");
             }
             DEBUG("STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);
@@ -750,7 +755,7 @@
 int main() {
     //INIT
     turnOffEverything();
-    if (!memoryIntegrityCheck()) { RET_coldBoot = 1; LED1on(5000);}
+    if (!memoryIntegrityCheck()) { RET_coldBoot = 1;}
     RTCticker.attach(&RTCtick, 1.0);
     button.fall(&buttonPress); //does this affect power?
     button.rise(&buttonRelease);
@@ -762,6 +767,7 @@
             case 0x00000001  :
                 DEBUG("0x%08x. Hard Reset ST:%d\n",NRF_POWER->RESETREAS, RET_state);
                 RET_coldBoot = 1;
+                dumpSettings();
             break;
             case 0x00000002  :
                 DEBUG("Watchdog ST:%d\n",RET_state);
@@ -773,7 +779,6 @@
         NRF_POWER->RESETREAS = 0xffffffff;
     }
     
-    //dumpSettings();
     
     //CHECK FOR FIRST BOOT
     if (RET_coldBoot == 1) { factoryReset(); dumpSettings(); }