| nRF5 SDK v12.0.0 |

Choose documentation: | nRF5 SDK | S130 SoftDevice API | S132 SoftDevice API | S212 SoftDevice API | S332 SoftDevice API

All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
Modules | Data Structures | Macros | Typedefs | Enumerations | Functions
SAADC driver
SAADC HAL and driver

nRF52 only: Successive Approximation Analog-to-Digital Converter (SAADC) driver. More...

## Modules
| SAADC peripheral driver configuration

## Data Structures
struct | nrf_drv_saadc_config_t
| Analog-to-digital converter driver configuration structure. More...
struct | nrf_drv_saadc_done_evt_t
| Analog-to-digital converter driver done event data. More...
struct | nrf_drv_saadc_limit_evt_t
| Analog-to-digital converter driver limit event data. More...
struct | nrf_drv_saadc_evt_t
| Analog-to-digital converter driver event structure. More...

## Macros
#define | NRF_DRV_SAADC_LIMITH_DISABLED (2047)
| Value that should be set as high limit to disable limit detection.
#define | NRF_DRV_SAADC_LIMITL_DISABLED (-2048)
| Value that should be set as low limit to disable limit detection.
#define | NRF_DRV_SAADC_DEFAULT_CONFIG
| Macro for setting nrf_drv_saadc_config_t to default settings. More...
#define | NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(PIN_P)
| Macro for setting nrf_saadc_channel_config_t to default settings in single ended mode. More...
#define | NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_DIFFERENTIAL(PIN_P, PIN_N)
| Macro for setting nrf_saadc_channel_config_t to default settings in differential mode. More...

## Typedefs
typedef void(* | nrf_drv_saadc_event_handler_t )(nrf_drv_saadc_evt_t const *p_event)
| ADC event handler. More...

## Enumerations
enum | nrf_drv_saadc_evt_type_t { NRF_DRV_SAADC_EVT_DONE, NRF_DRV_SAADC_EVT_LIMIT, NRF_DRV_SAADC_EVT_CALIBRATEDONE }
| Driver event types. More...

## Functions
ret_code_t | nrf_drv_saadc_init (nrf_drv_saadc_config_t const *p_config, nrf_drv_saadc_event_handler_t event_handler)
| Function for initializing the SAADC. More...
void | nrf_drv_saadc_uninit (void)
| Function for uninitializing the SAADC. More...
uint32_t | nrf_drv_saadc_sample_task_get (void)
| Function for getting the address of a SAMPLE SAADC task. More...
ret_code_t | nrf_drv_saadc_channel_init (uint8_t channel, nrf_saadc_channel_config_t const *const p_config)
| Function for initializing an SAADC channel. More...
ret_code_t | nrf_drv_saadc_channel_uninit (uint8_t channel)
| Function for uninitializing an SAADC channel. More...
ret_code_t | nrf_drv_saadc_sample (void)
| Function for starting SAADC sampling. More...
ret_code_t | nrf_drv_saadc_sample_convert (uint8_t channel, nrf_saadc_value_t *p_value)
| Blocking function for executing a single ADC conversion. More...
ret_code_t | nrf_drv_saadc_buffer_convert (nrf_saadc_value_t *buffer, uint16_t size)
| Function for issuing conversion of data to the buffer. More...
ret_code_t | nrf_drv_saadc_calibrate_offset (void)
| Function for triggering the ADC offset calibration. More...
bool | nrf_drv_saadc_is_busy (void)
| Function for retrieving the SAADC state. More...
void | nrf_drv_saadc_abort (void)
| Function for aborting ongoing and buffered conversions. More...
void | nrf_drv_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 result exceeds the defined bounds, the limit handler function is called. More...
__STATIC_INLINE nrf_saadc_input_t | nrf_drv_saadc_gpio_to_ain (uint32_t pin)
| Function for converting a GPIO pin number to an analog input pin number used in the channel configuration. More...

## Detailed Description

nRF52 only: Successive Approximation Analog-to-Digital Converter (SAADC) driver.

## Macro Definition Documentation

#define NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_DIFFERENTIAL | ( |  | PIN_P,
|  |  | PIN_N
| ) |  |

