init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Revision:
36:8e359069192b
Parent:
35:d9421d57d116
Child:
37:505ef618f06c
--- a/main.cpp	Sun Jan 13 23:48:41 2019 +0000
+++ b/main.cpp	Tue Jan 15 11:19:41 2019 +0000
@@ -3,7 +3,7 @@
 //------------------------------------------------------------------------------
 //DEFINES
 //------------------------------------------------------------------------------ 
-#define FW_VERSION 1
+#define FW_VERSION 2
 
 //MODES
 #define USE_NRF_TEMP_SENSOR                 1
@@ -26,81 +26,79 @@
 //------------------------------------------------------------------------------
 //FUNCTION PROTOTYPES
 //------------------------------------------------------------------------------ 
-static void mainStateEngine(void);
-static void selftest(void);
-static void buttonPress(void);
-static void buttonRelease(void);
-static void dumpSettings(void);
-static void saveEventTimes(void);
-static void event_location_tx(void);
+void mainStateEngine(void);
+void selftest(void);
+void buttonPress(void);
+void buttonRelease(void);
+void dumpSettings(void);
+void saveEventTimes(void);
 
 //------------------------------------------------------------------------------
-//GLOBAL VARS
+// GLOBALS
 //------------------------------------------------------------------------------ 
-char* GLOBAL_defaultApi = "b:gps2";
-bool GLOBAL_accel_healthy = false;
-bool GLOBAL_motionStopFlagTriggered = false;
-bool GLOBAL_debugLED = true;
-bool GLOBAL_needToConfigureLis3dh = false;
-bool GLOBAL_registeredOnNetwork = false;
-bool GLOBAL_modemOn = false;
-bool GLOBAL_LEDSequenceinProgress = false;
-time_t GLOBAL_wakeTime = 0;
-char GLOBAL_exceptionString[15];
-bool NRFuart_enabled = false;
-char GLOBAL_debug_buffer[200];
+char*            GLOBAL_defaultApi                   = "b:gps2";
+bool             GLOBAL_accel_healthy                = false;
+bool             GLOBAL_motionStopFlagTriggered      = false;
+bool             GLOBAL_debugLED                     = true;
+bool             GLOBAL_needToConfigureLis3dh        = false;
+bool             GLOBAL_registeredOnNetwork          = false;
+bool             GLOBAL_modemOn                      = false;
+bool             GLOBAL_LEDSequenceinProgress        = false;
+time_t           GLOBAL_wakeTime                     = 0;
+char             GLOBAL_exceptionString[15];
+char             GLOBAL_debug_buffer[200];
 
 
-static int              RET_setting_firmware;
-static uint32_t         RET_setting_minimumupdate_hrs;
-static uint8_t          RET_setting_location_mode;
-static uint8_t          RET_setting_location_accuracy;
-static uint32_t         RET_setting_location_tx_interval_mins;
-static uint32_t         RET_setting_location_tx_failsafe_hrs;
-static uint16_t         RET_setting_location_timeout;
-static uint32_t         RET_setting_activity_tx_interval_hrs;
-static uint32_t         RET_setting_environmental_tx_interval_mins;
-static uint16_t         RET_setting_motion_g;
-static time_t           RET_setting_motion_start_seconds;
-static time_t           RET_setting_motion_stop_seconds;
-static uint16_t         RET_setting_impact_g;
-static uint8_t          RET_setting_impact_alert;
-static uint16_t         RET_setting_connection_timeout;
-static uint16_t         RET_setting_beacon_interval_seconds;
-static uint16_t         RET_setting_beacon_scan;
+int              RET_setting_firmware;
+uint32_t         RET_setting_minimumupdate_hrs;
+uint8_t          RET_setting_location_mode;
+uint8_t          RET_setting_location_accuracy;
+uint32_t         RET_setting_location_tx_interval_mins;
+uint32_t         RET_setting_location_tx_failsafe_hrs;
+uint16_t         RET_setting_location_timeout;
+uint32_t         RET_setting_activity_tx_interval_hrs;
+uint32_t         RET_setting_environmental_tx_interval_mins;
+uint16_t         RET_setting_motion_g;
+time_t           RET_setting_motion_start_seconds;
+time_t           RET_setting_motion_stop_seconds;
+uint16_t         RET_setting_impact_g;
+uint8_t          RET_setting_impact_alert;
+uint16_t         RET_setting_connection_timeout;
+uint16_t         RET_setting_beacon_interval_seconds;
+uint16_t         RET_setting_beacon_scan;
 //STATE
