I used 2 channel saadc, channel 1 and channel 3.
Maybe in the first few minutes, it's done ok,
p_event->data.done.p_buffer[0] is belong channel1 adc value
p_event->data.done.p_buffer[1] is belong channel3 adc value
but, it will run error,
p_event->data.done.p_buffer[0] is belong channel3 adc value
p_event->data.done.p_buffer[1] is belong channel1 adc value
#define SAMPLE_CHANNEL_CNT 2
#define SAMPLE_CNT 2
#define ADC_TASK_TIMER APP_TIMER_TICKS(3)
APP_TIMER_DEF(m_adc_task_timer_id);
/*<adc sample value buffer*/
static nrf_saadc_value_t m_buffer[SAMPLE_CHANNEL_CNT][SAMPLE_CNT];
static void adc_timeout_handler(void* p_context);
static void saadc_callback(nrf_drv_saadc_evt_t const * p_event);
void saadc_module_init(void)
{
ret_code_t err_code;
nrfx_saadc_config_t saadc_config = NRF_DRV_SAADC_DEFAULT_CONFIG;
saadc_config.resolution = NRF_SAADC_RESOLUTION_14BIT;
saadc_config.interrupt_priority = 1;
saadc_config.low_power_mode = false;
err_code = nrf_drv_saadc_init(&saadc_config, saadc_callback);
APP_ERROR_CHECK(err_code);
// do a calibrate
while (nrf_drv_saadc_calibrate_offset() != NRFX_SUCCESS)
nrf_delay_ms(10);
while (nrf_drv_saadc_is_busy())
nrf_delay_ms(10);
pressure_adc_init();
battery_saadc_init();
err_code = nrf_drv_saadc_buffer_convert(m_buffer[0], SAMPLE_CNT);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_buffer_convert(m_buffer[1], SAMPLE_CNT);
APP_ERROR_CHECK(err_code);
err_code = app_timer_create(&m_adc_task_timer_id, APP_TIMER_MODE_REPEATED, adc_timeout_handler);
APP_ERROR_CHECK(err_code);
}
void saadc_module_start(void)
{
ret_code_t err_code;
err_code = app_timer_start(m_adc_task_timer_id, ADC_TASK_TIMER, NULL);
APP_ERROR_CHECK(err_code);
}
static void adc_timeout_handler(void* p_context)
{
ret_code_t err_code;
APP_ERROR_CHECK(nrf_drv_saadc_sample());
}
/**@brief adc module call back
*
* @param[in] p_event adc module event
*
*/
static void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
uint32_t err_code;
if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{
err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLE_CNT);
APP_ERROR_CHECK(err_code);
pressure_adc_update(p_event->data.done.p_buffer[0]);
battery_saadc_update(p_event->data.done.p_buffer[1]);
}
}