Value:
{ \
.resistor_p = NRF_SAADC_RESISTOR_DISABLED, \
.resistor_n = NRF_SAADC_RESISTOR_DISABLED, \
.gain = NRF_SAADC_GAIN1_6, \
.reference = NRF_SAADC_REFERENCE_INTERNAL, \
.acq_time = NRF_SAADC_ACQTIME_10US, \
.mode = NRF_SAADC_MODE_DIFFERENTIAL, \
.pin_p = (nrf_saadc_input_t)(PIN_P), \
.pin_n = (nrf_saadc_input_t)(PIN_N) \
}

Macro for setting nrf_saadc_channel_config_t to default settings in differential mode.

Parameters

  PIN_P | Positive analog input.
  PIN_N | Negative analog input.

#define NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE | ( |  | PIN_P | ) |

Value:
{ \
.resistor_p = NRF_SAADC_RESISTOR_DISABLED, \
.resistor_n = NRF_SAADC_RESISTOR_DISABLED, \
.gain = NRF_SAADC_GAIN1_6, \
.reference = NRF_SAADC_REFERENCE_INTERNAL, \
.acq_time = NRF_SAADC_ACQTIME_10US, \
.mode = NRF_SAADC_MODE_SINGLE_ENDED, \
.burst = NRF_SAADC_BURST_DISABLED, \
.pin_p = (nrf_saadc_input_t)(PIN_P), \
.pin_n = NRF_SAADC_INPUT_DISABLED \
}

Macro for setting nrf_saadc_channel_config_t to default settings in single ended mode.

Parameters

  PIN_P | Analog input.

#define NRF_DRV_SAADC_DEFAULT_CONFIG

Value:
{ \
.resolution = (nrf_saadc_resolution_t)SAADC_CONFIG_RESOLUTION, \
.oversample = (nrf_saadc_oversample_t)SAADC_CONFIG_OVERSAMPLE, \
.interrupt_priority = SAADC_CONFIG_IRQ_PRIORITY, \
.low_power_mode = SAADC_CONFIG_LP_MODE \
}

Macro for setting nrf_drv_saadc_config_t to default settings.

## Typedef Documentation

typedef void(* nrf_drv_saadc_event_handler_t)(nrf_drv_saadc_evt_t const *p_event)

ADC event handler.

Parameters

  [in] | p_event | Pointer to an ADC event. The event structure is allocated on the stack, so it is valid only within the context of the event handler.

## Enumeration Type Documentation

enum nrf_drv_saadc_evt_type_t

Driver event types.

Enumerator
NRF_DRV_SAADC_EVT_DONE | Event generated when the buffer is filled with samples.
NRF_DRV_SAADC_EVT_LIMIT | Event generated after one of the limits is reached.
NRF_DRV_SAADC_EVT_CALIBRATEDONE | Event generated when the calibration is complete.

## Function Documentation

void nrf_drv_saadc_abort | ( | void |  | ) |

Function for aborting ongoing and buffered conversions.

Note
  NRF_DRV_SAADC_EVT_DONE event will be generated if there is a conversion in progress. Event will contain number of words in the sample buffer.

ret_code_t nrf_drv_saadc_buffer_convert | ( | nrf_saadc_value_t * | buffer,
|  | uint16_t | size
| ) |  |

Function for issuing conversion of data to the buffer.

This function is non-blocking. The application is notified about filling the buffer by the event handler. Conversion will be done on all enabled channels. If the ADC is in idle state, the function will set up Easy DMA for the conversion. The ADC will be ready for sampling and wait for the SAMPLE task. It can be triggered manually by the nrf_drv_saadc_sample function or by PPI using the NRF_SAADC_TASK_SAMPLE task. If one buffer is already set and the conversion is ongoing, calling this function will result in queuing the given buffer. The driver will start filling the issued buffer when the first one is completed. If the function is called again before the first buffer is filled or calibration is in progress, it will return with error.

Parameters

  [in] | buffer | Result buffer.
  [in] | size | Buffer size in words.

Return values

  NRF_SUCCESS | If conversion was successful.
  NRF_ERROR_BUSY | If the driver already has two buffers set or calibration is in progress.

ret_code_t nrf_drv_saadc_calibrate_offset | ( | void |  | ) |

