fsl_gpio.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  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 the copyright holder 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. #ifndef _FSL_GPIO_H_
  31. #define _FSL_GPIO_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup gpio_driver
  35. * @{
  36. */
  37. /*******************************************************************************
  38. * Definitions
  39. ******************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. /*! @brief GPIO driver version 2.0.0. */
  43. #define FSL_GPIO_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
  44. /*@}*/
  45. /*! @brief GPIO direction definition. */
  46. typedef enum _gpio_pin_direction
  47. {
  48. kGPIO_DigitalInput = 0U, /*!< Set current pin as digital input.*/
  49. kGPIO_DigitalOutput = 1U, /*!< Set current pin as digital output.*/
  50. } gpio_pin_direction_t;
  51. /*! @brief GPIO interrupt mode definition. */
  52. typedef enum _gpio_interrupt_mode
  53. {
  54. kGPIO_NoIntmode = 0U, /*!< Set current pin general IO functionality.*/
  55. kGPIO_IntLowLevel = 1U, /*!< Set current pin interrupt is low-level sensitive.*/
  56. kGPIO_IntHighLevel = 2U, /*!< Set current pin interrupt is high-level sensitive.*/
  57. kGPIO_IntRisingEdge = 3U, /*!< Set current pin interrupt is rising-edge sensitive.*/
  58. kGPIO_IntFallingEdge = 4U, /*!< Set current pin interrupt is falling-edge sensitive.*/
  59. kGPIO_IntRisingOrFallingEdge = 5U, /*!< Enable the edge select bit to override the ICR register's configuration.*/
  60. } gpio_interrupt_mode_t;
  61. /*! @brief GPIO Init structure definition. */
  62. typedef struct _gpio_pin_config
  63. {
  64. gpio_pin_direction_t direction; /*!< Specifies the pin direction. */
  65. uint8_t outputLogic; /*!< Set a default output logic, which has no use in input */
  66. gpio_interrupt_mode_t interruptMode; /*!< Specifies the pin interrupt mode, a value of @ref gpio_interrupt_mode_t. */
  67. } gpio_pin_config_t;
  68. /*******************************************************************************
  69. * API
  70. ******************************************************************************/
  71. #if defined(__cplusplus)
  72. extern "C" {
  73. #endif
  74. /*!
  75. * @name GPIO Initialization and Configuration functions
  76. * @{
  77. */
  78. /*!
  79. * @brief Initializes the GPIO peripheral according to the specified
  80. * parameters in the initConfig.
  81. *
  82. * @param base GPIO base pointer.
  83. * @param pin Specifies the pin number
  84. * @param initConfig pointer to a @ref gpio_pin_config_t structure that
  85. * contains the configuration information.
  86. */
  87. void GPIO_PinInit(GPIO_Type* base, uint32_t pin, const gpio_pin_config_t* Config);
  88. /*@}*/
  89. /*!
  90. * @name GPIO Reads and Write Functions
  91. * @{
  92. */
  93. /*!
  94. * @brief Sets the output level of the individual GPIO pin to logic 1 or 0.
  95. *
  96. * @param base GPIO base pointer.
  97. * @param pin GPIO port pin number.
  98. * @param output GPIOpin output logic level.
  99. * - 0: corresponding pin output low-logic level.
  100. * - 1: corresponding pin output high-logic level. */
  101. void GPIO_WritePinOutput(GPIO_Type* base, uint32_t pin, uint8_t output);
  102. /*!
  103. * @brief Sets the output level of the multiple GPIO pins to the logic 1.
  104. *
  105. * @param base GPIO peripheral base pointer (GPIO1, GPIO2, GPIO3, and so on.)
  106. * @param mask GPIO pin number macro
  107. */
  108. static inline void GPIO_SetPinsOutput(GPIO_Type* base, uint32_t mask)
  109. {
  110. base->DR |= mask;
  111. }
  112. /*!
  113. * @brief Sets the output level of the multiple GPIO pins to the logic 0.
  114. *
  115. * @param base GPIO peripheral base pointer (GPIO1, GPIO2, GPIO3, and so on.)
  116. * @param mask GPIO pin number macro
  117. */
  118. static inline void GPIO_ClearPinsOutput(GPIO_Type* base, uint32_t mask)
  119. {
  120. base->DR &= ~mask;
  121. }
  122. /*!
  123. * @brief Reads the current input value of the GPIO port.
  124. *
  125. * @param base GPIO base pointer.
  126. * @param pin GPIO port pin number.
  127. * @retval GPIO port input value.
  128. */
  129. static inline uint32_t GPIO_ReadPinInput(GPIO_Type* base, uint32_t pin)
  130. {
  131. assert(pin < 32);
  132. return (((base->DR) >> pin) & 0x1U);
  133. }
  134. /*@}*/
  135. /*!
  136. * @name GPIO Reads Pad Status Functions
  137. * @{
  138. */
  139. /*!
  140. * @brief Reads the current GPIO pin pad status.
  141. *
  142. * @param base GPIO base pointer.
  143. * @param pin GPIO port pin number.
  144. * @retval GPIO pin pad status value.
  145. */
  146. static inline uint8_t GPIO_ReadPadStatus(GPIO_Type* base, uint32_t pin)
  147. {
  148. assert(pin < 32);
  149. return (uint8_t)(((base->PSR) >> pin) & 0x1U);
  150. }
  151. /*@}*/
  152. /*!
  153. * @name Interrupts and flags management functions
  154. * @{
  155. */
  156. /*!
  157. * @brief Sets the current pin interrupt mode.
  158. *
  159. * @param base GPIO base pointer.
  160. * @param pin GPIO port pin number.
  161. * @param pininterruptMode pointer to a @ref gpio_interrupt_mode_t structure
  162. * that contains the interrupt mode information.
  163. */
  164. void GPIO_SetPinInterruptConfig(GPIO_Type* base, uint32_t pin, gpio_interrupt_mode_t pinInterruptMode);
  165. /*!
  166. * @brief Enables the specific pin interrupt.
  167. *
  168. * @param base GPIO base pointer.
  169. * @param mask GPIO pin number macro.
  170. */
  171. static inline void GPIO_EnableInterrupts(GPIO_Type* base, uint32_t mask)
  172. {
  173. base->IMR |= mask;
  174. }
  175. /*!
  176. * @brief Disables the specific pin interrupt.
  177. *
  178. * @param base GPIO base pointer.
  179. * @param mask GPIO pin number macro.
  180. */
  181. static inline void GPIO_DisableInterrupts(GPIO_Type* base, uint32_t mask)
  182. {
  183. base->IMR &= ~mask;
  184. }
  185. /*!
  186. * @brief Reads individual pin interrupt status.
  187. *
  188. * @param base GPIO base pointer.
  189. * @retval current pin interrupt status flag.
  190. */
  191. static inline uint32_t GPIO_GetPinsInterruptFlags(GPIO_Type* base)
  192. {
  193. return base->ISR;
  194. }
  195. /*!
  196. * @brief Clears pin interrupt flag. Status flags are cleared by
  197. * writing a 1 to the corresponding bit position.
  198. *
  199. * @param base GPIO base pointer.
  200. * @param mask GPIO pin number macro.
  201. */
  202. static inline void GPIO_ClearPinsInterruptFlags(GPIO_Type* base, uint32_t mask)
  203. {
  204. base->ISR = mask;
  205. }
  206. /*@}*/
  207. #if defined(__cplusplus)
  208. }
  209. #endif
  210. /*!
  211. * @}
  212. */
  213. #endif /* _FSL_GPIO_H_*/