nrfx 4.5.0
Loading...
Searching...
No Matches
SAADC driver

Successive Approximation Analog-to-Digital Converter (SAADC) peripheral driver. More...

Topics

 SAADC peripheral driver configuration
 

Data Structures

struct  nrfx_saadc_channel_t
 SAADC channel configuration structure. More...
 
struct  nrfx_saadc_adv_config_t
 SAADC driver advanced mode configuration structure. More...
 
struct  nrfx_saadc_done_evt_t
 SAADC driver done event data. More...
 
struct  nrfx_saadc_limit_evt_t
 SAADC driver limit event data. More...
 
struct  nrfx_saadc_evt_t
 SAADC driver event structure. More...
 

Macros

#define NRFX_SAADC_DEFAULT_ACQTIME   NRF_SAADC_ACQTIME_10US
 Auxiliary symbol specifying default value for the SAADC acquisition time.
 
#define NRFX_SAADC_DEFAULT_CONV_TIME   7
 Auxiliary symbol specifying default value for the SAADC conversion time.
 
#define NRFX_SAADC_INTERNAL_TIMER_INTERVAL_MAX_US   128U
 Maximum value of the internal timer interval in microseconds.
 
#define NRFX_SAADC_REF_INTERNAL_VALUE   NRFX_ANALOG_REF_INTERNAL_VAL
 Symbol specifying internal reference voltage.
 
#define NRFX_SAADC_DEFAULT_CHANNEL_SE(_pin_p, _index)
 SAADC channel default configuration for the single-ended mode.
 
#define NRFX_SAADC_DEFAULT_CHANNEL_DIFFERENTIAL(_pin_p, _pin_n, _index)
 SAADC channel default configuration for the differential mode.
 
#define NRFX_SAADC_SAMPLES_TO_BYTES(_samples)
 Macro for getting number of bytes needed to store specified number of SAADC samples.
 
#define NRFX_SAADC_SAMPLE_GET(_samples, _index)
 Macro for getting specified SAADC sample from the filled buffer.
 
#define NRFX_SAADC_DEFAULT_ADV_CONFIG
 SAADC driver advanced mode default configuration.
 

Typedefs

typedef void(* nrfx_saadc_event_handler_t) (nrfx_saadc_evt_t const *p_event)
 SAADC driver event handler.
 

Enumerations

enum  nrfx_saadc_evt_type_t {
  NRFX_SAADC_EVT_DONE ,
  NRFX_SAADC_EVT_LIMIT ,
  NRFX_SAADC_EVT_CALIBRATEDONE ,
  NRFX_SAADC_EVT_BUF_REQ ,
  NRFX_SAADC_EVT_READY ,
  NRFX_SAADC_EVT_FINISHED
}
 SAADC driver event types. More...
 

Functions

int nrfx_saadc_init (uint8_t interrupt_priority)
 Function for initializing the SAADC driver.
 
void nrfx_saadc_uninit (void)
 Function for uninitializing the SAADC driver.
 
bool nrfx_saadc_init_check (void)
 Function for checking if the SAADC driver is initialized.
 
uint16_t nrfx_saadc_interval_to_cc (uint16_t interval_us)
 Function for getting the internal timer CC value from the interval in microseconds.
 
int nrfx_saadc_channels_config (nrfx_saadc_channel_t const *p_channels, uint32_t channel_count)
 Function for configuring multiple SAADC channels.
 
int nrfx_saadc_channel_config (nrfx_saadc_channel_t const *p_channel)
 Function for configuring single SAADC channel.
 
uint32_t nrfx_saadc_channels_configured_get (void)
 Function for getting the currently configured SAADC channels.
 
int nrfx_saadc_channels_deconfig (uint32_t channel_mask)
 Function for deconfiguring the specified SAADC channels.
 
int nrfx_saadc_simple_mode_set (uint32_t channel_mask, nrf_saadc_resolution_t resolution, nrf_saadc_oversample_t oversampling, nrfx_saadc_event_handler_t event_handler)
 Function for setting the SAADC driver in the simple mode.
 
