From 59014618345de4a8dd1c7e7cbb72b7653be476eb Mon Sep 17 00:00:00 2001 From: Pete Schwamb Date: Wed, 26 Apr 2017 20:47:10 -0500 Subject: [PATCH] Add sleep_enabled flag to config, and fix subsequent writing of config to flash --- Makefile | 4 ++-- pyaci/interactive_console.py | 4 ++-- pyaci/sensei_cmd.py | 8 +++++--- src/config.c | 21 +++++++++++++++++---- src/config.h | 1 + src/heartbeat.c | 1 + src/leds.h | 2 ++ src/main.c | 3 ++- src/scheduler.c | 13 ++++++++----- src/scheduler.h | 2 +- 10 files changed, 41 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index c84dbee..e466da0 100644 --- a/Makefile +++ b/Makefile @@ -24,10 +24,10 @@ 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-DN00CSZ7 # right #SERIAL_PORT := /dev/cu.usbserial-A105RB12 #SERIAL_PORT := /dev/cu.usbserial-FTZ86FTC # tag-connect -SERIAL_PORT := /dev/cu.usbserial-DO00C2G2 # Breadboard setup +#SERIAL_PORT := /dev/cu.usbserial-DO00C2G2 # Breadboard setup ifeq ($(USE_RBC_MESH_SERIAL), "yes") SERIAL_STRING := "_serial" diff --git a/pyaci/interactive_console.py b/pyaci/interactive_console.py index 6f2812e..cb73736 100644 --- a/pyaci/interactive_console.py +++ b/pyaci/interactive_console.py @@ -79,8 +79,8 @@ def runCommand(self, command): def setTime(self): self.runCommand(sensei_cmd.SetTime()) - def setConfig(self, sensor_id, serial_enabled, mesh_channel): - self.runCommand(sensei_cmd.SetConfig(sensor_id, serial_enabled, mesh_channel)) + def setConfig(self, sensor_id, serial_enabled, mesh_channel, sleep_enabled): + self.runCommand(sensei_cmd.SetConfig(sensor_id, serial_enabled, mesh_channel, sleep_enabled)) def getConfig(self): self.runCommand(sensei_cmd.GetConfig()) diff --git a/pyaci/sensei_cmd.py b/pyaci/sensei_cmd.py index ce9296c..5312669 100644 --- a/pyaci/sensei_cmd.py +++ b/pyaci/sensei_cmd.py @@ -19,13 +19,15 @@ def serialize(self): class SetConfig(object): OpCode = 0x03 - def __init__(self, sensor_id=0, serial_enabled=False, mesh_channel=38): + def __init__(self, sensor_id=0, serial_enabled=False, mesh_channel=38, sleep_enabled=True): self.sensor_id = sensor_id self.serial_enabled = serial_enabled - self.mesh_channel=mesh_channel + self.mesh_channel = mesh_channel + self.sleep_enabled = sleep_enabled + def serialize(self): - return struct.pack("BBBB", self.OpCode, self.sensor_id, self.serial_enabled, self.mesh_channel) + return struct.pack("BBBBB", self.OpCode, self.sensor_id, self.serial_enabled, self.mesh_channel, self.sleep_enabled) class GetConfig(object): OpCode = 0x04 diff --git a/src/config.c b/src/config.c index 0532c8f..f2b8984 100644 --- a/src/config.c +++ b/src/config.c @@ -9,13 +9,26 @@ #define APP_CONFIG_BLOCK 0 #define APP_CONFIG_OFFSET 0 +#define APP_CONFIG_STORAGE_SIZE ((sizeof(app_config_t)/16 + 1) * 16) + static pstorage_handle_t m_storage_handle; static app_config_t m_config; static bool m_loaded = false; static void m_storage_callback(pstorage_handle_t *p_handle, uint8_t op_code, uint32_t result, uint8_t *p_data, uint32_t data_len) { - if (op_code == PSTORAGE_STORE_OP_CODE && result == NRF_SUCCESS) { - toggle_led(LED_GREEN); + static pstorage_handle_t block; + switch (op_code) { + case PSTORAGE_STORE_OP_CODE: + if (result == NRF_SUCCESS) { + toggle_led(LED_BLUE); + } + break; + case PSTORAGE_CLEAR_OP_CODE: + // After clearing, we write the config + if (pstorage_block_identifier_get(&m_storage_handle, APP_CONFIG_BLOCK, &block) == NRF_SUCCESS) { + pstorage_store(&block, (uint8_t*)&m_config, APP_CONFIG_STORAGE_SIZE, APP_CONFIG_OFFSET); + } + break; } } @@ -41,6 +54,7 @@ static void init_config_to_defaults() { m_config.sensor_id = 0; m_config.serial_enabled = 1; m_config.mesh_channel = 38; + m_config.sleep_enabled = 1; } static bool ensure_config_loaded() { @@ -78,8 +92,7 @@ uint32_t set_config(app_config_t *config) { if (error_code != NRF_SUCCESS) { return error_code; } - // Size must be word-aligned - error_code = pstorage_store(&block, (uint8_t*)&m_config, ((sizeof(app_config_t)/16 + 1) * 16), APP_CONFIG_OFFSET); + error_code = pstorage_clear(&block, APP_CONFIG_STORAGE_SIZE); return error_code; } diff --git a/src/config.h b/src/config.h index 9b27a76..9193ede 100644 --- a/src/config.h +++ b/src/config.h @@ -10,6 +10,7 @@ typedef __packed_armcc struct uint8_t sensor_id; uint8_t serial_enabled; uint8_t mesh_channel; + uint8_t sleep_enabled; } __packed_gcc app_config_t; // Returns true on success diff --git a/src/heartbeat.c b/src/heartbeat.c index bf46e5e..ed93c83 100644 --- a/src/heartbeat.c +++ b/src/heartbeat.c @@ -52,6 +52,7 @@ void send_heartbeat_packet(uint8_t sensor_id, uint32_t epoch_seconds, uint16_t e void received_heartbeat(heartbeat_ad_t *p_heartbeat_ad, uint8_t rssi) { app_evt_t event; set_clock_time(p_heartbeat_ad->epoch_seconds, p_heartbeat_ad->epoch_ms, CLOCK_SOURCE_RF, p_heartbeat_ad->clock_version); + DBG_TICK_PIN(6); proximity_add_entry(p_heartbeat_ad->sensor_id, rssi); event.opcode = APP_EVT_OPCODE_HEARTBEAT; diff --git a/src/leds.h b/src/leds.h index a27ec31..ffb84ea 100644 --- a/src/leds.h +++ b/src/leds.h @@ -8,6 +8,8 @@ #define LED_BLUE 2 #define DBG_TICK_PIN(x) NRF_GPIO->OUTSET = (1 << (x)); \ + __NOP();\ + __NOP();\ __NOP();\ __NOP();\ NRF_GPIO->OUTCLR = (1 << (x)) diff --git a/src/main.c b/src/main.c index 0d77443..b8d238f 100644 --- a/src/main.c +++ b/src/main.c @@ -164,7 +164,8 @@ int main(void) tc_radio_params_set(MESH_ACCESS_ADDR, app_config.mesh_channel); } - scheduler_init(); // Initializes, but does not start, timer + scheduler_init(app_config.sleep_enabled); // Initializes, but does not start, clock + heartbeat_init(); // Inits structures for sending heartbeat /* Initialize serial ACI */ diff --git a/src/scheduler.c b/src/scheduler.c index 929d572..d3e3724 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -25,10 +25,10 @@ static app_timer_id_t m_clock_sync_timer_ID; static app_timer_id_t m_offset_timer_ID; static app_timer_id_t m_periodic_timer_ID; static scheduler_state_t m_scheduler_state; +static bool m_sleep_enabled = true; 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) @@ -50,10 +50,12 @@ static void report_debug_register() { static void periodic_timer_cb(void * p_context) { m_current_time += 1; + DBG_TICK_PIN(6); + if (m_current_time % 10 == 0) { //report_debug_register(); - led_config(LED_GREEN, 1); + //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(); @@ -110,7 +112,7 @@ static void delay_to_sleep() { static void do_sleep() { rbc_mesh_stop(); - led_config(LED_GREEN, 0); + //led_config(LED_GREEN, 0); } static void offset_timer_cb(void * p_context) { @@ -128,7 +130,7 @@ static void offset_timer_cb(void * p_context) { delay_to_sleep(); break; case SCHEDULER_STATE_REPORTING: - if (clock_is_synchronized()) { + if (m_sleep_enabled && clock_is_synchronized()) { do_sleep(); } m_scheduler_state = SCHEDULER_STATE_SLEEP; @@ -158,7 +160,8 @@ void start_clock(uint16_t start_delay) { } } -void scheduler_init() { +void scheduler_init(bool sleep_enabled) { + m_sleep_enabled = sleep_enabled; rand_prng_seed(&m_rand); m_scheduler_state = SCHEDULER_STATE_STOPPED; APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false); diff --git a/src/scheduler.h b/src/scheduler.h index dc75c1c..310648a 100644 --- a/src/scheduler.h +++ b/src/scheduler.h @@ -23,7 +23,7 @@ typedef enum { #define HEARTBEAT_WINDOW_MS (40) #define TOTAL_RADIO_WINDOW_MS (900) -void scheduler_init(); +void scheduler_init(bool sleep_enabled); void start_clock(uint16_t start_delay);