-static uint8_t          RET_coldBoot = 1;
-static bool             RET_haveSettings;
-static uint8_t          RET_state;
-static uint8_t          RET_state_prev;
-static uint8_t          RET_buttonPressCount;
-static time_t           RET_buttonPressTime;
-static time_t           RET_buttonHoldTime;
-static time_t           RET_RTCunixtime;
-static time_t           RET_SetupRunAt;
-static time_t           RET_SettingsGotAt;
+uint8_t          RET_coldBoot = 1;
+time_t           RET_RTCunixtime;
+bool             RET_haveSettings;
+uint8_t          RET_state;
+uint8_t          RET_state_prev;
+uint8_t          RET_buttonPressCount;
+time_t           RET_buttonPressTime;
+time_t           RET_buttonHoldTime;
+time_t           RET_SetupRunAt;
+time_t           RET_SettingsGotAt;
 //MOTION STATE
-static bool             RET_motionTriggered;
-static bool             RET_motionTriggeredInTXInterval;
-static time_t           RET_motionStartTime;
-static time_t           RET_motionStopTime;
-static bool             RET_motionPendingOnState;
-static bool             RET_motionPendingOffState;
-static bool             RET_motionState;
-static float            RET_motionTotalActivityHours;
-static time_t           RET_motionFrameStart;
-static char             RET_activityData[140];
+bool             RET_motionTriggered;
+bool             RET_motionTriggeredInTXInterval;
+time_t           RET_motionStartTime;
+time_t           RET_motionStopTime;
+bool             RET_motionPendingOnState;
+bool             RET_motionPendingOffState;
+bool             RET_motionState;
+float            RET_motionTotalActivityHours;
+time_t           RET_motionFrameStart;
+char             RET_activityData[140];
 //IMPACT
-static bool             RET_impactTriggered;
+bool             RET_impactTriggered;
 //EVENTS LOGGING
-static time_t           RET_eventTime_location_log;
-static time_t           RET_eventTime_environmental_log;
+time_t           RET_eventTime_location_log;
+time_t           RET_eventTime_environmental_log;
 //EVENTS TX
-static time_t           RET_eventTime_location_tx;
-static time_t           RET_eventTime_location_failsafe_tx;
-static time_t           RET_eventTime_environmental_tx;
-static time_t           RET_eventTime_activity_tx;
-static time_t           RET_eventTime_wakeFromDormant;
+time_t           RET_eventTime_location_tx;
+time_t           RET_eventTime_location_failsafe_tx;
+time_t           RET_eventTime_environmental_tx;
+time_t           RET_eventTime_activity_tx;
+time_t           RET_eventTime_wakeFromDormant;
 
 //------------------------------------------------------------------------------
 //GPIO
@@ -116,10 +114,6 @@
 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);
-
-//------------------------------------------------------------------------------
-//TIMERS
-//-----------------------------------------------mi------------------------------- 
 LowPowerTicker RTCticker; //no impact on power consumption
 
 //------------------------------------------------------------------------------
@@ -130,140 +124,6 @@
 //------------------------------------------------------------------------------
 // LOW LEVEL FUNCS
 //------------------------------------------------------------------------------
