fsl_gpio.h 9.3 KB

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