fsl_gpio.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright (c) 2015, 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
  35. * @{
  36. */
  37. /*******************************************************************************
  38. * Definitions
  39. ******************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. /*! @brief GPIO driver version 2.1.1. */
  43. #define FSL_GPIO_DRIVER_VERSION (MAKE_VERSION(2, 1, 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. #if defined(FSL_FEATURE_GPIO_HAS_ATTRIBUTE_CHECKER) && FSL_FEATURE_GPIO_HAS_ATTRIBUTE_CHECKER
  52. /*! @brief GPIO checker attribute */
  53. typedef enum _gpio_checker_attribute
  54. {
  55. kGPIO_UsernonsecureRWUsersecureRWPrivilegedsecureRW =
  56. 0x00U, /*!< User nonsecure:Read+Write; User Secure:Read+Write; Privileged Secure:Read+Write */
  57. kGPIO_UsernonsecureRUsersecureRWPrivilegedsecureRW =
  58. 0x01U, /*!< User nonsecure:Read; User Secure:Read+Write; Privileged Secure:Read+Write */
  59. kGPIO_UsernonsecureNUsersecureRWPrivilegedsecureRW =
  60. 0x02U, /*!< User nonsecure:None; User Secure:Read+Write; Privileged Secure:Read+Write */
  61. kGPIO_UsernonsecureRUsersecureRPrivilegedsecureRW =
  62. 0x03U, /*!< User nonsecure:Read; User Secure:Read; Privileged Secure:Read+Write */
  63. kGPIO_UsernonsecureNUsersecureRPrivilegedsecureRW =
  64. 0x04U, /*!< User nonsecure:None; User Secure:Read; Privileged Secure:Read+Write */
  65. kGPIO_UsernonsecureNUsersecureNPrivilegedsecureRW =
  66. 0x05U, /*!< User nonsecure:None; User Secure:None; Privileged Secure:Read+Write */
  67. kGPIO_UsernonsecureNUsersecureNPrivilegedsecureR =
  68. 0x06U, /*!< User nonsecure:None; User Secure:None; Privileged Secure:Read */
  69. kGPIO_UsernonsecureNUsersecureNPrivilegedsecureN =
  70. 0x07U, /*!< User nonsecure:None; User Secure:None; Privileged Secure:None */
  71. kGPIO_IgnoreAttributeCheck = 0x10U, /*!< Ignores the attribute check */
  72. } gpio_checker_attribute_t;
  73. #endif
  74. /*!
  75. * @brief The GPIO pin configuration structure.
  76. *
  77. * Each pin can only be configured as either an output pin or an input pin at a time.
  78. * If configured as an input pin, leave the outputConfig unused.
  79. * Note that in some use cases, the corresponding port property should be configured in advance
  80. * with the PORT_SetPinConfig().
  81. */
  82. typedef struct _gpio_pin_config
  83. {
  84. gpio_pin_direction_t pinDirection; /*!< GPIO direction, input or output */
  85. /* Output configurations; ignore if configured as an input pin */
  86. uint8_t outputLogic; /*!< Set a default output logic, which has no use in input */
  87. } gpio_pin_config_t;
  88. /*! @} */
  89. /*******************************************************************************
  90. * API
  91. ******************************************************************************/
  92. #if defined(__cplusplus)
  93. extern "C" {
  94. #endif
  95. /*!
  96. * @addtogroup gpio_driver
  97. * @{
  98. */
  99. /*! @name GPIO Configuration */
  100. /*@{*/
  101. /*!
  102. * @brief Initializes a GPIO pin used by the board.
  103. *
  104. * To initialize the GPIO, define a pin configuration, as either input or output, in the user file.
  105. * Then, call the GPIO_PinInit() function.
  106. *
  107. * This is an example to define an input pin or an output pin configuration.
  108. * @code
  109. * // Define a digital input pin configuration,
  110. * gpio_pin_config_t config =
  111. * {
  112. * kGPIO_DigitalInput,
  113. * 0,
  114. * }
  115. * //Define a digital output pin configuration,
  116. * gpio_pin_config_t config =
  117. * {
  118. * kGPIO_DigitalOutput,
  119. * 0,
  120. * }
  121. * @endcode
  122. *
  123. * @param base GPIO peripheral base pointer (GPIOA, GPIOB, GPIOC, and so on.)
  124. * @param pin GPIO port pin number
  125. * @param config GPIO pin configuration pointer
  126. */
  127. void GPIO_PinInit(GPIO_Type *base, uint32_t pin, const gpio_pin_config_t *config);
  128. /*@}*/
  129. /*! @name GPIO Output Operations */
  130. /*@{*/
  131. /*!
  132. * @brief Sets the output level of the multiple GPIO pins to the logic 1 or 0.
  133. *
  134. * @param base GPIO peripheral base pointer (GPIOA, GPIOB, GPIOC, and so on.)
  135. * @param pin GPIO pin number
  136. * @param output GPIO pin output logic level.
  137. * - 0: corresponding pin output low-logic level.
  138. * - 1: corresponding pin output high-logic level.
  139. */
  140. static inline void GPIO_WritePinOutput(GPIO_Type *base, uint32_t pin, uint8_t output)
  141. {
  142. if (output == 0U)
  143. {
  144. base->PCOR = 1U << pin;
  145. }
  146. else
  147. {
  148. base->PSOR = 1U << pin;
  149. }
  150. }
  151. /*!
  152. * @brief Sets the output level of the multiple GPIO pins to the logic 1.
  153. *
  154. * @param base GPIO peripheral base pointer (GPIOA, GPIOB, GPIOC, and so on.)
  155. * @param mask GPIO pin number macro
  156. */
  157. static inline void GPIO_SetPinsOutput(GPIO_Type *base, uint32_t mask)
  158. {
  159. base->PSOR = mask;
  160. }
  161. /*!
  162. * @brief Sets the output level of the multiple GPIO pins to the logic 0.
  163. *
  164. * @param base GPIO peripheral base pointer (GPIOA, GPIOB, GPIOC, and so on.)
  165. * @param mask GPIO pin number macro
  166. */
  167. static inline void GPIO_ClearPinsOutput(GPIO_Type *base, uint32_t mask)
  168. {
  169. base->PCOR = mask;
  170. }
  171. /*!
  172. * @brief Reverses the current output logic of the multiple GPIO pins.
  173. *
  174. * @param base GPIO peripheral base pointer (GPIOA, GPIOB, GPIOC, and so on.)
  175. * @param mask GPIO pin number macro
  176. */
  177. static inline void GPIO_TogglePinsOutput(GPIO_Type *base, uint32_t mask)
  178. {
  179. base->PTOR = mask;
  180. }
  181. /*@}*/
  182. /*! @name GPIO Input Operations */
  183. /*@{*/
  184. /*!
  185. * @brief Reads the current input value of the GPIO port.
  186. *
  187. * @param base GPIO peripheral base pointer (GPIOA, GPIOB, GPIOC, and so on.)
  188. * @param pin GPIO pin number
  189. * @retval GPIO port input value
  190. * - 0: corresponding pin input low-logic level.
  191. * - 1: corresponding pin input high-logic level.
  192. */
  193. static inline uint32_t GPIO_ReadPinInput(GPIO_Type *base, uint32_t pin)
  194. {
  195. return (((base->PDIR) >> pin) & 0x01U);
  196. }
  197. /*@}*/
  198. /*! @name GPIO Interrupt */
  199. /*@{*/
  200. /*!
  201. * @brief Reads the GPIO port interrupt status flag.
  202. *
  203. * If a pin is configured to generate the DMA request, the corresponding flag
  204. * is cleared automatically at the completion of the requested DMA transfer.
  205. * Otherwise, the flag remains set until a logic one is written to that flag.
  206. * If configured for a level sensitive interrupt that remains asserted, the flag
  207. * is set again immediately.
  208. *
  209. * @param base GPIO peripheral base pointer (GPIOA, GPIOB, GPIOC, and so on.)
  210. * @retval The current GPIO port interrupt status flag, for example, 0x00010001 means the
  211. * pin 0 and 17 have the interrupt.
  212. */
  213. uint32_t GPIO_GetPinsInterruptFlags(GPIO_Type *base);
  214. /*!
  215. * @brief Clears multiple GPIO pin interrupt status flags.
  216. *
  217. * @param base GPIO peripheral base pointer (GPIOA, GPIOB, GPIOC, and so on.)
  218. * @param mask GPIO pin number macro
  219. */
  220. void GPIO_ClearPinsInterruptFlags(GPIO_Type *base, uint32_t mask);
  221. #if defined(FSL_FEATURE_GPIO_HAS_ATTRIBUTE_CHECKER) && FSL_FEATURE_GPIO_HAS_ATTRIBUTE_CHECKER
  222. /*!
  223. * @brief The GPIO module supports a device-specific number of data ports, organized as 32-bit
  224. * words. Each 32-bit data port includes a GACR register, which defines the byte-level
  225. * attributes required for a successful access to the GPIO programming model. The attribute controls for the 4 data
  226. * bytes in the GACR follow a standard little endian
  227. * data convention.
  228. *
  229. * @param base GPIO peripheral base pointer (GPIOA, GPIOB, GPIOC, and so on.)
  230. * @param mask GPIO pin number macro
  231. */
  232. void GPIO_CheckAttributeBytes(GPIO_Type *base, gpio_checker_attribute_t attribute);
  233. #endif
  234. /*@}*/
  235. /*! @} */
  236. /*!
  237. * @addtogroup fgpio_driver
  238. * @{
  239. */
  240. /*
  241. * Introduces the FGPIO feature.
  242. *
  243. * The FGPIO features are only support on some Kinetis MCUs. The FGPIO registers are aliased to the IOPORT
  244. * interface. Accesses via the IOPORT interface occur in parallel with any instruction fetches and
  245. * complete in a single cycle. This aliased Fast GPIO memory map is called FGPIO.
  246. */
  247. #if defined(FSL_FEATURE_SOC_FGPIO_COUNT) && FSL_FEATURE_SOC_FGPIO_COUNT
  248. /*! @name FGPIO Configuration */
  249. /*@{*/
  250. /*!
  251. * @brief Initializes a FGPIO pin used by the board.
  252. *
  253. * To initialize the FGPIO driver, define a pin configuration, as either input or output, in the user file.
  254. * Then, call the FGPIO_PinInit() function.
  255. *
  256. * This is an example to define an input pin or an output pin configuration:
  257. * @code
  258. * // Define a digital input pin configuration,
  259. * gpio_pin_config_t config =
  260. * {
  261. * kGPIO_DigitalInput,
  262. * 0,
  263. * }
  264. * //Define a digital output pin configuration,
  265. * gpio_pin_config_t config =
  266. * {
  267. * kGPIO_DigitalOutput,
  268. * 0,
  269. * }
  270. * @endcode
  271. *
  272. * @param base FGPIO peripheral base pointer (FGPIOA, FGPIOB, FGPIOC, and so on.)
  273. * @param pin FGPIO port pin number
  274. * @param config FGPIO pin configuration pointer
  275. */
  276. void FGPIO_PinInit(FGPIO_Type *base, uint32_t pin, const gpio_pin_config_t *config);
  277. /*@}*/
  278. /*! @name FGPIO Output Operations */
  279. /*@{*/
  280. /*!
  281. * @brief Sets the output level of the multiple FGPIO pins to the logic 1 or 0.
  282. *
  283. * @param base FGPIO peripheral base pointer (FGPIOA, FGPIOB, FGPIOC, and so on.)
  284. * @param pin FGPIO pin number
  285. * @param output FGPIOpin output logic level.
  286. * - 0: corresponding pin output low-logic level.
  287. * - 1: corresponding pin output high-logic level.
  288. */
  289. static inline void FGPIO_WritePinOutput(FGPIO_Type *base, uint32_t pin, uint8_t output)
  290. {
  291. if (output == 0U)
  292. {
  293. base->PCOR = 1 << pin;
  294. }
  295. else
  296. {
  297. base->PSOR = 1 << pin;
  298. }
  299. }
  300. /*!
  301. * @brief Sets the output level of the multiple FGPIO pins to the logic 1.
  302. *
  303. * @param base FGPIO peripheral base pointer (FGPIOA, FGPIOB, FGPIOC, and so on.)
  304. * @param mask FGPIO pin number macro
  305. */
  306. static inline void FGPIO_SetPinsOutput(FGPIO_Type *base, uint32_t mask)
  307. {
  308. base->PSOR = mask;
  309. }
  310. /*!
  311. * @brief Sets the output level of the multiple FGPIO pins to the logic 0.
  312. *
  313. * @param base FGPIO peripheral base pointer (FGPIOA, FGPIOB, FGPIOC, and so on.)
  314. * @param mask FGPIO pin number macro
  315. */
  316. static inline void FGPIO_ClearPinsOutput(FGPIO_Type *base, uint32_t mask)
  317. {
  318. base->PCOR = mask;
  319. }
  320. /*!
  321. * @brief Reverses the current output logic of the multiple FGPIO pins.
  322. *
  323. * @param base FGPIO peripheral base pointer (FGPIOA, FGPIOB, FGPIOC, and so on.)
  324. * @param mask FGPIO pin number macro
  325. */
  326. static inline void FGPIO_TogglePinsOutput(FGPIO_Type *base, uint32_t mask)
  327. {
  328. base->PTOR = mask;
  329. }
  330. /*@}*/
  331. /*! @name FGPIO Input Operations */
  332. /*@{*/
  333. /*!
  334. * @brief Reads the current input value of the FGPIO port.
  335. *
  336. * @param base FGPIO peripheral base pointer (FGPIOA, FGPIOB, FGPIOC, and so on.)
  337. * @param pin FGPIO pin number
  338. * @retval FGPIO port input value
  339. * - 0: corresponding pin input low-logic level.
  340. * - 1: corresponding pin input high-logic level.
  341. */
  342. static inline uint32_t FGPIO_ReadPinInput(FGPIO_Type *base, uint32_t pin)
  343. {
  344. return (((base->PDIR) >> pin) & 0x01U);
  345. }
  346. /*@}*/
  347. /*! @name FGPIO Interrupt */
  348. /*@{*/
  349. /*!
  350. * @brief Reads the FGPIO port interrupt status flag.
  351. *
  352. * If a pin is configured to generate the DMA request, the corresponding flag
  353. * is cleared automatically at the completion of the requested DMA transfer.
  354. * Otherwise, the flag remains set until a logic one is written to that flag.
  355. * If configured for a level-sensitive interrupt that remains asserted, the flag
  356. * is set again immediately.
  357. *
  358. * @param base FGPIO peripheral base pointer (FGPIOA, FGPIOB, FGPIOC, and so on.)
  359. * @retval The current FGPIO port interrupt status flags, for example, 0x00010001 means the
  360. * pin 0 and 17 have the interrupt.
  361. */
  362. uint32_t FGPIO_GetPinsInterruptFlags(FGPIO_Type *base);
  363. /*!
  364. * @brief Clears the multiple FGPIO pin interrupt status flag.
  365. *
  366. * @param base FGPIO peripheral base pointer (FGPIOA, FGPIOB, FGPIOC, and so on.)
  367. * @param mask FGPIO pin number macro
  368. */
  369. void FGPIO_ClearPinsInterruptFlags(FGPIO_Type *base, uint32_t mask);
  370. #if defined(FSL_FEATURE_GPIO_HAS_ATTRIBUTE_CHECKER) && FSL_FEATURE_GPIO_HAS_ATTRIBUTE_CHECKER
  371. /*!
  372. * @brief The FGPIO module supports a device-specific number of data ports, organized as 32-bit
  373. * words. Each 32-bit data port includes a GACR register, which defines the byte-level
  374. * attributes required for a successful access to the GPIO programming model. The attribute controls for the 4 data
  375. * bytes in the GACR follow a standard little endian
  376. * data convention.
  377. *
  378. * @param base FGPIO peripheral base pointer (FGPIOA, FGPIOB, FGPIOC, and so on.)
  379. * @param mask FGPIO pin number macro
  380. */
  381. void FGPIO_CheckAttributeBytes(FGPIO_Type *base, gpio_checker_attribute_t attribute);
  382. #endif
  383. /*@}*/
  384. #endif /* FSL_FEATURE_SOC_FGPIO_COUNT */
  385. #if defined(__cplusplus)
  386. }
  387. #endif
  388. /*!
  389. * @}
  390. */
  391. #endif /* _FSL_GPIO_H_*/