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_MAX32572_GPIO_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32572_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 #define MXC_GPIO_PORT_1 ((uint32_t)(1UL << 1)) ///< Port 1  Define
54 #define MXC_GPIO_PORT_2 ((uint32_t)(1UL << 2)) ///< Port 2  Define
55 #define MXC_GPIO_PORT_3 ((uint32_t)(1UL << 3)) ///< Port 3  Define
56 /**@} end of gpio_port group*/
57 
58 /**
59  * @defgroup gpio_pin Pin Definitions
60  * @ingroup gpio_port_pin
61  * @{
62  */
63 #define MXC_GPIO_PIN_0 ((uint32_t)(1UL << 0)) ///< Pin 0 Define
64 #define MXC_GPIO_PIN_1 ((uint32_t)(1UL << 1)) ///< Pin 1 Define
65 #define MXC_GPIO_PIN_2 ((uint32_t)(1UL << 2)) ///< Pin 2 Define
66 #define MXC_GPIO_PIN_3 ((uint32_t)(1UL << 3)) ///< Pin 3 Define
67 #define MXC_GPIO_PIN_4 ((uint32_t)(1UL << 4)) ///< Pin 4 Define
68 #define MXC_GPIO_PIN_5 ((uint32_t)(1UL << 5)) ///< Pin 5 Define
69 #define MXC_GPIO_PIN_6 ((uint32_t)(1UL << 6)) ///< Pin 6 Define
70 #define MXC_GPIO_PIN_7 ((uint32_t)(1UL << 7)) ///< Pin 7 Define
71 #define MXC_GPIO_PIN_8 ((uint32_t)(1UL << 8)) ///< Pin 8 Define
72 #define MXC_GPIO_PIN_9 ((uint32_t)(1UL << 9)) ///< Pin 9 Define
73 #define MXC_GPIO_PIN_10 ((uint32_t)(1UL << 10)) ///< Pin 10 Define
74 #define MXC_GPIO_PIN_11 ((uint32_t)(1UL << 11)) ///< Pin 11 Define
75 #define MXC_GPIO_PIN_12 ((uint32_t)(1UL << 12)) ///< Pin 12 Define
76 #define MXC_GPIO_PIN_13 ((uint32_t)(1UL << 13)) ///< Pin 13 Define
77 #define MXC_GPIO_PIN_14 ((uint32_t)(1UL << 14)) ///< Pin 14 Define
78 #define MXC_GPIO_PIN_15 ((uint32_t)(1UL << 15)) ///< Pin 15 Define
79 #define MXC_GPIO_PIN_16 ((uint32_t)(1UL << 16)) ///< Pin 16 Define
80 #define MXC_GPIO_PIN_17 ((uint32_t)(1UL << 17)) ///< Pin 17 Define
81 #define MXC_GPIO_PIN_18 ((uint32_t)(1UL << 18)) ///< Pin 18 Define
82 #define MXC_GPIO_PIN_19 ((uint32_t)(1UL << 19)) ///< Pin 19 Define
83 #define MXC_GPIO_PIN_20 ((uint32_t)(1UL << 20)) ///< Pin 20 Define
84 #define MXC_GPIO_PIN_21 ((uint32_t)(1UL << 21)) ///< Pin 21 Define
85 #define MXC_GPIO_PIN_22 ((uint32_t)(1UL << 22)) ///< Pin 22 Define
86 #define MXC_GPIO_PIN_23 ((uint32_t)(1UL << 23)) ///< Pin 23 Define
87 #define MXC_GPIO_PIN_24 ((uint32_t)(1UL << 24)) ///< Pin 24 Define
88 #define MXC_GPIO_PIN_25 ((uint32_t)(1UL << 25)) ///< Pin 25 Define
89 #define MXC_GPIO_PIN_26 ((uint32_t)(1UL << 26)) ///< Pin 26 Define
90 #define MXC_GPIO_PIN_27 ((uint32_t)(1UL << 27)) ///< Pin 27 Define
91 #define MXC_GPIO_PIN_28 ((uint32_t)(1UL << 28)) ///< Pin 28 Define
92 #define MXC_GPIO_PIN_29 ((uint32_t)(1UL << 29)) ///< Pin 29 Define
93 #define MXC_GPIO_PIN_30 ((uint32_t)(1UL << 30)) ///< Pin 30 Define
94 #define MXC_GPIO_PIN_31 ((uint32_t)(1UL << 31)) ///< Pin 31 Define
95 /**@} end of gpio_pin group */
96 /**@} end of gpio_port_pin group */
97 
98 /**
99  * @brief      Type alias for a GPIO callback function with prototype:
100  * @code
101     void callback_fn(void *cbdata);
102  * @endcode
103  * @param      cbdata  A void pointer to the data type as registered when
104  *                     MXC_GPIO_RegisterCallback() was called.
105  */
106 typedef void (*mxc_gpio_callback_fn)(void *cbdata);
107 
108 /**
109  * @brief   Enumeration type for the GPIO Function Type
110  */
111 typedef enum {
112     MXC_GPIO_FUNC_IN, ///< GPIO Input
113     MXC_GPIO_FUNC_OUT, ///< GPIO Output
114     MXC_GPIO_FUNC_ALT1, ///< Alternate Function Selection
115     MXC_GPIO_FUNC_ALT2, ///< Alternate Function Selection
116     MXC_GPIO_FUNC_ALT3, ///< Alternate Function Selection
117     MXC_GPIO_FUNC_ALT4, ///< Alternate Function Selection
118 } mxc_gpio_func_t;
119 
120 /**
121  * @brief   Enumeration type for the voltage level on a given pin.
122  */
123 typedef enum {
124     MXC_GPIO_VSSEL_VDDIO, ///< Set pin to VIDDIO voltage
125     MXC_GPIO_VSSEL_VDDIOH, ///< Set pin to VIDDIOH voltage
126 } mxc_gpio_vssel_t;
127 
128 /**
129  * @brief   Enumeration type for drive strength on a given pin.
130  */
131 typedef enum {
132     MXC_GPIO_DRVSTR_0, ///< Drive Strength 0
133     MXC_GPIO_DRVSTR_1, ///< Drive Strength 1
134     MXC_GPIO_DRVSTR_2, ///< Drive Strength 2
135     MXC_GPIO_DRVSTR_3, ///< Drive Strength 3
136 } mxc_gpio_drvstr_t;
137 
138 /**
139  * @brief   Enumeration type for the type of GPIO pad on a given pin.
140  */
141 typedef enum {
142     MXC_GPIO_PAD_NONE, ///< No pull-up or pull-down
143     MXC_GPIO_PAD_PULL_UP, ///< Set pad to strong pull-up
144     MXC_GPIO_PAD_PULL_DOWN, ///< Set pad to strong pull-down
145     MXC_GPIO_PAD_WEAK_PULL_UP, ///< Set pad to weak pull-up
146     MXC_GPIO_PAD_WEAK_PULL_DOWN, ///< Set pad to weak pull-down
147 } mxc_gpio_pad_t;
148 
149 /**
150  * @brief   Structure type for configuring a GPIO port.
151  */
152 typedef struct {
153     mxc_gpio_regs_t *port; ///< Pointer to GPIO regs
154     uint32_t mask; ///< Pin mask (multiple pins may be set)
155     mxc_gpio_func_t func; ///< Function type
156     mxc_gpio_pad_t pad; ///< Pad type
157     mxc_gpio_vssel_t vssel; ///< Voltage select
158     mxc_gpio_drvstr_t drvstr; ///< Drive Strength select
159 } mxc_gpio_cfg_t;
160 
161 /**
162  * @brief   Enumeration type for the interrupt modes.
163  */
164 typedef enum {
165     MXC_GPIO_INT_LEVEL, ///< Interrupt is level sensitive
166     MXC_GPIO_INT_EDGE ///< Interrupt is edge sensitive
167 } mxc_gpio_int_mode_t;
168 
169 /**
170  * @brief   Enumeration type for the interrupt polarity.
171  */
172 typedef enum {
173     MXC_GPIO_INT_FALLING, ///< Interrupt triggers on falling edge
174     MXC_GPIO_INT_HIGH, ///< Interrupt triggers when level is high
175     MXC_GPIO_INT_RISING, ///< Interrupt triggers on rising edge
176     MXC_GPIO_INT_LOW, ///< Interrupt triggers when level is low
177     MXC_GPIO_INT_BOTH ///< Interrupt triggers on either edge
178 } mxc_gpio_int_pol_t;
179 
180 /* **** Function Prototypes **** */
181 
182 /**
183  * @brief      Initialize GPIO.
184  * @param      portMask     Mask for the port to be initialized
185  * @return     #E_NO_ERROR if everything is successful. See \ref MXC_Error_Codes for the list of error codes.
186  */
187 int MXC_GPIO_Init(uint32_t portMask);
188 
189 /**
190  * @brief      Shutdown GPIO.
191  * @param      portMask     Mask for the port to shutdown
192  * @return     #E_NO_ERROR if everything is successful. See \ref MXC_Error_Codes for the list of error codes.
193  */
194 int MXC_GPIO_Shutdown(uint32_t portMask);
195 
196 /**
197  * @brief      Reset GPIO.
198  * @param      portMask     Mask for the port to reset
199  * @return     #E_NO_ERROR if everything is successful. See \ref MXC_Error_Codes for the list of error codes.
200  */
201 int MXC_GPIO_Reset(uint32_t portMask);
202 
203 /**
204  * @brief      Configure GPIO pin(s).
205  * @param      cfg   Pointer to configuration structure describing the pin.
206  * @return     #E_NO_ERROR if everything is successful. See \ref MXC_Error_Codes for the list of error codes.
207  */
208 int MXC_GPIO_Config(const mxc_gpio_cfg_t *cfg);
209 
210 /**
211  * @brief      Gets the pin(s) input state.
212  * @param      port  Pointer to GPIO port.
213  * @param      mask  Mask of the pin to read
214  *
215  * @return     The requested pin state.
216  */
217 uint32_t MXC_GPIO_InGet(mxc_gpio_regs_t *port, uint32_t mask);
218 
219 /**
220  * @brief      Sets the pin(s) to a high level output.
221  * @param      port  Pointer to GPIO port.
222  * @param      mask  Mask of the pin to set
223  */
224 void MXC_GPIO_OutSet(mxc_gpio_regs_t *port, uint32_t mask);
225 
226 /**
227  * @brief      Clears the pin(s) to a low level output.
228  * @param      port  Pointer to GPIO port.
229  * @param      mask  Mask of the pin to clear
230  *
231  */
232 void MXC_GPIO_OutClr(mxc_gpio_regs_t *port, uint32_t mask);
233 
234 /**
235  * @brief      Gets the pin(s) output state.
236  * @param      port  Pointer to GPIO port.
237  * @param      mask  Mask of the pin to read the output state of.
238  *
239  * @return     The state of the requested pin.
240  *
241  */
242 uint32_t MXC_GPIO_OutGet(mxc_gpio_regs_t *port, uint32_t mask);
243 
244 /**
245  * @brief      Write the pin(s) to a desired output level.
246  * @param      port  Pointer to GPIO port.
247  * @param      mask  Mask of the pin to set output level of
248  * @param      val   Desired output level of the pin(s). This will be masked
249  *                   with the configuration mask.
250  */
251 void MXC_GPIO_OutPut(mxc_gpio_regs_t *port, uint32_t mask, uint32_t val);
252 
253 /**
254  * @brief      Toggles the the pin(s) output level.
255  * @param      port  Pointer to GPIO port.
256  * @param      mask  Mask of the pin to toggle the output
257  */
258 void MXC_GPIO_OutToggle(mxc_gpio_regs_t *port, uint32_t mask);
259 
260 /**
261  * @brief      Configure GPIO interrupt(s)
262  * @param      cfg   Pointer to configuration structure describing the pin.
263  * @param      pol   Requested interrupt polarity.
264  * @return     #E_NO_ERROR if everything is successful. See \ref MXC_Error_Codes for the list of error codes.
265  */
266 int MXC_GPIO_IntConfig(const mxc_gpio_cfg_t *cfg, mxc_gpio_int_pol_t pol);
267 
268 /**
269  * @brief      Enables the specified GPIO interrupt
270  * @param      port  Pointer to GPIO port.
271  * @param      mask  Mask of the pin to enable interrupt
272  */
273 void MXC_GPIO_EnableInt(mxc_gpio_regs_t *port, uint32_t mask);
274 
275 /**
276  * @brief      Disables the specified GPIO interrupt.
277  * @param      port  Pointer to GPIO port.
278  *  @param     mask  Mask of the pin to disable interrupt
279  */
280 void MXC_GPIO_DisableInt(mxc_gpio_regs_t *port, uint32_t mask);
281 
282 /**
283  * @brief      Gets the interrupt(s) status on a GPIO port
284  *
285  * @param      port   Pointer to the port requested
286  *
287  * @return     The requested interrupt status.
288  */
289 uint32_t MXC_GPIO_GetFlags(mxc_gpio_regs_t *port);
290 
291 /**
292  * @brief      Gets the interrupt(s) status on a GPIO port
293  *
294  * @param      port   Pointer to the port requested
295  * @param      flags  The flags to clear
296  */
297 void MXC_GPIO_ClearFlags(mxc_gpio_regs_t *port, uint32_t flags);
298 
299 /**
300  * @brief      Registers a callback for the interrupt on a given port and pin.
301  * @param      cfg       Pointer to configuration structure describing the pin
302  * @param      callback  A pointer to a function of type #mxc_gpio_callback_fn.
303  * @param      cbdata    The parameter to be passed to the callback function, #mxc_gpio_callback_fn, when an interrupt occurs.
304  *
305  */
306 void MXC_GPIO_RegisterCallback(const mxc_gpio_cfg_t *cfg, mxc_gpio_callback_fn callback,
307                                void *cbdata);
308 
309 /**
310  * @brief      GPIO IRQ Handler. @note If a callback is registered for a given
311  *             interrupt, the callback function will be called.
312  *
313  * @param      port number of the port that generated the interrupt service routine.
314  *
315  */
316 void MXC_GPIO_Handler(unsigned int port);
317 
318 /**
319  * @brief      Set Voltage select for pins to VDDIO or VDDIOH
320  *
321  * @param      port   The GPIO port
322  * @param[in]  vssel  VDDIO or VDDIOH to set the voltatge to
323  * @param[in]  mask   Pins in the GPIO port that will be set to the voltage.
324  *
325  * @return     #E_NO_ERROR if everything is successful. See \ref MXC_Error_Codes for the list of error codes.
326  */
327 int MXC_GPIO_SetVSSEL(mxc_gpio_regs_t *port, mxc_gpio_vssel_t vssel, uint32_t mask);
328 
329 /**
330  * @brief      Enables GPIO pins to be used as a wakeup source.
331  *
332  * @param      port   The GPIO port
333  * @param      mask   Pins in the GPIO port that will be enabled as a wakeup source.
334  */
335 void MXC_GPIO_SetWakeEn(mxc_gpio_regs_t *port, uint32_t mask);
336 
337 /**
338  * @brief      Disables GPIO pins from being used as a wakeup source.
339  *
340  * @param      port   The GPIO port
341  * @param      mask   Pins in the GPIO port that will be disabled as a wakeup source.
342  */
343 void MXC_GPIO_ClearWakeEn(mxc_gpio_regs_t *port, uint32_t mask);
344 
345 /**
346  * @brief      Returns the pins currently enabled as wakeup sources.
347  *
348  * @param      port   The GPIO port to check.
349  *
350  * @returns    The value of the wake enable register.
351  */
352 uint32_t MXC_GPIO_GetWakeEn(mxc_gpio_regs_t *port);
353 
354 /**
355  * @brief      Set Drive Strength for pins.
356  *
357  * @param      port   The GPIO port.
358  * @param[in]  ds     Drive strength level. Ref /mxc_gpio_ds_t enum type.
359  * @param[in]  mask   Pins in the GPIO port that will be set to the voltage.
360  */
361 int MXC_GPIO_SetDriveStrength(mxc_gpio_regs_t *port, mxc_gpio_drvstr_t drvstr, uint32_t mask);
362 
363 /**@} end of group gpio */
364 
365 #ifdef __cplusplus
366 }
367 #endif
368 
369 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32572_GPIO_H_
370