Function for triggering the ADC offset calibration.

This function is non-blocking. The application is notified about completion by the event handler. Calibration will also trigger DONE and RESULTDONE events.

The function will fail if ADC is busy or calibration is already in progress.

Return values

  NRF_SUCCESS | If calibration was started successfully.
  NRF_ERROR_BUSY | If the ADC driver is busy.

ret_code_t nrf_drv_saadc_channel_init | ( | uint8_t | channel,
|  | nrf_saadc_channel_config_t const *const | p_config
| ) |  |

Function for initializing an SAADC channel.

This function configures and enables the channel.

Return values

  NRF_SUCCESS | If initialization was successful.
  NRF_ERROR_INVALID_STATE | If the ADC was not initialized.
  NRF_ERROR_NO_MEM | If the specified channel was already allocated.

ret_code_t nrf_drv_saadc_channel_uninit | ( | uint8_t | channel | ) |

Function for uninitializing an SAADC channel.

Return values

  NRF_SUCCESS | If uninitialization was successful.
  NRF_ERROR_BUSY | If the ADC is busy.

__STATIC_INLINE nrf_saadc_input_t nrf_drv_saadc_gpio_to_ain | ( | uint32_t | pin | ) |

Function for converting a GPIO pin number to an analog input pin number used in the channel configuration.

Parameters

  [in] | pin | GPIO pin.

Returns
  Value representing an analog input pin. The function returns NRF_SAADC_INPUT_DISABLED if the specified pin is not an analog input.

ret_code_t nrf_drv_saadc_init | ( | nrf_drv_saadc_config_t const * | p_config,
|  | nrf_drv_saadc_event_handler_t | event_handler
| ) |  |

Function for initializing the SAADC.

Parameters

  [in] | p_config | Pointer to a configuration structure. If NULL, the default one is used.
  [in] | event_handler | Event handler provided by the user.

Return values

  NRF_SUCCESS | If initialization was successful.
  NRF_ERROR_INVALID_STATE | If the driver is already initialized.
  NRF_ERROR_INVALID_PARAM | If event_handler is NULL.

bool nrf_drv_saadc_is_busy | ( | void |  | ) |

Function for retrieving the SAADC state.

Return values

  true | If the ADC is busy.
  false | If the ADC is ready.

void nrf_drv_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 result exceeds the defined bounds, the limit handler function is called.

Parameters

  [in] | channel | SAADC channel number.
  [in] | limit_low | Lower limit (valid values from NRF_DRV_SAADC_LIMITL_DISABLED to NRF_DRV_SAADC_LIMITH_DISABLED). Conversion results below this value will trigger the handler function. Set to NRF_DRV_SAADC_LIMITL_DISABLED to disable this limit.
  [in] | limit_high | Upper limit (valid values from NRF_DRV_SAADC_LIMITL_DISABLED to NRF_DRV_SAADC_LIMITH_DISABLED). Conversion results above this value will trigger the handler function. Set to NRF_DRV_SAADC_LIMITH_DISABLED to disable this limit.

ret_code_t nrf_drv_saadc_sample | ( | void |  | ) |

Function for starting SAADC sampling.

Return values

  NRF_SUCCESS | If ADC sampling was triggered.
  NRF_ERROR_INVALID_STATE | If ADC is in idle state.

ret_code_t nrf_drv_saadc_sample_convert | ( | uint8_t | channel,
|  | nrf_saadc_value_t * | p_value
| ) |  |

Blocking function for executing a single ADC conversion.

This function selects the desired input, starts a single conversion, waits for it to finish, and returns the result.

The function will fail if ADC is busy.

Parameters

  [in] | channel | Channel.
  [out] | p_value | Pointer to the location where the result should be placed.

Return values

  NRF_SUCCESS | If conversion was successful.
  NRF_ERROR_BUSY | If the ADC driver is busy.

uint32_t nrf_drv_saadc_sample_task_get | ( | void |  | ) |

Function for getting the address of a SAMPLE SAADC task.

Returns
  Task address.

void nrf_drv_saadc_uninit | ( | void |  | ) |

Function for uninitializing the SAADC.

This function stops all ongoing conversions and disables all channels.