1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2020 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef _FSL_GPIO_H_
10 #define _FSL_GPIO_H_
11 
12 #include "fsl_common.h"
13 
14 /*!
15  * @addtogroup gpio_driver
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 
23 /*! @name Driver version */
24 /*@{*/
25 /*! @brief GPIO driver version. */
26 #define FSL_GPIO_DRIVER_VERSION (MAKE_VERSION(2, 0, 6))
27 /*@}*/
28 
29 /*! @brief GPIO direction definition. */
30 typedef enum _gpio_pin_direction
31 {
32     kGPIO_DigitalInput  = 0U, /*!< Set current pin as digital input.*/
33     kGPIO_DigitalOutput = 1U, /*!< Set current pin as digital output.*/
34 } gpio_pin_direction_t;
35 
36 /*! @brief GPIO interrupt mode definition. */
37 typedef enum _gpio_interrupt_mode
38 {
39     kGPIO_NoIntmode              = 0U, /*!< Set current pin general IO functionality.*/
40     kGPIO_IntLowLevel            = 1U, /*!< Set current pin interrupt is low-level sensitive.*/
41     kGPIO_IntHighLevel           = 2U, /*!< Set current pin interrupt is high-level sensitive.*/
42     kGPIO_IntRisingEdge          = 3U, /*!< Set current pin interrupt is rising-edge sensitive.*/
43     kGPIO_IntFallingEdge         = 4U, /*!< Set current pin interrupt is falling-edge sensitive.*/
44     kGPIO_IntRisingOrFallingEdge = 5U, /*!< Enable the edge select bit to override the ICR register's configuration.*/
45 } gpio_interrupt_mode_t;
46 
47 /*! @brief GPIO Init structure definition. */
48 typedef struct _gpio_pin_config
49 {
50     gpio_pin_direction_t direction; /*!< Specifies the pin direction. */
51     uint8_t outputLogic;            /*!< Set a default output logic, which has no use in input */
52     gpio_interrupt_mode_t
53         interruptMode; /*!< Specifies the pin interrupt mode, a value of @ref gpio_interrupt_mode_t. */
54 } gpio_pin_config_t;
55 
56 /*******************************************************************************
57  * API
58  ******************************************************************************/
59 
60 #if defined(__cplusplus)
61 extern "C" {
62 #endif
63 
64 /*!
65  * @name GPIO Initialization and Configuration functions
66  * @{
67  */
68 
69 /*!
70  * @brief Initializes the GPIO peripheral according to the specified
71  *        parameters in the initConfig.
72  *
73  * @param base GPIO base pointer.
74  * @param pin Specifies the pin number
75  * @param Config pointer to a @ref gpio_pin_config_t structure that
76  *        contains the configuration information.
77  */
78 void GPIO_PinInit(GPIO_Type *base, uint32_t pin, const gpio_pin_config_t *Config);
79 /*@}*/
80 
81 /*!
82  * @name GPIO Reads and Write Functions
83  * @{
84  */
85 
86 /*!
87  * @brief Sets the output level of the individual GPIO pin to logic 1 or 0.
88  *
89  * @param base GPIO base pointer.
90  * @param pin GPIO port pin number.
91  * @param output GPIOpin output logic level.
92  *        - 0: corresponding pin output low-logic level.
93  *        - 1: corresponding pin output high-logic level.
94  */
95 void GPIO_PinWrite(GPIO_Type *base, uint32_t pin, uint8_t output);
96 
97 /*!
98  * @brief Sets the output level of the individual GPIO pin to logic 1 or 0.
99  * @deprecated Do not use this function.  It has been superceded by @ref GPIO_PinWrite.
100  */
GPIO_WritePinOutput(GPIO_Type * base,uint32_t pin,uint8_t output)101 static inline void GPIO_WritePinOutput(GPIO_Type *base, uint32_t pin, uint8_t output)
102 {
103     GPIO_PinWrite(base, pin, output);
104 }
105 
106 /*!
107  * @brief Sets the output level of the multiple GPIO pins to the logic 1.
108  *
109  * @param base GPIO peripheral base pointer (GPIO1, GPIO2, GPIO3, and so on.)
110  * @param mask GPIO pin number macro
111  */
GPIO_PortSet(GPIO_Type * base,uint32_t mask)112 static inline void GPIO_PortSet(GPIO_Type *base, uint32_t mask)
113 {
114 #if (defined(FSL_FEATURE_IGPIO_HAS_DR_SET) && (FSL_FEATURE_IGPIO_HAS_DR_SET == 1))
115     base->DR_SET = mask;
116 #else
117     base->DR |= mask;
118 #endif /* FSL_FEATURE_IGPIO_HAS_DR_SET */
119 }
120 
121 /*!
122  * @brief Sets the output level of the multiple GPIO pins to the logic 1.
123  * @deprecated Do not use this function.  It has been superceded by @ref GPIO_PortSet.
124  */
GPIO_SetPinsOutput(GPIO_Type * base,uint32_t mask)125 static inline void GPIO_SetPinsOutput(GPIO_Type *base, uint32_t mask)
126 {
127     GPIO_PortSet(base, mask);
128 }
129 
130 /*!
131  * @brief Sets the output level of the multiple GPIO pins to the logic 0.
132  *
133  * @param base GPIO peripheral base pointer (GPIO1, GPIO2, GPIO3, and so on.)
134  * @param mask GPIO pin number macro
135  */
GPIO_PortClear(GPIO_Type * base,uint32_t mask)136 static inline void GPIO_PortClear(GPIO_Type *base, uint32_t mask)
137 {
138 #if (defined(FSL_FEATURE_IGPIO_HAS_DR_CLEAR) && (FSL_FEATURE_IGPIO_HAS_DR_CLEAR == 1))
139     base->DR_CLEAR = mask;
140 #else
141     base->DR &= ~mask;
142 #endif /* FSL_FEATURE_IGPIO_HAS_DR_CLEAR */
143 }
144 
145 /*!
146  * @brief Sets the output level of the multiple GPIO pins to the logic 0.
147  * @deprecated Do not use this function.  It has been superceded by @ref GPIO_PortClear.
148  */
GPIO_ClearPinsOutput(GPIO_Type * base,uint32_t mask)149 static inline void GPIO_ClearPinsOutput(GPIO_Type *base, uint32_t mask)
150 {
151     GPIO_PortClear(base, mask);
152 }
153 
154 /*!
155  * @brief Reverses the current output logic of the multiple GPIO pins.
156  *
157  * @param base GPIO peripheral base pointer (GPIO1, GPIO2, GPIO3, and so on.)
158  * @param mask GPIO pin number macro
159  */
GPIO_PortToggle(GPIO_Type * base,uint32_t mask)160 static inline void GPIO_PortToggle(GPIO_Type *base, uint32_t mask)
161 {
162 #if (defined(FSL_FEATURE_IGPIO_HAS_DR_TOGGLE) && (FSL_FEATURE_IGPIO_HAS_DR_TOGGLE == 1))
163     base->DR_TOGGLE = mask;
164 #else
165     base->DR ^= mask;
166 #endif /* FSL_FEATURE_IGPIO_HAS_DR_TOGGLE */
167 }
168 
169 /*!
170  * @brief Reads the current input value of the GPIO port.
171  *
172  * @param base GPIO base pointer.
173  * @param pin GPIO port pin number.
174  * @retval GPIO port input value.
175  */
GPIO_PinRead(GPIO_Type * base,uint32_t pin)176 static inline uint32_t GPIO_PinRead(GPIO_Type *base, uint32_t pin)
177 {
178     assert(pin < 32U);
179 
180     return (((base->DR) >> pin) & 0x1U);
181 }
182 
183 /*!
184  * @brief Reads the current input value of the GPIO port.
185  * @deprecated Do not use this function.  It has been superceded by @ref GPIO_PinRead.
186  */
GPIO_ReadPinInput(GPIO_Type * base,uint32_t pin)187 static inline uint32_t GPIO_ReadPinInput(GPIO_Type *base, uint32_t pin)
188 {
189     return GPIO_PinRead(base, pin);
190 }
191 /*@}*/
192 
193 /*!
194  * @name GPIO Reads Pad Status Functions
195  * @{
196  */
197 
198 /*!
199  * @brief Reads the current GPIO pin pad status.
200  *
201  * @param base GPIO base pointer.
202  * @param pin GPIO port pin number.
203  * @retval GPIO pin pad status value.
204  */
GPIO_PinReadPadStatus(GPIO_Type * base,uint32_t pin)205 static inline uint8_t GPIO_PinReadPadStatus(GPIO_Type *base, uint32_t pin)
206 {
207     assert(pin < 32U);
208 
209     return (uint8_t)(((base->PSR) >> pin) & 0x1U);
210 }
211 
212 /*!
213  * @brief Reads the current GPIO pin pad status.
214  * @deprecated Do not use this function.  It has been superceded by @ref GPIO_PinReadPadStatus.
215  */
GPIO_ReadPadStatus(GPIO_Type * base,uint32_t pin)216 static inline uint8_t GPIO_ReadPadStatus(GPIO_Type *base, uint32_t pin)
217 {
218     return GPIO_PinReadPadStatus(base, pin);
219 }
220 
221 /*@}*/
222 
223 /*!
224  * @name Interrupts and flags management functions
225  * @{
226  */
227 
228 /*!
229  * @brief Sets the current pin interrupt mode.
230  *
231  * @param base GPIO base pointer.
232  * @param pin GPIO port pin number.
233  * @param pinInterruptMode pointer to a @ref gpio_interrupt_mode_t structure
234  *        that contains the interrupt mode information.
235  */
236 void GPIO_PinSetInterruptConfig(GPIO_Type *base, uint32_t pin, gpio_interrupt_mode_t pinInterruptMode);
237 
238 /*!
239  * @brief Sets the current pin interrupt mode.
240  * @deprecated Do not use this function.  It has been superceded by @ref GPIO_PinSetInterruptConfig.
241  */
GPIO_SetPinInterruptConfig(GPIO_Type * base,uint32_t pin,gpio_interrupt_mode_t pinInterruptMode)242 static inline void GPIO_SetPinInterruptConfig(GPIO_Type *base, uint32_t pin, gpio_interrupt_mode_t pinInterruptMode)
243 {
244     GPIO_PinSetInterruptConfig(base, pin, pinInterruptMode);
245 }
246 
247 /*!
248  * @brief Enables the specific pin interrupt.
249  *
250  * @param base GPIO base pointer.
251  * @param mask GPIO pin number macro.
252  */
GPIO_PortEnableInterrupts(GPIO_Type * base,uint32_t mask)253 static inline void GPIO_PortEnableInterrupts(GPIO_Type *base, uint32_t mask)
254 {
255     base->IMR |= mask;
256 }
257 
258 /*!
259  * @brief Enables the specific pin interrupt.
260  *
261  * @param base GPIO base pointer.
262  * @param mask GPIO pin number macro.
263  */
GPIO_EnableInterrupts(GPIO_Type * base,uint32_t mask)264 static inline void GPIO_EnableInterrupts(GPIO_Type *base, uint32_t mask)
265 {
266     GPIO_PortEnableInterrupts(base, mask);
267 }
268 
269 /*!
270  * @brief Disables the specific pin interrupt.
271  *
272  * @param base GPIO base pointer.
273  * @param mask GPIO pin number macro.
274  */
GPIO_PortDisableInterrupts(GPIO_Type * base,uint32_t mask)275 static inline void GPIO_PortDisableInterrupts(GPIO_Type *base, uint32_t mask)
276 {
277     base->IMR &= ~mask;
278 }
279 
280 /*!
281  * @brief Disables the specific pin interrupt.
282  * @deprecated Do not use this function.  It has been superceded by @ref GPIO_PortDisableInterrupts.
283  */
GPIO_DisableInterrupts(GPIO_Type * base,uint32_t mask)284 static inline void GPIO_DisableInterrupts(GPIO_Type *base, uint32_t mask)
285 {
286     GPIO_PortDisableInterrupts(base, mask);
287 }
288 
289 /*!
290  * @brief Reads individual pin interrupt status.
291  *
292  * @param base GPIO base pointer.
293  * @retval current pin interrupt status flag.
294  */
GPIO_PortGetInterruptFlags(GPIO_Type * base)295 static inline uint32_t GPIO_PortGetInterruptFlags(GPIO_Type *base)
296 {
297     return base->ISR;
298 }
299 
300 /*!
301  * @brief Reads individual pin interrupt status.
302  *
303  * @param base GPIO base pointer.
304  * @retval current pin interrupt status flag.
305  */
GPIO_GetPinsInterruptFlags(GPIO_Type * base)306 static inline uint32_t GPIO_GetPinsInterruptFlags(GPIO_Type *base)
307 {
308     return GPIO_PortGetInterruptFlags(base);
309 }
310 
311 /*!
312  * @brief Clears pin interrupt flag. Status flags are cleared by
313  *        writing a 1 to the corresponding bit position.
314  *
315  * @param base GPIO base pointer.
316  * @param mask GPIO pin number macro.
317  */
GPIO_PortClearInterruptFlags(GPIO_Type * base,uint32_t mask)318 static inline void GPIO_PortClearInterruptFlags(GPIO_Type *base, uint32_t mask)
319 {
320     base->ISR = mask;
321 }
322 
323 /*!
324  * @brief Clears pin interrupt flag. Status flags are cleared by
325  *        writing a 1 to the corresponding bit position.
326  *
327  * @param base GPIO base pointer.
328  * @param mask GPIO pin number macro.
329  */
GPIO_ClearPinsInterruptFlags(GPIO_Type * base,uint32_t mask)330 static inline void GPIO_ClearPinsInterruptFlags(GPIO_Type *base, uint32_t mask)
331 {
332     GPIO_PortClearInterruptFlags(base, mask);
333 }
334 /*@}*/
335 
336 #if defined(__cplusplus)
337 }
338 #endif
339 
340 /*!
341  * @}
342  */
343 
344 #endif /* _FSL_GPIO_H_*/
345