You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

198 lines
6.4 KiB

#include "znordic.h"
#include <stdarg.h>
#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"
/*******************************************************************************
* 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() {
{
/*******************************************************************************
* 日志系统初始?? *
*******************************************************************************/
ret_code_t err_code = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(err_code);
NRF_LOG_DEFAULT_BACKENDS_INIT();
}
{
/*******************************************************************************
* 定时器初始化 *
*******************************************************************************/
ret_code_t err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
}
{
/*******************************************************************************
* 电源管理初始 *
*******************************************************************************/
ret_code_t err_code;
err_code = nrf_pwr_mgmt_init();
APP_ERROR_CHECK(err_code);
}
{
/*******************************************************************************
* 蓝牙协议栈使 *
*******************************************************************************/
ret_code_t err_code;
err_code = nrf_sdh_enable_request();
APP_ERROR_CHECK(err_code);
uint32_t ram_start = 0;
err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
APP_ERROR_CHECK(err_code);
// Enable BLE stack.
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
}
}
void znordic_loop() {
while (true) {
app_sched_execute();
if (NRF_LOG_PROCESS() == false) {
nrf_pwr_mgmt_run();
}
}
}
void znordic_force_flush_log() { NRF_LOG_FLUSH(); }
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);
}
int16_t znrf_adc_channel_read_val(uint16_t channel) {
nrf_saadc_value_t value;
ret_code_t err_code;
err_code = nrfx_saadc_sample_convert(channel, &value);
if (err_code != NRF_SUCCESS) {
ZLOGE("nrfx_saadc_sample_convert(%d) fail err_code:%d", channel, err_code);
return 0;
}
return value;
}
/*******************************************************************************
* RTC *
*******************************************************************************/
#define DEFAULT_TIME (1704038400 + 8 * 60 * 60) // 2024/01/01/00:00:00
volatile uint32_t g_timestamp = 0;
volatile uint32_t g_power_on_rtc = (DEFAULT_TIME);
// static uint8_t s_timeCount1second = 0;
uint32_t znordic_getpower_on_ms() {
__disable_irq();
uint32_t ret = g_timestamp * 125;
__enable_irq();
return ret;
}
uint32_t znordic_getpower_on_s() {
__disable_irq();
uint32_t reg = znordic_getpower_on_ms() / 1000;
__enable_irq();
return reg;
}
uint32_t znordic_haspassed_ms(uint32_t last) {
uint32_t now = znordic_getpower_on_ms();
if (now < last) {
return 0xFFFFFFFF - last + now;
} else {
return now - last;
}
}
void znordic_rtc_settime_s(uint32_t timestampNow) {
if (timestampNow < znordic_getpower_on_s()) {
return;
}
g_power_on_rtc = timestampNow - znordic_getpower_on_s();
}
uint32_t znordic_rtc_gettime_s(void) { return znordic_getpower_on_s() + 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);
uint32_t tosettime = nowtimestamp - znordic_getpower_on_s();
if (tosettime > DEFAULT_TIME) { // 2024/01/01/00:00:00
g_power_on_rtc = tosettime;
}
return;
}
void znordic_rtc_gettime(ztm_t* now) {
time_t now_s = (g_timestamp * 125) / 1000 + g_power_on_rtc;
*now = *localtime(&now_s);
}
static void rtcCallbackFunc(nrf_drv_rtc_int_type_t interruptType) {
if (interruptType == NRF_DRV_RTC_INT_TICK) // 中断类型:滴答中??
{
g_timestamp++;
// if (s_timeCount1second >= 7) // 125ms * 8 = 1s
// {
// s_timeCount1second = 0;
// g_timestamp++;
// } else {
// s_timeCount1second++;
// }
}
}
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;
}