Hello. I am facing an issue in configuring ADC for multichannels. I was looking at the Nordic fundamentals course Lesson 6 Exercise 2.In this example, the adc is configured to scan single channel when I changed some settings to scan 4 channels I got this error and my code is not working.
I don't know what am I doing wrong. I want adc to scan in interrupt. Here is my code.
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <nrfx_saadc.h>
#include <stdint.h>
/* STEP 3.1 - Declare the struct to hold the configuration for the SAADC channel used to sample the battery voltage */
static nrfx_saadc_channel_t channel[4] = {
NRFX_SAADC_DEFAULT_CHANNEL_SE(NRF_SAADC_INPUT_AIN0, 0),
NRFX_SAADC_DEFAULT_CHANNEL_SE(NRF_SAADC_INPUT_AIN1, 1),
NRFX_SAADC_DEFAULT_CHANNEL_SE(NRF_SAADC_INPUT_AIN2, 2),
NRFX_SAADC_DEFAULT_CHANNEL_SE(NRF_SAADC_INPUT_AIN3, 3)
};
/* STEP 3.2 - Declare the buffer to hold the SAAD sample value */
static int16_t sample[4];
static uint32_t counter = 0;
/* STEP 4.1 - Define the battery sample interval */
#define BATTERY_SAMPLE_INTERVAL_MS 1000
/* STEP 4.3 - Add forward declaration of timer callback handler */
static void battery_sample_timer_handler(struct k_timer *timer);
/* STEP 4.2 - Define the battery sample timer instance */
K_TIMER_DEFINE(battery_sample_timer, battery_sample_timer_handler, NULL);
/* STEP 7.1 - Implement timer callback handler function */
void battery_sample_timer_handler(struct k_timer *timer)
{
/* STEP 7.2 - Trigger the sampling */
nrfx_err_t err = nrfx_saadc_mode_trigger();
if (err != NRFX_SUCCESS)
{
printk("nrfx_saadc_mode_trigger error: %08x", err);
return;
}
printk("\n-------------------Reading - %d-------------------\n", counter++);
for (int i = 0; i < 4; i++)
{
printk("SAADC%d sample: %d \n", i, sample[i]);
}
}
static void configure_saadc(void)
{
/* STEP 5.1 - Connect ADC interrupt to nrfx interrupt handler */
IRQ_CONNECT(DT_IRQN(DT_NODELABEL(adc)), DT_IRQ(DT_NODELABEL(adc), priority), nrfx_isr, nrfx_saadc_irq_handler, 0);
/* STEP 5.2 - Connect ADC interrupt to nrfx interrupt handler */
nrfx_err_t err = nrfx_saadc_init(DT_IRQ(DT_NODELABEL(adc), priority));
if (err != NRFX_SUCCESS)
{
printk("nrfx_saadc_mode_trigger error: %08x", err);
return;
}
/* STEP 5.3 - Configure the SAADC channel */
for (int i = 0; i < 4; i++)
{
channel[i].channel_config.gain = NRF_SAADC_GAIN1_4;
channel[i].channel_config.acq_time = NRF_SAADC_ACQTIME_40US;
channel[i].channel_config.reference = NRF_SAADC_REFERENCE_VDD4;
err = nrfx_saadc_channels_config(&channel[i], (i + 1));
if (err != NRFX_SUCCESS)
{
printk("nrfx_saadc_channels_config error: %08x", err);
return;
}
}
/* STEP 5.4 - Configure nrfx_SAADC driver in simple and blocking mode */
err = nrfx_saadc_simple_mode_set(BIT(0) | BIT(1) | BIT(2) | BIT(3),
NRF_SAADC_RESOLUTION_12BIT,
NRF_SAADC_OVERSAMPLE_DISABLED,
NULL);
if (err != NRFX_SUCCESS)
{
printk("nrfx_saadc_simple_mode_set error: %08x", err);
return;
}
/* STEP 5.5 - Set buffer where sample will be stored */
err = nrfx_saadc_buffer_set(&sample, 4);
if (err != NRFX_SUCCESS)
{
printk("nrfx_saadc_buffer_set error: %08x", err);
return;
}
/* STEP 6 - Start periodic timer for battery sampling */
k_timer_start(&battery_sample_timer, K_NO_WAIT, K_MSEC(BATTERY_SAMPLE_INTERVAL_MS));
}
int main(void)
{
configure_saadc();
k_sleep(K_FOREVER);
return 0;
}