From efdb1b55c3432d3b539fb9fcecb14209874dae0d Mon Sep 17 00:00:00 2001 From: zhaohe Date: Tue, 23 Jan 2024 10:08:37 +0800 Subject: [PATCH] recode --- include/sys.h | 53 ------------------------------------------- include/znordic.h | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/sys.c | 51 ----------------------------------------- src/znordic.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 104 deletions(-) delete mode 100644 include/sys.h create mode 100644 include/znordic.h delete mode 100644 src/sys.c create mode 100644 src/znordic.c diff --git a/include/sys.h b/include/sys.h deleted file mode 100644 index 8d20c2f..0000000 --- a/include/sys.h +++ /dev/null @@ -1,53 +0,0 @@ -#pragma once -#include -#include - -#include "app_timer.h" -#include "app_uart.h" -#include "app_util_platform.h" -#include "ble_advdata.h" -#include "ble_advertising.h" -#include "ble_conn_params.h" -#include "ble_hci.h" -#include "ble_nus.h" -#include "bsp_btn_ble.h" -#include "diskio_blkdev.h" -#include "ff.h" -#include "nordic_common.h" -#include "nrf.h" -#include "nrf_ble_gatt.h" -#include "nrf_ble_qwr.h" -#include "nrf_block_dev_sdc.h" -#include "nrf_delay.h" -#include "nrf_drv_spi.h" -#include "nrf_log.h" -#include "nrf_log_ctrl.h" -#include "nrf_log_default_backends.h" -#include "nrf_pwr_mgmt.h" -#include "nrf_sdh.h" -#include "nrf_sdh_ble.h" -#include "nrf_sdh_soc.h" - -// -#define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */ - -void zsys_init(); -void zsys_loop(); - -#define ZLOGI(...) NRF_LOG_INFO(__VA_ARGS__) -#define ZLOGE(...) NRF_LOG_ERROR(__VA_ARGS__) -#define ZLOGW(...) NRF_LOG_WARNING(__VA_ARGS__) -#define ZLOGD(...) NRF_LOG_DEBUG(__VA_ARGS__) - -#define ZARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) - -#define ZPIN(port, num) NRF_GPIO_PIN_MAP(port, num) - -#define ZERROR_CHECK(err_code) \ - do { \ - const uint32_t LOCAL_ERR_CODE = (err_code); \ - if (LOCAL_ERR_CODE != NRF_SUCCESS) { \ - ZLOGI("%s:%d ZERROR_CHECK(%s) fail:%d", __FILE__, __LINE__, #err_code, LOCAL_ERR_CODE); \ - APP_ERROR_CHECK(LOCAL_ERR_CODE); \ - } \ - } while (0) diff --git a/include/znordic.h b/include/znordic.h new file mode 100644 index 0000000..252024a --- /dev/null +++ b/include/znordic.h @@ -0,0 +1,68 @@ +#pragma once +#include +#include + +#include "app_timer.h" +#include "app_uart.h" +#include "app_util_platform.h" +#include "ble_advdata.h" +#include "ble_advertising.h" +#include "ble_conn_params.h" +#include "ble_hci.h" +#include "ble_nus.h" +#include "bsp_btn_ble.h" +#include "diskio_blkdev.h" +#include "ff.h" +#include "nordic_common.h" +#include "nrf.h" +#include "nrf_ble_gatt.h" +#include "nrf_ble_qwr.h" +#include "nrf_block_dev_sdc.h" +#include "nrf_delay.h" +#include "nrf_drv_pwm.h" +#include "nrf_drv_saadc.h" +#include "nrf_drv_spi.h" +#include "nrf_drv_twi.h" +#include "nrf_drv_wdt.h" +#include "nrf_gpio.h" +#include "nrf_log.h" +#include "nrf_log_ctrl.h" +#include "nrf_log_default_backends.h" +#include "nrf_pwr_mgmt.h" +#include "nrf_sdh.h" +#include "nrf_sdh_ble.h" +#include "nrf_sdh_soc.h" + +// +#define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */ + +/******************************************************************************* + * UTILS * + *******************************************************************************/ +void znordic_init(); +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); + +/******************************************************************************* + * LOG * + *******************************************************************************/ + +#define ZLOGI(...) NRF_LOG_INFO(__VA_ARGS__) +#define ZLOGE(...) NRF_LOG_ERROR(__VA_ARGS__) +#define ZLOGW(...) NRF_LOG_WARNING(__VA_ARGS__) +#define ZLOGD(...) NRF_LOG_DEBUG(__VA_ARGS__) + +#define ZARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) + +#define ZPIN(port, num) NRF_GPIO_PIN_MAP(port, num) + +#define ZERROR_CHECK(err_code) \ + do { \ + const uint32_t LOCAL_ERR_CODE = (err_code); \ + if (LOCAL_ERR_CODE != NRF_SUCCESS) { \ + ZLOGI("%s:%d ZERROR_CHECK(%s) fail:%d", __FILE__, __LINE__, #err_code, LOCAL_ERR_CODE); \ + APP_ERROR_CHECK(LOCAL_ERR_CODE); \ + } \ + } while (0) diff --git a/src/sys.c b/src/sys.c deleted file mode 100644 index 436ce41..0000000 --- a/src/sys.c +++ /dev/null @@ -1,51 +0,0 @@ -#include "sys.h" -void zsys_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); - } -} - -void zsys_loop() { - while (true) { - if (NRF_LOG_PROCESS() == false) { - nrf_pwr_mgmt_run(); - } - } -} diff --git a/src/znordic.c b/src/znordic.c new file mode 100644 index 0000000..1ea1d3d --- /dev/null +++ b/src/znordic.c @@ -0,0 +1,67 @@ +#include "znordic.h" +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); + } +} + +void znordic_loop() { + while (true) { + 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; +} \ No newline at end of file