init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Files at this revision

API Documentation at this revision

Comitter:
pathfindr
Date:
Wed Nov 14 16:08:28 2018 +0000
Parent:
5:8f8951127724
Child:
7:e9a19750700d
Commit message:
update

Changed in this revision

LIS3DH_SPI.lib Show annotated file Show diff for this revision Revisions of this file
PF_LIS3DH_SPI.lib Show diff for this revision Revisions of this file
board.h 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
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
modes.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LIS3DH_SPI.lib	Wed Nov 14 16:08:28 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/Pathfindr/code/LIS3DH_SPI/#1b06721b55b9
--- a/PF_LIS3DH_SPI.lib	Mon Nov 12 10:56:23 2018 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://os.mbed.com/teams/Pathfindr/code/PF_LIS3DH_SPI/#5b8e56fd7571
--- a/board.h	Mon Nov 12 10:56:23 2018 +0000
+++ b/board.h	Wed Nov 14 16:08:28 2018 +0000
@@ -1,8 +1,47 @@
 #include "mbed.h"
 
+//PINS
+#define PN_SWDIO                SWDIO
+#define PN_SWCLK                SWCLK
+#define PN_RESET                P0_21
+#define PN_IO1                  P0_20
+#define PN_IO2                  P0_18
+#define PN_IO3                  P0_2
+#define PN_IO4                  P0_3
+#define PN_IN_BUTTON            P0_13
+#define PN_LED                  P0_11
+#define PN_GSM_WAKEUP_IN        P0_14
+#define PN_GSM_WAKE_DISABLE     P0_16
+#define PN_GSM_STATUS           P0_15
+#define PN_GSM_RESET            P0_25
+#define PN_GSM_PWR_KEY          P0_28
+#define PN_SPI_MOSI             P0_23
+#define PN_SPI_MISO             P0_19
+#define PN_SPI_CLK              P0_24
+#define PN_SPI_CS0              P0_22
+#define PN_SPI_CS1              P0_17
+#define PN_UART_RX              P0_8
+#define PN_UART_TX              P0_6
+#define PN_UART_CTS             P0_30
+#define PN_UART_RTS             P0_31
+#define PN_OUT_BUZZER           P0_7
+#define PN_ACC_INT1             P0_5
+#define PN_ACC_INT2             P0_4
+#define PN_VREG_EN              P0_29
+#define PN_I2C_SDA              P0_26
+#define PN_I2C_SCL              P0_27
+
+//NVSTORAGE KEYS
+#define NV_IDENTIFIER          1
+
+
+
 //LEDS
 extern DigitalOut led1;
 
 //PERIPHERAL PINS
 extern DigitalOut vreg_en;
-extern DigitalOut gsm_pwkey;
\ No newline at end of file
+extern DigitalOut gsm_pwkey;
+
+//extern DigitalIn lis3dh_int1;
+//extern DigitalIn lis3dh_int2;
\ No newline at end of file
--- a/main.cpp	Mon Nov 12 10:56:23 2018 +0000
+++ b/main.cpp	Wed Nov 14 16:08:28 2018 +0000
@@ -29,8 +29,9 @@
 //------------------------------------------------------------------------------ 
 #if defined ( __CC_ARM )
 /** THIS IS THE MBED ONLINE COMPILER TOOLCHAIN*/
-static uint8_t          RET_mode       __attribute__((section("noinit"),zero_init));        
-static uint32_t         RET_unixtime      __attribute__((section("noinit"),zero_init)); 
+static uint8_t          RET_mode                __attribute__((section("noinit"),zero_init));        
+static uint32_t         RET_unixtime_backup     __attribute__((section("noinit"),zero_init));
+static char             RET_buffer[64]          __attribute__((section("noinit"),zero_init));
 #elif defined ( __GNUC__ )
 #elif defined ( __ICCARM__ )
 #endif
@@ -69,28 +70,36 @@
 //------------------------------------------------------------------------------
 //Pin states
 //------------------------------------------------------------------------------ 
