fsl_gpio.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 _LPC_GPIO_H_
  35. #define _LPC_GPIO_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup lpc_gpio
  39. * @{
  40. */
  41. /*! @file */
  42. /*******************************************************************************
  43. * Definitions
  44. ******************************************************************************/
  45. /*! @name Driver version */
  46. /*@{*/
  47. /*! @brief LPC GPIO driver version 2.1.1. */
  48. #define FSL_GPIO_DRIVER_VERSION (MAKE_VERSION(2, 1, 1))
  49. /*@}*/
  50. /*! @brief LPC GPIO direction definition */
  51. typedef enum _gpio_pin_direction
  52. {
  53. kGPIO_DigitalInput = 0U, /*!< Set current pin as digital input*/
  54. kGPIO_DigitalOutput = 1U, /*!< Set current pin as digital output*/
  55. } gpio_pin_direction_t;
  56. /*!
  57. * @brief The GPIO pin configuration structure.
  58. *
  59. * Every pin can only be configured as either output pin or input pin at a time.
  60. * If configured as a input pin, then leave the outputConfig unused.
  61. */
  62. typedef struct _gpio_pin_config
  63. {
  64. gpio_pin_direction_t pinDirection; /*!< GPIO direction, input or output */
  65. /* Output configurations, please ignore if configured as a input one */
  66. uint8_t outputLogic; /*!< Set default output logic, no use in input */
  67. } gpio_pin_config_t;
  68. /*******************************************************************************
  69. * API
  70. ******************************************************************************/
  71. #if defined(__cplusplus)
  72. extern "C" {
  73. #endif
  74. /*! @name GPIO Configuration */
  75. /*@{*/
  76. /*!
  77. * @brief Initializes the GPIO peripheral.
  78. *
  79. * This function ungates the GPIO clock.
  80. *
  81. * @param base GPIO peripheral base pointer.
  82. * @param port GPIO port number.
  83. */
  84. void GPIO_PortInit(GPIO_Type *base, uint32_t port);
  85. /*!
  86. * @brief Initializes the GPIO peripheral.
  87. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PortInit.
  88. */
  89. static inline void GPIO_Init(GPIO_Type *base, uint32_t port)
  90. {
  91. GPIO_PortInit(base, port);
  92. }
  93. /*!
  94. * @brief Initializes a GPIO pin used by the board.
  95. *
  96. * To initialize the GPIO, define a pin configuration, either input or output, in the user file.
  97. * Then, call the GPIO_PinInit() function.
  98. *
  99. * This is an example to define an input pin or output pin configuration:
  100. * @code
  101. * // Define a digital input pin configuration,
  102. * gpio_pin_config_t config =
  103. * {
  104. * kGPIO_DigitalInput,
  105. * 0,
  106. * }
  107. * //Define a digital output pin configuration,
  108. * gpio_pin_config_t config =
  109. * {
  110. * kGPIO_DigitalOutput,
  111. * 0,
  112. * }
  113. * @endcode
  114. *
  115. * @param base GPIO peripheral base pointer(Typically GPIO)
  116. * @param port GPIO port number
  117. * @param pin GPIO pin number
  118. * @param config GPIO pin configuration pointer
  119. */
  120. void GPIO_PinInit(GPIO_Type *base, uint32_t port, uint32_t pin, const gpio_pin_config_t *config);
  121. /*@}*/
  122. /*! @name GPIO Output Operations */
  123. /*@{*/
  124. /*!
  125. * @brief Sets the output level of the one GPIO pin to the logic 1 or 0.
  126. *
  127. * @param base GPIO peripheral base pointer(Typically GPIO)
  128. * @param port GPIO port number
  129. * @param pin GPIO pin number
  130. * @param output GPIO pin output logic level.
  131. * - 0: corresponding pin output low-logic level.
  132. * - 1: corresponding pin output high-logic level.
  133. */
  134. static inline void GPIO_PinWrite(GPIO_Type *base, uint32_t port, uint32_t pin, uint8_t output)
  135. {
  136. base->B[port][pin] = output;
  137. }
  138. /*!
  139. * @brief Sets the output level of the one GPIO pin to the logic 1 or 0.
  140. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PinWrite.
  141. */
  142. static inline void GPIO_WritePinOutput(GPIO_Type *base, uint32_t port, uint32_t pin, uint8_t output)
  143. {
  144. base->B[port][pin] = output;
  145. }
  146. /*@}*/
  147. /*! @name GPIO Input Operations */
  148. /*@{*/
  149. /*!
  150. * @brief Reads the current input value of the GPIO PIN.
  151. *
  152. * @param base GPIO peripheral base pointer(Typically GPIO)
  153. * @param port GPIO port number
  154. * @param pin GPIO pin number
  155. * @retval GPIO port input value
  156. * - 0: corresponding pin input low-logic level.
  157. * - 1: corresponding pin input high-logic level.
  158. */
  159. static inline uint32_t GPIO_PinRead(GPIO_Type *base, uint32_t port, uint32_t pin)
  160. {
  161. return (uint32_t)base->B[port][pin];
  162. }
  163. /*!
  164. * @brief Reads the current input value of the GPIO PIN.
  165. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PinRead.
  166. */
  167. static inline uint32_t GPIO_ReadPinInput(GPIO_Type *base, uint32_t port, uint32_t pin)
  168. {
  169. return GPIO_PinRead(base, port, pin);
  170. }
  171. /*@}*/
  172. /*!
  173. * @brief Sets the output level of the multiple GPIO pins to the logic 1.
  174. *
  175. * @param base GPIO peripheral base pointer(Typically GPIO)
  176. * @param port GPIO port number
  177. * @param mask GPIO pin number macro
  178. */
  179. static inline void GPIO_PortSet(GPIO_Type *base, uint32_t port, uint32_t mask)
  180. {
  181. base->SET[port] = mask;
  182. }
  183. /*!
  184. * @brief Sets the output level of the multiple GPIO pins to the logic 1.
  185. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PortSet.
  186. */
  187. static inline void GPIO_SetPinsOutput(GPIO_Type *base, uint32_t port, uint32_t mask)
  188. {
  189. GPIO_PortSet(base, port, mask);
  190. }
  191. /*!
  192. * @brief Sets the output level of the multiple GPIO pins to the logic 0.
  193. *
  194. * @param base GPIO peripheral base pointer(Typically GPIO)
  195. * @param port GPIO port number
  196. * @param mask GPIO pin number macro
  197. */
  198. static inline void GPIO_PortClear(GPIO_Type *base, uint32_t port, uint32_t mask)
  199. {
  200. base->CLR[port] = mask;
  201. }
  202. /*!
  203. * @brief Sets the output level of the multiple GPIO pins to the logic 0.
  204. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PortClear.
  205. */
  206. static inline void GPIO_ClearPinsOutput(GPIO_Type *base, uint32_t port, uint32_t mask)
  207. {
  208. GPIO_PortClear(base, port, mask);
  209. }
  210. /*!
  211. * @brief Reverses current output logic of the multiple GPIO pins.
  212. *
  213. * @param base GPIO peripheral base pointer(Typically GPIO)
  214. * @param port GPIO port number
  215. * @param mask GPIO pin number macro
  216. */
  217. static inline void GPIO_PortToggle(GPIO_Type *base, uint32_t port, uint32_t mask)
  218. {
  219. base->NOT[port] = mask;
  220. }
  221. /*!
  222. * @brief Reverses current output logic of the multiple GPIO pins.
  223. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PortToggle.
  224. */
  225. static inline void GPIO_TogglePinsOutput(GPIO_Type *base, uint32_t port, uint32_t mask)
  226. {
  227. GPIO_PortToggle(base, port, mask);
  228. }
  229. /*@}*/
  230. /*!
  231. * @brief Reads the current input value of the whole GPIO port.
  232. *
  233. * @param base GPIO peripheral base pointer(Typically GPIO)
  234. * @param port GPIO port number
  235. */
  236. static inline uint32_t GPIO_PortRead(GPIO_Type *base, uint32_t port)
  237. {
  238. return (uint32_t)base->PIN[port];
  239. }
  240. /*!
  241. * @brief Reads the current input value of the whole GPIO port.
  242. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PortRead
  243. */
  244. static inline uint32_t GPIO_ReadPinsInput(GPIO_Type *base, uint32_t port)
  245. {
  246. return GPIO_PortRead(base, port);
  247. }
  248. /*@}*/
  249. /*! @name GPIO Mask Operations */
  250. /*@{*/
  251. /*!
  252. * @brief Sets port mask, 0 - enable pin, 1 - disable pin.
  253. *
  254. * @param base GPIO peripheral base pointer(Typically GPIO)
  255. * @param port GPIO port number
  256. * @param mask GPIO pin number macro
  257. */
  258. static inline void GPIO_PortMaskedSet(GPIO_Type *base, uint32_t port, uint32_t mask)
  259. {
  260. base->MASK[port] = mask;
  261. }
  262. /*!
  263. * @brief Sets port mask, 0 - enable pin, 1 - disable pin.
  264. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PortMaskedSet.
  265. */
  266. static inline void GPIO_SetPortMask(GPIO_Type *base, uint32_t port, uint32_t mask)
  267. {
  268. GPIO_PortMaskedSet(base, port, mask);
  269. }
  270. /*!
  271. * @brief Sets the output level of the masked GPIO port. Only pins enabled by GPIO_SetPortMask() will be affected.
  272. *
  273. * @param base GPIO peripheral base pointer(Typically GPIO)
  274. * @param port GPIO port number
  275. * @param output GPIO port output value.
  276. */
  277. static inline void GPIO_PortMaskedWrite(GPIO_Type *base, uint32_t port, uint32_t output)
  278. {
  279. base->MPIN[port] = output;
  280. }
  281. /*!
  282. * @brief Sets the output level of the masked GPIO port. Only pins enabled by GPIO_SetPortMask() will be affected.
  283. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PortMaskedWrite.
  284. */
  285. static inline void GPIO_WriteMPort(GPIO_Type *base, uint32_t port, uint32_t output)
  286. {
  287. GPIO_PortMaskedWrite(base, port, output);
  288. }
  289. /*!
  290. * @brief Reads the current input value of the masked GPIO port. Only pins enabled by GPIO_SetPortMask() will be
  291. * affected.
  292. *
  293. * @param base GPIO peripheral base pointer(Typically GPIO)
  294. * @param port GPIO port number
  295. * @retval masked GPIO port value
  296. */
  297. static inline uint32_t GPIO_PortMaskedRead(GPIO_Type *base, uint32_t port)
  298. {
  299. return (uint32_t)base->MPIN[port];
  300. }
  301. /*!
  302. * @brief Reads the current input value of the masked GPIO port. Only pins enabled by GPIO_SetPortMask() will be
  303. * affected.
  304. * @deprecated Do not use this function. It has been superceded by @ref GPIO_PortMaskedRead.
  305. */
  306. static inline uint32_t GPIO_ReadMPort(GPIO_Type *base, uint32_t port)
  307. {
  308. return GPIO_PortMaskedRead(base, port);
  309. }
  310. /*@}*/
  311. #if defined(__cplusplus)
  312. }
  313. #endif
  314. /*!
  315. * @}
  316. */
  317. #endif /* _LPC_GPIO_H_*/