-void NRFuart_init_nohwfc() {
-    if(NRFuart_enabled == false) {
-        //Configure UART0 pins.
-        nrf_gpio_cfg_output(PN_UART_TX);
-        nrf_gpio_cfg_input(PN_UART_RX, NRF_GPIO_PIN_NOPULL);
-        NRF_UART0->PSELTXD = PN_UART_TX;
-        NRF_UART0->PSELRXD = PN_UART_RX;
-        NRF_UART0->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos) | UART_CONFIG_HWFC_Disabled;
-        NRF_UART0->BAUDRATE = NRF_UART_BAUDRATE_115200;
-        NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Enabled;
-        NRF_UART0->EVENTS_RXDRDY = 0;
-        NRF_UART0->EVENTS_TXDRDY = 0;
-        NRF_UART0->EVENTS_ERROR  = 0;
-        NRF_UART0->EVENTS_TXDRDY = 0;
-        NRF_UART0->TASKS_STARTRX = 1;
-        NRF_UART0->TASKS_STARTTX = 1;
-        
-        //NRF_UART0->INTENCLR = 0xffffffffUL;
-        //NRF_UART0->INTENSET = UART_INTENSET_RXDRDY_Msk;
-        /*NRF_UART0->INTENSET = (UART_INTENSET_RXDRDY_Set << UART_INTENSET_RXDRDY_Pos) |
-                              (UART_INTENSET_TXDRDY_Set << UART_INTENSET_TXDRDY_Pos) |
-                              (UART_INTENSET_ERROR_Set << UART_INTENSET_ERROR_Pos);*/    
-        //NVIC_ClearPendingIRQ(UART0_IRQn);
-        //NVIC_SetPriority(UART0_IRQn, 1); //3
-        //NVIC_EnableIRQ(UART0_IRQn);
-        //NVIC_SetVector(UART0_IRQn, (uint32_t) UART0_IRQHandler);
-    }
-    NRFuart_enabled = true;
-};
-void NRFuart_uninit() {
-    if (NRFuart_enabled) {
-        NVIC_DisableIRQ(UART0_IRQn);
-        NRF_UART0->INTENCLR = 0xffffffffUL;
-        NRF_UART0->TASKS_STOPRX = 1;
-        NRF_UART0->TASKS_STOPTX = 1;
-        NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Disabled;
-        NRF_UART0->PSELTXD = 0xFFFFFFFF;
-        NRF_UART0->PSELRXD = 0xFFFFFFFF;
-        NRF_UART0->PSELRTS = 0xFFFFFFFF;
-        NRF_UART0->PSELCTS = 0xFFFFFFFF;
-        NRFuart_enabled = false;
-    }
-};
-void NRFuart_putc(char byte) {
-    if (!NRFuart_enabled) NRFuart_init_nohwfc();
-    NRF_UART0->TXD = byte;
-    uint32_t safetycounter = 0;
-    while(NRF_UART0->EVENTS_TXDRDY != 1 && safetycounter < 10000)
-    {
-        safetycounter ++; // Wait for the current TXD data to be sent.
-    }
-    NRF_UART0->EVENTS_TXDRDY = 0;
-};
-void NRFuart_puts(char* bytes) {
-    if (!NRFuart_enabled) NRFuart_init_nohwfc();
-    for(int i = 0; bytes[i] != '\0'; i++) {
-        NRFuart_putc(bytes[i]);
-    }
-};
-void NRFuart_puts_debug(char* bytes) {
-    if (!NRFuart_enabled) NRFuart_init_nohwfc();
-    for(int i = 0; bytes[i] != '\0'; i++) {
-        NRFuart_putc(bytes[i]);
-    }
-    NRFuart_putc('\n');
-};
-void debug_prep(){
-    memset(GLOBAL_debug_buffer, '\0', sizeof(GLOBAL_debug_buffer));   
-}
-void debug_exe(){
-    NRFuart_puts_debug(GLOBAL_debug_buffer);
-}
-char NRFuart_getc() {
-    if (!NRFuart_enabled) NRFuart_init_nohwfc();
-    uint32_t safetycounter = 0;
-    while(NRF_UART0->EVENTS_RXDRDY != 1 && safetycounter < 10000){
-        safetycounter ++;
-    }
-    NRF_UART0->EVENTS_RXDRDY = 0;
-    return (uint8_t)NRF_UART0->RXD;
-};
-char* NRFuart_gets(char terminator) {
-    if (!NRFuart_enabled) NRFuart_init_nohwfc();
-    static char buffer[200];
-    int charindex = 0;
-    memset(buffer,'\0',sizeof(buffer));
-    while(1) {
-        if (NRF_UART0->EVENTS_RXDRDY == 0) {
-            //Nothing available from the UART.
-            continue;
-        } else {
-            char inbyte = NRFuart_getc();
-            if (inbyte == terminator) {
-                break;
-            } else {
-                buffer[charindex] = inbyte;
-                charindex++;
-            }
-        }
-    }
-    buffer[charindex] = '\n'; //make sure we end with whitespace lf
-    return buffer;
-};
-void NRFuart_flush() {
-    if (!NRFuart_enabled) NRFuart_init_nohwfc();
-    //THIS HASNT BEEN TESTED
-    char char1 = 0;
-    while (NRFuart_readable()) {
-        char1 = NRFuart_getc();
-    }
-};
-bool NRFuart_readable() {
-    if (!NRFuart_enabled) NRFuart_init_nohwfc();
-    return (NRF_UART0->EVENTS_RXDRDY == 1);
-};
-#define CONSOLE_DEBUG 0
-#if CONSOLE_DEBUG
-#define DEBUG(str, ... ) do{\
-    /*int n;\
-    char buffer[200];\
-    memset(buffer, '\0', sizeof(buffer));\
-    n = snprintf(buffer, sizeof(buffer), str,  __VA_ARGS__);\
-    buffer[sizeof(buffer)-1] = 0x00;\
-    if(n < 0) { }\
-    else if(n >= sizeof(buffer)) { }\
-    else\
-    {\
-        NRFuart_puts_debug(buffer);\
-    }\
-}while(0)*/
-#else
-#define DEBUG(...)
-#endif
-
 void nrf_configureGPIOForSleep(){
     nrf_gpio_cfg_input(PN_SPI_MOSI, NRF_GPIO_PIN_NOPULL);
     nrf_gpio_cfg_input(PN_SPI_MISO, NRF_GPIO_PIN_NOPULL);
@@ -327,17 +187,25 @@
     }
     return voltage;
 }
