From e88a8dcd554b3d689285dcd0fa5c63e47a5206a5 Mon Sep 17 00:00:00 2001 From: Pete Schwamb Date: Mon, 24 Apr 2017 12:55:37 -0500 Subject: [PATCH] Adding battery sampling --- Makefile | 8 ++++---- src/battery.c | 33 +++++++++++++++++++++++++++++++++ src/battery.h | 8 ++++++++ src/handles.h | 8 ++++++++ src/main.c | 2 +- src/scheduler.c | 29 ++++++++++++++++++----------- src/scheduler.h | 6 +++++- src/sensor.c | 20 ++++++++++++++++---- 8 files changed, 93 insertions(+), 21 deletions(-) create mode 100644 src/battery.c create mode 100644 src/battery.h create mode 100644 src/handles.h diff --git a/Makefile b/Makefile index 7ac42c5..4e95d5b 100644 --- a/Makefile +++ b/Makefile @@ -23,11 +23,11 @@ RBC_MESH := rbc_mesh LINKER_SCRIPT := $(SIMBLEE_BASE)/variants/Simblee/linker_scripts/gcc/Simblee.ld RFD_LOADER := $(SIMBLEE_BASE)/RFDLoader_osx -#SERIAL_PORT := /dev/cu.usbserial-DN00D34P # left -SERIAL_PORT := /dev/cu.usbserial-DN00CSZ7 # right +SERIAL_PORT := /dev/cu.usbserial-DN00D34P # left +#SERIAL_PORT := /dev/cu.usbserial-DN00CSZ7 # right #SERIAL_PORT := /dev/cu.usbserial-A105RB12 #SERIAL_PORT := /dev/cu.usbserial-FTZ86FTC -#SERIAL_PORT := /dev/cu.usbserial-DO00C2G2 +SERIAL_PORT := /dev/cu.usbserial-DO00C2G2 # Sparkfun board ifeq ($(USE_RBC_MESH_SERIAL), "yes") SERIAL_STRING := "_serial" @@ -93,7 +93,7 @@ remduplicates = $(strip $(if $1,$(firstword $1) $(call remduplicates,$(filter-ou # source common to all targets -C_SOURCE_FILES += src/main.c src/leds.c src/config.c src/sensor.c src/app_cmd.c src/scheduler.c src/proximity.c src/heartbeat.c +C_SOURCE_FILES += src/main.c src/leds.c src/config.c src/sensor.c src/app_cmd.c src/scheduler.c src/proximity.c src/heartbeat.c src/battery.c C_SOURCE_FILES += $(COMPONENTS)/libraries/timer/app_timer.c CXX_SOURCE_FILES += $(SIMBLEE_BASE)/libraries/SimbleeBLE/SimbleeBLE.cpp diff --git a/src/battery.c b/src/battery.c new file mode 100644 index 0000000..01b8c34 --- /dev/null +++ b/src/battery.c @@ -0,0 +1,33 @@ +#include "battery.h" +#include "nrf_adc.h" + +uint8_t get_battery_adc() { + uint8_t res; + // Configure ADC + NRF_ADC->CONFIG = (ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos) | + (ADC_CONFIG_INPSEL_SupplyOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) | + (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | + (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) | + (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); + NRF_ADC->EVENTS_END = 0; + NRF_ADC->ENABLE = (ADC_ENABLE_ENABLE_Enabled << ADC_ENABLE_ENABLE_Pos); + + NRF_ADC->EVENTS_END = 0; // Stop any running conversions. + NRF_ADC->TASKS_START = 1; + + while (!NRF_ADC->EVENTS_END); + + res = NRF_ADC->RESULT; + + NRF_ADC->ENABLE = (ADC_ENABLE_ENABLE_Disabled << ADC_ENABLE_ENABLE_Pos); + NRF_ADC->TASKS_STOP = 1; + + // GPIOs release regarding PAN028 + NRF_ADC->CONFIG = (ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos) | + (ADC_CONFIG_INPSEL_SupplyTwoThirdsPrescaling << ADC_CONFIG_INPSEL_Pos) | + (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos) | + (ADC_CONFIG_PSEL_Disabled << ADC_CONFIG_PSEL_Pos) | + (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos); + + return res; +} diff --git a/src/battery.h b/src/battery.h new file mode 100644 index 0000000..9266de7 --- /dev/null +++ b/src/battery.h @@ -0,0 +1,8 @@ +#ifndef BATTERY_H +#define BATTERY_H + +#include "stdint.h" + +uint8_t get_battery_adc(); + +#endif // BATTERY_H diff --git a/src/handles.h b/src/handles.h new file mode 100644 index 0000000..c66c50d --- /dev/null +++ b/src/handles.h @@ -0,0 +1,8 @@ +#ifndef MESH_HANDLES_H +#define MESH_HANDLES_H + +#define SENSOR_HANDLE (0x0100 + get_sensor_id()) +#define TEST_LED_HANDLE (0xfe01) +#define DEBUG_REGISTER_HANDLE (0x1234) + +#endif // MESH_HANDLES_H diff --git a/src/main.c b/src/main.c index cbdd9d9..180f221 100644 --- a/src/main.c +++ b/src/main.c @@ -15,6 +15,7 @@ #include "transport_control.h" #include "scheduler.h" #include "heartbeat.h" +#include "handles.h" #include #include #include @@ -25,7 +26,6 @@ #define MESH_INTERVAL_MIN_MS (100) #define MESH_CHANNEL (38) #define MESH_CLOCK_SRC (NRF_CLOCK_LFCLKSRC_XTAL_75_PPM) -#define TEST_LED_HANDLE (0xfe01) /** @brief General error handler. */ static inline void error_loop(void) diff --git a/src/scheduler.c b/src/scheduler.c index 70f2a02..8d80aad 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -8,6 +8,7 @@ #include "heartbeat.h" #include "sensor.h" #include "rbc_mesh.h" +#include "handles.h" // Timer settings #define APP_TIMER_PRESCALER 15 // divisor value - 1 @@ -16,6 +17,7 @@ #define APP_TIMER_MAX_TIMERS 3 #define APP_TIMER_OP_QUEUE_SIZE 4 +static int32_t m_boot_time; static int32_t m_current_time; static int16_t m_clock_version = 0; static app_timer_id_t m_clock_sync_timer_ID; @@ -25,6 +27,7 @@ static scheduler_state_t m_scheduler_state; static prng_t m_rand; static uint32_t m_clock_second_start_counter_value; + #define MS_TO_TICKS(MS) ((TICKS_PER_100ms * (MS)) / 100) #define TICKS_TO_MS(TICKS) (100 * (TICKS) / TICKS_PER_100ms) @@ -32,7 +35,6 @@ static void offset_timer_cb(void * p_context); static void delay_to_heartbeat(); #define DEBUG_REGISTER_SIZE (16) -#define DEBUG_REGISTER_HANDLE (0x1234) static uint8_t debug_counter; static uint8_t debug_register[DEBUG_REGISTER_SIZE]; @@ -46,19 +48,19 @@ static void report_debug_register() { static void periodic_timer_cb(void * p_context) { - - led_config(LED_GREEN, 1); - app_timer_cnt_get(&m_clock_second_start_counter_value); - m_scheduler_state = SCHEDULER_STATE_BEFORE_HB; m_current_time += 1; - //rbc_mesh_start(); if (m_current_time % 10 == 0) { //report_debug_register(); + led_config(LED_GREEN, 1); + app_timer_cnt_get(&m_clock_second_start_counter_value); + m_scheduler_state = SCHEDULER_STATE_BEFORE_HB; + rbc_mesh_start(); + + // Delay to heartbeat + delay_to_heartbeat(); } - // Delay to heartbeat - delay_to_heartbeat(); } static void delay_to_heartbeat() { @@ -80,7 +82,7 @@ static void delay_to_heartbeat() { } static void do_heartbeat() { - led_config(LED_BLUE, 1); + //led_config(LED_BLUE, 1); uint32_t current_counter; app_timer_cnt_get(¤t_counter); // Modulo wraparound makes this ok @@ -101,7 +103,7 @@ static void delay_to_reporting() { } static void do_reporting() { - led_config(LED_BLUE, 0); + //led_config(LED_BLUE, 0); report_sensor_data(); } @@ -118,7 +120,7 @@ static void delay_to_sleep() { } static void do_sleep() { - //rbc_mesh_stop(); + rbc_mesh_stop(); led_config(LED_GREEN, 0); } @@ -188,6 +190,7 @@ void set_clock_time(int32_t epoch, uint16_t ms, clock_source_t clock_source, int } else if (clock_source == CLOCK_SOURCE_SERIAL) { m_clock_version++; } + m_boot_time += epoch - m_current_time; m_current_time = epoch; uint16_t start_delay = (1000 - ms) % 1000; app_timer_stop(m_periodic_timer_ID); @@ -199,6 +202,10 @@ int32_t get_clock_time() { return m_current_time; } +int32_t get_uptime() { + return m_current_time - m_boot_time; +} + int16_t get_clock_version() { return m_clock_version; } diff --git a/src/scheduler.h b/src/scheduler.h index 538dfad..2ae8177 100644 --- a/src/scheduler.h +++ b/src/scheduler.h @@ -20,15 +20,19 @@ typedef enum { #define MAX_EXPECTED_CLOCK_SKEW_MS (10) #define HEARTBEAT_WINDOW_MS (100) -#define TOTAL_RADIO_WINDOW_MS (400) +#define TOTAL_RADIO_WINDOW_MS (900) void scheduler_init(); void set_clock_time(int32_t epoch, uint16_t ms, clock_source_t clock_source, int16_t clock_version); // Returns unix epoch int32_t get_clock_time(); + // Clock version increments from a master source and is distributed by all // in the heartbeat message int16_t get_clock_version(); +// Seconds since the sensor started +int32_t get_uptime(); + #endif //__SCHEDULER_H_ diff --git a/src/sensor.c b/src/sensor.c index 47a2364..1321e5f 100644 --- a/src/sensor.c +++ b/src/sensor.c @@ -2,10 +2,11 @@ #include "sensor.h" #include "config.h" #include "rbc_mesh.h" +#include "scheduler.h" +#include "battery.h" +#include "handles.h" #include -#define SENSOR_HANDLE (0x0100 + get_sensor_id()) - static sensor_value_t m_value; void sensor_init() { @@ -14,8 +15,19 @@ void sensor_init() { APP_ERROR_CHECK(error_code); } +void gather_sensor_data() { + m_value.uptime = get_uptime(); + m_value.battery = get_battery_adc(); +} + void report_sensor_data() { uint32_t error_code; - error_code = rbc_mesh_value_set(SENSOR_HANDLE, (uint8_t*)&m_value, sizeof(sensor_value_t)); - APP_ERROR_CHECK(error_code); + + if (get_sensor_id() > 0) { + gather_sensor_data(); + error_code = rbc_mesh_value_set(SENSOR_HANDLE, (uint8_t*)&m_value, sizeof(sensor_value_t)); + APP_ERROR_CHECK(error_code); + } else { + // Would be nice to report this somewhere. + } }