-DigitalOut led1(p11);
-DigitalOut vreg_en(p29);
-DigitalOut gsm_pwkey(p28);
-DigitalOut lis3dh_cs(p22);
+DigitalOut led1(PN_LED);
+DigitalOut vreg_en(PN_VREG_EN);
+DigitalOut gsm_pwkey(PN_GSM_PWR_KEY);
+DigitalOut lis3dh_cs(PN_SPI_CS0);
+DigitalOut flash_cs(PN_SPI_CS1);
+DigitalIn lis3dh_int1(PN_ACC_INT1); //IMPACT
+DigitalIn lis3dh_int2(PN_ACC_INT2); //DISTURBED
 
 //------------------------------------------------------------------------------
 //Peripherals
 //------------------------------------------------------------------------------ 
 //BLE myble;
 #if NEED_CONSOLE_OUTPUT
-Serial uart(p6, p8, 115200);
+Serial uart(PN_UART_TX, PN_UART_RX, 115200);
 #endif
-//ATSerial atserial(p6,p8,115200);
+//ATSerial atserial(PN_UART_TX, PN_UART_RX, 115200);
 WatchdogTimer watchdog(65.0); //Do not set to less than 4500ms or can cause issues with softdevice
 
+//------------------------------------------------------------------------------
+//Singletons
+//------------------------------------------------------------------------------
+NVStore &nvstore = NVStore::get_instance();
+
 
 
 void gotoSleep(long sleep_milliseconds) {
     //accelerometer.configureForSleep();
     
     if (requireSoftReset) { //dont need to clear this var as reset changes it back to false
-        RET_unixtime = time(NULL); //save unixtime for reset
+        RET_unixtime_backup = time(NULL); //save unixtime for reset
         //NVIC_SystemReset();
         system_reset();
     }
@@ -108,13 +117,101 @@
     led1 = 1;
 }
 
+bool GSMon() {
+    //power on GSM
+    vreg_en = 1;
+    ThisThread::sleep_for(500);
+    gsm_pwkey = 0;
+    ThisThread::sleep_for(1500);
+    gsm_pwkey = 1;
+    LED1on(1000);
+}
+
+void setup_CS_LIS3DH()
+{
+    //Without this setup CS lines of AT45 & LIS3DH are in conflict, causing huge current consumption    
+    lis3dh_cs = 1; //not selected
+    flash_cs = 1; //not selected
+    //AT_RS = 0; //asserted == reset state
+    //wait_ms(100);
+    //AT_RS = 1;
+}
+
+void lis3dh_configureForSleep() {
+    //init
+    LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK);
+    requireSoftReset = true; //WE HAVE STARTED SPI SO NEED THIS
+    //Mode
+    lis3dh.InitLIS3DH(LIS3DH_NORMAL, LIS3DH_ODR_25Hz, LIS3DH_FULLSCALE_8);   
+    lis3dh.LIS3DH_SetIntMode(LIS3DH_INT_MODE_6D_MOVEMENT); 
+    //Int1
+    lis3dh.LIS3DH_SetInt1Pin(LIS3DH_CLICK_ON_PIN_INT1_DISABLE | LIS3DH_I1_INT1_ON_PIN_INT1_ENABLE |              
+                    LIS3DH_I1_INT2_ON_PIN_INT1_DISABLE | LIS3DH_I1_DRDY1_ON_INT1_DISABLE | LIS3DH_I1_DRDY2_ON_INT1_DISABLE |
+                    LIS3DH_WTM_ON_INT1_DISABLE | LIS3DH_INT1_OVERRUN_DISABLE );
+    lis3dh.LIS3DH_SetInt1Threshold(10);             
+    lis3dh.LIS3DH_SetIntConfiguration(LIS3DH_INT1_ZHIE_ENABLE | LIS3DH_INT1_ZLIE_ENABLE |
+                                       LIS3DH_INT1_YHIE_ENABLE | LIS3DH_INT1_YLIE_ENABLE |
+                                       LIS3DH_INT1_XHIE_ENABLE | LIS3DH_INT1_XLIE_ENABLE );   
+    lis3dh.LIS3DH_SetInt1Duration(0);
+    lis3dh.LIS3DH_Int1LatchEnable(MEMS_ENABLE);
+    //HP filter
+    lis3dh.LIS3DH_SetHPFMode(LIS3DH_HPM_AUTORESET_INT);
+    lis3dh.LIS3DH_SetFilterDataSel(MEMS_ENABLE);
+    lis3dh.LIS3DH_HPFAOI1Enable(MEMS_ENABLE);
+    lis3dh.LIS3DH_HPFAOI2Enable(MEMS_ENABLE);
+}
+
 void resetState() {
     firstBoot = true;
+    
+    //RESET VARS
     RET_mode = 0;
-    RET_unixtime = 0;
-    set_time(RET_unixtime);
+    RET_unixtime_backup = 0;
+    set_time(RET_unixtime_backup);
+    
+    //SET IDENTIFIER
+    uint32_t nv_value = 12345678;
+    int rc = nvstore.set(NV_IDENTIFIER, sizeof(nv_value), &nv_value);
 }
 