+float nrfTemperature() {
+    //INTERNAL NRF52 TEMP SENSOR
+    uint32_t safetycounter = 0;
+    float temperature = 0.0;
+    NRF_TEMP->TASKS_START=1;
+    while (NRF_TEMP->EVENTS_DATARDY==0 && safetycounter < 10000) {
+        safetycounter ++;
+    };
+    NRF_TEMP->EVENTS_DATARDY=0;
+    temperature = nrf_temp_read()/4.0;
+    NRF_TEMP->TASKS_STOP=1;
+    return temperature;
+}
 float getTemperature() {
     float temperature;
-    
+
     if (USE_NRF_TEMP_SENSOR) {
         //INTERNAL NRF52 TEMP SENSOR
-        uint32_t safetycounter = 0;
-        NRF_TEMP->TASKS_START=1;
-        while (NRF_TEMP->EVENTS_DATARDY==0 && safetycounter < 10000);
-        NRF_TEMP->EVENTS_DATARDY=0;
-        temperature = nrf_temp_read()/4.0;
-        NRF_TEMP->TASKS_STOP=1;
+        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
     }
@@ -410,7 +278,7 @@
     memset(GLOBAL_exceptionString,0x00,sizeof(GLOBAL_exceptionString));
     if (DEBUG_ON) {memset(GLOBAL_debug_buffer,0x00,sizeof(GLOBAL_debug_buffer));}
 }
-void factoryReset() {
+void setDefaults() {
     //IDENTITY 
     //RET_imei = 0;
     //STATE
@@ -486,12 +354,7 @@
     float temperature;
     if (USE_NRF_TEMP_SENSOR) {
         //INTERNAL NRF52 TEMP SENSOR
-        uint32_t safetycounter = 0;
-        NRF_TEMP->TASKS_START=1;
-        while (NRF_TEMP->EVENTS_DATARDY==0 && safetycounter < 10000);
-        NRF_TEMP->EVENTS_DATARDY=0;
-        temperature = nrf_temp_read()/4.0;
-        NRF_TEMP->TASKS_STOP=1;
+        temperature = nrfTemperature();
     } else {
         temperature = si7060.getTemperature();
     }
@@ -684,7 +547,7 @@
                     pass = false;
                     modem.off(false);
                     if(GLOBAL_debugLED) LED1errorCode(5,10); //ERROR 5
-                }              
+                }                           
             } else {
                 //Response error
             }
@@ -967,22 +830,30 @@
                 DEBUG("0x%08x. Hard Reset ST:%d\n",NRF_POWER->RESETREAS, RET_state);
                 RET_coldBoot = 1;
                 dumpSettings();
-                NRF_POWER->RESETREAS = 0xffffffff;
             break;
             case 0x00000002  :
                 DEBUG("Watchdog ST:%d\n",RET_state);
-                NRF_POWER->RESETREAS = 0xffffffff;
             break;
             case 0x00000004  :
                 DEBUG("Soft ST:%d\n",RET_state);
             break;
         }
+        NRF_POWER->RESETREAS = 0xffffffff;
     }
     
     //CHECK FOR FIRST BOOT
     if (RET_coldBoot == 1) { 
-        factoryReset();
-        LED1errorCode(6,2); 
+        setDefaults(); 
+        //check battery
+        float voltage = getBatteryV();
+        if (voltage > 2.5) {
+            //battery ok
+            LED1errorCode(6,2);
+        } else {
+            //battery low
+            LED1errorCode(10,2);
+        }
+        mainthread.wait(2000);
     }
     
     //MAIN LOOP