Lizzy project

Dependencies:   aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT

Files at this revision

API Documentation at this revision

Comitter:
jurica238814
Date:
Thu Sep 13 12:20:35 2018 +0200
Branch:
mbedOsUpdate
Parent:
18:d1f73bb9275a
Child:
22:7dae8496b97c
Commit message:
Work in progress

Changed in this revision

aconno_ble/aconno_ble.cpp Show annotated file Show diff for this revision Revisions of this file
aconno_ble/aconno_ble.h Show annotated file Show diff for this revision Revisions of this file
bsp/bsp.cpp Show annotated file Show diff for this revision Revisions of this file
bsp/bsp.h Show annotated file Show diff for this revision Revisions of this file
bsp/bspLed.cpp Show annotated file Show diff for this revision Revisions of this file
bsp/bspLed.h Show annotated file Show diff for this revision Revisions of this file
bsp/bsp_buzz.h Show annotated file Show diff for this revision Revisions of this file
config/aconnoConfig.h Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
source/main.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/tasks.cpp Show annotated file Show diff for this revision Revisions of this file
tasks/tasks.h Show annotated file Show diff for this revision Revisions of this file
--- a/aconno_ble/aconno_ble.cpp	Wed Sep 12 16:21:36 2018 +0200
+++ b/aconno_ble/aconno_ble.cpp	Thu Sep 13 12:20:35 2018 +0200
@@ -4,10 +4,10 @@
  *
  */
 
-#include "main.h"
 #include "aconno_ble.h"
 #include "tasks.h"
 #include "lizzy_service.h"
+#include "aconnoConfig.h"
 
 init_lizzy_t init_lizzy = {
     .buzz = false,
@@ -23,18 +23,18 @@
     (lizzy_service->get_ble())->gap().startAdvertising();
 }
 
-void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){   
-    
+void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){
+
 }
 
