Browse Source

update

master
zhaohe 1 year ago
parent
commit
c0196969d9
  1. 24
      include/znordic.h
  2. 41
      src/znordic.c

24
include/znordic.h

@ -53,10 +53,12 @@ int16_t znrf_adc_channel_read_val(uint16_t channel);
void znordic_rtc_settime_s(uint32_t timestamp_s); void znordic_rtc_settime_s(uint32_t timestamp_s);
uint32_t znordic_rtc_gettime_s(void); uint32_t znordic_rtc_gettime_s(void);
uint32_t znordic_getpower_on_s(void); //
uint32_t znordic_getpower_on_s(void); //
void znordic_rtc_gettime(ztm_t* now); 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(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 * * LOG *
*******************************************************************************/ *******************************************************************************/
@ -69,22 +71,22 @@ void znordic_rtc_settime(uint16_t year, uint8_t month, uint8_t day, uint8_t
#define ZLOGI_BLOCK(...) \ #define ZLOGI_BLOCK(...) \
{ \ { \
NRF_LOG_INFO(__VA_ARGS__); \ NRF_LOG_INFO(__VA_ARGS__); \
NRF_LOG_FINAL_FLUSH(); \
znordic_force_flush_log(); \
} }
#define ZLOGE_BLOCK(...) \ #define ZLOGE_BLOCK(...) \
{ \ { \
NRF_LOG_ERROR(__VA_ARGS__); \ NRF_LOG_ERROR(__VA_ARGS__); \
NRF_LOG_FINAL_FLUSH(); \
znordic_force_flush_log(); \
} }
#define ZLOGW_BLOCK(...) \ #define ZLOGW_BLOCK(...) \
{ \ { \
NRF_LOG_WARNING(__VA_ARGS__); \ NRF_LOG_WARNING(__VA_ARGS__); \
NRF_LOG_FINAL_FLUSH(); \
znordic_force_flush_log(); \
} }
#define ZLOGD_BLOCK(...) \ #define ZLOGD_BLOCK(...) \
{ \ { \
NRF_LOG_DEBUG(__VA_ARGS__); \ NRF_LOG_DEBUG(__VA_ARGS__); \
NRF_LOG_FINAL_FLUSH(); \
znordic_force_flush_log(); \
} }
#define ZARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) #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); \ APP_ERROR_CHECK(LOCAL_ERR_CODE); \
} \ } \
} while (0) } 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))

41
src/znordic.c

@ -1,5 +1,6 @@
#include "znordic.h" #include "znordic.h"
#include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
@ -29,7 +30,7 @@ void znordic_init() {
APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, 20); APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, 20);
{ {
/******************************************************************************* /*******************************************************************************
* æ¥å¿ç³»ç»Ÿåˆå§åŒ *
* ÈÕ־ϵͳ³õʼ?? *
*******************************************************************************/ *******************************************************************************/
ret_code_t err_code = NRF_LOG_INIT(NULL); ret_code_t err_code = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(err_code); APP_ERROR_CHECK(err_code);
@ -39,7 +40,7 @@ void znordic_init() {
{ {
/******************************************************************************* /*******************************************************************************
* 定æå¨åˆå§åŒ *
* ¨Ê±Æ÷³õʼ»¯ *
*******************************************************************************/ *******************************************************************************/
ret_code_t err_code = app_timer_init(); ret_code_t err_code = app_timer_init();
APP_ERROR_CHECK(err_code); APP_ERROR_CHECK(err_code);
@ -47,7 +48,7 @@ void znordic_init() {
{ {
/******************************************************************************* /*******************************************************************************
* çµæºç®¡çåˆå§åŒ *
* µçÔ´¹ÜÀí³õʼ?? *
*******************************************************************************/ *******************************************************************************/
ret_code_t err_code; ret_code_t err_code;
err_code = nrf_pwr_mgmt_init(); err_code = nrf_pwr_mgmt_init();
@ -55,7 +56,7 @@ void znordic_init() {
} }
{ {
/******************************************************************************* /*******************************************************************************
* èçå议栈使能 *
* ÀÑÀЭÒéջʹ?? *
*******************************************************************************/ *******************************************************************************/
ret_code_t err_code; ret_code_t err_code;
err_code = nrf_sdh_enable_request(); err_code = nrf_sdh_enable_request();
@ -72,7 +73,7 @@ void znordic_init() {
ret_code_t errCode; ret_code_t errCode;
nrf_drv_rtc_config_t rtcConfig = NRF_DRV_RTC_DEFAULT_CONFIG; // Initialize RTC instance 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); errCode = nrf_drv_rtc_init(&s_rtcHandle, &rtcConfig, rtcCallbackFunc);
APP_ERROR_CHECK(errCode); 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) { // 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); 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; 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; time_t now_s = g_timestamp + g_power_on_rtc;
*now = *localtime(&now_s); *now = *localtime(&now_s);
} }
static void rtcCallbackFunc(nrf_drv_rtc_int_type_t interruptType) { static void rtcCallbackFunc(nrf_drv_rtc_int_type_t interruptType) {
static uint8_t s_timeCount1second = 0; 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 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;
}
Loading…
Cancel
Save