int nrfx_saadc_advanced_mode_set (uint32_t channel_mask, nrf_saadc_resolution_t resolution, nrfx_saadc_adv_config_t const *p_config, nrfx_saadc_event_handler_t event_handler)
 Function for setting the SAADC driver in the advanced mode.
 
int nrfx_saadc_buffer_set (nrf_saadc_value_t *p_buffer, uint16_t size)
 Function for supplying the buffer to be used in the next part of the conversion.
 
int nrfx_saadc_mode_trigger (void)
 Function for triggering the conversion in the configured mode.
 
void nrfx_saadc_abort (void)
 Function for aborting the ongoing and buffered conversions.
 
int nrfx_saadc_limits_set (uint8_t channel, int16_t limit_low, int16_t limit_high)
 Function for setting the SAADC channel limits.
 
int nrfx_saadc_offset_calibrate (nrfx_saadc_event_handler_t calib_event_handler)
 Function for starting the SAADC offset calibration.
 

Detailed Description

Successive Approximation Analog-to-Digital Converter (SAADC) peripheral driver.

Macro Definition Documentation

◆ NRFX_SAADC_DEFAULT_ADV_CONFIG

#define NRFX_SAADC_DEFAULT_ADV_CONFIG
Value:
{ \
.oversampling = NRF_SAADC_OVERSAMPLE_DISABLED, \
.internal_timer_cc = 0, \
.start_on_end = false, \
}
@ NRF_SAADC_BURST_DISABLED
Burst mode is disabled (normal operation).
Definition nrf_saadc.h:587
@ NRF_SAADC_OVERSAMPLE_DISABLED
No oversampling.
Definition nrf_saadc.h:475

SAADC driver advanced mode default configuration.

This configuration sets up advanced mode of the SAADC driver with the following options:

  • oversampling disabled
  • burst disabled
  • internal sampling timer disabled
  • triggering of the START task on the END event disabled
Parameters
[in]_pin_pPositive input analog pin.
[in]_pin_nNegative input analog pin.
[in]_indexChannel index.
See also
nrfx_saadc_adv_config_t

◆ NRFX_SAADC_DEFAULT_CHANNEL_DIFFERENTIAL

#define NRFX_SAADC_DEFAULT_CHANNEL_DIFFERENTIAL ( _pin_p,
_pin_n,
_index )
Value:
{ \
.channel_config = \
{ \
NRFX_COND_CODE_1(NRF_SAADC_HAS_CH_CONFIG_RES, \
(.resistor_p = NRF_SAADC_RESISTOR_DISABLED, \
.resistor_n = NRF_SAADC_RESISTOR_DISABLED,), \
()) \
NRFX_COND_CODE_1(NRF_SAADC_HAS_CH_GAIN, \
(.gain = NRF_SAADC_GAIN1,), \
()) \
NRFX_COND_CODE_1(NRF_SAADC_HAS_CH_BURST, \
(.burst = NRF_SAADC_BURST_DISABLED,), \
()) \
NRFX_COND_CODE_1(NRF_SAADC_HAS_CH_CHOPPING, \
(.chopping = NRF_SAADC_CHOPPING_DISABLED,), \
()) \
NRFX_COND_CODE_1(NRF_SAADC_HAS_CH_HIGHSPEED, \
(.highspeed = NRF_SAADC_HIGHSPEED_DISABLED,), \
()) \
NRFX_COND_CODE_1(NRF_SAADC_HAS_CONVTIME, \
(.conv_time = NRFX_SAADC_DEFAULT_CONV_TIME,), \
()) \
}, \
.pin_p = (nrfx_analog_input_t)_pin_p, \
.pin_n = (nrfx_analog_input_t)_pin_n, \
.channel_index = _index, \
}
#define NRF_SAADC_HAS_CONVTIME
Symbol indicating whether the configuration of conversion time is present.
Definition nrf_saadc.h:78
#define NRF_SAADC_HAS_CH_GAIN
Symbol indicating whether SAADC channel specific gain configuration is present.
Definition nrf_saadc.h:186
#define NRF_SAADC_HAS_CH_HIGHSPEED
Symbol indicating whether SAADC channel specific highspeed mode configuration is present.
Definition nrf_saadc.h:193
#define NRF_SAADC_HAS_CH_CHOPPING
Symbol indicating whether SAADC channel specific chopping mode configuration is present.
Definition nrf_saadc.h:158
#define NRF_SAADC_HAS_CH_CONFIG_RES
Symbol indicating whether SAADC channel resistor control is present.
Definition nrf_saadc.h:113
#define NRF_SAADC_HAS_CH_BURST
Symbol indicating whether SAADC channel specific burst mode configuration is present.
Definition nrf_saadc.h:144
@ NRF_SAADC_RESISTOR_DISABLED
Bypass resistor ladder.
Definition nrf_saadc.h:490
@ NRF_SAADC_MODE_DIFFERENTIAL
Differential mode.
Definition nrf_saadc.h:580
@ NRF_SAADC_CHOPPING_DISABLED
Chopping mode is disabled.
Definition nrf_saadc.h:599
@ NRF_SAADC_HIGHSPEED_DISABLED
Highspeed mode is disabled.
Definition nrf_saadc.h:608
@ NRF_SAADC_REFERENCE_INTERNAL
Internal reference.
Definition nrf_saadc.h:548
@ NRF_SAADC_GAIN1
Gain factor 1.
Definition nrf_saadc.h:536
nrfx_analog_input_t
Generic analog input types.
Definition nrfx_analog_common.h:64
#define NRFX_SAADC_DEFAULT_CONV_TIME
Auxiliary symbol specifying default value for the SAADC conversion time.
Definition nrfx_saadc.h:61
#define NRFX_SAADC_DEFAULT_ACQTIME
Auxiliary symbol specifying default value for the SAADC acquisition time.
Definition nrfx_saadc.h:54

