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.
157 lines
5.6 KiB
157 lines
5.6 KiB
#include "znordic.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"
|
|
|
|
#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);
|
|
{
|
|
/*******************************************************************************
|
|
* 日志系统初始化 *
|
|
*******************************************************************************/
|
|
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
|
|
}
|
|
}
|
|
|
|
int32_t znordic_get_event_max_size() { return SCHED_MAX_EVENT_DATA_SIZE; }
|
|
|
|
void znordic_loop() {
|
|
while (true) {
|
|
app_sched_execute();
|
|
if (NRF_LOG_PROCESS() == false) {
|
|
nrf_pwr_mgmt_run();
|
|
}
|
|
}
|
|
}
|
|
|
|
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 *
|
|
*******************************************************************************/
|
|
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++;
|
|
}
|
|
}
|
|
}
|