1 /**
2  * @file    gpio.h
3  * @brief   General-Purpose Input/Output (GPIO) function prototypes and data types.
4  */
5 
6 /******************************************************************************
7  *
8  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9  * Analog Devices, Inc.),
10  * Copyright (C) 2023-2024 Analog Devices, Inc.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  ******************************************************************************/
25 
26 /* Define to prevent redundant inclusion */
27 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32660_GPIO_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32660_GPIO_H_
29 
30 /* **** Includes **** */
31 #include "gpio_regs.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 /**
38  * @defgroup gpio General-Purpose Input/Output (GPIO)
39  * @ingroup periphlibs
40  * @{
41  */
42 
43 /* **** Definitions **** */
44 /**
45  * @defgroup gpio_port_pin Port and Pin Definitions
46  * @ingroup gpio
47  * @{
48  * @defgroup gpio_port Port Definitions
49  * @ingroup gpio_port_pin
50  * @{
51  */
52 #define MXC_GPIO_PORT_0 ((uint32_t)(1UL << 0)) ///< Port 0  Define
53 /**@} end of gpio_port group*/
54 /**
55  * @defgroup gpio_pin Pin Definitions
56  * @ingroup gpio_port_pin
57  * @{
58  */
59 #define MXC_GPIO_PIN_0 ((uint32_t)(1UL << 0)) ///< Pin 0 Define
60 #define MXC_GPIO_PIN_1 ((uint32_t)(1UL << 1)) ///< Pin 1 Define
61 #define MXC_GPIO_PIN_2 ((uint32_t)(1UL << 2)) ///< Pin 2 Define
62 #define MXC_GPIO_PIN_3 ((uint32_t)(1UL << 3)) ///< Pin 3 Define
63 #define MXC_GPIO_PIN_4 ((uint32_t)(1UL << 4)) ///< Pin 4 Define
64 #define MXC_GPIO_PIN_5 ((uint32_t)(1UL << 5)) ///< Pin 5 Define
65 #define MXC_GPIO_PIN_6 ((uint32_t)(1UL << 6)) ///< Pin 6 Define
66 #define MXC_GPIO_PIN_7 ((uint32_t)(1UL << 7)) ///< Pin 7 Define
67 #define MXC_GPIO_PIN_8 ((uint32_t)(1UL << 8)) ///< Pin 8 Define
68 #define MXC_GPIO_PIN_9 ((uint32_t)(1UL << 9)) ///< Pin 9 Define
69 #define MXC_GPIO_PIN_10 ((uint32_t)(1UL << 10)) ///< Pin 10 Define
70 #define MXC_GPIO_PIN_11 ((uint32_t)(1UL << 11)) ///< Pin 11 Define
71 #define MXC_GPIO_PIN_12 ((uint32_t)(1UL << 12)) ///< Pin 12 Define
72 #define MXC_GPIO_PIN_13 ((uint32_t)(1UL << 13)) ///< Pin 13 Define
73 /**@} end of gpio_pin group */
74 /**@} end of gpio_port_pin group */
75 
76 /**
77  * @brief      Type alias for a GPIO callback function with prototype:
78  * @code
79     void callback_fn(void *cbdata);
80  * @endcode
81  * @param      cbdata  A void pointer to the data type as registered when
82  *                     MXC_GPIO_RegisterCallback() was called.
83  */
84 typedef void (*mxc_gpio_callback_fn)(void *cbdata);
85 
86 /**
87  * @brief   Enumeration type for the GPIO Function Type
88  */
89 typedef enum {
90     MXC_GPIO_FUNC_IN, ///< GPIO Input
91     MXC_GPIO_FUNC_OUT, ///< GPIO Output
92     MXC_GPIO_FUNC_ALT1, ///< Alternate Function Selection
93     MXC_GPIO_FUNC_ALT2, ///< Alternate Function Selection
94     MXC_GPIO_FUNC_ALT3, ///< Alternate Function Selection
95     MXC_GPIO_FUNC_ALT4, ///< Alternate Function Selection
96 } mxc_gpio_func_t;
97 
98 /**
99  * @brief   Enumeration type for the voltage level on a given pin.
100  */
101 typedef enum {
102     MXC_GPIO_VSSEL_VDDIO, ///< Set pin to VIDDIO voltage
103     MXC_GPIO_VSSEL_VDDIOH, ///< Set pin to VIDDIOH voltage
104 } mxc_gpio_vssel_t;
105 
106 /**
107  * @brief   Enumeration type for drive strength on a given pin.
108  */
109 typedef enum {
110     MXC_GPIO_DRVSTR_0, ///< Drive Strength 0
111     MXC_GPIO_DRVSTR_1, ///< Drive Strength 1
112     MXC_GPIO_DRVSTR_2, ///< Drive Strength 2
113     MXC_GPIO_DRVSTR_3, ///< Drive Strength 3
114 } mxc_gpio_drvstr_t;
115 
116 /**
117  * @brief   Enumeration type for the type of GPIO pad on a given pin.
118  */
119 typedef enum {
120     MXC_GPIO_PAD_NONE, ///< No pull-up or pull-down
121     MXC_GPIO_PAD_PULL_UP, ///< Set pad to weak pull-up
122     MXC_GPIO_PAD_PULL_DOWN, ///< Set pad to weak pull-down
123     MXC_GPIO_PAD_WEAK_PULL_UP = MXC_GPIO_PAD_PULL_UP, ///< Set pad to weak pull-up
124     MXC_GPIO_PAD_WEAK_PULL_DOWN = MXC_GPIO_PAD_PULL_DOWN, ///< Set pad to weak pull-down
125 } mxc_gpio_pad_t;
126 
127 /**
128  * @brief   Structure type for configuring a GPIO port.
129  */
130 typedef struct {
131     mxc_gpio_regs_t *port; ///< Pointer to GPIO regs
132     uint32_t mask; ///< Pin mask (multiple pins may be set)
133     mxc_gpio_func_t func; ///< Function type
134     mxc_gpio_pad_t pad; ///< Pad type
135     mxc_gpio_vssel_t vssel; ///< Voltage select
136     mxc_gpio_drvstr_t drvstr; ///< Drive Strength select
137 } mxc_gpio_cfg_t;
138 
139 /**
140  * @brief   Enumeration type for the interrupt modes.
141  */
142 typedef enum {
143     MXC_GPIO_INT_LEVEL, ///< Interrupt is level sensitive
144     MXC_GPIO_INT_EDGE ///< Interrupt is edge sensitive
145 } mxc_gpio_int_mode_t;
146 
147 /**
148  * @brief   Enumeration type for the interrupt polarity.
149  */
150 typedef enum {
151     MXC_GPIO_INT_FALLING, ///< Interrupt triggers on falling edge
152     MXC_GPIO_INT_HIGH, ///< Interrupt triggers when level is high
153     MXC_GPIO_INT_RISING, ///< Interrupt triggers on rising edge
154     MXC_GPIO_INT_LOW, ///< Interrupt triggers when level is low
155     MXC_GPIO_INT_BOTH ///< Interrupt triggers on either edge
156 } mxc_gpio_int_pol_t;
157 
158 /* **** Function Prototypes **** */
159 
160 /**
161  * @brief      Initialize GPIO.
162  * @param      portMask     Mask for the port to be initialized
163  * @return     #E_NO_ERROR if everything is successful. See \ref MXC_Error_Codes for the list of error codes.
164  */
165 int MXC_GPIO_Init(uint32_t portMask);
166 
167 /**
168  * @brief      Shutdown GPIO.
169  * @param      portMask     Mask for the port to be initialized
170  * @return     #E_NO_ERROR if everything is successful. See \ref MXC_Error_Codes for the list of error codes.
171  */
172 int MXC_GPIO_Shutdown(uint32_t portMask);
173 
174 /**
175  * @brief      Reset GPIO.
176  * @param      portMask     Mask for the port to be initialized
177  * @return     #E_NO_ERROR if everything is successful. See \ref MXC_Error_Codes for the list of error codes.
178  */
179 int MXC_GPIO_Reset(uint32_t portMask);
180 
181 /**
182  * @brief      Configure GPIO pin(s).
183  * @param      cfg   Pointer to configuration structure describing the pin.
184  * @return     #E_NO_ERROR if everything is successful. See \ref MXC_Error_Codes for the list of error codes.
185  */
186 int MXC_GPIO_Config(const mxc_gpio_cfg_t *cfg);
187 
188 /**
189  * @brief      Gets the pin(s) input state.
190  * @param      port  Pointer to the GPIO port registers
191  * @param      mask  Mask of the pin(s) to read
192  * @return     The requested pin state.
193  */
194 uint32_t MXC_GPIO_InGet(mxc_gpio_regs_t *port, uint32_t mask);
195 
196 /**
197  * @brief      Sets the pin(s) to a high level output.
198  * @param      port  Pointer to the GPIO port registers
199  * @param      mask  Mask of the pin(s) to set
200  */
201 void MXC_GPIO_OutSet(mxc_gpio_regs_t *port, uint32_t mask);
202 
203 /**
204  * @brief      Clears the pin(s) to a low level output.
205  * @param      port  Pointer to the GPIO port registers
206  * @param      mask  Mask of the pin(s) to clear
207  */
208 void MXC_GPIO_OutClr(mxc_gpio_regs_t *port, uint32_t mask);
209 
210 /**
211  * @brief      Gets the pin(s) output state.
212  * @param      port  Pointer to the GPIO port registers
213  * @param      mask  Mask of the pin(s) to read the output state of
214  * @return     The state of the requested pin.
215  *
216  */
217 uint32_t MXC_GPIO_OutGet(mxc_gpio_regs_t *port, uint32_t mask);
218 
219 /**
220  * @brief      Write the pin(s) to a desired output level.
221  * @param      port  Pointer to the GPIO port registers
222  * @param      mask  Mask of the pin(s) to set output level of
223  * @param      val   Desired output level of the pin(s). This will be masked
224  *                   with the configuration mask.
225  */
226 void MXC_GPIO_OutPut(mxc_gpio_regs_t *port, uint32_t mask, uint32_t val);
227 
228 /**
229  * @brief      Toggles the the pin(s) output level.
230  * @param      port  Pointer to the GPIO port registers
231  * @param      mask  Mask of the pin(s) to toggle
232  */
233 void MXC_GPIO_OutToggle(mxc_gpio_regs_t *port, uint32_t mask);
234 
235 /**
236  * @brief      Configure GPIO interrupt(s)
237  * @param      cfg   Pointer to configuration structure describing the pin.
238  * @param      pol   Requested interrupt polarity.
239  * @return     #E_NO_ERROR if everything is successful. See \ref MXC_Error_Codes for the list of error codes.
240  */
241 int MXC_GPIO_IntConfig(const mxc_gpio_cfg_t *cfg, mxc_gpio_int_pol_t pol);
242 
243 /**
244  * @brief      Enables the specified GPIO interrupt
245  * @param      port  Pointer to the GPIO port registers
246  * @param      mask  Mask of the pin(s) to enable interrupts for
247  *
248  */
249 void MXC_GPIO_EnableInt(mxc_gpio_regs_t *port, uint32_t mask);
250 
251 /**
252  * @brief      Disables the specified GPIO interrupt.
253  * @param      port  Pointer to the GPIO port registers
254  * @param      mask  Mask of the pin(s) to disable interrupts for
255  */
256 void MXC_GPIO_DisableInt(mxc_gpio_regs_t *port, uint32_t mask);
257 
258 /**
259  * @brief      Gets the interrupt(s) status on a GPIO port
260  *
261  * @param      port   Pointer to the port requested
262  *
263  * @return     The requested interrupt status.
264  */
265 uint32_t MXC_GPIO_GetFlags(mxc_gpio_regs_t *port);
266 
267 /**
268  * @brief      Gets the interrupt(s) status on a GPIO port
269  *
270  * @param      port   Pointer to the port requested
271  * @param      flags  The flags to clear
272  */
273 void MXC_GPIO_ClearFlags(mxc_gpio_regs_t *port, uint32_t flags);
274 
275 /**
276  * @brief      Registers a callback for the interrupt on a given port and pin.
277  * @param      cfg       Pointer to configuration structure describing the pin
278  * @param      callback  A pointer to a function of type #callback_fn.
279  * @param      cbdata    The parameter to be passed to the callback function, #callback_fn, when an interrupt occurs.
280  *
281  */
282 void MXC_GPIO_RegisterCallback(const mxc_gpio_cfg_t *cfg, mxc_gpio_callback_fn callback,
283                                void *cbdata);
284 
285 /**
286  * @brief      GPIO IRQ Handler. @note If a callback is registered for a given
287  *             interrupt, the callback function will be called.
288  *
289  * @param      port Number of the port that generated the interrupt service routine.
290  *
291  */
292 void MXC_GPIO_Handler(unsigned int port);
293 
294 /**
295  * @brief      Set Voltage select for pins to VDDIO or VDDIOH
296  *
297  * @param      port   The GPIO port
298  * @param[in]  vssel  VDDIO or VDDIOH to set the voltatge to
299  * @param[in]  mask   Pins in the GPIO port that will be set to the voltage.
300  *
301  * @return     #E_NO_ERROR if everything is successful. See \ref MXC_Error_Codes for the list of error codes.
302  */
303 int MXC_GPIO_SetVSSEL(mxc_gpio_regs_t *port, mxc_gpio_vssel_t vssel, uint32_t mask);
304 
305 /**
306  * @brief      Enables GPIO pins to be used as a wakeup source.
307  *
308  * @param      port   The GPIO port
309  * @param      mask   Pins in the GPIO port that will be enabled as a wakeup source.
310  */
311 void MXC_GPIO_SetWakeEn(mxc_gpio_regs_t *port, uint32_t mask);
312 
313 /**
314  * @brief      Disables GPIO pins from being used as a wakeup source.
315  *
316  * @param      port   The GPIO port
317  * @param      mask   Pins in the GPIO port that will be disabled as a wakeup source.
318  */
319 void MXC_GPIO_ClearWakeEn(mxc_gpio_regs_t *port, uint32_t mask);
320 
321 /**
322  * @brief      Returns the pins currently enabled as wakeup sources.
323  *
324  * @param      port   The GPIO port to check.
325  *
326  * @returns    The value of the wake enable register.
327  */
328 uint32_t MXC_GPIO_GetWakeEn(mxc_gpio_regs_t *port);
329 
330 /**
331  * @brief      Set Drive Strength for pins.
332  *
333  * @param      port   The GPIO port.
334  * @param[in]  ds     Drive strength level. Ref /mxc_gpio_ds_t enum type.
335  * @param[in]  mask   Pins in the GPIO port that will be set to the voltage.
336  */
337 int MXC_GPIO_SetDriveStrength(mxc_gpio_regs_t *port, mxc_gpio_drvstr_t drvstr, uint32_t mask);
338 
339 /**@} end of group gpio */
340 
341 #ifdef __cplusplus
342 }
343 #endif
344 
345 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32660_GPIO_H_
346