SAADC channel default configuration for the differential mode.

This configuration sets up differential SAADC channel with the following options:

  • resistor ladder disabled
  • gain: 1/6
  • reference voltage: internal
  • sample acquisition time: 10 us
  • burst disabled
Parameters
[in]_pin_pPositive input analog pin.
[in]_pin_nNegative input analog pin.
[in]_indexChannel index.
See also
nrfx_saadc_channel_t

◆ NRFX_SAADC_DEFAULT_CHANNEL_SE

#define NRFX_SAADC_DEFAULT_CHANNEL_SE ( _pin_p,
_index )
Value:
{ \
.channel_config = \
{ \
NRFX_COND_CODE_1(NRF_SAADC_HAS_CH_CONFIG_RES, \
(.resistor_p = NRF_SAADC_RESISTOR_DISABLED, \
.resistor_n = NRF_SAADC_RESISTOR_DISABLED,), \
()) \
NRFX_COND_CODE_1(NRF_SAADC_HAS_CH_GAIN, \
(.gain = NRF_SAADC_GAIN1,), \
()) \
NRFX_COND_CODE_1(NRF_SAADC_HAS_CH_BURST, \
(.burst = NRF_SAADC_BURST_DISABLED,), \
()) \
NRFX_COND_CODE_1(NRF_SAADC_HAS_CH_CHOPPING, \
(.chopping = NRF_SAADC_CHOPPING_DISABLED,), \
()) \
NRFX_COND_CODE_1(NRF_SAADC_HAS_CH_HIGHSPEED, \
(.highspeed = NRF_SAADC_HIGHSPEED_DISABLED,), \
()) \
NRFX_COND_CODE_1(NRF_SAADC_HAS_CONVTIME, \
(.conv_time = NRFX_SAADC_DEFAULT_CONV_TIME,), \
()) \
}, \
.pin_p = (nrfx_analog_input_t)_pin_p, \
.channel_index = _index, \
}
@ NRF_SAADC_MODE_SINGLE_ENDED
Single-ended mode. PSELN will be ignored, negative input to ADC shorted to GND.
Definition nrf_saadc.h:579
@ NRFX_ANALOG_INPUT_DISABLED
Analog input disabled.
Definition nrfx_analog_common.h:93

