1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
10 *
11 * o Redistributions in binary form must reproduce the above copyright notice, this
12 * list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #ifndef __GPIO_IMX_H__
32 #define __GPIO_IMX_H__
33
34 #include <stdint.h>
35 #include <stdbool.h>
36 #include <assert.h>
37 #include "device_imx.h"
38
39 /*!
40 * @addtogroup gpio_driver
41 * @{
42 */
43
44 /*******************************************************************************
45 * Definitions
46 ******************************************************************************/
47
48 /*! @brief GPIO direction definition. */
49 typedef enum _gpio_pin_direction
50 {
51 gpioDigitalInput = 0U, /*!< Set current pin as digital input.*/
52 gpioDigitalOutput = 1U, /*!< Set current pin as digital output.*/
53 } gpio_pin_direction_t;
54
55 /*! @brief GPIO interrupt mode definition. */
56 typedef enum _gpio_interrupt_mode
57 {
58 gpioIntLowLevel = 0U, /*!< Set current pin interrupt is low-level sensitive.*/
59 gpioIntHighLevel = 1U, /*!< Set current pin interrupt is high-level sensitive.*/
60 gpioIntRisingEdge = 2U, /*!< Set current pin interrupt is rising-edge sensitive.*/
61 gpioIntFallingEdge = 3U, /*!< Set current pin interrupt is falling-edge sensitive.*/
62 gpioNoIntmode = 4U, /*!< Set current pin general IO functionality. */
63 } gpio_interrupt_mode_t;
64
65 /*! @brief GPIO pin(bit) value definition. */
66 typedef enum _gpio_pin_action
67 {
68 gpioPinClear = 0U, /*!< Clear GPIO Pin.*/
69 gpioPinSet = 1U, /*!< Set GPIO Pin.*/
70 } gpio_pin_action_t;
71
72 /*! @brief GPIO Init structure definition. */
73 typedef struct _gpio_init_config
74 {
75 uint32_t pin; /*!< Specifies the pin number. */
76 gpio_pin_direction_t direction; /*!< Specifies the pin direction. */
77 gpio_interrupt_mode_t interruptMode; /*!< Specifies the pin interrupt mode, a value of @ref gpio_interrupt_mode_t. */
78 } gpio_init_config_t;
79
80 /*******************************************************************************
81 * API
82 ******************************************************************************/
83
84 #if defined(__cplusplus)
85 extern "C" {
86 #endif
87
88 /*!
89 * @name GPIO Initialization and Configuration functions
90 * @{
91 */
92
93 /*!
94 * @brief Initializes the GPIO peripheral according to the specified
95 * parameters in the initConfig.
96 *
97 * @param base GPIO base pointer.
98 * @param initConfig pointer to a @ref gpio_init_config_t structure that
99 * contains the configuration information.
100 */
101 void GPIO_Init(GPIO_Type* base, const gpio_init_config_t* initConfig);
102
103 /*@}*/
104
105 /*!
106 * @name GPIO Read and Write Functions
107 * @{
108 */
109
110 /*!
111 * @brief Reads the current input value of the pin when pin's direction is configured as input.
112 *
113 * @param base GPIO base pointer.
114 * @param pin GPIO port pin number.
115 * @return GPIO pin input value.
116 */
GPIO_ReadPinInput(GPIO_Type * base,uint32_t pin)117 static inline uint8_t GPIO_ReadPinInput(GPIO_Type* base, uint32_t pin)
118 {
119 assert(pin < 32);
120
121 return (uint8_t)((GPIO_DR_REG(base) >> pin) & 1U);
122 }
123
124 /*!
125 * @brief Reads the current input value of a specific GPIO port when port's direction are all configured as input.
126 * This function gets all 32-pin input as a 32-bit integer.
127 *
128 * @param base GPIO base pointer.
129 * @return GPIO port input data.
130 */
GPIO_ReadPortInput(GPIO_Type * base)131 static inline uint32_t GPIO_ReadPortInput(GPIO_Type* base)
132 {
133 return GPIO_DR_REG(base);
134 }
135
136 /*!
137 * @brief Reads the current pin output.
138 *
139 * @param base GPIO base pointer.
140 * @param pin GPIO port pin number.
141 * @return Current pin output value.
142 */
GPIO_ReadPinOutput(GPIO_Type * base,uint32_t pin)143 static inline uint8_t GPIO_ReadPinOutput(GPIO_Type* base, uint32_t pin)
144 {
145 assert(pin < 32);
146
147 return (uint8_t)((GPIO_DR_REG(base) >> pin) & 0x1U);
148 }
149
150 /*!
151 * @brief Reads out all pin output status of the current port.
152 * This function operates all 32 port pins.
153 *
154 * @param base GPIO base pointer.
155 * @return Current port output status.
156 */
GPIO_ReadPortOutput(GPIO_Type * base)157 static inline uint32_t GPIO_ReadPortOutput(GPIO_Type* base)
158 {
159 return GPIO_DR_REG(base);
160 }
161
162 /*!
163 * @brief Sets the output level of the individual GPIO pin to logic 1 or 0.
164 *
165 * @param base GPIO base pointer.
166 * @param pin GPIO port pin number.
167 * @param pinVal pin output value (See @ref gpio_pin_action_t structure).
168 */
169 void GPIO_WritePinOutput(GPIO_Type* base, uint32_t pin, gpio_pin_action_t pinVal);
170
171 /*!
172 * @brief Sets the output of the GPIO port pins to a specific logic value.
173 * This function operates all 32 port pins.
174 *
175 * @param base GPIO base pointer.
176 * @param portVal data to configure the GPIO output.
177 */
GPIO_WritePortOutput(GPIO_Type * base,uint32_t portVal)178 static inline void GPIO_WritePortOutput(GPIO_Type* base, uint32_t portVal)
179 {
180 GPIO_DR_REG(base) = portVal;
181 }
182
183 /*@}*/
184
185 /*!
186 * @name GPIO Read Pad Status Functions
187 * @{
188 */
189
190 /*!
191 * @brief Reads the current GPIO pin pad status.
192 *
193 * @param base GPIO base pointer.
194 * @param pin GPIO port pin number.
195 * @return GPIO pin pad status value.
196 */
GPIO_ReadPadStatus(GPIO_Type * base,uint32_t pin)197 static inline uint8_t GPIO_ReadPadStatus(GPIO_Type* base, uint32_t pin)
198 {
199 assert(pin < 32);
200
201 return (uint8_t)((GPIO_PSR_REG(base) >> pin) & 1U);
202 }
203
204 /*@}*/
205
206 /*!
207 * @name Interrupts and flags management functions
208 * @{
209 */
210
211 /*!
212 * @brief Enable or Disable the specific pin interrupt.
213 *
214 * @param base GPIO base pointer.
215 * @param pin GPIO pin number.
216 * @param enable Enable or disable interrupt.
217 * - true: Enable GPIO interrupt.
218 * - false: Disable GPIO interrupt.
219 */
220 void GPIO_SetPinIntMode(GPIO_Type* base, uint32_t pin, bool enable);
221
222 /*!
223 * @brief Check individual pin interrupt status.
224 *
225 * @param base GPIO base pointer.
226 * @param pin GPIO port pin number.
227 * @return current pin interrupt status flag.
228 */
GPIO_IsIntPending(GPIO_Type * base,uint32_t pin)229 static inline bool GPIO_IsIntPending(GPIO_Type* base, uint32_t pin)
230 {
231 assert(pin < 32);
232
233 return (bool)((GPIO_ISR_REG(base) >> pin) & 1U);
234 }
235
236 /*!
237 * @brief Clear pin interrupt flag. Status flags are cleared by
238 * writing a 1 to the corresponding bit position.
239 *
240 * @param base GPIO base pointer.
241 * @param pin GPIO port pin number.
242 */
GPIO_ClearStatusFlag(GPIO_Type * base,uint32_t pin)243 static inline void GPIO_ClearStatusFlag(GPIO_Type* base, uint32_t pin)
244 {
245 assert(pin < 32);
246
247 GPIO_ISR_REG(base) = (1U << pin);
248 }
249
250 /*!
251 * @brief Enable or disable the edge select bit to override
252 * the ICR register's configuration.
253 *
254 * @param base GPIO base pointer.
255 * @param pin GPIO port pin number.
256 * @param enable Enable or disable edge select bit.
257 */
258 void GPIO_SetIntEdgeSelect(GPIO_Type* base, uint32_t pin, bool enable);
259
260 /*@}*/
261
262 #if defined(__cplusplus)
263 }
264 #endif
265
266 /*! @} */
267
268 #endif /* __GPIO_IMX_H__*/
269
270 /*******************************************************************************
271 * EOF
272 ******************************************************************************/
273