init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Revision:
6:388d3c7efdd9
Parent:
5:8f8951127724
Child:
7:e9a19750700d
--- 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