SAADC channel default configuration for the single-ended mode.

This configuration sets up single-ended SAADC channel with the following options:

  • resistor ladder disabled
  • gain: 1
  • reference voltage: internal
  • sample acquisition time: 10 us
  • burst disabled
Parameters
[in]_pin_pPositive input analog pin.
[in]_indexChannel index.
See also
nrfx_saadc_channel_t

◆ NRFX_SAADC_SAMPLE_GET

#define NRFX_SAADC_SAMPLE_GET ( _samples,
_index )
Value:
(((int16_t *)(_samples))[(_index)])

Macro for getting specified SAADC sample from the filled buffer.

Parameters
[in]_samplesPointer to the buffer filled with SAADC samples.
[in]_indexSample index.
Returns
Specified sample.

◆ NRFX_SAADC_SAMPLES_TO_BYTES

#define NRFX_SAADC_SAMPLES_TO_BYTES ( _samples)
Value:
(_samples * 2)

Macro for getting number of bytes needed to store specified number of SAADC samples.

Parameters
[in]_samplesNumber of samples.
Returns
Number of bytes needed to store specified number of samples.

Typedef Documentation

◆ nrfx_saadc_event_handler_t

typedef void(* nrfx_saadc_event_handler_t) (nrfx_saadc_evt_t const *p_event)

SAADC driver event handler.

When operating in the advanced mode:

Parameters
[in]p_eventPointer to an SAADC driver event. The event structure is allocated on the stack, so it is valid only within the context of the event handler.

Enumeration Type Documentation

◆ nrfx_saadc_evt_type_t

SAADC driver event types.

Enumerator
NRFX_SAADC_EVT_DONE 

Event generated when the buffer is filled with samples.

NRFX_SAADC_EVT_LIMIT 

Event generated when one of the limits is reached.

NRFX_SAADC_EVT_CALIBRATEDONE 

Event generated when the calibration is complete.

NRFX_SAADC_EVT_BUF_REQ 

Event generated when the next buffer for continuous conversion is requested.

NRFX_SAADC_EVT_READY 

Event generated when the first buffer is acquired by the peripheral and sampling can be started.

NRFX_SAADC_EVT_FINISHED 

Event generated when all supplied buffers are filled with results.

Function Documentation

◆ nrfx_saadc_abort()

void nrfx_saadc_abort ( void )

Function for aborting the ongoing and buffered conversions.

Warning
Aborting blocking conversion or calibration from different context is not supported. Perform the operation in non-blocking manner instead.
Note
NRFX_SAADC_EVT_DONE event will be generated if there is a conversion in progress. Event will contain number of words in the sample buffer.

◆ nrfx_saadc_advanced_mode_set()

int nrfx_saadc_advanced_mode_set ( uint32_t channel_mask,
nrf_saadc_resolution_t resolution,
nrfx_saadc_adv_config_t const * p_config,
nrfx_saadc_event_handler_t event_handler )

Function for setting the SAADC driver in the advanced mode.

The advanced mode allows performing double-buffered conversions of arbitrary length. The conversions can be done in a blocking or non-blocking manner. When performing conversions in the non-blocking manner and nrfx_saadc_adv_config_t::internal_timer_cc is set to 0, sampling needs to be done by triggering NRF_SAADC_TASK_SAMPLE externally (for example by using the TIMER and/or the PPI/DPPI). When performing conversions in the non-blocking manner and nrfx_saadc_adv_config_t::start_on_end is false, the NRF_SAADC_TASK_START needs to be triggered on NRF_SAADC_EVENT_END externally (for example by using the PPI/DPPI). If nrfx_saadc_adv_config_t::start_on_end is true, the NRF_SAADC_TASK_START is triggered by the driver in the interrupt context or by the hardware using dedicated shorts, if available. Sampling is initiated by calling nrfx_saadc_mode_trigger(). In case of performing conversions in the blocking manner, nrfx_saadc_mode_trigger() may need to be called several times as each call sample each requested channel once.

