2 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
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.
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.
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.
30 #ifndef __FSL_GPIO_HAL_H__
31 #define __FSL_GPIO_HAL_H__
36 #include "fsl_gpio_features.h"
37 #include "fsl_device_registers.h"
40 * @addtogroup gpio_hal
45 * @file fsl_gpio_hal.h
47 * @brief GPIO hardware driver configuration. Use these functions to set the GPIO input/output,
48 * set output logic or get input logic. Check the GPIO header file for base address. Each
49 * GPIO instance has 32 pins with numbers from 0 to 31.
52 /*******************************************************************************
54 ******************************************************************************/
56 /*! @brief GPIO direction definition*/
57 typedef enum _gpio_pin_direction {
58 kGpioDigitalInput = 0, /*!< Set current pin as digital input*/
59 kGpioDigitalOutput = 1 /*!< Set current pin as digital output*/
60 } gpio_pin_direction_t;
62 /*******************************************************************************
64 ******************************************************************************/
66 #if defined(__cplusplus)
76 * @brief Sets the individual GPIO pin to general input or output.
78 * @param baseAddr GPIO base address(HW_GPIOA, HW_GPIOB, HW_GPIOC, etc.)
79 * @param pin GPIO port pin number
80 * @param direction GPIO directions
81 * - kGpioDigitalInput: set to input
82 * - kGpioDigitalOutput: set to output
84 void GPIO_HAL_SetPinDir(uint32_t baseAddr, uint32_t pin,
85 gpio_pin_direction_t direction);
88 * @brief Sets the GPIO port pins to general input or output.
90 * This function operates all 32 port pins.
92 * @param baseAddr GPIO base address (HW_GPIOA, HW_GPIOB, HW_GPIOC, etc.)
93 * @param direction GPIO directions
99 static inline void GPIO_HAL_SetPortDir(uint32_t baseAddr, uint32_t direction)
101 HW_GPIO_PDDR_WR(baseAddr, direction);
112 * @brief Gets the current direction of the individual GPIO pin.
114 * @param baseAddr GPIO base address(HW_GPIOA, HW_GPIOB, HW_GPIOC, etc.)
115 * @param pin GPIO port pin number
116 * @return GPIO directions
117 * - kGpioDigitalInput: corresponding pin is set to input.
118 * - kGpioDigitalOutput: corresponding pin is set to output.
120 static inline gpio_pin_direction_t GPIO_HAL_GetPinDir(uint32_t baseAddr, uint32_t pin)
123 return (gpio_pin_direction_t)((HW_GPIO_PDDR_RD(baseAddr) >> pin) & 1U);
127 * @brief Gets the GPIO port pins direction.
129 * This function gets all 32-pin directions as a 32-bit integer.
131 * @param baseAddr GPIO base address (HW_GPIOA, HW_GPIOB, HW_GPIOC, etc.)
132 * @return GPIO directions. Each bit represents one pin. For each bit:
133 * - 0: corresponding pin is set to input
134 * - 1: corresponding pin is set to output
138 static inline uint32_t GPIO_HAL_GetPortDir(uint32_t baseAddr)
140 return HW_GPIO_PDDR_RD(baseAddr);
146 * @name Output Operation
151 * @brief Sets the output level of the individual GPIO pin to logic 1 or 0.
153 * @param baseAddr GPIO base address(HW_GPIOA, HW_GPIOB, HW_GPIOC, etc.)
154 * @param pin GPIO port pin number
155 * @param output pin output logic level
157 void GPIO_HAL_WritePinOutput(uint32_t baseAddr, uint32_t pin, uint32_t output);
160 * @brief Reads the current pin output.
162 * @param baseAddr GPIO base address (HW_GPIOA, HW_GPIOB, HW_GPIOC, etc.)
163 * @param pin GPIO port pin number
164 * @return current pin output status. 0 - Low logic, 1 - High logic
166 static inline uint32_t GPIO_HAL_ReadPinOutput(uint32_t baseAddr, uint32_t pin)
169 return ((HW_GPIO_PDOR_RD(baseAddr) >> pin) & 0x1U);
173 * @brief Sets the output level of the individual GPIO pin to logic 1.
175 * @param baseAddr GPIO base address(HW_GPIOA, HW_GPIOB, HW_GPIOC, etc.)
176 * @param pin GPIO port pin number
178 static inline void GPIO_HAL_SetPinOutput(uint32_t baseAddr, uint32_t pin)
181 HW_GPIO_PSOR_WR(baseAddr, 1U << pin);
185 * @brief Clears the output level of the individual GPIO pin to logic 0.
187 * @param baseAddr GPIO base address(HW_GPIOA, HW_GPIOB, HW_GPIOC, etc.)
188 * @param pin GPIO port pin number
190 static inline void GPIO_HAL_ClearPinOutput(uint32_t baseAddr, uint32_t pin)
193 HW_GPIO_PCOR_WR(baseAddr, 1U << pin);
197 * @brief Reverses the current output logic of the individual GPIO pin.
199 * @param baseAddr GPIO base address(HW_GPIOA, HW_GPIOB, HW_GPIOC, etc.)
200 * @param pin GPIO port pin number
202 static inline void GPIO_HAL_TogglePinOutput(uint32_t baseAddr, uint32_t pin)
205 HW_GPIO_PTOR_WR(baseAddr, 1U << pin);
209 * @brief Sets the output of the GPIO port to a specific logic value.
211 * This function operates all 32 port pins.
213 * @param baseAddr GPIO base address (HW_GPIOA, HW_GPIOB, HW_GPIOC, etc.)
214 * @param portOutput data to configure the GPIO output. Each bit represents one pin. For each bit:
215 * - 0: set logic level 0 to pin
216 * - 1: set logic level 1 to pin
220 static inline void GPIO_HAL_WritePortOutput(uint32_t baseAddr, uint32_t portOutput)
222 HW_GPIO_PDOR_WR(baseAddr, portOutput);
226 * @brief Reads out all pin output status of the current port.
228 * This function operates all 32 port pins.
230 * @param baseAddr GPIO base address (HW_GPIOA, HW_GPIOB, HW_GPIOC, etc.)
231 * @return current port output status. Each bit represents one pin. For each bit:
232 * - 0: corresponding pin is outputting logic level 0
233 * - 1: corresponding pin is outputting logic level 1
237 static inline uint32_t GPIO_HAL_ReadPortOutput(uint32_t baseAddr)
239 return HW_GPIO_PDOR_RD(baseAddr);
245 * @name Input Operation
250 * @brief Reads the current input value of the individual GPIO pin.
252 * @param baseAddr GPIO base address(HW_GPIOA, HW_GPIOB, HW_GPIOC, etc.)
253 * @param pin GPIO port pin number
254 * @return GPIO port input value
255 * - 0: Pin logic level is 0, or is not configured for use by digital function.
256 * - 1: Pin logic level is 1
258 static inline uint32_t GPIO_HAL_ReadPinInput(uint32_t baseAddr, uint32_t pin)
261 return (HW_GPIO_PDIR_RD(baseAddr) >> pin) & 1U;
265 * @brief Reads the current input value of a specific GPIO port.
267 * This function gets all 32-pin input as a 32-bit integer.
269 * @param baseAddr GPIO base address(HW_GPIOA, HW_GPIOB, HW_GPIOC, etc.)
270 * @return GPIO port input data. Each bit represents one pin. For each bit:
271 * - 0: Pin logic level is 0, or is not configured for use by digital function.
272 * - 1: Pin logic level is 1.
276 static inline uint32_t GPIO_HAL_ReadPortInput(uint32_t baseAddr)
278 return HW_GPIO_PDIR_RD(baseAddr);
284 * @name FGPIO Operation
286 * @note FGPIO (Fast GPIO) is only available in a few MCUs. FGPIO and GPIO share the same
287 * peripheral but use different registers. FGPIO is closer to the core than the regular GPIO
288 * and it's faster to read and write.
292 #if FSL_FEATURE_GPIO_HAS_FAST_GPIO
295 * @name Output Operation
300 * @brief Sets the output level of an individual FGPIO pin to logic 1.
302 * @param baseAddr GPIO base address(HW_FPTA, HW_FPTB, HW_FPTC, etc.)
303 * @param pin FGPIO port pin number
305 static inline void FGPIO_HAL_SetPinOutput(uint32_t baseAddr, uint32_t pin)
308 HW_FGPIO_PSOR_WR(baseAddr, 1U << pin);
312 * @brief Clears the output level of an individual FGPIO pin to logic 0.
314 * @param baseAddr GPIO base address(HW_FPTA, HW_FPTB, HW_FPTC, etc.)
315 * @param pin FGPIO port pin number
317 static inline void FGPIO_HAL_ClearPinOutput(uint32_t baseAddr, uint32_t pin)
320 HW_FGPIO_PCOR_WR(baseAddr, 1U << pin);
324 * @brief Reverses the current output logic of an individual FGPIO pin.
326 * @param baseAddr GPIO base address(HW_FPTA, HW_FPTB, HW_FPTC, etc.)
327 * @param pin FGPIO port pin number
329 static inline void FGPIO_HAL_TogglePinOutput(uint32_t baseAddr, uint32_t pin)
332 HW_FGPIO_PTOR_WR(baseAddr, 1U << pin);
336 * @brief Sets the output of the FGPIO port to a specific logic value.
338 * This function affects all 32 port pins.
340 * @param baseAddr GPIO base address(HW_FPTA, HW_FPTB, HW_FPTC, etc.)
341 * @param portOutput data to configure the GPIO output. Each bit represents one pin. For each bit:
342 * - 0: set logic level 0 to pin.
343 * - 1: set logic level 1 to pin.
347 static inline void FGPIO_HAL_WritePortOutput(uint32_t baseAddr, uint32_t portOutput)
349 HW_FGPIO_PDOR_WR(baseAddr, portOutput);
355 * @name Input Operation
360 * @brief Gets the current input value of an individual FGPIO pin.
362 * @param baseAddr GPIO base address(HW_FPTA, HW_FPTB, HW_FPTC, etc.)
363 * @param pin FGPIO port pin number
364 * @return FGPIO port input data
365 * - 0: Pin logic level is 0, or is not configured for use by digital function.
366 * - 1: Pin logic level is 1.
368 static inline uint32_t FGPIO_HAL_ReadPinInput(uint32_t baseAddr, uint32_t pin)
371 return (HW_FGPIO_PDIR_RD(baseAddr) >> pin) & 1U;
375 * @brief Gets the current input value of a specific FGPIO port.
377 * This function gets all 32-pin input as a 32-bit integer.
379 * @param baseAddr GPIO base address(HW_FPTA, HW_FPTB, HW_FPTC, etc.).
380 * @return FGPIO port input data. Each bit represents one pin. For each bit:
381 * - 0: Pin logic level is 0, or is not configured for use by digital function.
382 * - 1: Pin logic level is 1.
386 static inline uint32_t FGPIO_HAL_ReadPortInput(uint32_t baseAddr)
388 return HW_FGPIO_PDIR_RD(baseAddr);
393 #endif /* FSL_FEATURE_GPIO_HAS_FAST_GPIO*/
395 #if defined(__cplusplus)
401 #endif /* __FSL_GPIO_HAL_H__*/
402 /*******************************************************************************
404 ******************************************************************************/