fsl_gpio.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef _FSL_GPIO_H_
  35. #define _FSL_GPIO_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup gpio_driver
  39. * @{
  40. */
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. /*! @name Driver version */
  45. /*@{*/
  46. /*! @brief GPIO driver version 2.0.1. */
  47. #define FSL_GPIO_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
  48. /*@}*/
  49. /*! @brief GPIO direction definition. */
  50. typedef enum _gpio_pin_direction
  51. {
  52. kGPIO_DigitalInput = 0U, /*!< Set current pin as digital input.*/
  53. kGPIO_DigitalOutput = 1U, /*!< Set current pin as digital output.*/
  54. } gpio_pin_direction_t;
  55. /*! @brief GPIO interrupt mode definition. */
  56. typedef enum _gpio_interrupt_mode
  57. {
  58. kGPIO_NoIntmode = 0U, /*!< Set current pin general IO functionality.*/
  59. kGPIO_IntLowLevel = 1U, /*!< Set current pin interrupt is low-level sensitive.*/
  60. kGPIO_IntHighLevel = 2U, /*!< Set current pin interrupt is high-level sensitive.*/
  61. kGPIO_IntRisingEdge = 3U, /*!< Set current pin interrupt is rising-edge sensitive.*/
  62. kGPIO_IntFallingEdge = 4U, /*!< Set current pin interrupt is falling-edge sensitive.*/
  63. kGPIO_IntRisingOrFallingEdge = 5U, /*!< Enable the edge select bit to override the ICR register's configuration.*/
  64. } gpio_interrupt_mode_t;
  65. /*! @brief GPIO Init structure definition. */
  66. typedef struct _gpio_pin_config
  67. {
  68. gpio_pin_direction_t direction; /*!< Specifies the pin direction. */
  69. uint8_t outputLogic; /*!< Set a default output logic, which has no use in input */
  70. gpio_interrupt_mode_t
  71. interruptMode; /*!< Specifies the pin interrupt mode, a value of @ref gpio_interrupt_mode_t. */
  72. } gpio_pin_config_t;
  73. /*******************************************************************************
  74. * API
  75. ******************************************************************************/
  76. #if defined(__cplusplus)
  77. extern "C" {
  78. #endif
  79. /*!
  80. * @name GPIO Initialization and Configuration functions
  81. * @{
  82. */
  83. /*!
  84. * @brief Initializes the GPIO peripheral according to the specified
  85. * parameters in the initConfig.
  86. *
  87. * @param base GPIO base pointer.
  88. * @param pin Specifies the pin number
  89. * @param initConfig pointer to a @ref gpio_pin_config_t structure that
  90. * contains the configuration information.
  91. */
  92. void GPIO_PinInit(GPIO_Type *base, uint32_t pin, const gpio_pin_config_t *Config);
  93. /*@}*/
  94. /*!
  95. * @name GPIO Reads and Write Functions
  96. * @{
  97. */
  98. /*!
  99. * @brief Sets the output level of the individual GPIO pin to logic 1 or 0.
  100. *
  101. * @param base GPIO base pointer.
  102. * @param pin GPIO port pin number.
  103. * @param output GPIOpin output logic level.
  104. * - 0: corresponding pin output low-logic level.
  105. * - 1: corresponding pin output high-logic level.
  106. */
  107. void GPIO_PinWrite(GPIO_Type *base, uint32_t pin, uint8_t output);
  108. /*!
  109. * @brief Sets the output level of the individual GPIO pin to logic 1 or 0.
  110. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PinWrite.
  111. */
  112. static inline void GPIO_WritePinOutput(GPIO_Type *base, uint32_t pin, uint8_t output)
  113. {
  114. GPIO_PinWrite(base, pin, output);
  115. }
  116. /*!
  117. * @brief Sets the output level of the multiple GPIO pins to the logic 1.
  118. *
  119. * @param base GPIO peripheral base pointer (GPIO1, GPIO2, GPIO3, and so on.)
  120. * @param mask GPIO pin number macro
  121. */
  122. static inline void GPIO_PortSet(GPIO_Type *base, uint32_t mask)
  123. {
  124. #if (defined(FSL_FEATURE_IGPIO_HAS_DR_SET) && (FSL_FEATURE_IGPIO_HAS_DR_SET == 1))
  125. base->DR_SET = mask;
  126. #else
  127. base->DR |= mask;
  128. #endif /* FSL_FEATURE_IGPIO_HAS_DR_SET */
  129. }
  130. /*!
  131. * @brief Sets the output level of the multiple GPIO pins to the logic 1.
  132. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PortSet.
  133. */
  134. static inline void GPIO_SetPinsOutput(GPIO_Type *base, uint32_t mask)
  135. {
  136. GPIO_PortSet(base, mask);
  137. }
  138. /*!
  139. * @brief Sets the output level of the multiple GPIO pins to the logic 0.
  140. *
  141. * @param base GPIO peripheral base pointer (GPIO1, GPIO2, GPIO3, and so on.)
  142. * @param mask GPIO pin number macro
  143. */
  144. static inline void GPIO_PortClear(GPIO_Type *base, uint32_t mask)
  145. {
  146. #if (defined(FSL_FEATURE_IGPIO_HAS_DR_CLEAR) && (FSL_FEATURE_IGPIO_HAS_DR_CLEAR == 1))
  147. base->DR_CLEAR = mask;
  148. #else
  149. base->DR &= ~mask;
  150. #endif /* FSL_FEATURE_IGPIO_HAS_DR_CLEAR */
  151. }
  152. /*!
  153. * @brief Sets the output level of the multiple GPIO pins to the logic 0.
  154. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PortClear.
  155. */
  156. static inline void GPIO_ClearPinsOutput(GPIO_Type *base, uint32_t mask)
  157. {
  158. GPIO_PortClear(base, mask);
  159. }
  160. /*!
  161. * @brief Reverses the current output logic of the multiple GPIO pins.
  162. *
  163. * @param base GPIO peripheral base pointer (GPIO1, GPIO2, GPIO3, and so on.)
  164. * @param mask GPIO pin number macro
  165. */
  166. static inline void GPIO_PortToggle(GPIO_Type *base, uint32_t mask)
  167. {
  168. #if (defined(FSL_FEATURE_IGPIO_HAS_DR_TOGGLE) && (FSL_FEATURE_IGPIO_HAS_DR_TOGGLE == 1))
  169. base->DR_TOGGLE = mask;
  170. #endif /* FSL_FEATURE_IGPIO_HAS_DR_TOGGLE */
  171. }
  172. /*!
  173. * @brief Reads the current input value of the GPIO port.
  174. *
  175. * @param base GPIO base pointer.
  176. * @param pin GPIO port pin number.
  177. * @retval GPIO port input value.
  178. */
  179. static inline uint32_t GPIO_PinRead(GPIO_Type *base, uint32_t pin)
  180. {
  181. assert(pin < 32);
  182. return (((base->DR) >> pin) & 0x1U);
  183. }
  184. /*!
  185. * @brief Reads the current input value of the GPIO port.
  186. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PinRead.
  187. */
  188. static inline uint32_t GPIO_ReadPinInput(GPIO_Type *base, uint32_t pin)
  189. {
  190. return GPIO_PinRead(base, pin);
  191. }
  192. /*@}*/
  193. /*!
  194. * @name GPIO Reads Pad Status Functions
  195. * @{
  196. */
  197. /*!
  198. * @brief Reads the current GPIO pin pad status.
  199. *
  200. * @param base GPIO base pointer.
  201. * @param pin GPIO port pin number.
  202. * @retval GPIO pin pad status value.
  203. */
  204. static inline uint8_t GPIO_PinReadPadStatus(GPIO_Type *base, uint32_t pin)
  205. {
  206. assert(pin < 32);
  207. return (uint8_t)(((base->PSR) >> pin) & 0x1U);
  208. }
  209. /*!
  210. * @brief Reads the current GPIO pin pad status.
  211. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PinReadPadStatus.
  212. */
  213. static inline uint8_t GPIO_ReadPadStatus(GPIO_Type *base, uint32_t pin)
  214. {
  215. return GPIO_PinReadPadStatus(base, pin);
  216. }
  217. /*@}*/
  218. /*!
  219. * @name Interrupts and flags management functions
  220. * @{
  221. */
  222. /*!
  223. * @brief Sets the current pin interrupt mode.
  224. *
  225. * @param base GPIO base pointer.
  226. * @param pin GPIO port pin number.
  227. * @param pininterruptMode pointer to a @ref gpio_interrupt_mode_t structure
  228. * that contains the interrupt mode information.
  229. */
  230. void GPIO_PinSetInterruptConfig(GPIO_Type *base, uint32_t pin, gpio_interrupt_mode_t pinInterruptMode);
  231. /*!
  232. * @brief Sets the current pin interrupt mode.
  233. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PinSetInterruptConfig.
  234. */
  235. static inline void GPIO_SetPinInterruptConfig(GPIO_Type *base, uint32_t pin, gpio_interrupt_mode_t pinInterruptMode)
  236. {
  237. GPIO_PinSetInterruptConfig(base, pin, pinInterruptMode);
  238. }
  239. /*!
  240. * @brief Enables the specific pin interrupt.
  241. *
  242. * @param base GPIO base pointer.
  243. * @param mask GPIO pin number macro.
  244. */
  245. static inline void GPIO_PortEnableInterrupts(GPIO_Type *base, uint32_t mask)
  246. {
  247. base->IMR |= mask;
  248. }
  249. /*!
  250. * @brief Enables the specific pin interrupt.
  251. *
  252. * @param base GPIO base pointer.
  253. * @param mask GPIO pin number macro.
  254. */
  255. static inline void GPIO_EnableInterrupts(GPIO_Type *base, uint32_t mask)
  256. {
  257. GPIO_PortEnableInterrupts(base, mask);
  258. }
  259. /*!
  260. * @brief Disables the specific pin interrupt.
  261. *
  262. * @param base GPIO base pointer.
  263. * @param mask GPIO pin number macro.
  264. */
  265. static inline void GPIO_PortDisableInterrupts(GPIO_Type *base, uint32_t mask)
  266. {
  267. base->IMR &= ~mask;
  268. }
  269. /*!
  270. * @brief Disables the specific pin interrupt.
  271. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PortDisableInterrupts.
  272. */
  273. static inline void GPIO_DisableInterrupts(GPIO_Type *base, uint32_t mask)
  274. {
  275. GPIO_PortDisableInterrupts(base, mask);
  276. }
  277. /*!
  278. * @brief Reads individual pin interrupt status.
  279. *
  280. * @param base GPIO base pointer.
  281. * @retval current pin interrupt status flag.
  282. */
  283. static inline uint32_t GPIO_PortGetInterruptFlags(GPIO_Type *base)
  284. {
  285. return base->ISR;
  286. }
  287. /*!
  288. * @brief Reads individual pin interrupt status.
  289. *
  290. * @param base GPIO base pointer.
  291. * @retval current pin interrupt status flag.
  292. */
  293. static inline uint32_t GPIO_GetPinsInterruptFlags(GPIO_Type *base)
  294. {
  295. return GPIO_PortGetInterruptFlags(base);
  296. }
  297. /*!
  298. * @brief Clears pin interrupt flag. Status flags are cleared by
  299. * writing a 1 to the corresponding bit position.
  300. *
  301. * @param base GPIO base pointer.
  302. * @param mask GPIO pin number macro.
  303. */
  304. static inline void GPIO_PortClearInterruptFlags(GPIO_Type *base, uint32_t mask)
  305. {
  306. base->ISR = mask;
  307. }
  308. /*!
  309. * @brief Clears pin interrupt flag. Status flags are cleared by
  310. * writing a 1 to the corresponding bit position.
  311. *
  312. * @param base GPIO base pointer.
  313. * @param mask GPIO pin number macro.
  314. */
  315. static inline void GPIO_ClearPinsInterruptFlags(GPIO_Type *base, uint32_t mask)
  316. {
  317. GPIO_PortClearInterruptFlags(base, mask);
  318. }
  319. /*@}*/
  320. #if defined(__cplusplus)
  321. }
  322. #endif
  323. /*!
  324. * @}
  325. */
  326. #endif /* _FSL_GPIO_H_*/