1 /***************************************************************************//**
2 * \file cyhal_opamp.h
3 *
4 * \brief
5 * Provides a high level interface for interacting with the Infineon Opamp.
6 * This interface abstracts out the chip specific details. If any chip specific
7 * functionality is required, the low level functions can be used directly.
8 *
9 ********************************************************************************
10 * \copyright
11 * Copyright 2018-2022 Cypress Semiconductor Corporation (an Infineon company) or
12 * an affiliate of Cypress Semiconductor Corporation
13 *
14 * SPDX-License-Identifier: Apache-2.0
15 *
16 * Licensed under the Apache License, Version 2.0 (the "License");
17 * you may not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 *     http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 *******************************************************************************/
28 
29 /**
30 * \addtogroup group_hal_opamp Opamp (Operational Amplifier)
31 * \ingroup group_hal
32 * \{
33 * High level interface for interacting with the Operational Amplifier (Opamp).
34 *
35 * \section cyhal_opamp_features Features
36 * Each opamp can operate in one of two modes:
37 * - Opamp: Bare opamp with two input pins.
38 * - Follower: Also known as unity gain. Buffers the signal on a single input pin
39 *             and drives the same voltage on the output.
40 *
41 * In both modes, the output is driven off chip via another pin.
42 *
43 * \section cyhal_opamp_quickstart Quickstart
44 * Call \ref cyhal_opamp_init to initialize an opamp instance by providing the opamp
45 * object (**obj**), non-inverting input pin (**vin_p**), inverting input pin (**vin_m**),
46 * and output pin (**vout**). If follower mode is desired, pass `NC` for **vin_m**.
47 *
48 * Use \ref cyhal_opamp_set_power to configure the opamp power.
49 *
50 * \section subsection_opamp_snippets Code Snippets
51 * \note Error checking is omitted for clarity
52 * \section subsection_opamp_snippet_1 Snippet 1: Bare opamp initialization
53 * The following snippet initializes a bare opamp. Note that any passive components
54 * (e.g. resistive feedback) must be implemented off-chip.
55 * \snippet hal_opamp.c snippet_cyhal_opamp_init_diff
56 * \section subsection_opamp_snippet_2 Snippet 2: Opamp follower initialization
57 * The following snippet initializes an opamp as a follower.
58 * \snippet hal_opamp.c snippet_cyhal_opamp_init_follower
59 * \section subsection_opamp_snippet_3 Snippet 3: Opamp powering-off and on
60 * The following snippet demonstrates temporarily powering-off the opamp without freeing it.
61 * \snippet hal_opamp.c snippet_cyhal_opamp_start_stop
62 */
63 
64 #pragma once
65 
66 #include <stdint.h>
67 #include <stdbool.h>
68 #include "cy_result.h"
69 #include "cyhal_hw_types.h"
70 
71 /** \addtogroup group_hal_results_opamp Opamp HAL Results
72  *  Opamp specific return codes
73  *  \ingroup group_hal_results
74  *  \{ *//**
75  */
76 
77 /** The requested pins are invalid */
78 #define CYHAL_OPAMP_RSLT_ERR_INVALID_PIN                  \
79     (CY_RSLT_CREATE_EX(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_HAL, CYHAL_RSLT_MODULE_OPAMP, 1))
80 /** Bad argument */
81 #define CYHAL_OPAMP_RSLT_BAD_ARGUMENT                     \
82     (CY_RSLT_CREATE_EX(CY_RSLT_TYPE_ERROR, CY_RSLT_MODULE_ABSTRACTION_HAL, CYHAL_RSLT_MODULE_OPAMP, 2))
83 /**
84  * \}
85  */
86 
87 #if defined(__cplusplus)
88 extern "C" {
89 #endif
90 
91 /** Initialize the opamp peripheral.
92  *  If vin_m is @ref NC, the opamp will be initialized in follower mode (unity gain).
93  *
94  * The opamp will be initialized but not powered-on until @ref cyhal_opamp_set_power is called.
95  *
96  * @param[out] obj  Pointer to an opamp object. The caller must allocate the memory
97  *                  for this object but the init function will initialize its contents.
98  * @param[in] vin_p Non-inverting input
99  * @param[in] vin_m Inverting input
100  * @param[in] vout  opamp output
101  * @return The status of the init request
102  */
103 cy_rslt_t cyhal_opamp_init(cyhal_opamp_t *obj, cyhal_gpio_t vin_p, cyhal_gpio_t vin_m, cyhal_gpio_t vout);
104 
105 /** Initialize the opamp peripheral using a configurator generated configuration struct
106   *
107  * @param[out] obj              Pointer to an opamp object. The caller must allocate the memory
108  *                              for this object but the init function will initialize its contents.
109  * @param[in] cfg               Configuration structure generated by a configurator.
110  * @return The status of the init request
111  */
112  cy_rslt_t cyhal_opamp_init_cfg(cyhal_opamp_t *obj, const cyhal_opamp_configurator_t *cfg);
113 
114 /** Deinitialize the opamp peripheral and free associated resources.
115  *
116  * This will disconnect all inputs and outputs, including internal feedback.
117  *
118  * @param[in] obj The opamp object
119  */
120 void cyhal_opamp_free(cyhal_opamp_t *obj);
121 
122 /** Changes the current operating power level of the opamp.
123  *
124  * If the power level is set to @ref CYHAL_POWER_LEVEL_OFF, the opamp will be powered-off
125  * but it will retain its configuration, so it is not necessary to reconfigure it when changing
126  * the power level from @ref CYHAL_POWER_LEVEL_OFF to any other value.
127  *
128  * @param[in] obj   Opamp object
129  * @param[in] power The power level to set
130  * @return The status of the set power request
131  */
132 cy_rslt_t cyhal_opamp_set_power(cyhal_opamp_t *obj, cyhal_power_level_t power);
133 
134 #if defined(__cplusplus)
135 }
136 #endif
137 
138 #ifdef CYHAL_OPAMP_IMPL_HEADER
139 #include CYHAL_OPAMP_IMPL_HEADER
140 #endif /* CYHAL_OPAMP_IMPL_HEADER */
141 
142 /** \} group_hal_opamp */
143