From 75bac4942446a40e70c2e5d1cbfe5171a869fd69 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Fri, 26 Jan 2024 20:13:45 +0800 Subject: [PATCH] update --- include/znordic.h | 32 +++++++++++++++++++- src/znordic.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 118 insertions(+), 3 deletions(-) diff --git a/include/znordic.h b/include/znordic.h index bb3ef56..b71dbde 100644 --- a/include/znordic.h +++ b/include/znordic.h @@ -1,7 +1,8 @@ #pragma once #include #include - +#include +// #include "app_scheduler.h" #include "app_timer.h" #include "app_uart.h" @@ -41,6 +42,8 @@ /******************************************************************************* * UTILS * *******************************************************************************/ +typedef struct tm ztm_t; + void znordic_init(); int32_t znordic_get_event_max_size(); void znordic_loop(); @@ -48,6 +51,12 @@ 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); + /******************************************************************************* * LOG * *******************************************************************************/ @@ -57,6 +66,27 @@ int16_t znrf_adc_channel_read_val(uint16_t channel); #define ZLOGW(...) NRF_LOG_WARNING(__VA_ARGS__) #define ZLOGD(...) NRF_LOG_DEBUG(__VA_ARGS__) +#define ZLOGI_BLOCK(...) \ + { \ + NRF_LOG_INFO(__VA_ARGS__); \ + NRF_LOG_FINAL_FLUSH(); \ + } +#define ZLOGE_BLOCK(...) \ + { \ + NRF_LOG_ERROR(__VA_ARGS__); \ + NRF_LOG_FINAL_FLUSH(); \ + } +#define ZLOGW_BLOCK(...) \ + { \ + NRF_LOG_WARNING(__VA_ARGS__); \ + NRF_LOG_FINAL_FLUSH(); \ + } +#define ZLOGD_BLOCK(...) \ + { \ + NRF_LOG_DEBUG(__VA_ARGS__); \ + NRF_LOG_FINAL_FLUSH(); \ + } + #define ZARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) #define ZPIN(port, num) NRF_GPIO_PIN_MAP(port, num) diff --git a/src/znordic.c b/src/znordic.c index 8ad3ae0..5cec7ce 100644 --- a/src/znordic.c +++ b/src/znordic.c @@ -1,5 +1,29 @@ #include "znordic.h" -#define SCHED_MAX_EVENT_DATA_SIZE APP_TIMER_SCHED_EVENT_DATA_SIZE /**< Maximum size of scheduler events. */ + +#include +#include + +#include "boards.h" +#include "nrf_delay.h" +#include "nrf_drv_clock.h" +#include "nrf_drv_rtc.h" +#include "nrf_drv_timer.h" +#include "nrf_log.h" +#include "nrf_log_ctrl.h" +#include "nrf_log_default_backends.h" +#include "nrfx_rtc.h" + +#define SCHED_MAX_EVENT_DATA_SIZE APP_TIMER_SCHED_EVENT_DATA_SIZE /**< Maximum size of scheduler events. */ + +/******************************************************************************* + * RTC * + *******************************************************************************/ +static const nrf_drv_rtc_t s_rtcHandle = NRF_DRV_RTC_INSTANCE(2); // Declaring an instance of nrf_drv_rtc for RTC2. +static void rtcCallbackFunc(nrf_drv_rtc_int_type_t interruptType); + +/******************************************************************************* + * CODE * + *******************************************************************************/ void znordic_init() { // // APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, 20); @@ -43,6 +67,19 @@ void znordic_init() { err_code = nrf_sdh_ble_enable(&ram_start); APP_ERROR_CHECK(err_code); } + + { + 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 + + errCode = nrf_drv_rtc_init(&s_rtcHandle, &rtcConfig, rtcCallbackFunc); + APP_ERROR_CHECK(errCode); + + nrf_drv_rtc_tick_enable(&s_rtcHandle, true); // Enable tick event & interrupt + nrf_drv_rtc_enable(&s_rtcHandle); // Power on RTC instance + } } int32_t znordic_get_event_max_size() { return SCHED_MAX_EVENT_DATA_SIZE; } @@ -69,4 +106,52 @@ int16_t znrf_adc_channel_read_val(uint16_t channel) { return 0; } return value; -} \ No newline at end of file +} + +/******************************************************************************* + * RTC * + *******************************************************************************/ +volatile uint32_t g_timestamp = 0; +volatile uint32_t g_power_on_rtc = 0; +uint32_t znordic_getpower_on_s() { return g_timestamp; } + +void znordic_rtc_settime_s(uint32_t timestampNow) { + if (timestampNow < g_timestamp) { + return; + } + g_power_on_rtc = timestampNow - g_timestamp; +} +uint32_t znordic_rtc_gettime_s(void) { return g_timestamp + g_power_on_rtc; } +void znordic_rtc_settime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec) { + static struct tm s_tm; + memset(&s_tm, 0, sizeof(s_tm)); + s_tm.tm_year = year - 1900; + s_tm.tm_mon = month - 1; + s_tm.tm_mday = day; + s_tm.tm_hour = hour; + s_tm.tm_min = min; + s_tm.tm_sec = sec; + s_tm.tm_isdst = -1; + + uint32_t nowtimestamp = mktime(&s_tm); + g_power_on_rtc = nowtimestamp - g_timestamp; +} + +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 (s_timeCount1second >= 7) // 125ms * 8 = 1s + { + s_timeCount1second = 0; + g_timestamp++; + } else { + s_timeCount1second++; + } + } +}