fsl_gpio.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2018 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _LPC_GPIO_H_
  9. #define _LPC_GPIO_H_
  10. #include "fsl_common.h"
  11. /*!
  12. * @addtogroup lpc_gpio
  13. * @{
  14. */
  15. /*! @file */
  16. /*******************************************************************************
  17. * Definitions
  18. ******************************************************************************/
  19. /*! @name Driver version */
  20. /*@{*/
  21. /*! @brief LPC GPIO driver version 2.1.3. */
  22. #define FSL_GPIO_DRIVER_VERSION (MAKE_VERSION(2, 1, 3))
  23. /*@}*/
  24. /*! @brief LPC GPIO direction definition */
  25. typedef enum _gpio_pin_direction
  26. {
  27. kGPIO_DigitalInput = 0U, /*!< Set current pin as digital input*/
  28. kGPIO_DigitalOutput = 1U, /*!< Set current pin as digital output*/
  29. } gpio_pin_direction_t;
  30. /*!
  31. * @brief The GPIO pin configuration structure.
  32. *
  33. * Every pin can only be configured as either output pin or input pin at a time.
  34. * If configured as a input pin, then leave the outputConfig unused.
  35. */
  36. typedef struct _gpio_pin_config
  37. {
  38. gpio_pin_direction_t pinDirection; /*!< GPIO direction, input or output */
  39. /* Output configurations, please ignore if configured as a input one */
  40. uint8_t outputLogic; /*!< Set default output logic, no use in input */
  41. } gpio_pin_config_t;
  42. /*******************************************************************************
  43. * API
  44. ******************************************************************************/
  45. #if defined(__cplusplus)
  46. extern "C" {
  47. #endif
  48. /*! @name GPIO Configuration */
  49. /*@{*/
  50. /*!
  51. * @brief Initializes the GPIO peripheral.
  52. *
  53. * This function ungates the GPIO clock.
  54. *
  55. * @param base GPIO peripheral base pointer.
  56. * @param port GPIO port number.
  57. */
  58. void GPIO_PortInit(GPIO_Type *base, uint32_t port);
  59. /*!
  60. * @brief Initializes a GPIO pin used by the board.
  61. *
  62. * To initialize the GPIO, define a pin configuration, either input or output, in the user file.
  63. * Then, call the GPIO_PinInit() function.
  64. *
  65. * This is an example to define an input pin or output pin configuration:
  66. * @code
  67. * // Define a digital input pin configuration,
  68. * gpio_pin_config_t config =
  69. * {
  70. * kGPIO_DigitalInput,
  71. * 0,
  72. * }
  73. * //Define a digital output pin configuration,
  74. * gpio_pin_config_t config =
  75. * {
  76. * kGPIO_DigitalOutput,
  77. * 0,
  78. * }
  79. * @endcode
  80. *
  81. * @param base GPIO peripheral base pointer(Typically GPIO)
  82. * @param port GPIO port number
  83. * @param pin GPIO pin number
  84. * @param config GPIO pin configuration pointer
  85. */
  86. void GPIO_PinInit(GPIO_Type *base, uint32_t port, uint32_t pin, const gpio_pin_config_t *config);
  87. /*@}*/
  88. /*! @name GPIO Output Operations */
  89. /*@{*/
  90. /*!
  91. * @brief Sets the output level of the one GPIO pin to the logic 1 or 0.
  92. *
  93. * @param base GPIO peripheral base pointer(Typically GPIO)
  94. * @param port GPIO port number
  95. * @param pin GPIO pin number
  96. * @param output GPIO pin output logic level.
  97. * - 0: corresponding pin output low-logic level.
  98. * - 1: corresponding pin output high-logic level.
  99. */
  100. static inline void GPIO_PinWrite(GPIO_Type *base, uint32_t port, uint32_t pin, uint8_t output)
  101. {
  102. base->B[port][pin] = output;
  103. }
  104. /*@}*/
  105. /*! @name GPIO Input Operations */
  106. /*@{*/
  107. /*!
  108. * @brief Reads the current input value of the GPIO PIN.
  109. *
  110. * @param base GPIO peripheral base pointer(Typically GPIO)
  111. * @param port GPIO port number
  112. * @param pin GPIO pin number
  113. * @retval GPIO port input value
  114. * - 0: corresponding pin input low-logic level.
  115. * - 1: corresponding pin input high-logic level.
  116. */
  117. static inline uint32_t GPIO_PinRead(GPIO_Type *base, uint32_t port, uint32_t pin)
  118. {
  119. return (uint32_t)base->B[port][pin];
  120. }
  121. /*@}*/
  122. /*!
  123. * @brief Sets the output level of the multiple GPIO pins to the logic 1.
  124. *
  125. * @param base GPIO peripheral base pointer(Typically GPIO)
  126. * @param port GPIO port number
  127. * @param mask GPIO pin number macro
  128. */
  129. static inline void GPIO_PortSet(GPIO_Type *base, uint32_t port, uint32_t mask)
  130. {
  131. base->SET[port] = mask;
  132. }
  133. /*!
  134. * @brief Sets the output level of the multiple GPIO pins to the logic 0.
  135. *
  136. * @param base GPIO peripheral base pointer(Typically GPIO)
  137. * @param port GPIO port number
  138. * @param mask GPIO pin number macro
  139. */
  140. static inline void GPIO_PortClear(GPIO_Type *base, uint32_t port, uint32_t mask)
  141. {
  142. base->CLR[port] = mask;
  143. }
  144. /*!
  145. * @brief Reverses current output logic of the multiple GPIO pins.
  146. *
  147. * @param base GPIO peripheral base pointer(Typically GPIO)
  148. * @param port GPIO port number
  149. * @param mask GPIO pin number macro
  150. */
  151. static inline void GPIO_PortToggle(GPIO_Type *base, uint32_t port, uint32_t mask)
  152. {
  153. base->NOT[port] = mask;
  154. }
  155. /*@}*/
  156. /*!
  157. * @brief Reads the current input value of the whole GPIO port.
  158. *
  159. * @param base GPIO peripheral base pointer(Typically GPIO)
  160. * @param port GPIO port number
  161. */
  162. static inline uint32_t GPIO_PortRead(GPIO_Type *base, uint32_t port)
  163. {
  164. return (uint32_t)base->PIN[port];
  165. }
  166. /*@}*/
  167. /*! @name GPIO Mask Operations */
  168. /*@{*/
  169. /*!
  170. * @brief Sets port mask, 0 - enable pin, 1 - disable pin.
  171. *
  172. * @param base GPIO peripheral base pointer(Typically GPIO)
  173. * @param port GPIO port number
  174. * @param mask GPIO pin number macro
  175. */
  176. static inline void GPIO_PortMaskedSet(GPIO_Type *base, uint32_t port, uint32_t mask)
  177. {
  178. base->MASK[port] = mask;
  179. }
  180. /*!
  181. * @brief Sets the output level of the masked GPIO port. Only pins enabled by GPIO_SetPortMask() will be affected.
  182. *
  183. * @param base GPIO peripheral base pointer(Typically GPIO)
  184. * @param port GPIO port number
  185. * @param output GPIO port output value.
  186. */
  187. static inline void GPIO_PortMaskedWrite(GPIO_Type *base, uint32_t port, uint32_t output)
  188. {
  189. base->MPIN[port] = output;
  190. }
  191. /*!
  192. * @brief Reads the current input value of the masked GPIO port. Only pins enabled by GPIO_SetPortMask() will be
  193. * affected.
  194. *
  195. * @param base GPIO peripheral base pointer(Typically GPIO)
  196. * @param port GPIO port number
  197. * @retval masked GPIO port value
  198. */
  199. static inline uint32_t GPIO_PortMaskedRead(GPIO_Type *base, uint32_t port)
  200. {
  201. return (uint32_t)base->MPIN[port];
  202. }
  203. /*@}*/
  204. #if defined(__cplusplus)
  205. }
  206. #endif
  207. /*!
  208. * @}
  209. */
  210. #endif /* _LPC_GPIO_H_*/