fsl_gpio.h 7.3 KB

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