init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Files at this revision

API Documentation at this revision

Comitter:
pathfindr
Date:
Sun Dec 16 15:21:38 2018 +0000
Parent:
12:8345612bf867
Child:
14:9a54b1b65bc8
Commit message:
5

Changed in this revision

SI7060.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
modem.cpp Show annotated file Show diff for this revision Revisions of this file
modem.h Show annotated file Show diff for this revision Revisions of this file
sensors.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/SI7060.cpp	Sun Dec 16 01:06:14 2018 +0000
+++ b/SI7060.cpp	Sun Dec 16 15:21:38 2018 +0000
@@ -6,7 +6,7 @@
 
 SI7060::SI7060(PinName sda, PinName scl):  _i2c(sda,scl)
 {
-    requireSoftReset = true;  //TODO: this can be removed when i2c sleep issue resolved    
+    GLOBAL_requireSoftReset = true;  //TODO: this can be removed when i2c sleep issue resolved    
 }
 
 char SI7060::readValue(uint8_t registerAdd) {
--- a/main.cpp	Sun Dec 16 01:06:14 2018 +0000
+++ b/main.cpp	Sun Dec 16 15:21:38 2018 +0000
@@ -12,51 +12,56 @@
 //------------------------------------------------------------------------------
 //GLOBAL VARS / CLEARED ON SLEEP (IF USING SOFT REBOOT HACK)
 //------------------------------------------------------------------------------ 
-bool accel_healthy = false;
-bool requireSoftReset = false;
-bool motionFlagTriggered = false;
-bool debugLED = false;
-bool needToConfigureLis3dh = false;
+bool GLOBAL_accel_healthy = false;
+bool GLOBAL_requireSoftReset = false;
+bool GLOBAL_motionFlagTriggered = false;
+bool GLOBAL_debugLED = false;
+bool GLOBAL_needToConfigureLis3dh = false;
+bool GLOBAL_registeredOnNetwork = false;
+long long GLOBAL_imei = 0;
+float GLOBAL_voltage = 0.0;
+time_t GLOBAL_RTCunixtime = 0;
+time_t GLOBAL_wakeTime = 0;
 
 //------------------------------------------------------------------------------
 //RETAINED NOINIT RAM VARS
 //------------------------------------------------------------------------------ 
-#if defined ( __CC_ARM ) /** THIS IS THE MBED ONLINE COMPILER TOOLCHAIN*/
-static uint8_t          RET_coldBoot                    __attribute__((section("noinit"),zero_init));
-static uint8_t          RET_mode                    __attribute__((section("noinit"),zero_init));
-static uint8_t          RET_mode_prev                    __attribute__((section("noinit"),zero_init));
-static uint8_t          RET_buttonPressCount      __attribute__((section("noinit"),zero_init));
-static time_t           RET_buttonPressTime         __attribute__((section("noinit"),zero_init));
-static time_t           RET_buttonReleaseTime       __attribute__((section("noinit"),zero_init));
-static time_t           RET_buttonHoldTime       __attribute__((section("noinit"),zero_init));
-static time_t           RET_RTCunixtime         __attribute__((section("noinit"),zero_init));
-static long             RET_RTCmicros         __attribute__((section("noinit"),zero_init));
-static bool             RET_requireImpactFlag       __attribute__((section("noinit"),zero_init));
-static char             RET_buffer[64]              __attribute__((section("noinit"),zero_init));
-
+#if defined ( __CC_ARM ) /** THIS IS THE MBED ONLINE COMPILER TOOLCHAIN*/ //MUST BE STATICS
+//IDENTITY
+static long long        RET_imei                            __attribute__((section("noinit"),zero_init));
+//STATE
+static uint8_t          RET_coldBoot                        __attribute__((section("noinit"),zero_init));
+static uint8_t          RET_mode                            __attribute__((section("noinit"),zero_init));
+static uint8_t          RET_mode_prev                       __attribute__((section("noinit"),zero_init));
+static uint8_t          RET_buttonPressCount                __attribute__((section("noinit"),zero_init));
+static time_t           RET_buttonPressTime                 __attribute__((section("noinit"),zero_init));
+static time_t           RET_buttonReleaseTime               __attribute__((section("noinit"),zero_init));
+static time_t           RET_buttonHoldTime                  __attribute__((section("noinit"),zero_init));
+static time_t           RET_RTCunixtime                     __attribute__((section("noinit"),zero_init));
+static long             RET_RTCmicros                       __attribute__((section("noinit"),zero_init));
+static bool             RET_requireImpactFlag               __attribute__((section("noinit"),zero_init));
 //MOTION SETTINGS
-static bool             RET_impactTriggered              __attribute__((section("noinit"),zero_init));
-static bool             RET_motionTriggered              __attribute__((section("noinit"),zero_init));
-static time_t           RET_motionStartTime              __attribute__((section("noinit"),zero_init));
-static time_t           RET_motionStopTime              __attribute__((section("noinit"),zero_init));
+static bool             RET_impactTriggered                 __attribute__((section("noinit"),zero_init));
+static bool             RET_motionTriggered                 __attribute__((section("noinit"),zero_init));
+static time_t           RET_motionStartTime                 __attribute__((section("noinit"),zero_init));
+static time_t           RET_motionStopTime                  __attribute__((section("noinit"),zero_init));
 static time_t           RET_motionStartThreshold_seconds    __attribute__((section("noinit"),zero_init));
 static time_t           RET_motionStopThreshold_seconds     __attribute__((section("noinit"),zero_init));
-static bool             RET_motionPendingOnState              __attribute__((section("noinit"),zero_init));
-static bool             RET_motionPendingOffState              __attribute__((section("noinit"),zero_init));
-static bool             RET_motionState              __attribute__((section("noinit"),zero_init));
-static float            RET_motionTotalActivityHours     __attribute__((section("noinit"),zero_init));
-static char             RET_motionData[100]              __attribute__((section("noinit"),zero_init));
-
+static bool             RET_motionPendingOnState            __attribute__((section("noinit"),zero_init));
+static bool             RET_motionPendingOffState           __attribute__((section("noinit"),zero_init));
+static bool             RET_motionState                     __attribute__((section("noinit"),zero_init));
+static float            RET_motionTotalActivityHours        __attribute__((section("noinit"),zero_init));
+static char             RET_motionData[100]                 __attribute__((section("noinit"),zero_init));
 //EVENT HANDLING
 static time_t           RET_eventInterval_location_log      __attribute__((section("noinit"),zero_init));
-static time_t           RET_eventTime_location_log      __attribute__((section("noinit"),zero_init));
-static time_t           RET_eventTime_location_bc      __attribute__((section("noinit"),zero_init));
+static time_t           RET_eventTime_location_log          __attribute__((section("noinit"),zero_init));
+static time_t           RET_eventTime_location_bc           __attribute__((section("noinit"),zero_init));
 
 static time_t           RET_eventInterval_environmental_log __attribute__((section("noinit"),zero_init));
-static time_t           RET_eventTime_environmental_log __attribute__((section("noinit"),zero_init));
-static time_t           RET_eventTime_environmental_bc __attribute__((section("noinit"),zero_init));
-static time_t           RET_eventTime_activity_bc      __attribute__((section("noinit"),zero_init));
-static time_t           RET_eventTime_wakeFromDormant      __attribute__((section("noinit"),zero_init));
+static time_t           RET_eventTime_environmental_log     __attribute__((section("noinit"),zero_init));
+static time_t           RET_eventTime_environmental_bc      __attribute__((section("noinit"),zero_init));
+static time_t           RET_eventTime_activity_bc           __attribute__((section("noinit"),zero_init));
+static time_t           RET_eventTime_wakeFromDormant       __attribute__((section("noinit"),zero_init));
 #elif defined ( __GNUC__ )
 #elif defined ( __ICCARM__ )
 #endif
@@ -101,15 +106,15 @@
 void gotoSleep(long sleep_milliseconds) {
     turnOffEverything();
     watchdog.kick();
-    if (requireSoftReset) { //dont need to clear this var as reset changes it back to false
+    if (GLOBAL_requireSoftReset) { //dont need to clear this var as reset changes it back to false
         system_reset();
     }
     mainthread.wait(sleep_milliseconds);
 }
 void RTCtick() {
-    //RET_unixtime ++;
     RET_RTCmicros += 100;
     RET_RTCunixtime = (RET_RTCmicros / 1000);
+    GLOBAL_RTCunixtime = RET_RTCunixtime;
     //button logic - check for hold
     if (RET_buttonHoldTime > 4000) {
         RET_buttonHoldTime = 0;
@@ -139,12 +144,13 @@
     }
 }
 void factoryReset() {
-    //firstBoot = true;
-    requireSoftReset = false;
+    DEBUG("Factory Reset \n");
+    GLOBAL_requireSoftReset = false;
     
     //RESET RETAINED VARS
+    RET_imei = 0;
     RET_coldBoot = 1;
-    RET_mode = MODE_NORMAL;
+    RET_mode = MODE_SETUP;
     RET_mode_prev = RET_mode;
     RET_RTCunixtime = 0;
     RET_RTCmicros = 0;
@@ -173,10 +179,9 @@
     
     //CONFIGURE ACCELL DEFAULT
     lis3dh_configureForSleep(10,127);
-    
-    //SET IDENTIFIER
-    //uint32_t nv_value = 12345678;
-    //int rc = nvstore.set(NV_IDENTIFIER, sizeof(nv_value), &nv_value);
+}
+void copyRETtoGLOBAL() {
+    GLOBAL_imei = RET_imei;   
 }
 void selfTest() {
     //Accelerometer
@@ -207,44 +212,81 @@
 //------------------------------------------------------------------------------ 
 void checkMotion() {
     if (lis3dh_int2) {
-        if (debugLED) LED1blink(2,100);
+        if (GLOBAL_debugLED) LED1blink(2,100);
         RET_motionTriggered = true;
-        needToConfigureLis3dh = true; //interrupt has fire so need to clear it
+        GLOBAL_needToConfigureLis3dh = true; //interrupt has fire so need to clear it
         if (!RET_motionPendingOnState) {
             RET_motionPendingOnState = true;
             RET_motionPendingOffState = false;
             // Log start motion time
-            RET_motionStartTime = time(NULL);
+            RET_motionStartTime = RET_RTCunixtime;
         }
     } else {
-        if (debugLED) LED1blink(2,500);
+        if (GLOBAL_debugLED) LED1blink(2,500);
         RET_motionTriggered = false;
         RET_motionPendingOnState = false;
         if (!RET_motionPendingOffState) {
             RET_motionPendingOffState = true;
             //log stop motion time
-            RET_motionStopTime = time(NULL);
+            RET_motionStopTime = RET_RTCunixtime;
         }
     }
     //calculate motion state
     if (RET_motionPendingOnState) {
         //check if above threshold
-        time_t inMotionForSeconds = (time(NULL) - RET_motionStartTime);
+        time_t inMotionForSeconds = (RET_RTCunixtime - RET_motionStartTime);
         if (inMotionForSeconds >= RET_motionStartThreshold_seconds) {
             RET_motionState = true;
-            if (debugLED) LED1blink(10,100);
+            if (GLOBAL_debugLED) LED1blink(10,100);
         }
     }
     if (RET_motionPendingOffState) {
-        time_t noMotionForSeconds = (time(NULL) - RET_motionStopTime);
+        time_t noMotionForSeconds = (RET_RTCunixtime - RET_motionStopTime);
         if (noMotionForSeconds >= RET_motionStartThreshold_seconds) {
             RET_motionState = false;
             RET_motionTotalActivityHours += (float(RET_motionStopTime - RET_motionStartTime) / 3600.0); 
-            if (debugLED) LED1blink(5,500);
+            if (GLOBAL_debugLED) LED1blink(5,500);
         }
     }
 }
 
+
+//------------------------------------------------------------------------------
+// SETUP
+//------------------------------------------------------------------------------ 
+bool setup() {
+    bool pass = true;
+    Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN);
+    if (modem.on()) {
+        RET_imei = modem.getIMEI();
+        DEBUG("imei: %lld \n",RET_imei);
+        if (modem.registerOnNetwork(3,60000)) {
+                char* result = modem.USSDmessage("blahblah\0", true, 2);
+                if (result != "err") {
+                    DEBUG("result: %s \n",result);
+                }
+        } else {
+            //modem failed to register on network
+        }
+    } else {
+        pass = false;
+        //Modem failed to turn on   
+    }
+     
+    while (1) {
+        Thread::wait(60000);
+        watchdog.kick();
+    }
+    
+    //RESULT
+    if (pass) {
+        return true;
+    } else {
+        return false;   
+    }
+}
+
+
 //------------------------------------------------------------------------------
 // STATE ENGINE
 //------------------------------------------------------------------------------ 
@@ -252,7 +294,9 @@
     switch(RET_mode) {
         case MODE_SETUP :
             factoryReset();
-            //selftest();
+            if (setup()) {
+                RET_mode = MODE_NORMAL;
+            }
             break;
         case MODE_NORMAL :
             //check and log motion
@@ -312,7 +356,6 @@
 //------------------------------------------------------------------------------
 // MAIN
 //------------------------------------------------------------------------------ 
-
 int main() {
     RTCticker.attach(&RTCtick, 0.1);
     turnOffEverything();
@@ -326,7 +369,7 @@
         switch(NRF_POWER->RESETREAS) {
             case 0x00000001  :
                 DEBUG("reset_reason: 0x%08x. - Hard Reset\n",NRF_POWER->RESETREAS);
-                factoryReset();
+                RET_coldBoot = 1;
             break;
             case 0x00000002  :
                 DEBUG("reset_reason: 0x%08x. - Watchdog\n",NRF_POWER->RESETREAS);
@@ -340,58 +383,29 @@
     //CHECK FOR FIRST BOOT
     if (RET_coldBoot != 0) factoryReset();
     
+    copyRETtoGLOBAL();
+    
     while(true) {
         //SLEEP
-        if (RET_coldBoot != 1) gotoSleep(30000); //THIS HAS TO BE THE FIRST ITEM IN THE MAIN LOOP
+        if (RET_coldBoot != 1) gotoSleep(10000); //THIS HAS TO BE THE FIRST ITEM IN THE MAIN LOOP
         watchdog.kick();
         
-        //TEST SPACE
-        Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN);
-        if (modem.on()) {
-            if (modem.registerOnNetwork(3,60000)) {
-                    if (modem.USSDsend("blahblah", 2)) {
-                        while(1) {
-                        char* response = modem.USSDreceive(105000);
-                        DEBUG("response: %s",response);
-                        }
-                    }
-            } else {
-                //modem failed to register on network
-            }
-        } else {
-            //Modem failed to turn on   
-        }
-        
-        
-        
-        while (1) {
-            Thread::wait(60000);
-            watchdog.kick();
-        }
-        
+        //LOG START TIME
+        GLOBAL_wakeTime = RET_RTCunixtime;
         
         //MAIN LOGIC
         DEBUG("mode: %i  time: %i, %i, %i, %i \n", RET_mode, RET_RTCmicros, RET_RTCunixtime, RET_buttonHoldTime, RET_buttonPressCount);
+        
+        Thread::wait(5000);
+        RET_coldBoot = 0;
+        system_reset();
+        
         mainStateEngine();
         
         //PRE-SLEEP ACTIONS
-        if (needToConfigureLis3dh) { //ONLY RESET ACCELL IF WE NEED TO
+        if (GLOBAL_needToConfigureLis3dh) { //ONLY RESET ACCELL IF WE NEED TO
             lis3dh_configureForSleep(10,127);
         }
         RET_coldBoot = 0;
-        
-        
-        /*
-        firstBoot = false; //temp
-        if (!firstBoot) gotoSleep(10000);
-        
-        //INIT
-        //Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN);
-        
-        //MAIN STATE ENGINE
-        
-        //Configure for sleep
-        lis3dh_configureForSleep(10,127);
-        */
     }
 }
\ No newline at end of file
--- a/main.h	Sun Dec 16 01:06:14 2018 +0000
+++ b/main.h	Sun Dec 16 15:21:38 2018 +0000
@@ -76,8 +76,18 @@
 //------------------------------------------------------------------------------
 //Global data structures
 //------------------------------------------------------------------------------
+//PERFS
+extern Serial uart;
 
 //VARS
-extern bool requireSoftReset;
-extern Serial uart;
+extern bool GLOBAL_accel_healthy;
+extern bool GLOBAL_requireSoftReset;
+extern bool GLOBAL_motionFlagTriggered;
+extern bool GLOBAL_debugLED;
+extern bool GLOBAL_needToConfigureLis3dh;
+extern bool GLOBAL_registeredOnNetwork;
+extern long long GLOBAL_imei;
+extern float GLOBAL_voltage;
+extern time_t GLOBAL_RTCunixtime;
+extern time_t GLOBAL_wakeTime;
 #endif 
--- a/modem.cpp	Sun Dec 16 01:06:14 2018 +0000
+++ b/modem.cpp	Sun Dec 16 15:21:38 2018 +0000
@@ -1,10 +1,8 @@
 #include "modem.h"
 
-static char ATinBuffer[180];
-
 Modem::Modem(PinName tx, PinName rx, PinName cts, PinName rts, PinName pwrkey, PinName vreg_en):  _uart(tx,rx,115200), _pwrkey(pwrkey), _vreg_en(vreg_en)
 {
-    requireSoftReset = true;  //TODO: this can be removed when uart sleep issue resolved
+    GLOBAL_requireSoftReset = true;  //TODO: this can be removed when uart sleep issue resolved
 }
 
 void Modem::flushSerial(void)
@@ -69,7 +67,7 @@
     _pwrkey = 1;
     if (ATwaitForWord("RDY",10000)) {
         //TURN OFF ECHO
-        ATsendCMD("ATE 0");
+        ATsendCMD("ATE0");
         ATwaitForWord("OK",5000);
         return true;
     } else {
@@ -86,9 +84,20 @@
     _vreg_en = 0;
 }
 
+long long Modem::getIMEI() 
+{
+    long long imei = 0;
+    char ATinBuffer[32];
+    ATsendCMD("AT+GSN");
+    ATwaitForWord("\n",5000);
+    _uart.scanf("%s", ATinBuffer);
+    imei = atoll(ATinBuffer);
+    flushSerial();
+    return imei;
+}
+
 bool Modem::registerOnNetwork(int maxAttempts, uint32_t timeout) 
 {
-    bool registered = false;
     int attempt = 0;
     Timer t;
     t.start();
@@ -98,21 +107,21 @@
         t.reset();
         uint32_t startmillis = t.read_ms();
         uint32_t runtime = 0;
-        while(!registered && runtime < timeout) {
+        while(!GLOBAL_registeredOnNetwork && runtime < timeout) {
             runtime = (t.read_ms() - startmillis);
             Thread::wait(1000);
             ATsendCMD("AT+CREG?");
             if (ATwaitForWord("+CREG: 0,5",5000)) {
-                registered = true;
+                GLOBAL_registeredOnNetwork = true;
             };
         }
-        if (!registered) {
+        if (!GLOBAL_registeredOnNetwork) {
             off(true);
             on();   
         }
     }
     t.stop();
-    if (registered) {
+    if (GLOBAL_registeredOnNetwork) {
         return true;
     } else {
         return false;   
@@ -127,7 +136,7 @@
     while (!sent && attempt <= maxAttempts) {
         attempt ++;
         char bytestosend[160];
-        sprintf(bytestosend, "AT+CUSD=1,\"#469*%s#\"", message);
+        snprintf(bytestosend, sizeof(bytestosend), "AT+CUSD=1,\"#469*%s#\"", message);
         ATsendCMD(bytestosend);
         if (ATwaitForWord("+CUSD: 0",10000)) {
             flushSerial();
@@ -141,20 +150,20 @@
     }
 }
 
-char* Modem::USSDreceive(uint32_t timeout) 
+char* Modem::USSDreceive() 
 {
     bool received = false;
+    uint32_t timeout = 15000;
     int bufferIndex = 0;
+    char ATinBuffer[180];
     Timer t;
     t.start();
     //TRY UNTIL TIMEOUT
     uint32_t startmillis = t.read_ms();
     uint32_t runtime = 0;
-    memset(ATinBuffer, 0, sizeof(ATinBuffer));
     while(!received && runtime < timeout) {
-        runtime = (t.read_ms() - startmillis); 
-        //ATsendCMD("AT+CUSD?");
-        if (ATwaitForWord("+CUSD: 0,\"%",2000)) {
+        runtime = (t.read_ms() - startmillis);
+        if (ATwaitForWord("+CUSD: 0,\"%",5000)) {
             led1 = 0;
             while(!received && runtime < timeout) {
                 runtime = (t.read_ms() - startmillis);
@@ -180,4 +189,27 @@
 }
 
 
+char* Modem::USSDmessage(char* message, bool needResponse, int maxAttempts) 
+{
+    uint8_t postStatus = 0;
+    uint8_t postStatusTarget = 1;
+    if (needResponse) {
+        postStatusTarget = 2;
+    }
+    int messageLength = strlen(message);
+    if (messageLength > USSD_MAXLENGTH) {
+           char* message;
+           sprintf(message,"('a':'%lld','b':'gps2','v':<voltage>)",GLOBAL_imei,1.0);
+    } else {
+        
+    }
+    
+    if (messageLength <= USSD_MAXLENGTH) {
+        return message;
+    } else {
+        return "toolong";
+    }
+    
+}
+
 Modem::~Modem(){};
\ No newline at end of file
--- a/modem.h	Sun Dec 16 01:06:14 2018 +0000
+++ b/modem.h	Sun Dec 16 15:21:38 2018 +0000
@@ -7,6 +7,7 @@
 
     #define UC20_BAUDRATE       115200
     #define SIM7600_BAUDRATE    115200
+    #define USSD_MAXLENGTH      130
     
     class Modem
     {
@@ -21,13 +22,14 @@
             //funcs
             bool registerOnNetwork(int maxAttempts, uint32_t timeout);
             bool USSDsend(char* message, int maxAttempts);
-            char* USSDreceive(uint32_t timeout);
-            bool USSDmessage(char* message, int maxAttempts, bool needResponse);
+            char* USSDreceive();
+            char* USSDmessage(char* message, bool needResponse, int maxAttempts);
             
             //AT
             void flushSerial(void);
             void ATsendCMD(char* cmd);
             bool ATwaitForWord(char* word, uint32_t timeout);
+            long long getIMEI(void);
         private:
         
                 
--- a/sensors.cpp	Sun Dec 16 01:06:14 2018 +0000
+++ b/sensors.cpp	Sun Dec 16 15:21:38 2018 +0000
@@ -10,7 +10,7 @@
 void lis3dh_configureForSleep(uint8_t motion_threshold, uint8_t impact_threshold) {
     //init
     LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK);
-    requireSoftReset = true; //WE HAVE STARTED SPI SO NEED THIS
+    GLOBAL_requireSoftReset = true; //WE HAVE STARTED SPI SO NEED THIS
      
     //Mode
     lis3dh.LIS3DH_WriteReg(LIS3DH_CTRL_REG1, 0b00111111); // 00111111 25hz low power mode all axis. 50hz = 6ua  /   25hz = 4ua  /   10hz = 3ua