Note
Unless NRF_SAADC_HAS_INTERNAL_TIMER_SCAN is set, the internal timer can only be used when a single input channel is enabled. When the symbol is set, the internal timer can also be used with multiple channels enabled (scan mode).
The internal timer can only be used in the non-blocking mode.
Parameters
[in]channel_maskBitmask of channels to be used in the advanced mode.
[in]resolutionResolution configuration.
[in]p_configPointer to the structure with the advanced mode configuration.
[in]event_handlerEvent handler provided by the user. In case of providing NULL, the conversion will be performed in the blocking manner.
Return values
0Initialization was successful.
-EBUSYThere is a conversion or calibration ongoing.
-EINVALAttempt to activate channel that is not configured.
-ENOTSUPAttempt to activate either of the following:
  • internal timer in the blocking mode,
  • internal timer with multiple channels enabled when NRF_SAADC_HAS_INTERNAL_TIMER_SCAN is not set,
  • oversampling without burst with multiple channels enabled.

◆ nrfx_saadc_buffer_set()

int nrfx_saadc_buffer_set ( nrf_saadc_value_t * p_buffer,
uint16_t size )

Function for supplying the buffer to be used in the next part of the conversion.

Parameters
[in]p_bufferPointer to the buffer to be filled with conversion results.
[in]sizeNumber of samples in the buffer.
Return values
0Buffer was supplied successfully.
-EACCESThe provided buffer is not in the Data RAM region.
-E2BIGThe provided buffer is not aligned to the number of activated channels or is too long for the EasyDMA to handle.
-EINPROGRESSThe driver is in the idle mode.
-EALREADYBoth buffers for double-buffered conversions are already set.

◆ nrfx_saadc_channel_config()

int nrfx_saadc_channel_config ( nrfx_saadc_channel_t const * p_channel)

Function for configuring single SAADC channel.

Note
The values of the nrf_saadc_channel_config_t::burst fields in channel configurations are ignored. They will be overridden with the value suitable for the selected driver operation mode.
Warning
This function overrides previous configuration done on specified channel by nrfx_saadc_channels_config or nrfx_saadc_channel_config.
Parameters
[in]p_channelPointer to the channel configuration structure.
Return values
0Configuration was successful.
-EBUSYThere is a conversion or calibration ongoing.
-EINVALAttempt to configure an invalid analog pin.

◆ nrfx_saadc_channels_config()

int nrfx_saadc_channels_config ( nrfx_saadc_channel_t const * p_channels,
uint32_t channel_count )

Function for configuring multiple SAADC channels.

Note
The values of the nrf_saadc_channel_config_t::burst fields in channel configurations are ignored. They will be overridden with the value suitable for the selected driver operation mode.
The desired mode (simple or advanced) must be set after the channels are configured.
Warning
This function overrides previous configuration done on any channel by nrfx_saadc_channels_config or nrfx_saadc_channel_config.
Parameters
[in]p_channelsPointer to the array of channel configuration structures.
[in]channel_countNumber of channels to be configured.
Return values
0Configuration was successful.
-EBUSYThere is a conversion or calibration ongoing.
-EINVALAttempt to configure the same channel more than once or attempt to configure an invalid analog pin.

◆ nrfx_saadc_channels_configured_get()

uint32_t nrfx_saadc_channels_configured_get ( void )

Function for getting the currently configured SAADC channels.

Returns
Bitmask of configured channels.

◆ nrfx_saadc_channels_deconfig()

int nrfx_saadc_channels_deconfig ( uint32_t channel_mask)

Function for deconfiguring the specified SAADC channels.

Warning
Pins associated with the deconfigured channels will be released after next nrfx_saadc_simple_mode_set() or nrfx_saadc_advanced_mode_set() call.
Parameters
[in]channel_maskBitmask of channels to be deconfigured.
Return values
0Deconfiguration was successful.
-EBUSYThere is a conversion or calibration ongoing.

◆ nrfx_saadc_init()

int nrfx_saadc_init ( uint8_t interrupt_priority)

Function for initializing the SAADC driver.

Parameters
[in]interrupt_priorityInterrupt priority.
Return values
0Initialization was successful.
-EALREADYThe driver is already initialized.

