PSOC E8XXGP Device Support Library

| PSOC E8XXGP Device Support Library

API Reference | Typedefs | Enumerations | Functions
COMP (Analog Comparator)
HAL API Reference » HAL Drivers

## General Description

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

cy_rslt_t rslt;
mtb_hal_comp_t comp_obj;
// Setup the comparator
rslt = mtb_hal_comp_setup(&comp_obj, &comp_config_struct);
// Read the comparator value
bool comp_value = mtb_hal_comp_read(&comp_obj);
mtb_hal_comp_read
bool mtb_hal_comp_read(mtb_hal_comp_t *obj)
Reads the Comparator state.
mtb_hal_comp_setup
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.
mtb_hal_comp_t
Comparator object.
Definition: mtb_hal_hw_types_comp.h:83
cy_rslt_t
uint32_t cy_rslt_t
Provides the result of an operation as a structured bitfield.
Definition: cy_result.h:459

## API Reference
| Comparator HAL Results
| Comparator specific return codes.

## Typedefs
typedef void(* | mtb_hal_comp_event_callback_t) (void *callback_arg, mtb_hal_comp_event_t event)
| Handler for Comparator events.

## Enumerations
enum | mtb_hal_comp_event_t { MTB_HAL_COMP_EDGE_NONE = 0 , MTB_HAL_COMP_RISING_EDGE = 1 , MTB_HAL_COMP_FALLING_EDGE = 2 , MTB_HAL_COMP_BOTH_EDGE = 3 }
| Comparator event types. More...

## Functions
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...

## Enumeration Type Documentation

## ◆ mtb_hal_comp_event_t

enum 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.

## Function Documentation

## ◆ mtb_hal_comp_setup()

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.

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()

bool mtb_hal_comp_read | ( | mtb_hal_comp_t * | obj | ) |

Reads the Comparator state.

Parameters

  [in] | obj | Comparator object

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()

cy_rslt_t mtb_hal_comp_set_ref | ( | mtb_hal_comp_t * | obj,
|  | uint16_t | ref_mv
| ) |  |

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()

cy_rslt_t mtb_hal_comp_enable | ( | mtb_hal_comp_t * | obj,
|  | bool | 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()

cy_rslt_t mtb_hal_comp_slice_enable | ( | mtb_hal_comp_t * | obj,
|  | bool | 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()

bool mtb_hal_comp_slice_read | ( | mtb_hal_comp_t * | obj | ) |

Read the output state of the slope generator slice.

Parameters

  [in] | obj | The Comp object

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()

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.

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()

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.

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()

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.

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()

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.

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()

cy_rslt_t mtb_hal_comp_process_interrupt | ( | mtb_hal_comp_t * | obj | ) |

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