init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Revision:
50:a94e70c00fff
Parent:
49:15ddd84ec9fa
Child:
51:9078e6928412
diff -r 15ddd84ec9fa -r a94e70c00fff main.cpp
--- a/main.cpp	Thu Feb 07 00:34:30 2019 +0000
+++ b/main.cpp	Thu May 16 12:31:44 2019 +0000
@@ -109,9 +109,27 @@
 //------------------------------------------------------------------------------
 // LOW LEVEL FUNCS
 //------------------------------------------------------------------------------
-void nrf_configureGPIOForSleep(){
-    nrf_gpio_cfg_input(PN_SPI_MOSI, NRF_GPIO_PIN_NOPULL);
+void nrf_configureForSleep(){
+    //Disable SPI pins to reduce power
+    //nrf_gpio_cfg_input(PN_SPI_MOSI, NRF_GPIO_PIN_NOPULL);  //Don't need this one
     nrf_gpio_cfg_input(PN_SPI_MISO, NRF_GPIO_PIN_NOPULL);
+    
+    //disable modem control lines
+    nrf_gpio_cfg_input(PN_GSM_PWR_KEY, NRF_GPIO_PIN_NOPULL);
+    nrf_gpio_cfg_input(PN_GSM_WAKE_DISABLE, NRF_GPIO_PIN_NOPULL);
+    
+    //ERRATA 89, shut down i2c channels properly
+    //TWI0
+    //NRF_TWI0->ENABLE=0;//TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
+    //NRF_TWI0->ENABLE=1;//TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
+    *(volatile uint32_t *)0x40003FFC = 0;
+    *(volatile uint32_t *)0x40003FFC;
+    *(volatile uint32_t *)0x40003FFC = 1;
+    //TWI1
+    //NRF_TWI1->ENABLE=TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
+    *(volatile uint32_t *)0x40004FFC = 0;
+    *(volatile uint32_t *)0x40004FFC;
+    *(volatile uint32_t *)0x40004FFC = 1;
 }
 void setState(uint8_t state) {
     RET_state_prev = RET_state;
@@ -244,21 +262,21 @@
                 switch (RET_buttonPressCount) {   //double catches to help with debounce
                     case 1 :
                     case 2 :
-                        RET_state_prev = RET_state;
+                        if(RET_state_prev != RET_state) {RET_state_prev = RET_state;}
                         RET_state = STATE_BUTTONPRESS1;
                         RET_buttonPressCount = 0;
                         mainthread.release();
                         break;
                     case 3 :
                     case 4 :
-                        RET_state_prev = RET_state;
+                        if(RET_state_prev != RET_state) {RET_state_prev = RET_state;}
                         RET_state = STATE_BUTTONPRESS3;
                         RET_buttonPressCount = 0;
                         mainthread.release();
                         break;
                     case 5 :
                     case 6 :
-                        RET_state_prev = RET_state;
+                        if(RET_state_prev != RET_state) {RET_state_prev = RET_state;}
                         RET_state = STATE_BUTTONPRESS5;
                         RET_buttonPressCount = 0;
                         mainthread.release();
@@ -270,24 +288,6 @@
             }
         }
     }
-    
-    /*
-    if (RET_setupInProgress) {
-        led1 = 0;
-    } else if (!GLOBAL_LEDSequenceinProgress) {
-        led1 = 1;
-    }
-    */
-    /*
-    if (RTCtick_ledflash_count > 0) {
-        led1 = !led1;
-        RTCtick_ledflash_count --;
-        if (RTCtick_ledflash_count <= 0) {
-            RTCtick_ledflash_count = 0;   
-            led1 = 1; //turn off led
-        }
-    }
-    */
 }
 void resetGlobals() {
     GLOBAL_accel_healthy = false;
@@ -414,7 +414,7 @@
         //INTERNAL NRF52 TEMP SENSOR
         temperature = nrfTemperature();
     } else {
-        temperature = si7060.getTemperature();
+        //temperature = si7060.getTemperature();
     }
     if (temperature > -40 && temperature < 60) {
         test_pass ++;
@@ -461,7 +461,7 @@
         //check if above threshold
         time_t inMotionForSeconds = ((RET_RTCunixtime - RET_motionStartTime) + (DEFAULT_SLEEP_FRAME / 1000)); //Plus DEFAULT_SLEEP_FRAME as it should include frame time
         if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "MOT:%u s",inMotionForSeconds);debug_exe();}
