Hi all,
I am developing ADC functions with Mynewt on nrf52 DK PCA10040 board.
The working example code based on SAADC driver nrfx 1.7.0 won't work on nrfx 2.1.0.
Example in the Mynewt official website (https://mynewt.apache.org/latest/tutorials/sensors/nrf52_adc.html).
I searched online in the latest SDK (16.0), however, it seems that the nrfx is still the old one. It is not updated to nrfx 2.1.0 or nrfx 2.2.0. The APIs have changed but the documentations are not.
Here I list the code and the errors. I tried to modify the code to make it complie succsessfully, however, the ADC always returns a value of 0.
Would you please help check what was done wrong with the newer version of nrfx. And how I can make it work again? Thank you very much.
Example code works with nrfx 1.7.0:
#include <assert.h> #include <os/os.h> #include <string.h> /* ADC */ #include "myadc/myadc.h" #include "nrf.h" #include <adc/adc.h> /* Nordic headers */ #include <nrfx.h> #include <nrf_saadc.h> #include <nrfx_saadc.h> #include <nrfx_config.h> #define ADC_NUMBER_SAMPLES (2) #define ADC_NUMBER_CHANNELS (1) nrfx_saadc_config_t adc_config = NRFX_SAADC_DEFAULT_CONFIG; struct adc_dev *adc; uint8_t *sample_buffer1; uint8_t *sample_buffer2; void * adc_init(void) { nrf_saadc_channel_config_t cc = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1); cc.gain = NRF_SAADC_GAIN1_6; cc.reference = NRF_SAADC_REFERENCE_INTERNAL; adc = (struct adc_dev *) os_dev_open("adc0", 0, &adc_config); assert(adc != NULL); adc_chan_config(adc, 0, &cc); sample_buffer1 = malloc(adc_buf_size(adc, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES)); sample_buffer2 = malloc(adc_buf_size(adc, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES)); memset(sample_buffer1, 0, adc_buf_size(adc, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES)); memset(sample_buffer2, 0, adc_buf_size(adc, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES)); adc_buf_set(adc, sample_buffer1, sample_buffer2, adc_buf_size(adc, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES)); return adc; } int adc_read(void *buffer, int buffer_len) { int i; int adc_result; int my_result_mv = 0; int rc; for (i = 0; i < ADC_NUMBER_SAMPLES; i++) { rc = adc_buf_read(adc, buffer, buffer_len, i, &adc_result); if (rc != 0) { goto err; } my_result_mv = adc_result_mv(adc, 0, adc_result); } adc_buf_release(adc, buffer, buffer_len); return my_result_mv; err: return (rc); }
Error shown with version nrfx 2.1.0:
Error: libs/my_drivers/myadc/src/myadc.c:18:1: error: unknown type name 'nrfx_saadc_config_t'; did you mean 'nrf_saadc_config_t'?
nrfx_saadc_config_t adc_config = NRFX_SAADC_DEFAULT_ADV_CONFIG;
^~~~~~~~~~~~~~~~~~~
nrf_saadc_config_t
In file included from libs/my_drivers/myadc/src/myadc.c:12:0:
repos/apache-mynewt-core/hw/mcu/nordic/src/ext/nrfx/drivers/include/nrfx_saadc.h:132:5: error: field name not in record or union initializer
.oversampling = NRF_SAADC_OVERSAMPLE_DISABLED, \
^
libs/my_drivers/myadc/src/myadc.c:18:34: note: in expansion of macro 'NRFX_SAADC_DEFAULT_ADV_CONFIG'
nrfx_saadc_config_t adc_config = NRFX_SAADC_DEFAULT_ADV_CONFIG;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
repos/apache-mynewt-core/hw/mcu/nordic/src/ext/nrfx/drivers/include/nrfx_saadc.h:132:5: note: (near initialization for 'adc_config')
.oversampling = NRF_SAADC_OVERSAMPLE_DISABLED, \
^
libs/my_drivers/myadc/src/myadc.c:18:34: note: in expansion of macro 'NRFX_SAADC_DEFAULT_ADV_CONFIG'
nrfx_saadc_config_t adc_config = NRFX_SAADC_DEFAULT_ADV_CONFIG;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
repos/apache-mynewt-core/hw/mcu/nordic/src/ext/nrfx/drivers/include/nrfx_saadc.h:133:5: error: field name not in record or union initializer
.burst = NRF_SAADC_BURST_DISABLED, \
^
libs/my_drivers/myadc/src/myadc.c:18:34: note: in expansion of macro 'NRFX_SAADC_DEFAULT_ADV_CONFIG'
nrfx_saadc_config_t adc_config = NRFX_SAADC_DEFAULT_ADV_CONFIG;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
repos/apache-mynewt-core/hw/mcu/nordic/src/ext/nrfx/drivers/include/nrfx_saadc.h:133:5: note: (near initialization for 'adc_config')
.burst = NRF_SAADC_BURST_DISABLED, \
^
libs/my_drivers/myadc/src/myadc.c:18:34: note: in expansion of macro 'NRFX_SAADC_DEFAULT_ADV_CONFIG'
nrfx_saadc_config_t adc_config = NRFX_SAADC_DEFAULT_ADV_CONFIG;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
repos/apache-mynewt-core/hw/mcu/nordic/src/ext/nrfx/drivers/include/nrfx_saadc.h:133:26: error: excess elements in scalar initializer [-Werror]
.burst = NRF_SAADC_BURST_DISABLED, \
^
libs/my_drivers/myadc/src/myadc.c:18:34: note: in expansion of macro 'NRFX_SAADC_DEFAULT_ADV_CONFIG'
nrfx_saadc_config_t adc_config = NRFX_SAADC_DEFAULT_ADV_CONFIG;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
repos/apache-mynewt-core/hw/mcu/nordic/src/ext/nrfx/drivers/include/nrfx_saadc.h:133:26: note: (near initialization for 'adc_config')
.burst = NRF_SAADC_BURST_DISABLED, \
^
libs/my_drivers/myadc/src/myadc.c:18:34: note: in expansion of macro 'NRFX_SAADC_DEFAULT_ADV_CONFIG'
nrfx_saadc_config_t adc_config = NRFX_SAADC_DEFAULT_ADV_CONFIG;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
repos/apache-mynewt-core/hw/mcu/nordic/src/ext/nrfx/drivers/include/nrfx_saadc.h:134:5: error: field name not in record or union initializer
.internal_timer_cc = 0, \
^
libs/my_drivers/myadc/src/myadc.c:18:34: note: in expansion of macro 'NRFX_SAADC_DEFAULT_ADV_CONFIG'
nrfx_saadc_config_t adc_config = NRFX_SAADC_DEFAULT_ADV_CONFIG;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
repos/apache-mynewt-core/hw/mcu/nordic/src/ext/nrfx/drivers/include/nrfx_saadc.h:134:5: note: (near initialization for 'adc_config')
.internal_timer_cc = 0, \
^
libs/my_drivers/myadc/src/myadc.c:18:34: note: in expansion of macro 'NRFX_SAADC_DEFAULT_ADV_CONFIG'
nrfx_saadc_config_t adc_config = NRFX_SAADC_DEFAULT_ADV_CONFIG;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
repos/apache-mynewt-core/hw/mcu/nordic/src/ext/nrfx/drivers/include/nrfx_saadc.h:134:26: error: excess elements in scalar initializer [-Werror]
.internal_timer_cc = 0, \
^
libs/my_drivers/myadc/src/myadc.c:18:34: note: in expansion of macro 'NRFX_SAADC_DEFAULT_ADV_CONFIG'
nrfx_saadc_config_t adc_config = NRFX_SAADC_DEFAULT_ADV_CONFIG;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
repos/apache-mynewt-core/hw/mcu/nordic/src/ext/nrfx/drivers/include/nrfx_saadc.h:134:26: note: (near initialization for 'adc_config')
.internal_timer_cc = 0, \
^
libs/my_drivers/myadc/src/myadc.c:18:34: note: in expansion of macro 'NRFX_SAADC_DEFAULT_ADV_CONFIG'
nrfx_saadc_config_t adc_config = NRFX_SAADC_DEFAULT_ADV_CONFIG;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
repos/apache-mynewt-core/hw/mcu/nordic/src/ext/nrfx/drivers/include/nrfx_saadc.h:135:5: error: field name not in record or union initializer
.start_on_end = false, \
^
libs/my_drivers/myadc/src/myadc.c:18:34: note: in expansion of macro 'NRFX_SAADC_DEFAULT_ADV_CONFIG'
nrfx_saadc_config_t adc_config = NRFX_SAADC_DEFAULT_ADV_CONFIG;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
repos/apache-mynewt-core/hw/mcu/nordic/src/ext/nrfx/drivers/include/nrfx_saadc.h:135:5: note: (near initialization for 'adc_config')
.start_on_end = false, \
^
libs/my_drivers/myadc/src/myadc.c:18:34: note: in expansion of macro 'NRFX_SAADC_DEFAULT_ADV_CONFIG'
nrfx_saadc_config_t adc_config = NRFX_SAADC_DEFAULT_ADV_CONFIG;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libs/my_drivers/myadc/src/myadc.c:18:34: error: excess elements in scalar initializer [-Werror]
libs/my_drivers/myadc/src/myadc.c:18:34: note: (near initialization for 'adc_config')
libs/my_drivers/myadc/src/myadc.c: In function 'adc_init':
libs/my_drivers/myadc/src/myadc.c:28:5: error: unknown type name 'nrfx_saadc_channel_config_t'; did you mean 'nrf_saadc_channel_config_t'?
nrfx_saadc_channel_config_t cc = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1);
^~~~~~~~~~~~~~~~~~~~~~~~~~~
nrf_saadc_channel_config_t
libs/my_drivers/myadc/src/myadc.c:28:38: error: implicit declaration of function 'NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE'; did you mean 'NRFX_SAADC_DEFAULT_CHANNEL_SE'? [-Werror=implicit-function-declaration]
nrfx_saadc_channel_config_t cc = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NRFX_SAADC_DEFAULT_CHANNEL_SE
libs/my_drivers/myadc/src/myadc.c:29:7: error: request for member 'gain' in something not a structure or union
cc.gain = NRF_SAADC_GAIN1_6;
^
libs/my_drivers/myadc/src/myadc.c:30:7: error: request for member 'reference' in something not a structure or union
cc.reference = NRF_SAADC_REFERENCE_INTERNAL;
^
cc1: all warnings being treated as errors
Modified code without compiling error but with ADC value always 0: (changes are highlighted)
Any help is appreciated.
Thank you very much.