diff --git a/.vscode/settings.json b/.vscode/settings.json index c05f83c..6d86b83 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -41,6 +41,7 @@ "zhrs_service.h": "c", "ble_srv_common.h": "c", "zdatachannel_service.h": "c", - "ble_link_ctx_manager.h": "c" + "ble_link_ctx_manager.h": "c", + "nrf_drv_saadc.h": "c" } } \ No newline at end of file diff --git a/README.md b/README.md index de8b202..2a234bd 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ sdk\components\boards\pca10100.h 清风开发板 + AIN2 -> 光感 + ``` @@ -41,4 +43,16 @@ sdk\components\boards\pca10100.h 蓝牙已连接 +``` +``` +难点: + 1.采样间隔2ms (500HZ) + 2.数据往flash中写入时间消耗,cache选择多大,数据什么时候上报。 + 1. ADC:单次采样,阻塞拿数值(50us) + 2. 电池电量的采集ADC和电压采集ADC用的是同一个ADC + + (这里先使用最简单的阻塞式ADC采集,电池ADC3次,采样ADC3次) + + + ``` \ No newline at end of file diff --git a/app/app.uvoptx b/app/app.uvoptx index c2bba90..3792195 100644 --- a/app/app.uvoptx +++ b/app/app.uvoptx @@ -627,7 +627,7 @@ nRF_Drivers - 0 + 1 0 0 0 @@ -727,6 +727,30 @@ 0 0 + + 7 + 27 + 1 + 0 + 0 + 0 + ..\sdk\modules\nrfx\drivers\src\nrfx_adc.c + nrfx_adc.c + 0 + 0 + + + 7 + 28 + 1 + 0 + 0 + 0 + ..\sdk\modules\nrfx\drivers\src\nrfx_saadc.c + nrfx_saadc.c + 0 + 0 + @@ -737,7 +761,7 @@ 0 8 - 27 + 29 1 0 0 @@ -749,7 +773,7 @@ 8 - 28 + 30 1 0 0 @@ -761,7 +785,7 @@ 8 - 29 + 31 1 0 0 @@ -773,7 +797,7 @@ 8 - 30 + 32 1 0 0 @@ -785,7 +809,7 @@ 8 - 31 + 33 1 0 0 @@ -797,7 +821,7 @@ 8 - 32 + 34 1 0 0 @@ -809,7 +833,7 @@ 8 - 33 + 35 1 0 0 @@ -821,7 +845,7 @@ 8 - 34 + 36 1 0 0 @@ -833,7 +857,7 @@ 8 - 35 + 37 1 0 0 @@ -845,7 +869,7 @@ 8 - 36 + 38 1 0 0 @@ -857,7 +881,7 @@ 8 - 37 + 39 1 0 0 @@ -869,7 +893,7 @@ 8 - 38 + 40 1 0 0 @@ -881,7 +905,7 @@ 8 - 39 + 41 1 0 0 @@ -893,7 +917,7 @@ 8 - 40 + 42 1 0 0 @@ -905,7 +929,7 @@ 8 - 41 + 43 1 0 0 @@ -917,7 +941,7 @@ 8 - 42 + 44 1 0 0 @@ -929,7 +953,7 @@ 8 - 43 + 45 1 0 0 @@ -941,7 +965,7 @@ 8 - 44 + 46 1 0 0 @@ -953,7 +977,7 @@ 8 - 45 + 47 1 0 0 @@ -965,7 +989,7 @@ 8 - 46 + 48 1 0 0 @@ -977,7 +1001,7 @@ 8 - 47 + 49 1 0 0 @@ -989,7 +1013,7 @@ 8 - 48 + 50 1 0 0 @@ -1001,7 +1025,7 @@ 8 - 49 + 51 1 0 0 @@ -1013,7 +1037,7 @@ 8 - 50 + 52 1 0 0 @@ -1025,7 +1049,7 @@ 8 - 51 + 53 1 0 0 @@ -1045,7 +1069,7 @@ 0 9 - 52 + 54 1 0 0 @@ -1057,7 +1081,7 @@ 9 - 53 + 55 1 0 0 @@ -1069,7 +1093,7 @@ 9 - 54 + 56 1 0 0 @@ -1081,7 +1105,7 @@ 9 - 55 + 57 1 0 0 @@ -1093,7 +1117,7 @@ 9 - 56 + 58 1 0 0 @@ -1105,7 +1129,7 @@ 9 - 57 + 59 1 0 0 @@ -1125,7 +1149,7 @@ 0 10 - 58 + 60 1 0 0 @@ -1137,7 +1161,7 @@ 10 - 59 + 61 1 0 0 @@ -1149,7 +1173,7 @@ 10 - 60 + 62 1 0 0 @@ -1169,7 +1193,7 @@ 0 11 - 61 + 63 1 0 0 @@ -1181,7 +1205,7 @@ 11 - 62 + 64 1 0 0 @@ -1193,7 +1217,7 @@ 11 - 63 + 65 1 0 0 @@ -1213,7 +1237,7 @@ 0 12 - 64 + 66 1 0 0 diff --git a/app/app.uvprojx b/app/app.uvprojx index e9e26aa..323e180 100644 --- a/app/app.uvprojx +++ b/app/app.uvprojx @@ -1614,6 +1614,16 @@ + + nrfx_adc.c + 1 + ..\sdk\modules\nrfx\drivers\src\nrfx_adc.c + + + nrfx_saadc.c + 1 + ..\sdk\modules\nrfx\drivers\src\nrfx_saadc.c + @@ -5283,6 +5293,16 @@ + + nrfx_adc.c + 1 + ..\sdk\modules\nrfx\drivers\src\nrfx_adc.c + + + nrfx_saadc.c + 1 + ..\sdk\modules\nrfx\drivers\src\nrfx_saadc.c + diff --git a/app/config/sdk_config.h b/app/config/sdk_config.h index 8e946e6..749e9c8 100644 --- a/app/config/sdk_config.h +++ b/app/config/sdk_config.h @@ -3435,7 +3435,7 @@ // NRFX_SAADC_ENABLED - nrfx_saadc - SAADC peripheral driver //========================================================== #ifndef NRFX_SAADC_ENABLED -#define NRFX_SAADC_ENABLED 0 +#define NRFX_SAADC_ENABLED 1 #endif // NRFX_SAADC_CONFIG_RESOLUTION - Resolution @@ -5485,7 +5485,7 @@ // SAADC_ENABLED - nrf_drv_saadc - SAADC peripheral driver - legacy layer //========================================================== #ifndef SAADC_ENABLED -#define SAADC_ENABLED 0 +#define SAADC_ENABLED 1 #endif // SAADC_CONFIG_RESOLUTION - Resolution @@ -11979,12 +11979,12 @@ // <2=> NRF_CLOCK_LF_SRC_SYNTH #ifndef NRF_SDH_CLOCK_LF_SRC -#define NRF_SDH_CLOCK_LF_SRC 1 +#define NRF_SDH_CLOCK_LF_SRC 0 #endif // NRF_SDH_CLOCK_LF_RC_CTIV - SoftDevice calibration timer interval. #ifndef NRF_SDH_CLOCK_LF_RC_CTIV -#define NRF_SDH_CLOCK_LF_RC_CTIV 0 +#define NRF_SDH_CLOCK_LF_RC_CTIV 16 #endif // NRF_SDH_CLOCK_LF_RC_TEMP_CTIV - SoftDevice calibration timer interval under constant temperature. @@ -11992,7 +11992,7 @@ // if the temperature has not changed. #ifndef NRF_SDH_CLOCK_LF_RC_TEMP_CTIV -#define NRF_SDH_CLOCK_LF_RC_TEMP_CTIV 0 +#define NRF_SDH_CLOCK_LF_RC_TEMP_CTIV 2 #endif // NRF_SDH_CLOCK_LF_ACCURACY - External clock accuracy used in the LL to compute timing. @@ -12011,7 +12011,7 @@ // <11=> NRF_CLOCK_LF_ACCURACY_1_PPM #ifndef NRF_SDH_CLOCK_LF_ACCURACY -#define NRF_SDH_CLOCK_LF_ACCURACY 7 +#define NRF_SDH_CLOCK_LF_ACCURACY 1 #endif // diff --git a/app/main.c b/app/main.c index dd75ef7..31756e9 100644 --- a/app/main.c +++ b/app/main.c @@ -67,10 +67,18 @@ void on_service_init(void) { } static void test_tx_timer_cb(void* p_context) { - static uint32_t data; - uint16_t txlen = 4; - data++; - zdatachannel_data_send((uint8_t*)&data, &txlen); + // static uint32_t data; + // uint16_t txlen = 4; + // data++; + // zdatachannel_data_send((uint8_t*)&data, &txlen); + + ZLOGI("adc channel %d %d", adc_module_heart_elect_channel_read_val(), adc_module_battery_channel_read_val()); +} + +static void board_init() { + adc_module_init(); + adc_module_battery_channel_init(NRF_SAADC_INPUT_VDD); + adc_module_heart_elect_channel_init(NRF_SAADC_INPUT_AIN2); } int main(void) { @@ -81,14 +89,16 @@ int main(void) { static zble_module_cfg_t cfg = // { - .deviceName = "iflytop2", + .deviceName = "iflytop_test_ble", .on_service_init = on_service_init, }; zble_module_init(&cfg); + board_init(); + NRF_SDH_BLE_OBSERVER(m_ble_observer, 3, ble_evt_handler, NULL); ZERROR_CHECK(app_timer_create(&m_test_tx_timer, APP_TIMER_MODE_REPEATED, test_tx_timer_cb)); - ZERROR_CHECK(app_timer_start(m_test_tx_timer, APP_TIMER_TICKS(5), NULL)); + ZERROR_CHECK(app_timer_start(m_test_tx_timer, APP_TIMER_TICKS(100), NULL)); zble_module_start_adv(); zsys_loop(); diff --git a/app/src/board.c b/app/src/board.c index 45541de..a429f91 100644 --- a/app/src/board.c +++ b/app/src/board.c @@ -4,6 +4,17 @@ #include "nrf_gpio.h" #include "sys.h" +static int16_t 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; +} + /******************************************************************************* * DEBUG_LIGHT * *******************************************************************************/ @@ -55,7 +66,7 @@ static void enter_sleep_tmr_cb(void* p_context) { // Go to system-off mode (this function will not return; wakeup will cause a reset). ret_code_t err_code; err_code = sd_power_system_off(); - if(err_code != NRF_SUCCESS) { + if (err_code != NRF_SUCCESS) { ZLOGE("sd_power_system_off err_code: %x", err_code); } } @@ -105,3 +116,31 @@ void zbsp_gpio_state_monitor(int dumpstate_period, uint8_t* io_index, int32_t ni } void board_init() {} + +/******************************************************************************* + * ADC * + *******************************************************************************/ + +static void saadc_callback(nrf_drv_saadc_evt_t const* p_event) { + if (p_event->type == NRF_DRV_SAADC_EVT_DONE) { + } +} + +void adc_module_init() { + nrf_drv_saadc_config_t adccfg = NRFX_SAADC_DEFAULT_CONFIG; + adccfg.resolution = NRF_SAADC_RESOLUTION_12BIT; // 4096 等于满采样率 + ZERROR_CHECK(nrf_drv_saadc_init(&adccfg, saadc_callback)); +} + +void adc_module_battery_channel_init(nrf_saadc_input_t channelpin) { + nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(channelpin); + ZERROR_CHECK(nrfx_saadc_channel_init(0, &channel_config)); +} +int16_t adc_module_battery_channel_read_val() { return adc_channel_read_val(0); } + +void adc_module_heart_elect_channel_init(nrf_saadc_input_t channelpin) { + nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(channelpin); + channel_config.acq_time = NRF_SAADC_ACQTIME_40US; + ZERROR_CHECK(nrfx_saadc_channel_init(1, &channel_config)); +} +int16_t adc_module_heart_elect_channel_read_val() { return adc_channel_read_val(1); } diff --git a/app/src/board.h b/app/src/board.h index 158e34a..db22952 100644 --- a/app/src/board.h +++ b/app/src/board.h @@ -1,6 +1,7 @@ #pragma once #include +#include "nrf_drv_saadc.h" #include "sys.h" /******************************************************************************* @@ -13,7 +14,7 @@ void debug_light_init(int io_index); *******************************************************************************/ /** - * @brief + * @brief 调用此方法前,需要确保底层协议栈已经初始化 * * @param after_ms * @param wakeup_io_index @@ -26,4 +27,16 @@ void zbsp_enter_sleep(int32_t after_ms, int32_t wakeup_io_index, bool mirror); void zbsp_gpio_state_monitor(int dumpstate_period, uint8_t* io_index, int32_t nio); void zbsp_gpio_state_monitor_without_initio(int dumpstate_period, uint8_t* io_index, int32_t nio); -void board_init(); \ No newline at end of file +void board_init(); + +/******************************************************************************* + * ADC * + *******************************************************************************/ + +void adc_module_init(); + +void adc_module_battery_channel_init(nrf_saadc_input_t channelpin); +int16_t adc_module_battery_channel_read_val(); + +void adc_module_heart_elect_channel_init(nrf_saadc_input_t channelpin); +int16_t adc_module_heart_elect_channel_read_val(); \ No newline at end of file