-        if (inMotionForSeconds >= RET_setting_motion_start_seconds && RET_motionState == false) {
+        if (inMotionForSeconds >= (RET_setting_motion_start_seconds + 15) && RET_motionState == false) {
             RET_motionState = true;
             RET_motionTriggeredInLocTXInterval = true;
             //if (GLOBAL_debugLED) LED1blink(1,50);
@@ -476,7 +476,7 @@
     if (RET_motionPendingOffState) {
         time_t noMotionForSeconds = ((RET_RTCunixtime - RET_motionStopTime) + (DEFAULT_SLEEP_FRAME / 1000)); //Plus DEFAULT_SLEEP_FRAME as it should include frame time
         if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "MOTLESS:%u s",noMotionForSeconds);debug_exe();}
-        if (noMotionForSeconds >= RET_setting_motion_stop_seconds && RET_motionState == true) {
+        if (noMotionForSeconds >= (RET_setting_motion_stop_seconds + 15) && RET_motionState == true) {
             //if (GLOBAL_debugLED) LED1blink(2,50);
             RET_motionPendingOffState = false;
             RET_motionState = false;
@@ -505,15 +505,31 @@
     int critical_fail_count = 0;
     int TEMP_a = -1; time_t TEMP_b = 0; int TEMP_c = -1; int TEMP_d = -1; int TEMP_e = -1; int TEMP_f = -1; int TEMP_g = -1; int TEMP_h = -1; int TEMP_i = -1; int TEMP_j = -1; 
     int TEMP_k = -1; int TEMP_l = -1; int TEMP_m = -1; int TEMP_n = -1; int TEMP_o = -1; int TEMP_p = -1; int TEMP_q = -1; int TEMP_r = -1; int TEMP_s = -1; int TEMP_t = -1;
-    //int varTotal = 19; //change this if var number changes!!!!!!!!
+    
+    //if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RESP: %s",settingsBuffer);debug_exe();}
+    
     if ( (matchCount = sscanf(settingsBuffer,"a:%d,b:%u,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d,o:%d,p:%d,q:%d,r:%d,s:%d,t:%d",
     &TEMP_a,&TEMP_b,&TEMP_c,&TEMP_d,&TEMP_e,&TEMP_f,&TEMP_g,&TEMP_h,&TEMP_i,&TEMP_j,&TEMP_k,&TEMP_l,&TEMP_m,&TEMP_n,&TEMP_o,&TEMP_p,&TEMP_q,&TEMP_r,&TEMP_s,&TEMP_t) ) > 0 ) {
         
-        if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "FROMSERVER: a:%d,b:%u,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d,o:%d,p:%d,q:%d,r:%d,s:%d,t:%d\n",
-        TEMP_a,TEMP_b,TEMP_c,TEMP_d,TEMP_e,TEMP_f,TEMP_g,TEMP_h,TEMP_i,TEMP_j,TEMP_k,TEMP_l,TEMP_m,TEMP_n,TEMP_o,TEMP_p,TEMP_q,TEMP_r,TEMP_s,TEMP_t);debug_exe();}
+        //if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "SERVERPARAMS: a:%d,b:%u,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d,o:%d,p:%d,q:%d,r:%d,s:%d,t:%d\n",
+        //TEMP_a,TEMP_b,TEMP_c,TEMP_d,TEMP_e,TEMP_f,TEMP_g,TEMP_h,TEMP_i,TEMP_j,TEMP_k,TEMP_l,TEMP_m,TEMP_n,TEMP_o,TEMP_p,TEMP_q,TEMP_r,TEMP_s,TEMP_t);debug_exe();}
         
         if(TEMP_a != -1) { RET_setting_minimumupdate_hrs = TEMP_a;                      }
-        if(TEMP_b != 0)  { RET_RTCunixtime = TEMP_b;                                    } else { critical_fail_count++; }
+        if(TEMP_b > 1552915630) {
+            if (RET_RTCunixtime < 1552915630) {
+                //assume this is first proper unixtime and set
+                RET_RTCunixtime = TEMP_b;
+            } else {
+                //this is not a first unixtime, so check within safe range
+                if ((TEMP_b - RET_RTCunixtime) < 604800) { //allow 1 week difference
+                    RET_RTCunixtime = TEMP_b;
+                } else {
+                    critical_fail_count++;
+                }
+            }
+        } else { 
+            critical_fail_count++;
+        }
         if(TEMP_c != -1) { RET_setting_firmware = TEMP_c;                               } else { RET_setting_firmware = -1;}
         if(TEMP_d != -1) { setState(TEMP_d);                                            } else { setState(STATE_NORMAL);}
         if(TEMP_e != -1) { RET_setting_location_mode = TEMP_e;                          }
@@ -541,7 +557,7 @@
 
         if (critical_fail_count == 0) { 
             RET_receivedNewSettings = true;
-            if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "SETTINGS OK");debug_exe();}
+            //if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "SETTINGS OK");debug_exe();}
             dumpSettings();
             RET_haveSettings = true;
             GLOBAL_needToConfigureLis3dh = true;
@@ -563,8 +579,7 @@
             }
             return true;
         } else {
-            if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "CRITICAL FAILS:%d",critical_fail_count);debug_exe();}
-            dumpSettings();
+            //if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "CRITICAL FAILS:%d",critical_fail_count);debug_exe();}
             addToExceptionString("SCF");
             //dont set RET_haveSettings = false; here, if we already have settings continue to use them
             return false;