+void mainStateEngine() {
+    
+    RET_mode = MODE_NORMAL;
+    
+    
+    switch(RET_mode) {
+        case MODE_SETUP :
+            resetState();
+            //selftest();
+            break;
+            
+        case MODE_NORMAL :
+            
+            if (RET_requireMotionFlag) {
+                   bool MotionFlagTriggered = false;
+                   
+                   //check interrupt
+                   if (lis3dh_int2) {
+                       MotionFlagTriggered = true;
+                   }
+            }
+            
+            break;
+            
+        case MODE_DORMANT :
+           
+            break;
+            
+        case MODE_OFF_48HRS :
+           
+            break;
+      
+        default :
+            RET_mode = MODE_SETUP;
+    }
+}
+
+
 int main() {
     led1 = 1;
     
@@ -128,11 +225,11 @@
             break;
             case 0x00000002  :
                 DEBUG("reset_reason: 0x%08x. - Watchdog\n",NRF_POWER->RESETREAS);
-                set_time(RET_unixtime);
+                set_time(RET_unixtime_backup);
             break;
             case 0x00000004  :
                 DEBUG("reset_reason: 0x%08x. - Soft reset\n",NRF_POWER->RESETREAS);
-                set_time(RET_unixtime);
+                set_time(RET_unixtime_backup);
             break;
         }
         NRF_POWER->RESETREAS = 0xffffffff;
@@ -149,66 +246,6 @@
     }
 }
 
-bool GSMon() {
-    //power on GSM
-    vreg_en = 1;
-    ThisThread::sleep_for(500);
-    gsm_pwkey = 0;
-    ThisThread::sleep_for(1500);
-    gsm_pwkey = 1;
-    LED1on(1000);
-}
-
-
-void mainStateEngine() {
-    
-    //TEST AREA
-    LIS3DH accelerometer(p23, p19, p24, p22, LIS3DH_DR_NR_LP_25HZ, LIS3DH_FS_8G);
-    requireSoftReset = true;
-    if (!accelerometer.selfTest()) {
-         LED1on(100);
-    }
-    accelerometer.configureForSleep();
-    //ThisThread::sleep_for(10000);
-    
-    
-    
-    
-    
-    
-    
-    bool accel_awake;
-
-    RET_mode = MODE_SETUP;
-    
-    switch(RET_mode) {
-        case MODE_SETUP :
-            
-            //selftest();
-            break;
-        
-        case MODE_INTERVAL :
-          
-            break;
-            
-        case MODE_INTELLIGENT :
-          
-            break;
-            
-        case MODE_MTU :
-            //mode_mtu();
-            break;
-            
-        case MODE_DORMANT :
-           
-            break;
-      
-        default :
-            RET_mode = MODE_SETUP;
-    }
-    
-}
-
 
 
 /*
@@ -223,9 +260,11 @@
         accel_healthy = true;
     }
 }
+*/
 
