I'm trying to setup the SAADC, I do this as much as possible without examples, so I really understand what is going on.
Setting up:
ret_code_t err_code; adc_config.resolution = NRF_SAADC_RESOLUTION_14BIT; adc_config.oversample = NRF_SAADC_OVERSAMPLE_8X; adc_config.interrupt_priority = 2; adc_config.low_power_mode = false; err_code = nrfx_saadc_init(&adc_config,event_handler); APP_ERROR_CHECK(err_code); //RESULT = [V(P) – V(N) ] * GAIN/REFERENCE * 2^(RESOLUTION - m) //try first alone ain2 = Vcell10 adc_channel_config.resistor_p = NRF_SAADC_RESISTOR_DISABLED; adc_channel_config.resistor_n = NRF_SAADC_RESISTOR_DISABLED; //Input ADC ideally around 0.6V So gain of 0.6/3 = 0.2 adc_channel_config.gain = SAADC_CH_CONFIG_GAIN_Gain1_5; adc_channel_config.reference = SAADC_CH_CONFIG_REFSEL_Internal; adc_channel_config.acq_time = NRF_SAADC_ACQTIME_15US; adc_channel_config.mode = NRF_SAADC_MODE_SINGLE_ENDED; adc_channel_config.burst = NRF_SAADC_BURST_ENABLED; adc_channel_config.pin_p = NRF_SAADC_INPUT_AIN2; adc_channel_config.pin_n = NRF_SAADC_INPUT_DISABLED; //Single ended err_code = nrfx_saadc_channel_init(2,&adc_channel_config); APP_ERROR_CHECK(err_code);
After this I try to start an ADC task but it fails. In the function for starting:
nrfx_err_t nrfx_saadc_sample()
{
NRFX_ASSERT(m_cb.state != NRFX_DRV_STATE_UNINITIALIZED);
nrfx_err_t err_code = NRFX_SUCCESS;
if (m_cb.adc_state != NRF_SAADC_STATE_BUSY)
{
err_code = NRFX_ERROR_INVALID_STATE;
}
else if (m_cb.low_power_mode)
{
nrf_saadc_task_trigger(NRF_SAADC_TASK_START);
}
else
{
nrf_saadc_task_trigger(NRF_SAADC_TASK_SAMPLE);
}
NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
return err_code;
}
There is the if statement if (m_cb.adc_state != NRF_SAADC_STATE_BUSY) but should it not be if (m_cb.adc_state == NRF_SAADC_STATE_BUSY) ?
Because if the ADC is busy then it will start an adc sampling, while you would expect when it is busy it should not and vica versa.
When I enter the function, the state is NRF_SAADC_STATE_IDLE.