@@ -892,6 +907,7 @@
         }
         case STATE_DORMANT :
         {
+            RET_busy = true;
             if (RET_RTCunixtime >= RET_eventTime_wakeFromDormant) {
                 if (RET_haveSettings) {
                     setState(STATE_NORMAL); 
@@ -900,10 +916,12 @@
                 }
             }
             if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);debug_exe();}
+            RET_busy = false;
             break;
         }
         case STATE_BUTTONPRESS1 :
         {
+            RET_busy = true;
             if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 1\n");debug_exe();}
             if (getBatteryV() < 2.5f) {
                 LED1blink(3,500);
@@ -916,11 +934,17 @@
                     LED1blink(2,500);
                 }
             }
-            setState(RET_state_prev);
+            if (RET_state_prev != STATE_NORMAL || RET_state_prev != STATE_DORMANT) {
+                setState(STATE_NORMAL);
+            } else {
+                setState(RET_state_prev);
+            }
+            RET_busy = false;
             break;
         }
         case STATE_BUTTONPRESS3 :
         {
+            RET_busy = true;
             if(RET_state_prev == STATE_DORMANT || RET_state_prev == STATE_NORMAL) {
                 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 3\n");debug_exe();}
                 LED1blink(3,500);
@@ -929,10 +953,12 @@
                 LED1off();
             }
             setState(STATE_NORMAL); //turns device back on
+            RET_busy = false;
             break;
         }
         case STATE_BUTTONPRESS5 :
         {
+            RET_busy = true;
             if(RET_state_prev == STATE_DORMANT || RET_state_prev == STATE_NORMAL) {
                 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 5\n");debug_exe();}
                 LED1blink(5,500);
@@ -941,10 +967,12 @@
                 LED1off();
             }
             setState(STATE_NORMAL); //turns device back on
+            RET_busy = false;
             break;  
         }
         case STATE_BUTTONHOLD :
         {
+            RET_busy = true;
             if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON HOLD \n");debug_exe();}
             if (RET_state_prev == STATE_NORMAL) {
                 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + THREEDAYSINSECONDS);
@@ -962,10 +990,12 @@
                 if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"TURNING ON\n");debug_exe();}
                 LED1blink(20,100);
             }
+            RET_busy = false;
             break;
         }
         case STATE_SCORCHEDEARTH :
         {
+            RET_busy = true;
             if(DEBUG_ON){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"SCORCHED EARTH - RESETTING");debug_exe();}
             setState(STATE_SETUP); //this wont be used but just incase to kick it out of this state
             read_app_data_from_flash(&app_data);
@@ -973,6 +1003,7 @@
             app_data.target_firmware_version = 0;
             write_app_data_to_flash(&app_data);
             system_reset();
+            RET_busy = false;
             break;
         }
         default :
@@ -985,7 +1016,7 @@
 //------------------------------------------------------------------------------
 // MAIN
 //------------------------------------------------------------------------------ 
-int main() {    
+int main() {
     //INIT
     watchdog.configure(300.0); //5 mins
     LED1off();
@@ -1036,7 +1067,7 @@
     }
     
     //MAIN LOOP
-    while(true) {
+    while(true) {    
         //WATCHDOG
         watchdogKick();
         RET_asleep = false;
@@ -1064,14 +1095,6 @@
                 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "F:%d, S:%d, WAKE@:%u\n", FW_VERSION, RET_state, RET_eventTime_wakeFromDormant);debug_exe();
             }
         }
-        
-        /*
-        if (modem.on(RET_force2G)) {
-            if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
-                modem.USSDsend("(b:gps2,a:act,e:1.7!0.15!,t:1547937626,r:0.13,c:1)\0",2);
-            }
-        }
-        */
 
         //STATE ENGINE
         mainStateEngine();
@@ -1081,13 +1104,13 @@
             recordFirmwareAsValid();
             RET_coldBoot = 0;
         }
-        
+                
         //PRE-SLEEP ACTIONS
         LED1off();
         modem.off(false);
         RET_motionTriggeredinFrame = false;
         if (GLOBAL_needToConfigureLis3dh) { lis3dh_configureForSleep(RET_setting_motion_g,RET_setting_impact_g); }
-        nrf_configureGPIOForSleep();
+        nrf_configureForSleep();
         NRFuart_uninit();
         watchdogKick();