I need to measure with SAADC two channels in the same time (NRF_SAADC_INPUT_AIN6 and NRF_SAADC_INPUT_AIN4).
Is possible ?
Have you an code example?
Thanks
Marco
I need to measure with SAADC two channels in the same time (NRF_SAADC_INPUT_AIN6 and NRF_SAADC_INPUT_AIN4).
Is possible ?
Have you an code example?
Thanks
Marco
Dear, all.
I have activated the second channel:
#define NRFX_SAADC_PULSE_CHANNEL_CONFIG_SE(PIN_P) \ { \ .resistor_p = NRF_SAADC_RESISTOR_DISABLED, \ .resistor_n = NRF_SAADC_RESISTOR_DISABLED, \ .gain = NRF_SAADC_GAIN1_6, \ .reference = NRF_SAADC_REFERENCE_INTERNAL, \ .acq_time = NRF_SAADC_ACQTIME_3US, \ .mode = NRF_SAADC_MODE_SINGLE_ENDED, \ .burst = NRF_SAADC_BURST_DISABLED, \ .pin_p = (nrf_saadc_input_t)(PIN_P), \ .pin_n = NRF_SAADC_INPUT_DISABLED \ } #define NRFX_SAADC_INEX_CHANNEL_CONFIG_SE(PIN_P) \ { \ .resistor_p = NRF_SAADC_RESISTOR_DISABLED, \ .resistor_n = NRF_SAADC_RESISTOR_DISABLED, \ .gain = NRF_SAADC_GAIN1_6, \ .reference = NRF_SAADC_REFERENCE_INTERNAL, \ .acq_time = NRF_SAADC_ACQTIME_3US, \ .mode = NRF_SAADC_MODE_SINGLE_ENDED, \ .burst = NRF_SAADC_BURST_DISABLED, \ .pin_p = (nrf_saadc_input_t)(PIN_P), \ .pin_n = NRF_SAADC_INPUT_DISABLED \ } nrf_saadc_channel_config_t channel_config_INEX = NRFX_SAADC_INEX_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN6); nrf_saadc_channel_config_t channel_config_PULSE = NRFX_SAADC_PULSE_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN4); void saadc_init(unsigned char type) { ret_code_t err_code; err_code = nrf_drv_saadc_init(NULL, saadc_PULSE_INEX_callback); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_channel_init(0, &channel_config_INEX); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_channel_init(1, &channel_config_PULSE); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool_scann[0], SAMPLES_IN_BUFFER_SCANN); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool_scann[1], SAMPLES_IN_BUFFER_SCANN); APP_ERROR_CHECK(err_code); }
seem work but sametime in the buffer I find the values exchanged between channels. WHY ?
this is my callback function:
void saadc_PULSE_INEX_callback(nrf_drv_saadc_evt_t const * p_event) { nrf_saadc_value_t adc_result; uint16_t value_mV_local; uint16_t value_mV_acc; int i; if (p_event->type == NRF_DRV_SAADC_EVT_DONE) { ret_code_t err_code; err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER_SCANN); APP_ERROR_CHECK(err_code); adc_result = p_event->data.done.p_buffer[0]; adc_value_inex = ADC_RESULT_IN_MILLI_VOLTS(adc_result);//+ DIODE_FWD_VOLT_DROP_MILLIVOLTS; adc_result = p_event->data.done.p_buffer[1]; adc_value_pulse = ADC_RESULT_IN_MILLI_VOLTS(adc_result);//+ DIODE_FWD_VOLT_DROP_MILLIVOLTS; } }
with this code I ask a start of new conversion
nrf_drv_saadc_sample();
while(!u_STATO10.STATO10_bit.mm_EOC);
the bit mm_EOC is for waiting the end of conversion. This is set in the callback with the event DONE.
Marco
I had forgotten to say:
#define SAMPLES_IN_BUFFER_SCANN 2
static nrf_saadc_value_t m_buffer_pool_scann[2][SAMPLES_IN_BUFFER_SCANN];
Marco
Yes. The buffer isn't aware of how many channels you use, so you should set the buffer equal to N * number of channels, N=0,1,2,3,... in order to always have the same channel on the same place in your buffer.
Yes. The buffer isn't aware of how many channels you use, so you should set the buffer equal to N * number of channels, N=0,1,2,3,... in order to always have the same channel on the same place in your buffer.
Then ? how to set it ?. I don't understand.
static nrf_saadc_value_t m_buffer_pool_scann[2][2]; This is NOT correct ?
I use 2 channels. Could you tell me how ?
Sorry. I meant the SAMPLES_IN_BUFFER define should be N * the numbers of channels you use.
I don't know what chip or SDK version you use, but this is written in SDK15.3.0, and tested with nRF52832:
/** * Copyright (c) 2014 - 2019, Nordic Semiconductor ASA * * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * 2. Redistributions in binary form, except as embedded into a Nordic * Semiconductor ASA integrated circuit in a product or a software update for * such product, must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * 3. Neither the name of Nordic Semiconductor ASA nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * 4. This software, with or without modification, must only be used with a * Nordic Semiconductor ASA integrated circuit. * * 5. Any software provided in binary form under this license must not be reverse * engineered, decompiled, modified and/or disassembled. * * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ /** @file * @defgroup nrf_adc_example main.c * @{ * @ingroup nrf_adc_example * @brief ADC Example Application main file. * * This file contains the source code for a sample application using ADC. * * @image html example_board_setup_a.jpg "Use board setup A for this example." */ #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <string.h> #include "nrf.h" #include "nrf_drv_saadc.h" #include "nrf_drv_ppi.h" #include "nrf_drv_timer.h" #include "boards.h" #include "app_error.h" #include "nrf_delay.h" #include "app_util_platform.h" #include "nrf_pwr_mgmt.h" #include "nrf_log.h" #include "nrf_log_ctrl.h" #include "nrf_log_default_backends.h" #define SAMPLES_IN_BUFFER 2 volatile uint8_t state = 1; static const nrf_drv_timer_t m_timer = NRF_DRV_TIMER_INSTANCE(0); static nrf_saadc_value_t m_buffer_pool[2][SAMPLES_IN_BUFFER]; static nrf_ppi_channel_t m_ppi_channel; static uint32_t m_adc_evt_counter; void timer_handler(nrf_timer_event_t event_type, void * p_context) { } void saadc_sampling_event_init(void) { ret_code_t err_code; err_code = nrf_drv_ppi_init(); APP_ERROR_CHECK(err_code); nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG; timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32; err_code = nrf_drv_timer_init(&m_timer, &timer_cfg, timer_handler); APP_ERROR_CHECK(err_code); /* setup m_timer for compare event every 400ms */ uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer, 400); nrf_drv_timer_extended_compare(&m_timer, NRF_TIMER_CC_CHANNEL0, ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false); nrf_drv_timer_enable(&m_timer); uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&m_timer, NRF_TIMER_CC_CHANNEL0); uint32_t saadc_sample_task_addr = nrf_drv_saadc_sample_task_get(); /* setup ppi channel so that timer compare event is triggering sample task in SAADC */ err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel); APP_ERROR_CHECK(err_code); err_code = nrf_drv_ppi_channel_assign(m_ppi_channel, timer_compare_event_addr, saadc_sample_task_addr); APP_ERROR_CHECK(err_code); } void saadc_sampling_event_enable(void) { ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel); APP_ERROR_CHECK(err_code); } void saadc_callback(nrf_drv_saadc_evt_t const * p_event) { if (p_event->type == NRF_DRV_SAADC_EVT_DONE) { ret_code_t err_code; err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); int i; NRF_LOG_INFO("ADC event number: %d", (int)m_adc_evt_counter); for (i = 0; i < SAMPLES_IN_BUFFER; i++) { NRF_LOG_INFO("%d", p_event->data.done.p_buffer[i]); } m_adc_evt_counter++; } } void saadc_init(void) { ret_code_t err_code; nrf_saadc_channel_config_t channel_config0 = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0); nrf_saadc_channel_config_t channel_config1 = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1); err_code = nrf_drv_saadc_init(NULL, saadc_callback); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_channel_init(0, &channel_config0); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_channel_init(1, &channel_config1); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); } /** * @brief Function for main application entry. */ int main(void) { uint32_t err_code = NRF_LOG_INIT(NULL); APP_ERROR_CHECK(err_code); NRF_LOG_DEFAULT_BACKENDS_INIT(); ret_code_t ret_code = nrf_pwr_mgmt_init(); APP_ERROR_CHECK(ret_code); saadc_init(); saadc_sampling_event_init(); saadc_sampling_event_enable(); NRF_LOG_INFO("SAADC HAL simple example started."); while (1) { nrf_pwr_mgmt_run(); NRF_LOG_FLUSH(); } } /** @} */
Thank, but your file is exatly same at my code.
I ask a conversion each 1 mS but sometime the vale are swapped.
Marco
Can you test this one?
/** * Copyright (c) 2014 - 2019, Nordic Semiconductor ASA * * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * 2. Redistributions in binary form, except as embedded into a Nordic * Semiconductor ASA integrated circuit in a product or a software update for * such product, must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. * * 3. Neither the name of Nordic Semiconductor ASA nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * 4. This software, with or without modification, must only be used with a * Nordic Semiconductor ASA integrated circuit. * * 5. Any software provided in binary form under this license must not be reverse * engineered, decompiled, modified and/or disassembled. * * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ /** @file * @defgroup nrf_adc_example main.c * @{ * @ingroup nrf_adc_example * @brief ADC Example Application main file. * * This file contains the source code for a sample application using ADC. * * @image html example_board_setup_a.jpg "Use board setup A for this example." */ #include <stdbool.h> #include <stdint.h> #include <stdio.h> #include <string.h> #include "nrf.h" #include "nrf_drv_saadc.h" #include "nrf_drv_ppi.h" #include "nrf_drv_timer.h" #include "boards.h" #include "app_error.h" #include "nrf_delay.h" #include "app_util_platform.h" #include "nrf_pwr_mgmt.h" #include "nrf_log.h" #include "nrf_log_ctrl.h" #include "nrf_log_default_backends.h" #define SAMPLES_IN_BUFFER 2 volatile uint8_t state = 1; static const nrf_drv_timer_t m_timer = NRF_DRV_TIMER_INSTANCE(0); static nrf_saadc_value_t m_buffer_pool[2][SAMPLES_IN_BUFFER]; static nrf_ppi_channel_t m_ppi_channel; static uint32_t m_adc_evt_counter; void timer_handler(nrf_timer_event_t event_type, void * p_context) { } void saadc_sampling_event_init(void) { ret_code_t err_code; err_code = nrf_drv_ppi_init(); APP_ERROR_CHECK(err_code); nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG; timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32; err_code = nrf_drv_timer_init(&m_timer, &timer_cfg, timer_handler); APP_ERROR_CHECK(err_code); /* setup m_timer for compare event every 400ms */ uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer, 1); nrf_drv_timer_extended_compare(&m_timer, NRF_TIMER_CC_CHANNEL0, ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false); nrf_drv_timer_enable(&m_timer); uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&m_timer, NRF_TIMER_CC_CHANNEL0); uint32_t saadc_sample_task_addr = nrf_drv_saadc_sample_task_get(); /* setup ppi channel so that timer compare event is triggering sample task in SAADC */ err_code = nrf_drv_ppi_channel_alloc(&m_ppi_channel); APP_ERROR_CHECK(err_code); err_code = nrf_drv_ppi_channel_assign(m_ppi_channel, timer_compare_event_addr, saadc_sample_task_addr); APP_ERROR_CHECK(err_code); } void saadc_sampling_event_enable(void) { ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel); APP_ERROR_CHECK(err_code); } void saadc_callback(nrf_drv_saadc_evt_t const * p_event) { if (p_event->type == NRF_DRV_SAADC_EVT_DONE) { ret_code_t err_code; err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); if(m_adc_evt_counter%100 == 0) { int i; NRF_LOG_RAW_INFO("%d, ", (int)m_adc_evt_counter); for (i = 0; i < SAMPLES_IN_BUFFER; i++) { NRF_LOG_RAW_INFO("%d,", p_event->data.done.p_buffer[i]); } NRF_LOG_RAW_INFO("\r\n"); } m_adc_evt_counter++; } } void saadc_init(void) { ret_code_t err_code; nrf_saadc_channel_config_t channel_config0 = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0); nrf_saadc_channel_config_t channel_config1 = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1); err_code = nrf_drv_saadc_init(NULL, saadc_callback); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_channel_init(0, &channel_config0); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_channel_init(1, &channel_config1); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAMPLES_IN_BUFFER); APP_ERROR_CHECK(err_code); } /** * @brief Function for main application entry. */ int main(void) { uint32_t err_code = NRF_LOG_INIT(NULL); APP_ERROR_CHECK(err_code); NRF_LOG_DEFAULT_BACKENDS_INIT(); ret_code_t ret_code = nrf_pwr_mgmt_init(); APP_ERROR_CHECK(ret_code); saadc_init(); saadc_sampling_event_init(); saadc_sampling_event_enable(); NRF_LOG_INFO("SAADC HAL simple example started."); while (1) { nrf_pwr_mgmt_run(); NRF_LOG_FLUSH(); } } /** @} */
I can't see that the values are swapped. Are they if you run the project with this main.c file?
In your case, how do you determine that the values/channels are swapped?
No, is impossible for me now.
static void lpcomp_event_handler(nrf_lpcomp_event_t event) { if (event == NRF_LPCOMP_EVENT_DOWN) { nrf_gpio_pin_set(PIN_P0_20_TP6); u_STATO10.STATO10_bit.mm_EOC = 0; nrf_drv_saadc_sample(); while(!u_STATO10.STATO10_bit.mm_EOC); buff_I[puntIP]=adc_value_inex; buff_P[puntIP]=adc_value_pulse; puntIP++; if (puntIP>99)puntIP=0; nrf_gpio_pin_clear(PIN_P0_20_TP6); } }
I working on my target and I don' start a conversion with a timer but when a pulse arrive on AIN5.
I trigger this event with a LPCOMP event DOWN on AIN5.
I have tested it with the PO_20 test point and this event is perfectly synchronized with the AIN5.
I have discovered that if the time between 2 conversion is more then 10 mS the error do not appear
I have send you my situation.
In the report the signal RED is connected to AIN5 in order to generate an event on LPCOMP
In BLU one of the 2 signal that I need to measure with the SAADC
In yellow the signal PO_20 (test point) this signal go up when the evend LPCOMP down arrive and go down after the conversion (see my code above).
In the buffer buff_I and buff_P I store my conversion and sometime I read a zero value instead the correct value. see file in attach.
Note that on the oscilloscope the signal BLU is always UP when the YELLOW signal arrive.