High level interface for interacting with an analog Comparator.
Features
The analog comparator measures one input voltage from the non-inverting pin against a second voltage provided on the inverting pin. The result of this comparison can be used in two ways:
- Output to pin or other on-chip destination (configured via configurator)
- Read state via firmware (see mtb_hal_comp_read)
These abilities can be used in any combination.
Quickstart
Call mtb_hal_comp_setup to initialize the HAL for a peripheral instance. The peripheral must] have already been initialized and configured using the PDL.
Use mtb_hal_comp_read to read the comparator state from firmware.
Code Snippets:
- Note
- Error checking is omitted for clarity
Snippet 1: Comparator read value
The following snippet reads the current comparator value into a variable
bool mtb_hal_comp_read(mtb_hal_comp_t *obj)
Reads the Comparator state.
cy_rslt_t mtb_hal_comp_setup(mtb_hal_comp_t *obj, const mtb_hal_comp_configurator_t *config)
Sets up a HAL instance to use the specified hardware resource.
Comparator object.
Definition: mtb_hal_hw_types_comp.h:84
uint32_t cy_rslt_t
Provides the result of an operation as a structured bitfield.
Definition: cy_result.h:481
|
|
typedef void(* | mtb_hal_comp_event_callback_t) (void *callback_arg, mtb_hal_comp_event_t event) |
| | Handler for Comparator events.
|
| |
|
| cy_rslt_t | mtb_hal_comp_setup (mtb_hal_comp_t *obj, const mtb_hal_comp_configurator_t *config) |
| | Sets up a HAL instance to use the specified hardware resource. More...
|
| |
| bool | mtb_hal_comp_read (mtb_hal_comp_t *obj) |
| | Reads the Comparator state. More...
|
| |
| cy_rslt_t | mtb_hal_comp_set_ref (mtb_hal_comp_t *obj, uint16_t ref_mv) |
| | Sets the reference voltage value. More...
|
| |
| cy_rslt_t | mtb_hal_comp_enable (mtb_hal_comp_t *obj, bool enable) |
| | Enable/disable the comparator. More...
|
| |
| cy_rslt_t | mtb_hal_comp_slice_enable (mtb_hal_comp_t *obj, bool enable) |
| | Enable/disable the slope generator slice. More...
|
| |
| bool | mtb_hal_comp_slice_read (mtb_hal_comp_t *obj) |
| | Read the output state of the slope generator slice. More...
|
| |
| cy_rslt_t | mtb_hal_comp_slice_set_ref_count (mtb_hal_comp_t *obj, uint32_t count) |
| | Sets the reference count of the slope generator slice. More...
|
| |
| cy_rslt_t | mtb_hal_comp_slice_set_ref_mv (mtb_hal_comp_t *obj, uint32_t ref_mv) |
| | Sets the reference voltage of the slope generator slice. More...
|
| |
| void | mtb_hal_comp_register_callback (mtb_hal_comp_t *obj, mtb_hal_comp_event_callback_t callback, void *callback_arg) |
| | Register/clear a callback handler for Comp events. More...
|
| |
| void | mtb_hal_comp_enable_event (mtb_hal_comp_t *obj, mtb_hal_comp_event_t event, bool enable) |
| | Enable or Disable the specified Comp event. More...
|
| |
| cy_rslt_t | mtb_hal_comp_process_interrupt (mtb_hal_comp_t *obj) |
| | Process interrupts related related to a Comp instance. More...
|
| |
◆ mtb_hal_comp_event_t
Comparator event types.
| Enumerator |
|---|
| MTB_HAL_COMP_EDGE_NONE | No trigger on either edge.
|
| MTB_HAL_COMP_RISING_EDGE | Rising edge on comparator output.
|
| MTB_HAL_COMP_FALLING_EDGE | Falling edge on comparator output.
|
| MTB_HAL_COMP_BOTH_EDGE | Falling edge on comparator output.
|
◆ mtb_hal_comp_setup()
Sets up a HAL instance to use the specified hardware resource.
This hardware resource must have already been configured via the PDL.
- Parameters
-
| [out] | obj | The HAL driver instance object. The caller must allocate the memory for this object, but the HAL will initialize its contents |
| [in] | config | The configurator-generated HAL config structure for this peripheral instance |
- Returns
- the status of the HAL setup
◆ mtb_hal_comp_read()
Reads the Comparator state.
- Parameters
-
- Returns
- The Comparator state. True if the non-inverting pin voltage is greater than the inverting pin voltage, false otherwise.
◆ mtb_hal_comp_set_ref()
Sets the reference voltage value.
- Parameters
-
| [in] | obj | Comparator object |
| [in] | ref_mv | Reference voltage in millivolts |
- Returns
- The status of the set reference request
◆ mtb_hal_comp_enable()
Enable/disable the comparator.
- Parameters
-
| [in] | obj | The Comp object |
| [in] | enable | Enable/disable |
- Returns
- The status of the enable request
◆ mtb_hal_comp_slice_enable()
Enable/disable the slope generator slice.
- Parameters
-
| [in] | obj | The Comp object |
| [in] | enable | Enable/disable |
- Returns
- The status of the enable request
◆ mtb_hal_comp_slice_read()
Read the output state of the slope generator slice.
- Parameters
-
- Returns
- The Comparator state. True if the non-inverting pin voltage is greater than the inverting pin voltage, false otherwise.
◆ mtb_hal_comp_slice_set_ref_count()
Sets the reference count of the slope generator slice.
- Parameters
-
| [in] | obj | The Comp object |
| [in] | count | Reference count |
- Returns
- The status of the set reference request
◆ mtb_hal_comp_slice_set_ref_mv()
Sets the reference voltage of the slope generator slice.
- Parameters
-
| [in] | obj | The Comp object |
| [in] | ref_mv | Reference voltage in millivolts |
- Returns
- The status of the set reference request
◆ mtb_hal_comp_register_callback()
Register/clear a callback handler for Comp events.
The referenced function will be called when one of the events enabled by by mtb_hal_comp_enable_event occurs.
- Parameters
-
| [in] | obj | The Comp object |
| [in] | callback | The callback handler which will be invoked when the event occurs |
| [in] | callback_arg | Generic argument that will be provided to the callback when called |
◆ mtb_hal_comp_enable_event()
Enable or Disable the specified Comp event.
When an enabled event occurs, the function specified by mtb_hal_comp_register_callback will be called.
- Parameters
-
| [in] | obj | The Comp object |
| [in] | event | The Comp event |
| [in] | enable | True to turn on interrupts, False to turn off |
◆ mtb_hal_comp_process_interrupt()
Process interrupts related related to a Comp instance.
- Parameters
-
| obj | HAL object for which the interrupt should be processed |
- Returns
- CY_RSLT_SUCCESS if the interrupt was processed successfully; otherwise an error