| 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
COMP driver
COMP HAL and driver

nRF52 only: Comparator (COMP) driver. More...

## Modules
| COMP peripheral driver configuration

## Data Structures
struct | nrf_drv_comp_config_t
| COMP configuration. More...

## Macros
#define | VOLTAGE_THRESHOLD_TO_INT(vol, ref) (uint8_t)(((vol) > ((ref) / 64)) ? (ROUNDED_DIV((vol) * 64,(ref)) - 1) : 0)
| Macro to convert the threshold voltage to an integer value (needed by the COMP_TH register). More...
#define | COMP_CONFIG_TH
| COMP threshold default configuration. More...
#define | NRF_DRV_COMP_CONF_DEFAULT_CONFIG(INPUT)
| COMP driver default configuration including the COMP HAL configuration. More...

## Typedefs
typedef void(* | comp_events_handler_t )(nrf_comp_event_t event)
| COMP event handler function type. More...

## Enumerations
enum | nrf_drv_comp_short_mask_t { NRF_DRV_COMP_SHORT_STOP_AFTER_CROSS_EVT = COMP_SHORTS_CROSS_STOP_Msk, NRF_DRV_COMP_SHORT_STOP_AFTER_UP_EVT = COMP_SHORTS_UP_STOP_Msk, NRF_DRV_COMP_SHORT_STOP_AFTER_DOWN_EVT = COMP_SHORTS_DOWN_STOP_Msk }
| COMP shortcut masks. More...
enum | nrf_drv_comp_evt_en_mask_t { NRF_DRV_COMP_EVT_EN_CROSS_MASK = COMP_INTENSET_CROSS_Msk, NRF_DRV_COMP_EVT_EN_UP_MASK = COMP_INTENSET_UP_Msk, NRF_DRV_COMP_EVT_EN_DOWN_MASK = COMP_INTENSET_DOWN_Msk, NRF_DRV_COMP_EVT_EN_READY_MASK = COMP_INTENSET_READY_Msk }
| COMP events masks. More...

## Functions
ret_code_t | nrf_drv_comp_init (const nrf_drv_comp_config_t *p_config, comp_events_handler_t event_handler)
| Function for initializing the COMP driver. More...
void | nrf_drv_comp_uninit (void)
| Function for uninitializing the COMP driver. More...
void | nrf_drv_comp_pin_select (nrf_comp_input_t psel)
| Function for setting the analog input. More...
void | nrf_drv_comp_start (uint32_t comp_evt_en_mask, uint32_t comp_shorts_mask)
| Function for starting the COMP peripheral and interrupts. More...
void | nrf_drv_comp_stop (void)
| Function for stopping the COMP peripheral. More...
uint32_t | nrf_drv_comp_sample (void)
| Function for copying the current state of the comparator result to the RESULT register. More...
__STATIC_INLINE uint32_t | nrf_drv_comp_task_address_get (nrf_comp_task_t comp_task)
| Function for getting the task address. More...
__STATIC_INLINE uint32_t | nrf_drv_comp_event_address_get (nrf_comp_event_t comp_event)
| Function for getting the event address. More...
__STATIC_INLINE nrf_comp_input_t | nrf_drv_comp_gpio_to_ain (uint8_t pin)
| Function for converting a GPIO pin number to an analog COMP channel. More...
__STATIC_INLINE uint8_t | nrf_drv_comp_ain_to_gpio (nrf_comp_input_t ain)
| Function for converting a COMP channel to a GPIO pin number. More...

## Detailed Description

nRF52 only: Comparator (COMP) driver.

## Macro Definition Documentation

#define COMP_CONFIG_TH

Value:
{ \
.th_down = VOLTAGE_THRESHOLD_TO_INT(0.5, 1.8), \
.th_up = VOLTAGE_THRESHOLD_TO_INT(1.5, 1.8) \
}

COMP threshold default configuration.

#define NRF_DRV_COMP_CONF_DEFAULT_CONFIG | ( |  | INPUT | ) |

Value:
{ \
.reference = (nrf_comp_ref_t)COMP_CONFIG_REF, \
.main_mode = (nrf_comp_main_mode_t)COMP_CONFIG_MAIN_MODE, \
.threshold = COMP_CONFIG_TH, \
.speed_mode = (nrf_comp_sp_mode_t)COMP_CONFIG_SPEED_MODE, \
.hyst = (nrf_comp_hyst_t)COMP_CONFIG_HYST, \
.isource = (nrf_isource_t)COMP_CONFIG_ISOURCE, \
.input = (nrf_comp_input_t)INPUT, \
.interrupt_priority = COMP_CONFIG_IRQ_PRIORITY \
}

COMP driver default configuration including the COMP HAL configuration.

#define VOLTAGE_THRESHOLD_TO_INT | ( |  | vol,
|  |  | ref
| ) |  | (uint8_t)(((vol) > ((ref) / 64)) ? (ROUNDED_DIV((vol) * 64,(ref)) - 1) : 0)

Macro to convert the threshold voltage to an integer value (needed by the COMP_TH register).

Parameters

  [in] | vol | Voltage to be changed to COMP_TH register value. This value must not be smaller than reference voltage divided by 64.
  [in] | ref | Reference voltage.

