Hi there. I really love all the help and support this forum gives, especially compared to others. So thank you for taking the time to read this.
I have a custom PCB that I have designed and it works great. I am using an nRF52810 and the SAADC example (given below for reference):
/** * Copyright (c) 2014 - 2018, 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 5 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_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0); err_code = nrf_drv_saadc_init(NULL, saadc_callback); APP_ERROR_CHECK(err_code); err_code = nrf_drv_saadc_channel_init(0, &channel_config); 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(); } } /** @} */
My code is *slightly* different, as I am using UART printf() instead of logging the ADC value to a file - but it's pretty much the same.
Anyway, I find this code pretty confusing. I understand there is an initilisation function, a timer setup function, and a callback function which is triggered or something when the timer is called. But the code within each function is very confusing to me but I am keen to understand it better.I just wanted help on three minor parts:
- 1. Extending the example to use 7 of the analogue input channels (AIN0 to AIN6)
- 2. Using scanning mode to make this as fast as possible (I think the datasheet says if multiple channels are initialised this is enabled automatically anyway - am I correct?)
- 3. Making the sampling rate per channel 1kS/s (which corresponds to sampling 7 channels every 12ms) or faster. The datasheet says it should be able to do up to 200kS/s so I hope this isn't asking too much. I don't need to print the data at this rate, but at least be able to check (if adcValue > 500 then print for example)
I have attempted this already with the following code changes, basically just guessing:
void saadc_callback(nrf_drv_saadc_evt_t const * p_event) { /* SAADC Event is finished - read the channels */ if (p_event->type == NRF_DRV_SAADC_EVT_DONE) { /* Get ADC readings */ nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER); /* Print determined values */ printf("Channel 0: %d\r\n", p_event->data.done.p_buffer[0]); printf("Channel 1: %d\r\n", p_event->data.done.p_buffer[1]); printf("Channel 2: %d\r\n", p_event->data.done.p_buffer[2]); printf("Channel 3: %d\r\n", p_event->data.done.p_buffer[3]); printf("Channel 4: %d\r\n", p_event->data.done.p_buffer[4]); printf("Channel 5: %d\r\n", p_event->data.done.p_buffer[5]); printf("Channel 6: %d\r\n\r\n", p_event->data.done.p_buffer[6]); /* Not sure what this does */ m_adc_evt_counter++; if (m_adc_evt_counter == 7) { m_adc_evt_counter = 0; } /* Toggle LED */ //nrf_gpio_pin_toggle(LED_PIN_NUMBER); } } void saadc_init(void) { /* Create the configuration structs for all channels */ nrf_saadc_channel_config_t channel_0_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0); nrf_saadc_channel_config_t channel_1_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1); nrf_saadc_channel_config_t channel_2_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN2); nrf_saadc_channel_config_t channel_3_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN3); nrf_saadc_channel_config_t channel_4_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN4); nrf_saadc_channel_config_t channel_5_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN5); nrf_saadc_channel_config_t channel_6_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN6); /* Initialise the SAADC peripheral */ nrf_drv_saadc_init(NULL, saadc_callback); /* Initialise all channels */ nrf_drv_saadc_channel_init(0, &channel_0_config); nrf_drv_saadc_channel_init(1, &channel_1_config); nrf_drv_saadc_channel_init(2, &channel_2_config); nrf_drv_saadc_channel_init(3, &channel_3_config); nrf_drv_saadc_channel_init(4, &channel_4_config); nrf_drv_saadc_channel_init(5, &channel_5_config); nrf_drv_saadc_channel_init(6, &channel_6_config); /* Not sure what this does */ nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER); nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAMPLES_IN_BUFFER); nrf_drv_saadc_buffer_convert(m_buffer_pool[2], SAMPLES_IN_BUFFER); nrf_drv_saadc_buffer_convert(m_buffer_pool[3], SAMPLES_IN_BUFFER); nrf_drv_saadc_buffer_convert(m_buffer_pool[4], SAMPLES_IN_BUFFER); nrf_drv_saadc_buffer_convert(m_buffer_pool[5], SAMPLES_IN_BUFFER); nrf_drv_saadc_buffer_convert(m_buffer_pool[6], SAMPLES_IN_BUFFER); } void timer_handler(nrf_timer_event_t event_type, void * p_context) { /* Not sure what this is */ } void saadc_sampling_event_enable(void) { nrf_drv_ppi_channel_enable(m_ppi_channel); } void saadc_sampling_event_init(void) { /* Initialise PPI */ nrf_drv_ppi_init(); /* Set the configuration struct to a default timer */ nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG; /* Customise the bit width to 32 bits */ timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32; /* Initialise the timer */ nrf_drv_timer_init(&m_timer, &timer_cfg, timer_handler); /* setup m_timer for compare event every 12ms */ uint32_t ticks = nrf_drv_timer_ms_to_ticks(&m_timer, 12); 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 */ nrf_drv_ppi_channel_alloc(&m_ppi_channel); nrf_drv_ppi_channel_assign(m_ppi_channel, timer_compare_event_addr, saadc_sample_task_addr); }
However, this did not really work and gave some weird output results. So I must be doing something wrong. Things like what SAMPLES_IN_BUFFER should be or what it means, or what m_adc_evt_counter means and what its importance is, or what nrf_drv_saadc_buffer_convert(m_buffer_pool[<Channel Number>], SAMPLES_IN_BUFFER); does exactly for each channel.
I know this is a big question but it should be a simple concept - just utilising the ADC on more channels, and at a faster rate. So I hope someone can help and I really appreciate if so!
Thanks for your time reading! :)