Skip to content

Commit

Permalink
drivers: adc: add API to support calibration
Browse files Browse the repository at this point in the history
Add a flag to the sequence structure that tells the driver it should
calibrate the ADC prior to initiating the sample.

Implement this for nRF SAADC.  The implementation supports the
workarounds for PAN-86 and PAN-178.

Relates-to: issue zephyrproject-rtos#11922
Signed-off-by: Peter A. Bigot <[email protected]>
  • Loading branch information
pabigot committed May 11, 2019
1 parent 3ce6770 commit eed3b68
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
25 changes: 20 additions & 5 deletions drivers/adc/adc_nrfx_saadc.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,14 @@ static int adc_nrfx_channel_setup(struct device *dev,

static void adc_context_start_sampling(struct adc_context *ctx)
{
ARG_UNUSED(ctx);

nrf_saadc_enable();

nrf_saadc_task_trigger(NRF_SAADC_TASK_START);
nrf_saadc_task_trigger(NRF_SAADC_TASK_SAMPLE);
if (ctx->sequence.calibrate) {
nrf_saadc_task_trigger(NRF_SAADC_TASK_CALIBRATEOFFSET);
} else {
nrf_saadc_task_trigger(NRF_SAADC_TASK_START);
nrf_saadc_task_trigger(NRF_SAADC_TASK_SAMPLE);
}
}

static void adc_context_update_buffer_pointer(struct adc_context *ctx,
Expand Down Expand Up @@ -365,6 +367,17 @@ static void saadc_irq_handler(void *param)
nrf_saadc_disable();

adc_context_on_sampling_done(&m_data.ctx, dev);
} else if (nrf_saadc_event_check(NRF_SAADC_EVENT_CALIBRATEDONE)) {
nrf_saadc_event_clear(NRF_SAADC_EVENT_CALIBRATEDONE);

/*
* The workaround for Nordic nRF52832 anomalies 86 and
* 178 is an explicit STOP after CALIBRATEOFFSET
* before issuing START.
*/
nrf_saadc_task_trigger(NRF_SAADC_TASK_STOP);
nrf_saadc_task_trigger(NRF_SAADC_TASK_START);
nrf_saadc_task_trigger(NRF_SAADC_TASK_SAMPLE);
}
}

Expand All @@ -373,7 +386,9 @@ DEVICE_DECLARE(adc_0);
static int init_saadc(struct device *dev)
{
nrf_saadc_event_clear(NRF_SAADC_EVENT_END);
nrf_saadc_int_enable(NRF_SAADC_INT_END);
nrf_saadc_event_clear(NRF_SAADC_EVENT_CALIBRATEDONE);
nrf_saadc_int_enable(NRF_SAADC_INT_END
| NRF_SAADC_INT_CALIBRATEDONE);
NRFX_IRQ_ENABLE(DT_NORDIC_NRF_SAADC_ADC_0_IRQ);

IRQ_CONNECT(DT_NORDIC_NRF_SAADC_ADC_0_IRQ,
Expand Down
10 changes: 10 additions & 0 deletions include/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ struct adc_sequence {
* a specific mode (e.g. when sampling multiple channels).
*/
u8_t oversampling;

/**
* Perform calibration before the reading is taken if requested.
*
* The impact of channel configuration on the calibration
* process is specific to the underlying hardware. ADC
* implementations that do not support calibration should
* ignore this flag.
*/
bool calibrate;
};


Expand Down

0 comments on commit eed3b68

Please sign in to comment.