## Typedef Documentation

typedef void(* comp_events_handler_t)(nrf_comp_event_t event)

COMP event handler function type.

Parameters

  [in] | event | COMP event.

## Enumeration Type Documentation

enum nrf_drv_comp_evt_en_mask_t

COMP events masks.

Enumerator
NRF_DRV_COMP_EVT_EN_CROSS_MASK | CROSS event (generated after VIN+ == VIN-).
NRF_DRV_COMP_EVT_EN_UP_MASK | UP event (generated when VIN+ crosses VIN- while increasing).
NRF_DRV_COMP_EVT_EN_DOWN_MASK | DOWN event (generated when VIN+ crosses VIN- while decreasing).
NRF_DRV_COMP_EVT_EN_READY_MASK | READY event (generated when the module is ready).

enum nrf_drv_comp_short_mask_t

COMP shortcut masks.

Enumerator
NRF_DRV_COMP_SHORT_STOP_AFTER_CROSS_EVT | Shortcut between the CROSS event and the STOP task.
NRF_DRV_COMP_SHORT_STOP_AFTER_UP_EVT | Shortcut between the UP event and the STOP task.
NRF_DRV_COMP_SHORT_STOP_AFTER_DOWN_EVT | Shortcut between the DOWN event and the STOP task.

## Function Documentation

__STATIC_INLINE uint8_t nrf_drv_comp_ain_to_gpio | ( | nrf_comp_input_t | ain | ) |

Function for converting a COMP channel to a GPIO pin number.

Parameters

  [in] | ain | COMP channel.

Returns
  GPIO pin number. The function returns UINT8_MAX if the specified channel is not a GPIO pin.

__STATIC_INLINE uint32_t nrf_drv_comp_event_address_get | ( | nrf_comp_event_t | comp_event | ) |

Function for getting the event address.

Parameters

  [in] | comp_event | COMP event.

Returns
  Address of the given COMP event.

__STATIC_INLINE nrf_comp_input_t nrf_drv_comp_gpio_to_ain | ( | uint8_t | pin | ) |

Function for converting a GPIO pin number to an analog COMP channel.

Parameters

  [in] | pin | GPIO pin number.

Returns
  COMP channel. The function returns UINT8_MAX if the specified pin is not an analog input.

ret_code_t nrf_drv_comp_init | ( | const nrf_drv_comp_config_t * | p_config,
|  | comp_events_handler_t | event_handler
| ) |  |

Function for initializing the COMP driver.

This function initializes the COMP driver, but does not enable the peripheral or any interrupts. To start the driver, call the function nrf_drv_comp_start() after initialization.

If no configuration structure is provided, the driver is initialized with the default settings.

Parameters

  [in] | p_config | Initial configuration. If NULL, the default configuration is used.
  [in] | event_handler | Handler function.

Return values

  NRF_ERROR_INVALID_PARAM | If the configuration is invalid.
  NRF_ERROR_INVALID_STATE | If the driver has already been initialized.
  NRF_ERROR_BUSY | If the LPCOMP driver is initialized.

void nrf_drv_comp_pin_select | ( | nrf_comp_input_t | psel | ) |

Function for setting the analog input.

Parameters

  [in] | psel | COMP analog pin selection.

uint32_t nrf_drv_comp_sample | ( | void |  | ) |

Function for copying the current state of the comparator result to the RESULT register.

Return values

  0 | If the input voltage is below the threshold (VIN+ < VIN-).
  1 | If the input voltage is above the threshold (VIN+ > VIN-).

void nrf_drv_comp_start | ( | uint32_t | comp_evt_en_mask,
|  | uint32_t | comp_shorts_mask
| ) |  |

Function for starting the COMP peripheral and interrupts.

Before calling this function, the driver must be initialized. This function enables the COMP peripheral and its interrupts.

Parameters

  [in] | comp_evt_en_mask | Mask of events to be enabled. This parameter should be built as 'or' of elements from nrf_drv_comp_evt_en_mask_t.
  [in] | comp_shorts_mask | Mask of shorts to be enabled. This parameter should be built as 'or' of elements from nrf_drv_comp_short_mask_t.

See Also
  nrf_drv_comp_init()

void nrf_drv_comp_stop | ( | void |  | ) |

Function for stopping the COMP peripheral.

Before calling this function, the driver must be enabled. This function disables the COMP peripheral and its interrupts.

See Also
  nrf_drv_comp_uninit()

__STATIC_INLINE uint32_t nrf_drv_comp_task_address_get | ( | nrf_comp_task_t | comp_task | ) |

Function for getting the task address.

Before calling this function, the driver must be enabled.

Parameters

  [in] | comp_task | COMP task.

Returns
  Address of the given COMP task.

void nrf_drv_comp_uninit | ( | void |  | ) |

Function for uninitializing the COMP driver.

This function uninitializes the COMP driver. The COMP peripheral and its interrupts are disabled, and local variables are cleaned. After this call, you must initialize the driver again by calling nrf_drv_comp_init() if you want to use it.

See Also
  nrf_drv_comp_stop()