fsl_gpio.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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.1. */
  43. #define FSL_GPIO_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
  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. */
  102. void GPIO_PinWrite(GPIO_Type* base, uint32_t pin, uint8_t output);
  103. /*!
  104. * @brief Sets the output level of the individual GPIO pin to logic 1 or 0.
  105. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PinWrite.
  106. */
  107. static inline void GPIO_WritePinOutput(GPIO_Type* base, uint32_t pin, uint8_t output)
  108. {
  109. GPIO_PinWrite(base, pin, output);
  110. }
  111. /*!
  112. * @brief Sets the output level of the multiple GPIO pins to the logic 1.
  113. *
  114. * @param base GPIO peripheral base pointer (GPIO1, GPIO2, GPIO3, and so on.)
  115. * @param mask GPIO pin number macro
  116. */
  117. static inline void GPIO_PortSet(GPIO_Type* base, uint32_t mask)
  118. {
  119. base->DR |= mask;
  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. */
  125. static inline void GPIO_SetPinsOutput(GPIO_Type* base, uint32_t mask)
  126. {
  127. GPIO_PortSet(base, mask);
  128. }
  129. /*!
  130. * @brief Sets the output level of the multiple GPIO pins to the logic 0.
  131. *
  132. * @param base GPIO peripheral base pointer (GPIO1, GPIO2, GPIO3, and so on.)
  133. * @param mask GPIO pin number macro
  134. */
  135. static inline void GPIO_PortClear(GPIO_Type* base, uint32_t mask)
  136. {
  137. base->DR &= ~mask;
  138. }
  139. /*!
  140. * @brief Sets the output level of the multiple GPIO pins to the logic 0.
  141. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PortClear.
  142. */
  143. static inline void GPIO_ClearPinsOutput(GPIO_Type* base, uint32_t mask)
  144. {
  145. GPIO_PortClear(base, mask);
  146. }
  147. /*!
  148. * @brief Reads the current input value of the GPIO port.
  149. *
  150. * @param base GPIO base pointer.
  151. * @param pin GPIO port pin number.
  152. * @retval GPIO port input value.
  153. */
  154. static inline uint32_t GPIO_PinRead(GPIO_Type* base, uint32_t pin)
  155. {
  156. assert(pin < 32);
  157. return (((base->DR) >> pin) & 0x1U);
  158. }
  159. /*!
  160. * @brief Reads the current input value of the GPIO port.
  161. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PinRead.
  162. */
  163. static inline uint32_t GPIO_ReadPinInput(GPIO_Type* base, uint32_t pin)
  164. {
  165. return GPIO_PinRead(base, pin);
  166. }
  167. /*@}*/
  168. /*!
  169. * @name GPIO Reads Pad Status Functions
  170. * @{
  171. */
  172. /*!
  173. * @brief Reads the current GPIO pin pad status.
  174. *
  175. * @param base GPIO base pointer.
  176. * @param pin GPIO port pin number.
  177. * @retval GPIO pin pad status value.
  178. */
  179. static inline uint8_t GPIO_PinReadPadStatus(GPIO_Type* base, uint32_t pin)
  180. {
  181. assert(pin < 32);
  182. return (uint8_t)(((base->PSR) >> pin) & 0x1U);
  183. }
  184. /*!
  185. * @brief Reads the current GPIO pin pad status.
  186. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PinReadPadStatus.
  187. */
  188. static inline uint8_t GPIO_ReadPadStatus(GPIO_Type* base, uint32_t pin)
  189. {
  190. return GPIO_PinReadPadStatus(base, pin);
  191. }
  192. /*@}*/
  193. /*!
  194. * @name Interrupts and flags management functions
  195. * @{
  196. */
  197. /*!
  198. * @brief Sets the current pin interrupt mode.
  199. *
  200. * @param base GPIO base pointer.
  201. * @param pin GPIO port pin number.
  202. * @param pininterruptMode pointer to a @ref gpio_interrupt_mode_t structure
  203. * that contains the interrupt mode information.
  204. */
  205. void GPIO_PinSetInterruptConfig(GPIO_Type* base, uint32_t pin, gpio_interrupt_mode_t pinInterruptMode);
  206. /*!
  207. * @brief Sets the current pin interrupt mode.
  208. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PinSetInterruptConfig.
  209. */
  210. static inline void GPIO_SetPinInterruptConfig(GPIO_Type* base, uint32_t pin, gpio_interrupt_mode_t pinInterruptMode)
  211. {
  212. GPIO_PinSetInterruptConfig(base, pin, pinInterruptMode);
  213. }
  214. /*!
  215. * @brief Enables the specific pin interrupt.
  216. *
  217. * @param base GPIO base pointer.
  218. * @param mask GPIO pin number macro.
  219. */
  220. static inline void GPIO_PortEnableInterrupts(GPIO_Type* base, uint32_t mask)
  221. {
  222. base->IMR |= mask;
  223. }
  224. /*!
  225. * @brief Enables the specific pin interrupt.
  226. *
  227. * @param base GPIO base pointer.
  228. * @param mask GPIO pin number macro.
  229. */
  230. static inline void GPIO_EnableInterrupts(GPIO_Type* base, uint32_t mask)
  231. {
  232. GPIO_PortEnableInterrupts(base, mask);
  233. }
  234. /*!
  235. * @brief Disables the specific pin interrupt.
  236. *
  237. * @param base GPIO base pointer.
  238. * @param mask GPIO pin number macro.
  239. */
  240. static inline void GPIO_PortDisableInterrupts(GPIO_Type* base, uint32_t mask)
  241. {
  242. base->IMR &= ~mask;
  243. }
  244. /*!
  245. * @brief Disables the specific pin interrupt.
  246. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PortDisableInterrupts.
  247. */
  248. static inline void GPIO_DisableInterrupts(GPIO_Type* base, uint32_t mask)
  249. {
  250. GPIO_PortDisableInterrupts(base, mask);
  251. }
  252. /*!
  253. * @brief Reads individual pin interrupt status.
  254. *
  255. * @param base GPIO base pointer.
  256. * @retval current pin interrupt status flag.
  257. */
  258. static inline uint32_t GPIO_PortGetInterruptFlags(GPIO_Type* base)
  259. {
  260. return base->ISR;
  261. }
  262. /*!
  263. * @brief Reads individual pin interrupt status.
  264. *
  265. * @param base GPIO base pointer.
  266. * @retval current pin interrupt status flag.
  267. */
  268. static inline uint32_t GPIO_GetPinsInterruptFlags(GPIO_Type* base)
  269. {
  270. return GPIO_PortGetInterruptFlags(base);
  271. }
  272. /*!
  273. * @brief Clears pin interrupt flag. Status flags are cleared by
  274. * writing a 1 to the corresponding bit position.
  275. *
  276. * @param base GPIO base pointer.
  277. * @param mask GPIO pin number macro.
  278. */
  279. static inline void GPIO_PortClearInterruptFlags(GPIO_Type* base, uint32_t mask)
  280. {
  281. base->ISR = mask;
  282. }
  283. /*!
  284. * @brief Clears pin interrupt flag. Status flags are cleared by
  285. * writing a 1 to the corresponding bit position.
  286. *
  287. * @param base GPIO base pointer.
  288. * @param mask GPIO pin number macro.
  289. */
  290. static inline void GPIO_ClearPinsInterruptFlags(GPIO_Type* base, uint32_t mask)
  291. {
  292. GPIO_PortClearInterruptFlags(base, mask);
  293. }
  294. /*@}*/
  295. #if defined(__cplusplus)
  296. }
  297. #endif
  298. /*!
  299. * @}
  300. */
  301. #endif /* _FSL_GPIO_H_*/