Hardware Abstraction Layer (HAL)
Opamp (Operational Amplifier)

General Description

High level interface for interacting with the Operational Amplifier (Opamp).

Features

Each opamp can operate in one of two modes:

In both modes, the output is driven off chip via another pin.

Quickstart

Call cyhal_opamp_init to initialize an opamp instance by providing the opamp object (obj), non-inverting input pin (vin_p), inverting input pin (vin_m), and output pin (vout). If follower mode is desired, pass NC for vin_m.

Use cyhal_opamp_set_power to configure the opamp power.

Code Snippets

Note
Error checking is omitted for clarity

Snippet 1: Bare opamp initialization

The following snippet initializes a bare opamp. Note that any passive components (e.g. resistive feedback) must be implemented off-chip.

cy_rslt_t rslt;
cyhal_opamp_t opamp_obj;
// Initialize opamp, using pin PIN_VINP for the non-inverting input, pin PIN_VINM for the
// inverting input, and PIN_VOUT for the output
rslt = cyhal_opamp_init(&opamp_obj, PIN_VINP, PIN_VINM, PIN_VOUT);
// Power on the opamp
// Release opamp object after use
cyhal_opamp_free(&opamp_obj);
@ CYHAL_POWER_LEVEL_DEFAULT
Default comparator power and speed.
Definition: cyhal_general_types.h:152
OPAMP object.
Definition: cyhal_hw_types.h:685
cy_rslt_t cyhal_opamp_set_power(cyhal_opamp_t *obj, cyhal_power_level_t power)
Changes the current operating power level of the opamp.
cy_rslt_t cyhal_opamp_init(cyhal_opamp_t *obj, cyhal_gpio_t vin_p, cyhal_gpio_t vin_m, cyhal_gpio_t vout)
Initialize the opamp peripheral.
void cyhal_opamp_free(cyhal_opamp_t *obj)
Deinitialize the opamp peripheral and free associated resources.
uint32_t cy_rslt_t
Provides the result of an operation as a structured bitfield.
Definition: cy_result.h:426

Snippet 2: Opamp follower initialization

The following snippet initializes an opamp as a follower.

cy_rslt_t rslt;
cyhal_opamp_t opamp_obj;
// Initialize opamp as follower, using pin PIN_VINP for the input and PIN_VOUT for the output
rslt = cyhal_opamp_init(&opamp_obj, PIN_VINP, NC, PIN_VOUT);
// Power on the opamp
// Release opamp object after use
cyhal_opamp_free(&opamp_obj);
@ NC
No Connect/Invalid Pin.
Definition: cyhal_psoc6_01_104_m_csp_ble.h:53

Snippet 3: Opamp powering-off and on

The following snippet demonstrates temporarily powering-off the opamp without freeing it.

// This assumes that the opamp has already been initialized as shown in snippet 1 or 2
// Power on the opamp
// When the opamp is needed again, power it back on
@ CYHAL_POWER_LEVEL_OFF
Power-off the comparator, while retaining configuration.
Definition: cyhal_general_types.h:144

API Reference

 Opamp HAL Results
 Opamp specific return codes.
 

Functions

cy_rslt_t cyhal_opamp_init (cyhal_opamp_t *obj, cyhal_gpio_t vin_p, cyhal_gpio_t vin_m, cyhal_gpio_t vout)
 Initialize the opamp peripheral. More...
 
cy_rslt_t cyhal_opamp_init_cfg (cyhal_opamp_t *obj, const cyhal_opamp_configurator_t *cfg)
 Initialize the opamp peripheral using a configurator generated configuration struct. More...
 
void cyhal_opamp_free (cyhal_opamp_t *obj)
 Deinitialize the opamp peripheral and free associated resources. More...
 
cy_rslt_t cyhal_opamp_set_power (cyhal_opamp_t *obj, cyhal_power_level_t power)
 Changes the current operating power level of the opamp. More...
 

Function Documentation

◆ cyhal_opamp_init()

cy_rslt_t cyhal_opamp_init ( cyhal_opamp_t obj,
cyhal_gpio_t  vin_p,
cyhal_gpio_t  vin_m,
cyhal_gpio_t  vout 
)

Initialize the opamp peripheral.

If vin_m is NC, the opamp will be initialized in follower mode (unity gain).

The opamp will be initialized but not powered-on until cyhal_opamp_set_power is called.

Parameters
[out]objPointer to an opamp object. The caller must allocate the memory for this object but the init function will initialize its contents.
[in]vin_pNon-inverting input
[in]vin_mInverting input
[in]voutopamp output
Returns
The status of the init request

◆ cyhal_opamp_init_cfg()

cy_rslt_t cyhal_opamp_init_cfg ( cyhal_opamp_t obj,
const cyhal_opamp_configurator_t cfg 
)

Initialize the opamp peripheral using a configurator generated configuration struct.

Parameters
[out]objPointer to an opamp object. The caller must allocate the memory for this object but the init function will initialize its contents.
[in]cfgConfiguration structure generated by a configurator.
Returns
The status of the init request

◆ cyhal_opamp_free()

void cyhal_opamp_free ( cyhal_opamp_t obj)

Deinitialize the opamp peripheral and free associated resources.

This will disconnect all inputs and outputs, including internal feedback.

Parameters
[in]objThe opamp object

◆ cyhal_opamp_set_power()

cy_rslt_t cyhal_opamp_set_power ( cyhal_opamp_t obj,
cyhal_power_level_t  power 
)

Changes the current operating power level of the opamp.

If the power level is set to CYHAL_POWER_LEVEL_OFF, the opamp will be powered-off but it will retain its configuration, so it is not necessary to reconfigure it when changing the power level from CYHAL_POWER_LEVEL_OFF to any other value.

Parameters
[in]objOpamp object
[in]powerThe power level to set
Returns
The status of the set power request