From c0196969d9981245400057e8c8ef8567fc6c93ab Mon Sep 17 00:00:00 2001 From: zhaohe Date: Sun, 28 Jan 2024 12:43:28 +0800 Subject: [PATCH] update --- include/znordic.h | 32 ++++++++++++++++++++++---------- src/znordic.c | 41 ++++++++++++++++++++++++++++++++++------- 2 files changed, 56 insertions(+), 17 deletions(-) diff --git a/include/znordic.h b/include/znordic.h index b71dbde..d622f6c 100644 --- a/include/znordic.h +++ b/include/znordic.h @@ -51,12 +51,14 @@ void znordic_loop(); void znrf_gpio_cfg_output(uint32_t pin_number, nrf_gpio_pin_pull_t pull); int16_t znrf_adc_channel_read_val(uint16_t channel); -void znordic_rtc_settime_s(uint32_t timestamp_s); -uint32_t znordic_rtc_gettime_s(void); -uint32_t znordic_getpower_on_s(void); // 获得上电时间 -void znordic_rtc_gettime(ztm_t* now); -void znordic_rtc_settime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec); - +void znordic_rtc_settime_s(uint32_t timestamp_s); +uint32_t znordic_rtc_gettime_s(void); +uint32_t znordic_getpower_on_s(void); // ϵʱ +void znordic_rtc_gettime(ztm_t* now); +void znordic_rtc_settime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec); +void znordic_force_flush_log(); +const char* fmt(const char* fmt, ...); +const char* hex2str(const uint8_t* data, uint16_t len); /******************************************************************************* * LOG * *******************************************************************************/ @@ -69,22 +71,22 @@ void znordic_rtc_settime(uint16_t year, uint8_t month, uint8_t day, uint8_t #define ZLOGI_BLOCK(...) \ { \ NRF_LOG_INFO(__VA_ARGS__); \ - NRF_LOG_FINAL_FLUSH(); \ + znordic_force_flush_log(); \ } #define ZLOGE_BLOCK(...) \ { \ NRF_LOG_ERROR(__VA_ARGS__); \ - NRF_LOG_FINAL_FLUSH(); \ + znordic_force_flush_log(); \ } #define ZLOGW_BLOCK(...) \ { \ NRF_LOG_WARNING(__VA_ARGS__); \ - NRF_LOG_FINAL_FLUSH(); \ + znordic_force_flush_log(); \ } #define ZLOGD_BLOCK(...) \ { \ NRF_LOG_DEBUG(__VA_ARGS__); \ - NRF_LOG_FINAL_FLUSH(); \ + znordic_force_flush_log(); \ } #define ZARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) @@ -99,3 +101,13 @@ void znordic_rtc_settime(uint16_t year, uint8_t month, uint8_t day, uint8_t APP_ERROR_CHECK(LOCAL_ERR_CODE); \ } \ } while (0) + +#define ZASSERT(expr) \ + do { \ + if (!(expr)) { \ + ZLOGI("%s:%d ZASSERT(%s) fail", __FILE__, __LINE__, #expr); \ + NRF_LOG_PROCESS(); \ + app_sched_execute(); \ + app_error_handler_bare(0); \ + } \ + } while (!(expr)) \ No newline at end of file diff --git a/src/znordic.c b/src/znordic.c index 5cec7ce..1ea3945 100644 --- a/src/znordic.c +++ b/src/znordic.c @@ -1,5 +1,6 @@ #include "znordic.h" +#include #include #include @@ -29,7 +30,7 @@ void znordic_init() { APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, 20); { /******************************************************************************* - * 日志系统初始化 * + * ־ϵͳʼ?? * *******************************************************************************/ ret_code_t err_code = NRF_LOG_INIT(NULL); APP_ERROR_CHECK(err_code); @@ -39,7 +40,7 @@ void znordic_init() { { /******************************************************************************* - * 定时器初始化 * + * ʱʼ * *******************************************************************************/ ret_code_t err_code = app_timer_init(); APP_ERROR_CHECK(err_code); @@ -47,7 +48,7 @@ void znordic_init() { { /******************************************************************************* - * 电源管理初始化 * + * Դʼ?? * *******************************************************************************/ ret_code_t err_code; err_code = nrf_pwr_mgmt_init(); @@ -55,7 +56,7 @@ void znordic_init() { } { /******************************************************************************* - * 蓝牙协议栈使能 * + * Эջʹ?? * *******************************************************************************/ ret_code_t err_code; err_code = nrf_sdh_enable_request(); @@ -72,7 +73,7 @@ void znordic_init() { ret_code_t errCode; nrf_drv_rtc_config_t rtcConfig = NRF_DRV_RTC_DEFAULT_CONFIG; // Initialize RTC instance - rtcConfig.prescaler = 4095; // 如实现8HZ的频率,则PRESCALER寄存器应该设为32768/8-1 = 4095 + rtcConfig.prescaler = 4095; // ʵ??8HZƵʣPRESCALERĴӦ??32768/8-1 = 4095 errCode = nrf_drv_rtc_init(&s_rtcHandle, &rtcConfig, rtcCallbackFunc); APP_ERROR_CHECK(errCode); @@ -93,6 +94,14 @@ void znordic_loop() { } } +void znordic_force_flush_log() { + // for (size_t i = 0; i < 5; i++) { + // app_sched_execute(); + // NRF_LOG_FLUSH(); + // nrf_delay_ms(1); + // } +} + void znrf_gpio_cfg_output(uint32_t pin_number, nrf_gpio_pin_pull_t pull) { // nrf_gpio_cfg(pin_number, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, pull, NRF_GPIO_PIN_S0S1, NRF_GPIO_PIN_NOSENSE); } @@ -137,14 +146,14 @@ void znordic_rtc_settime(uint16_t year, uint8_t month, uint8_t day, uint8_t g_power_on_rtc = nowtimestamp - g_timestamp; } -void znordic_rtc_gettime(ztm_t *now) { +void znordic_rtc_gettime(ztm_t* now) { time_t now_s = g_timestamp + g_power_on_rtc; *now = *localtime(&now_s); } static void rtcCallbackFunc(nrf_drv_rtc_int_type_t interruptType) { static uint8_t s_timeCount1second = 0; - if (interruptType == NRF_DRV_RTC_INT_TICK) // 中断类型:滴答中断 + if (interruptType == NRF_DRV_RTC_INT_TICK) // жͣδ?? { if (s_timeCount1second >= 7) // 125ms * 8 = 1s { @@ -155,3 +164,21 @@ static void rtcCallbackFunc(nrf_drv_rtc_int_type_t interruptType) { } } } + +const char* fmt(const char* fmt, ...) { + static char buf[256]; + va_list args; + va_start(args, fmt); + vsnprintf(buf, 255, fmt, args); + va_end(args); + return buf; +} + +const char* hex2str(const uint8_t* data, uint16_t len) { + static char buf[256]; + for (size_t i = 0; i < len; i++) { + buf[i * 2] = (data[i] >> 4) + '0'; + buf[i * 2 + 1] = (data[i] & 0x0f) + '0'; + } + return buf; +} \ No newline at end of file