◆ nrfx_saadc_init_check()

bool nrfx_saadc_init_check ( void )

Function for checking if the SAADC driver is initialized.

Return values
trueDriver is already initialized.
falseDriver is not initialized.

◆ nrfx_saadc_interval_to_cc()

uint16_t nrfx_saadc_interval_to_cc ( uint16_t interval_us)

Function for getting the internal timer CC value from the interval in microseconds.

Note
The internal timer runs at 16 MHz, so to convert the interval in microseconds to the internal timer CC value, we can use the formula: interval_cc = interval_us * 16 MHz where 16 MHz is the frequency of the internal timer.
The maximum value for interval_cc is 2047, which corresponds to approximately 7816 Hz ~ 128us. The minimum value for interval_cc depends on the SoC.
Parameters
[in]interval_usThe interval in microseconds to be converted.
Returns
The internal timer CC value.

◆ nrfx_saadc_limits_set()

int nrfx_saadc_limits_set ( uint8_t channel,
int16_t limit_low,
int16_t limit_high )

Function for setting the SAADC channel limits.

When limits are enabled and the conversion result exceeds the defined bounds, the handler function is called with the corresponding event as parameter.

Note
Before the limits are set, the driver operation mode (simple or advanced) has to be configured. Only non-blocking conversions can be monitored.
Changing of the driver operation mode disables all configured limits.
Parameters
[in]channelChannel index.
[in]limit_lowLimit low value to generate interrupt. Use INT16_MIN to disable interrupt generation.
[in]limit_highLimit high value to generate interrupt. Use INT16_MAX to disable interrupt generation.
Return values
0Requested channel limits were set.
-EINVALAttempt to activate the limits on disabled channel.
-EPERMAttempt to activate the limits for blocking conversions.
-EINPROGRESSAttempt to activate the limits without configured mode.

◆ nrfx_saadc_mode_trigger()

int nrfx_saadc_mode_trigger ( void )

Function for triggering the conversion in the configured mode.

Return values
0Operation finished successfully in the blocking manner or started successfully in the non-blocking manner.
-EBUSYThe driver is performing the conversion in the advanced blocking mode. Call the function again to continue the conversion.
-ENOMEMThere is no buffer provided. Supply the buffer using nrfx_saadc_buffer_set() and try again.
-EINPROGRESSThere is an ongoing conversion or calibration being performed in the non-blocking manner or the driver is in the idle mode.

◆ nrfx_saadc_offset_calibrate()

int nrfx_saadc_offset_calibrate ( nrfx_saadc_event_handler_t calib_event_handler)

Function for starting the SAADC offset calibration.

Parameters
[in]calib_event_handlerCalibration event handler provided by the user. In case of providing NULL, the calibration will be performed in the blocking manner.
Return values
0Calibration finished successfully in the blocking manner or started successfully in the non-blocking manner.
-EBUSYThere is a conversion or calibration ongoing.

◆ nrfx_saadc_simple_mode_set()

int nrfx_saadc_simple_mode_set ( uint32_t channel_mask,
nrf_saadc_resolution_t resolution,
nrf_saadc_oversample_t oversampling,
nrfx_saadc_event_handler_t event_handler )

Function for setting the SAADC driver in the simple mode.

The simple mode allows obtaining a single sample from each requested channel. The conversion can be done in a blocking or non-blocking manner. Sampling is initiated by calling nrfx_saadc_mode_trigger() once.

Parameters
[in]channel_maskBitmask of channels to be used in the simple mode.
[in]resolutionResolution configuration.
[in]oversamplingOversampling configuration.
[in]event_handlerEvent handler provided by the user. In case of providing NULL, the conversion will be performed in the blocking manner.
Return values
0Initialization was successful.
-EBUSYThere is a conversion or calibration ongoing.
-EINVALAttempt to activate channel that is not configured.

◆ nrfx_saadc_uninit()

void nrfx_saadc_uninit ( void )

Function for uninitializing the SAADC driver.

This function stops all ongoing conversions and disables all channels.