+/*
 void mode_mtu() {
-            bool accel_awake = accelerometer.getINT2();
+                bool accel_awake = accelerometer.getINT2();
             
                 //CHECK FOR MOTION
                 if (accel_awake == true) {
@@ -303,6 +342,7 @@
 
 
 
+
 /*
 
 int selftest() {
@@ -410,4 +450,22 @@
 
         //time_t unixtime = time(NULL);
         //DEBUG("got: %i, %d, %d \n", RET_mode, RET_unixtime, unixtime);
-        
\ No newline at end of file
+     
+/*
+uint8_t acc_id;
+        lis3dh.LIS3DH_GetWHO_AM_I(&acc_id);
+        uart.printf("%i \n\r", acc_id);
+        */   
+        
+/*
+ AxesRaw_t                   accel_raw; 
+        lis3dh.LIS3DH_GetAccAxesRaw(&accel_raw);
+        
+        uart.printf("x=");
+        uart.printf("%i", accel_raw.AXIS_X);
+        uart.printf("   y=");
+        uart.printf("%i", accel_raw.AXIS_Y);
+        uart.printf("   z=");
+        uart.printf("%i", accel_raw.AXIS_Z);
+        uart.printf("\n\r");
+*/
\ No newline at end of file
--- a/main.h	Mon Nov 12 10:56:23 2018 +0000
+++ b/main.h	Wed Nov 14 16:08:28 2018 +0000
@@ -21,7 +21,7 @@
 #include "mbed.h"
 #include "WatchdogTimer.h"
 #include "ble/BLE.h"
-//#include "nvstore.h"
+#include "nvstore.h"
 //#include "ATCmdParser.h"
 //#include "UARTSerial.h"
 
@@ -72,7 +72,7 @@
 #define lowByte(w) ((uint8_t) ((w) & 0xff))
 #define highByte(w) ((uint8_t) ((w) >> 8))
 
-#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; * it will have an impact on code-size and power consumption. */
+#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; * it will have an impact on code-size and power consumption. */
 #if NEED_CONSOLE_OUTPUT
 #define DEBUG(...) { uart.printf(__VA_ARGS__); }
 #else
--- a/mbed_app.json	Mon Nov 12 10:56:23 2018 +0000
+++ b/mbed_app.json	Wed Nov 14 16:08:28 2018 +0000
@@ -10,7 +10,12 @@
             "target.OUTPUT_EXT": "bin",
             "target.uart_hwfc": 0,
             "target.device_has_add": ["LOWPOWERTIMER"],
-            "target.macros_add": ["MBED_TICKLESS"]
+            "target.macros_add": ["MBED_TICKLESS"],
+            "nvstore.max_keys": 20,
+            "nvstore.area_1_address": "0x0007e000",
+            "nvstore.area_1_size"   : "0x1000",
+            "nvstore.area_2_address": "0x0007f000",
+            "nvstore.area_2_size"   : "0x1000"
         }
     }
 }
\ No newline at end of file
--- a/modes.h	Mon Nov 12 10:56:23 2018 +0000
+++ b/modes.h	Wed Nov 14 16:08:28 2018 +0000
@@ -1,6 +1,15 @@
 #define MODE_SETUP              0
-#define MODE_INTERVAL           2
-#define MODE_INTELLIGENT        3
-#define MODE_MTU                4
+#define MODE_NORMAL             1
 #define MODE_DORMANT            99
-#define MODE_OFF                100
\ No newline at end of file
+#define MODE_OFF_48HRS          100
+
+
+//TASKS
+
+/*
+Location - OperationMode, FixType, timeout, timesbitmap, 
+Environmental - Log interval, Broadcast Interval
+Impact - Threshold, Log, AlarmBroadcast
+Activity - G Threshold, T Threshold, Broadcast Interval
+
+*/
\ No newline at end of file