|
|
@ -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 <stdbool.h> |
|
|
|
#include <stdint.h> |
|
|
|
|
|
|
|
#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; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/******************************************************************************* |
|
|
|
* 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++; |
|
|
|
} |
|
|
|
} |
|
|
|
} |