#include "board.h" #include "app_timer.h" #include "nrf_gpio.h" #include "sys.h" /******************************************************************************* * DEBUG_LIGHT * *******************************************************************************/ APP_TIMER_DEF(m_debug_light_tmr); static int32_t m_debug_light_io_index; static void debug_light_tmr_cb_handler(void* p_context) { static bool state = false; if (state) { nrf_gpio_pin_set(m_debug_light_io_index); } else { nrf_gpio_pin_clear(m_debug_light_io_index); } state = !state; } void debug_light_init(int io_index) { m_debug_light_io_index = io_index; nrf_gpio_cfg(m_debug_light_io_index, // NRF_GPIO_PIN_DIR_OUTPUT, // NRF_GPIO_PIN_INPUT_DISCONNECT, // NRF_GPIO_PIN_PULLUP, // NRF_GPIO_PIN_S0S1, // NRF_GPIO_PIN_NOSENSE); ret_code_t err_code; err_code = app_timer_create(&m_debug_light_tmr, APP_TIMER_MODE_REPEATED, debug_light_tmr_cb_handler); APP_ERROR_CHECK(err_code); app_timer_start(m_debug_light_tmr, APP_TIMER_TICKS(100), NULL); } /******************************************************************************* * zbsp_enter_sleep * *******************************************************************************/ APP_TIMER_DEF(enter_sleep_mode_tmr); static int32_t m_wakeup_io_index; static bool m_wakeup_io_mirror; static void enter_sleep_tmr_cb(void* p_context) { ZLOGI("enter sleep mode, wakeup io index: %d", m_wakeup_io_index); if (m_wakeup_io_mirror) { nrf_gpio_cfg_sense_input(m_wakeup_io_index, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_LOW); nrf_gpio_cfg_sense_set(m_wakeup_io_index, NRF_GPIO_PIN_SENSE_LOW); } else { nrf_gpio_cfg_sense_input(m_wakeup_io_index, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_SENSE_HIGH); nrf_gpio_cfg_sense_set(m_wakeup_io_index, NRF_GPIO_PIN_SENSE_HIGH); } // Go to system-off mode (this function will not return; wakeup will cause a reset). ret_code_t err_code; err_code = sd_power_system_off(); if(err_code != NRF_SUCCESS) { ZLOGE("sd_power_system_off err_code: %x", err_code); } } void zbsp_enter_sleep(int32_t after_ms, int32_t wakeup_io_index, bool mirror) { m_wakeup_io_index = wakeup_io_index; m_wakeup_io_mirror = mirror; ret_code_t err_code; APP_ERROR_CHECK(app_timer_create(&enter_sleep_mode_tmr, APP_TIMER_MODE_SINGLE_SHOT, enter_sleep_tmr_cb)); APP_ERROR_CHECK(app_timer_start(enter_sleep_mode_tmr, APP_TIMER_TICKS(after_ms), NULL)); ZLOGI("enter sleep mode after %d ms", after_ms); } /******************************************************************************* * gpio_io_state_monitor * *******************************************************************************/ APP_TIMER_DEF(gpio_io_state_monitor_tmr); static uint8_t* m_io_index; static int32_t m_nio; static void gpio_io_state_monitor_tmr_cb(void* p_context) { // ioindex:state ioindex:state ioindex:state if (m_nio == 1) { ZLOGI("iostate %d:%d ", m_io_index[0], nrf_gpio_pin_read(m_io_index[0])); } else if (m_nio == 2) { ZLOGI("iostate %d:%d %d:%d", m_io_index[0], nrf_gpio_pin_read(m_io_index[0]), // m_io_index[1], nrf_gpio_pin_read(m_io_index[1])); } else if (m_nio >= 3) { ZLOGI("iostate %d:%d %d:%d %d:%d", m_io_index[0], nrf_gpio_pin_read(m_io_index[0]), // m_io_index[1], nrf_gpio_pin_read(m_io_index[1]), // m_io_index[2], nrf_gpio_pin_read(m_io_index[2])); } } void zbsp_gpio_state_monitor_without_initio(int dumpstate_period, uint8_t* io_index, int32_t nio) { m_io_index = io_index; m_nio = nio; APP_ERROR_CHECK(app_timer_create(&gpio_io_state_monitor_tmr, APP_TIMER_MODE_REPEATED, gpio_io_state_monitor_tmr_cb)); APP_ERROR_CHECK(app_timer_start(gpio_io_state_monitor_tmr, APP_TIMER_TICKS(dumpstate_period), NULL)); } void zbsp_gpio_state_monitor(int dumpstate_period, uint8_t* io_index, int32_t nio) { m_io_index = io_index; m_nio = nio; for (int i = 0; i < nio; i++) { nrf_gpio_cfg_input(io_index[i], NRF_GPIO_PIN_PULLUP); } zbsp_gpio_state_monitor_without_initio(dumpstate_period, io_index, nio); } void board_init() {}