Hello,
I would like to sample the battery level of my nrf52832 DK and send it through BLE. I have started from the example ble_app_hrs for SDK12.2 since the service was already present and I had just to modify the battery simulator there present by the SAADC sampling procedure.
I have configured my SAADC like this
static uint32_t saadc_start(batt_meas_param_t batt_meas_init)
{
uint32_t err_code;
nrf_drv_saadc_config_t saadc_config = NRF_DRV_SAADC_DEFAULT_CONFIG;
err_code = nrf_drv_saadc_init(&saadc_config, saadc_handler_interrupt);
APP_ERROR_CHECK(err_code);
nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(batt_meas_init.adc_pin_no);
err_code = nrf_drv_saadc_channel_init(0, &channel_config);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_buffer_convert(m_buffer, ADC_BUF_SIZE);
APP_ERROR_CHECK(err_code);
return M_BATT_STATUS_CODE_SUCCESS;
}
The handler saadc_handler_interrupt looks like
void saadc_handler_interrupt(nrf_drv_saadc_evt_t const * const p_event)
{
uint32_t err_code;
uint16_t voltage;
if (p_event->type == NRF_DRV_SAADC_EVT_CALIBRATEDONE)
{
m_adc_cal_in_progress = false;
}
else if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{
err_code = adc_to_batt_voltage(*p_event->data.done.p_buffer, &voltage);
APP_ERROR_CHECK(err_code);
batt_event_handler_adc(voltage);
}
nrf_drv_saadc_uninit();
}
Running into debug shows that the function saadc_start is executed without error, then I have a timer of 4s that will trigger a battery update. When this happens the function triggered is
static void battery_level_update(void)
{
uint32_t err_code;
uint8_t battery_level;
NRF_LOG_INFO("Read value from saadc %d\n",0);
err_code = nrf_drv_saadc_sample();
APP_ERROR_CHECK(err_code);
}
Debugger shows how saadc_sample() is called, but after this call the program goes directly to function app_error_fault_handler(). I see that within the call nrf_drv_saadc_sample() it enters on the conition m_cb.low_power_mode, but never goes back and therefore I am prompted into the app_error_fault_handler()....
ret_code_t nrf_drv_saadc_sample()
{
ASSERT(m_cb.state != NRF_DRV_STATE_UNINITIALIZED);
ret_code_t err_code = NRF_SUCCESS;
if (m_cb.adc_state != NRF_SAADC_STATE_BUSY)
{
err_code = NRF_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);
}
NRF_LOG_INFO("Function: %s, error code: %s.\r\n", (uint32_t)__func__, (uint32_t)ERR_TO_STR(err_code));
return err_code;
}
Any hint why this is happening?
THanks in advance!