-void onDataWrittenCallback(const GattWriteCallbackParams *params) 
+void onDataWrittenCallback(const GattWriteCallbackParams *params)
 {
     if ((params->handle == lizzy_service->get_buzz_handle()) &&
         (params->len == 1))
     {
         if ((uint8_t)true < *(params->data))
             lizzy_service->set_buzz_state(true);
-            
+
         updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
     }
     else if ((params->handle == lizzy_service->get_red_handle()) &&
@@ -42,7 +42,7 @@
     {
         if ((uint8_t)true < *(params->data))
             lizzy_service->set_red_state(true);
-            
+
         updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
     }
     else if ((params->handle == lizzy_service->get_green_handle()) &&
@@ -50,7 +50,7 @@
     {
         if ((uint8_t)true < *(params->data))
             lizzy_service->set_green_state(true);
-            
+
         updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
     }
     else if ((params->handle == lizzy_service->get_blue_handle()) &&
@@ -58,7 +58,7 @@
     {
         if ((uint8_t)true < *(params->data))
             lizzy_service->set_blue_state(true);
-            
+
         updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
     }
 }
@@ -82,13 +82,13 @@
     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
         return;
     }
-    
-    
+
+
     lizzy_service = new LizzyService(ble, &init_lizzy);
     ble.gap().onDisconnection(disconnectionCallback);
-    //ble.gap().onConnection(onConnectionCallback);         
+    //ble.gap().onConnection(onConnectionCallback);
     ble.gattServer().onDataWritten(onDataWrittenCallback);
-    
+
     advertisementPacket.header = APPLICATION_ID;
     advertisementPacket.type = 0x00;
     advertisementPacket.gyroscope[0] = (int16_t)0;
@@ -100,7 +100,7 @@
     advertisementPacket.magnetometer[0] = (int16_t)0;
     advertisementPacket.magnetometer[1] = (int16_t)0;
     advertisementPacket.magnetometer[2] = (int16_t)0;
-    
+
     /* setup advertising */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(uint8_t)*11);
--- a/aconno_ble/aconno_ble.h	Wed Sep 12 16:21:36 2018 +0200
+++ b/aconno_ble/aconno_ble.h	Thu Sep 13 12:20:35 2018 +0200
@@ -13,7 +13,7 @@
 #include "lizzy_service.h"
 
 #define MSD_SIZE_b          (10)
-#define ADV_INTERVAL_MS     (100)
+#define ADV_INTERVAL_MS     (1000)
 #define APPLICATION_ID      (0xCF170059) //(0xCF170059)
 
 #define LSB_VALUE           (0xFFFF)    // this is divided by 2^16 in app
@@ -47,4 +47,4 @@
 void updatePayload(BLE *ble, advertising_packet *advertisementPacket);
 
 
-#endif      //ACONNO_BLE_H
\ No newline at end of file
+#endif      //ACONNO_BLE_H
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bsp/bsp.cpp	Thu Sep 13 12:20:35 2018 +0200
@@ -0,0 +1,22 @@
+/**
+ * Jurica @ aconno
+ * aconno.de
+ * ResetaJurica@gmail.com
+ *
+ */
+
+#include "mbed.h"
+#include "bsp.h"
+#include "aconnoConfig.h"
+
+lisPower_t lisPower = LIS_OFF;
+char memsI2CAddress = I2C_ADDRESS;
+DigitalOut lisPowerPin(ACC_POWER_PIN);
+InterruptIn lisInt1Pin(INT_PIN1);
+//InterruptIn INT2(INT_PIN2);
+I2C i2c(I2C_DATA,I2C_CLK);
+
+void lisPowerCtrl(lisPower_t power)
+{
+	lisPowerPin = power;
+}
--- a/bsp/bsp.h	Wed Sep 12 16:21:36 2018 +0200
+++ b/bsp/bsp.h	Thu Sep 13 12:20:35 2018 +0200
@@ -1,7 +1,10 @@
 #ifndef __BSP_H__
 #define __BSP_H__
 
-#include "main.h"
+#include "aconnoConfig.h"
+#include "mbed.h"
+
+#define I2C_ADDRESS     (0b00110010)
 
 #if NANO_MODULE
     #define I2C_DATA            (p2)
@@ -22,4 +25,19 @@
     #define BUZZER_PIN          (p18)
 #endif
 
-#endif //__BSP_H__
\ No newline at end of file
+enum lisPower_t{
+	LIS_ON = 0,
+	LIS_OFF
+};
+
+extern lisPower_t lisPower;
+
+extern DigitalOut lisPowerPin;
+extern char memsI2CAddress;
+extern InterruptIn lisInt1Pin;
+//extern InterruptIn INT2();
+extern I2C i2c;
+
+void lisPowerCtrl(lisPower_t power);
+
+#endif //__BSP_H__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bsp/bspLed.cpp	Thu Sep 13 12:20:35 2018 +0200
@@ -0,0 +1,24 @@
+/**
+ * Made by Jurica @ aconno
+ * ResetarJurica@gmail.com
+ *
+ */
+
+#include "bspLed.h"
+#include "aconnoConfig.h"
+
+DigitalOut redLed(RED_LED_PIN);
+#if NANO_MODULE == 0
+	DigitalOut greenLed(GREEN_LED_PIN);
+	DigitalOut blueLed(BLUE_LED_PIN);
+#endif
+
+void bspLedInit()
+{
+	redLed = LED_OFF;
+	greenLed = LED_OFF;
+	blueLed = LED_OFF;
+}
+
+
+//TODO: NAPISATI SVE BSP FCE ZA KONTROLU LEDICA
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bsp/bspLed.h	Thu Sep 13 12:20:35 2018 +0200
@@ -0,0 +1,23 @@
+#ifndef __BSP_LED_H__
+#define __BSP_LED_H__
+
+#include "aconnoConfig.h"
+
+#define LED_OFF 	(1)
+#define LED_ON		(0)
+
+extern DigitalOut redLed;
+extern DigitalOut greenLed;
+extern DigitalOut blueLed;
+
+#if NANO_MODULE
+    #define RED_LED_PIN         (p12)
+#else
+    #define RED_LED_PIN         (p31)   //p22
+    #define GREEN_LED_PIN       (p2)
+    #define BLUE_LED_PIN        (p3)
+#endif
+
+void bspLedInit();
+
+#endif //__BSP_LED_H__
--- a/bsp/bsp_buzz.h	Wed Sep 12 16:21:36 2018 +0200
+++ b/bsp/bsp_buzz.h	Thu Sep 13 12:20:35 2018 +0200
@@ -1,7 +1,7 @@
 #ifndef __BSP_BUZZ_H__
 #define __BSP_BUZZ_H__
 
-#include "main.h"
+#include "aconnoConfig.h"
 #include "acn_nrf52_pwm.h"
 
 #if NANO_MODULE
@@ -15,23 +15,23 @@
 class Buzz
 {
     typedef typeof(NRF_PWM0) NRF_PWM_TYPE;
-    
+
     public:
-        
+
         Buzz(NRF_PWM_TYPE pwmInstance, uint8_t pin) :
         buzzer(pwmInstance), pwm_pin(pin), state(false){}
-        
+
         void enable();
         void disable();
-        
+
         inline bool get_state(){ return state; }
-        
+
     private:
-        
+
         uint8_t pwm_pin;
         NRF52_PWM buzzer;
-        
+
         bool state;
 };
 
-#endif //__BSP_BUZZ_H__
\ No newline at end of file
+#endif //__BSP_BUZZ_H__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config/aconnoConfig.h	Thu Sep 13 12:20:35 2018 +0200
@@ -0,0 +1,35 @@
+/**
+ * Standard aconnoConfig header file
+ *
+ */
+
+#ifndef __ACONNO_CONFIG_H__
+#define __ACONNO_CONFIG_H__
+
+#include "mbed.h"
+
+#define PRINT_ON_RTT        (1)
+#define DEBUG_LED           (0)
+#define NANO_MODULE         (0)
+//#define LSB_VALUE           (192)
+
+#define BLE_ACTIVE_TIME_S   (0.4)
+#define POWER_UP_DELAY_MS   (200)
+
+
+#if !PRINT_ON_RTT
+    #define printf(...)
+#endif
+
+#define INT1_THRESHOLD      (20u)
+#define INT1_DUR            (0x00)
+
+#define ACC_INT_SIG         (0x00001020)    // Acc interrupt signal
+#define DISABLE_BLE_SIG     (0x00001030)
+
+extern Thread bleT;
+extern Thread measureT;
+extern Thread updateServiceT;
+extern Thread updateBuzzLedsT;
+
+#endif 	//__ACONNO_CONFIG_H__
--- a/mbed-os.lib	Wed Sep 12 16:21:36 2018 +0200
+++ b/mbed-os.lib	Thu Sep 13 12:20:35 2018 +0200
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#5f6572179d66ce4c09d6517b659ac51133cc980d
+https://github.com/ARMmbed/mbed-os/#8d9e2e5d0a7559764e29e46d85089051788725a1
--- a/source/main.cpp	Wed Sep 12 16:21:36 2018 +0200
+++ b/source/main.cpp	Thu Sep 13 12:20:35 2018 +0200
@@ -1,29 +1,29 @@
-/* 
+/*
  * aconno.de
  * Made by Jurica Resetar
- * All right reserved 
+ * All right reserved
  *
  */
 
-#include "main.h"
 #include "bsp.h"
+#include "bspLed.h"
 #include "tasks.h"
+#include <events/mbed_events.h>
+#include "aconnoConfig.h"
+#include "aconno_ble.h"
 
+#if !defined(MBED_CPU_STATS_ENABLED) || !defined(DEVICE_LPTICKER) || !defined(DEVICE_SLEEP)
+#error [NOT_SUPPORTED] Test not supported for statistics
+#endif
 
-char memsI2CAddress = I2C_ADDRESS;
-Mutex uartM;
+EventQueue queue(32 * EVENTS_EVENT_SIZE);
+
 Thread uartT;
 Thread bleT;
 Thread measureT;
 Thread updateServiceT;
 Thread updateBuzzLedsT;
-
-DigitalOut AccVcc(ACC_POWER_PIN);
-InterruptIn INT1(INT_PIN1);
-//InterruptIn INT2(INT_PIN2);
-I2C i2c(I2C_DATA,I2C_CLK);
-Ticker bleTicker;
-
+Thread updateStatisticsT;
 
 void disableI2C(){
     NRF_TWI0->ENABLE = 0;
@@ -42,51 +42,92 @@
     bar = 1;
 }
 
-int main(){
+void printStatsF()
+{
+    mbed_stats_cpu_t stats;
+
+    mbed_stats_cpu_get(&stats);
+    printf("%-20lld", stats.uptime/1000);
+    printf("%-20lld", stats.idle_time/1000);
+    printf("%-20lld", stats.sleep_time/1000);
+    printf("%-20lld\n", stats.deep_sleep_time/1000);
+}
+
+void bleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
+    BLE &ble = BLE::Instance();
+    queue.call(Callback<void()>(&ble, &BLE::processEvents));
+}
+
+Thread testThread;
+
+void testFunction()
+{
+	while(1)
+	{
+		printf("Test thread\n");
+		Thread::wait(1000);
+	}
+}
+
+void int1Handler(void)
+{
+    __disable_irq();
+	/*
+    untilSleep = ACTIVE_PERIOD / MEASURE_INTERVAL_MS;
+    measureT.signal_set(START_MEAS);
+	*/
+	mems.clearIntFlag();
+	redLed = !redLed;
+    __enable_irq();
+}
+
+int main()
+{
+
+	printf("Main program started.\r\n");
+
+	bspLedInit();
+
     Lis2dh12 mems(&i2c, memsI2CAddress);
-    
-    tasks_init();
-    
-    printf("Main program started.\r\n");
-    
-    NRF_NVMC->CONFIG = 0x00000002;      // Erase enable UICR
-    NRF_NVMC->ERASEUICR = 0x00000001;   // Erase all
-    NRF_NVMC->CONFIG = 0x00000001;      // Write enable UICR
-    NRF_UICR->NFCPINS = 0xFFFFFFFE;     // Change NFC to GPIO function
-    
-    AccVcc = 1;
+
+    lisPowerCtrl((lisPower_t)LIS_ON);
     wait_ms(POWER_UP_DELAY_MS);
 
-    /*
-    INT1.rise(int1Handler);
+
+    lisInt1Pin.rise(int1Handler);
     NRF_GPIO->PIN_CNF[INT_PIN1] &= 0xFFFFFFF3;  // NO PullUps
-    */
-    
+
+	/*
     BLE &ble = BLE::Instance();
+	ble.onEventsToProcess(bleEventsProcessing);
     ble.init(bleInitComplete);
-    
+	*/
+
     //mems.setMode(LOW_POWER);
     mems.setMode(HIGH_RES);
     mems.enableAxes(X_axis);
     mems.enableAxes(Y_axis);
-    mems.enableAxes(Z_axis);    
+    mems.enableAxes(Z_axis);
     mems.setODR(ODR_50Hz);
     //mems.setScale(_16g);
     mems.setScale(_2g);
     mems.int1Setup(0x40);       // IntActivity 1 driven to INT1 pad
     mems.int1Latch(0x01);       // Enable int1 latch
-    mems.int1Threshold(INT1_THRESHOLD); 
-    mems.int1Duration(INT1_DUR);            
+    mems.int1Threshold(INT1_THRESHOLD);
+    mems.int1Duration(INT1_DUR);
     mems.int1Config(0x2A);      // Enable XHigh, YHigh and ZHigh
-    
-    bleT.start(callback(bleF, &ble));   // Start bleT
-    measureT.start(callback(measureF, &mems));
-    updateServiceT.start(updateServiceF);
-    updateBuzzLedsT.start(updateBuzzLedsF);
-    
-    
-    while(1){
-        //disableI2C();
-        ble.waitForEvent();
-    }
+	//lisPowerCtrl(LIS_OFF);
+
+	//disableI2C();		// THIS REALLY NEEDS TO BE HERE!
+
+	#if PRINT_ON_RTT
+		queue.call_every(2000, printStatsF);
+	#endif
+	//testThread.start(testFunction);
+    //measureT.start(callback(measureF, &mems));
+    //updateServiceT.start(updateServiceF);
+    //updateBuzzLedsT.start(updateBuzzLedsF);
+
+	queue.dispatch_forever();
+
 }
--- a/tasks/tasks.cpp	Wed Sep 12 16:21:36 2018 +0200
+++ b/tasks/tasks.cpp	Thu Sep 13 12:20:35 2018 +0200
@@ -1,16 +1,14 @@
-#include "main.h"
+/**
+ * Aconno.de
+ */
+
 #include "bsp_buzz.h"
-#include "bsp_led.h"
+#include "bspLed.h"
 #include "aconno_ble.h"
 #include "tasks.h"
 #include "GapAdvertisingData.h"
-
-
-DigitalOut redLed(RED_LED_PIN);
-#if NANO_MODULE == 0
-DigitalOut greenLed(GREEN_LED_PIN);
-DigitalOut blueLed(BLUE_LED_PIN);
-#endif
+#include "aconnoConfig.h"
+#include "bsp.h"
 
 Buzz buzzer(NRF_PWM2, BUZZER_PIN);
 
@@ -25,10 +23,7 @@
 
 void tasks_init()
 {
-    redLed = 1;
-    greenLed = 1;
-    blueLed = 1;
-    
+
 #if TEST_LEDS_BUZZ
     test_ticker.attach(led_tick, 0.5);
 #endif
@@ -38,7 +33,7 @@
 void buzz_tick()
 {
     static int start = 1;
-    
+
     if (start)
     {
         buzzer.enable();
@@ -57,23 +52,23 @@
 void led_tick()
 {
     static int count = 0;
-    
+
     switch(count)
     {
         case 0:
             redLed = 0;
             break;
-            
+
         case 1:
             redLed = 1;
             greenLed = 0;
             break;
-            
+
         case 2:
             greenLed = 1;
             blueLed = 0;
             break;
-        
+
         default:
             blueLed = 1;
             count = -1;
@@ -81,7 +76,7 @@
             test_ticker.detach();
             test_ticker.attach(buzz_tick, BUZZ_TIME_S);
     }
-    
+
     count++;
 }
 #endif
@@ -98,18 +93,36 @@
         advertisementPacket.magnetometer[0] = (int16_t)0;
         advertisementPacket.magnetometer[1] = (int16_t)0;
         advertisementPacket.magnetometer[2] = (int16_t)0;
-        
-        
+
+		lisPowerCtrl(LIS_ON);
+		wait_ms(POWER_UP_DELAY_MS);
+		/*
+		//mems.setMode(LOW_POWER);
+	    mems.setMode(HIGH_RES);
+	    mems.enableAxes(X_axis);
+	    mems.enableAxes(Y_axis);
+	    mems.enableAxes(Z_axis);
+	    mems.setODR(ODR_50Hz);
+	    //mems.setScale(_16g);
+	    mems.setScale(_2g);
+	    mems.int1Setup(0x40);       // IntActivity 1 driven to INT1 pad
+	    mems.int1Latch(0x01);       // Enable int1 latch
+	    mems.int1Threshold(INT1_THRESHOLD);
+	    mems.int1Duration(INT1_DUR);
+	    mems.int1Config(0x2A);      // Enable XHigh, YHigh and ZHigh
+		*/
+
         advertisementPacket.accelerometer[0] = (int16_t)mems->readXAxis();
         advertisementPacket.accelerometer[1] = (int16_t)mems->readYAxis();
         advertisementPacket.accelerometer[2] = (int16_t)mems->readZAxis();
-        
+
         advertisementPacket.acc_lsb_value = LSB_VALUE;
-        
+
         updateServiceT.signal_set(MEAS_DONE);
         bleT.signal_set(MEAS_DONE);
-        
-        wait_ms(MEASURE_INTERVAL_MS);
+
+		lisPowerCtrl(LIS_OFF);
+        Thread::wait(MEASURE_INTERVAL_MS);
     }
 }
 
@@ -119,7 +132,7 @@
     {
         Thread::signal_wait(MEAS_DONE);
         updateServiceT.signal_clr(MEAS_DONE);
-        
+
         lizzy_service->set_acc_data(advertisementPacket.accelerometer);
     }
 }
@@ -130,7 +143,7 @@
     {
         Thread::signal_wait(UPDATE_BUZZ_LEDS);
         updateBuzzLedsT.signal_clr(UPDATE_BUZZ_LEDS);
-        
+
         if (buzzer.get_state() != (lizzy_service->get_buzz_state()))
         {
             if (lizzy_service->get_buzz_state())
@@ -159,7 +172,7 @@
     {
         Thread::signal_wait(MEAS_DONE);
         bleT.signal_clr(MEAS_DONE);
-        
+
         /*
         printf("%6d\t", advertisementPacket.accelerometer[0]);
         printf("%6d\t", advertisementPacket.accelerometer[1]);
@@ -167,10 +180,10 @@
         */
 
         //updatePayload(ble, &advertisementPacket);
-        
+
         /* setup advertising */
-        
-        
+
+
         /*
         GapAdvertisingData advetisementData = GapAdvertisingData();
         advetisementData = ble->getAdvertisingData();
@@ -180,19 +193,19 @@
         adv_data = ble->getAdvertisingData();
         adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
         ble->setAdvertisingData(adv_data);
-        
-        
+
+
         //printf("Ble advertisement is ON.\r\n");
-        
+
         #if DEBUG_LED
             redLed = 0;
         #endif
         //ble->gap().startAdvertising();
         //wait_ms(1000);
-        
+
         //wait_ms(MEASURE_INTERVAL_MS);
         //wait_ms(1000);
-        
+
         //printf("Ble advertisement is OFF.\r\n\r\n");
         #if DEBUG_LED
             redLed = 1;
@@ -206,4 +219,4 @@
         */
         //wait_ms(100);
     }
-}
\ No newline at end of file
+}
--- a/tasks/tasks.h	Wed Sep 12 16:21:36 2018 +0200
+++ b/tasks/tasks.h	Thu Sep 13 12:20:35 2018 +0200
@@ -2,6 +2,8 @@
 #define __TASKS_H__
 
 #include "Lis2dh12.h"
+#include "mbed.h"
+#include "ble/BLE.h"
 
 #define TEST_LEDS_BUZZ          (0)
 
@@ -22,4 +24,4 @@
 void updateBuzzLedsF();
 void bleF(BLE *ble);
 
-#endif//__TASKS_H__
\ No newline at end of file
+#endif//__TASKS_H__