Skip to content

Commit

Permalink
Adding battery sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
ps2 committed Apr 24, 2017
1 parent 81fde7b commit e88a8dc
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 21 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions src/battery.c
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 8 additions & 0 deletions src/battery.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef BATTERY_H
#define BATTERY_H

#include "stdint.h"

uint8_t get_battery_adc();

#endif // BATTERY_H
8 changes: 8 additions & 0 deletions src/handles.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "transport_control.h"
#include "scheduler.h"
#include "heartbeat.h"
#include "handles.h"
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
Expand All @@ -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)
Expand Down
29 changes: 18 additions & 11 deletions src/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -25,14 +27,14 @@ 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)

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];

Expand All @@ -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() {
Expand All @@ -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(&current_counter);
// Modulo wraparound makes this ok
Expand All @@ -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();
}

Expand All @@ -118,7 +120,7 @@ static void delay_to_sleep() {
}

static void do_sleep() {
//rbc_mesh_stop();
rbc_mesh_stop();
led_config(LED_GREEN, 0);
}

Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
6 changes: 5 additions & 1 deletion src/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
20 changes: 16 additions & 4 deletions src/sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#include "sensor.h"
#include "config.h"
#include "rbc_mesh.h"
#include "scheduler.h"
#include "battery.h"
#include "handles.h"
#include <app_error.h>

#define SENSOR_HANDLE (0x0100 + get_sensor_id())

static sensor_value_t m_value;

void sensor_init() {
Expand All @@ -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.
}
}

0 comments on commit e88a8dc

Please sign in to comment.