gpio_8xx.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * @brief LPC8xx GPIO driver
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2012
  6. * All rights reserved.
  7. *
  8. * @par
  9. * Software that is described herein is for illustrative purposes only
  10. * which provides customers with programming information regarding the
  11. * LPC products. This software is supplied "AS IS" without any warranties of
  12. * any kind, and NXP Semiconductors and its licensor disclaim any and
  13. * all warranties, express or implied, including all implied warranties of
  14. * merchantability, fitness for a particular purpose and non-infringement of
  15. * intellectual property rights. NXP Semiconductors assumes no responsibility
  16. * or liability for the use of the software, conveys no license or rights under any
  17. * patent, copyright, mask work right, or any other intellectual property rights in
  18. * or to any products. NXP Semiconductors reserves the right to make changes
  19. * in the software without notification. NXP Semiconductors also makes no
  20. * representation or warranty that such application will be suitable for the
  21. * specified use without further testing or modification.
  22. *
  23. * @par
  24. * Permission to use, copy, modify, and distribute this software and its
  25. * documentation is hereby granted, under NXP Semiconductors' and its
  26. * licensor's relevant copyrights in the software, without fee, provided that it
  27. * is used in conjunction with NXP Semiconductors microcontrollers. This
  28. * copyright, permission, and disclaimer notice must appear in all copies of
  29. * this code.
  30. */
  31. #ifndef __GPIO_8XX_H_
  32. #define __GPIO_8XX_H_
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** @defgroup GPIO_8XX CHIP: LPC8xx GPIO driver
  37. * @ingroup CHIP_8XX_Drivers
  38. * @{
  39. */
  40. /**
  41. * @brief GPIO port register block structure
  42. */
  43. typedef struct { /*!< GPIO_PORT Structure */
  44. __IO uint8_t B[128][32]; /*!< Offset 0x0000: Byte pin registers ports 0 to n; pins PIOn_0 to PIOn_31 */
  45. __IO uint32_t W[32][32]; /*!< Offset 0x1000: Word pin registers port 0 to n */
  46. __IO uint32_t DIR[32]; /*!< Offset 0x2000: Direction registers port n */
  47. __IO uint32_t MASK[32]; /*!< Offset 0x2080: Mask register port n */
  48. __IO uint32_t PIN[32]; /*!< Offset 0x2100: Portpin register port n */
  49. __IO uint32_t MPIN[32]; /*!< Offset 0x2180: Masked port register port n */
  50. __IO uint32_t SET[32]; /*!< Offset 0x2200: Write: Set register for port n Read: output bits for port n */
  51. __O uint32_t CLR[32]; /*!< Offset 0x2280: Clear port n */
  52. __O uint32_t NOT[32]; /*!< Offset 0x2300: Toggle port n */
  53. __O uint32_t DIRSET[32]; /*!< Offset 0x2380: Set Direction */
  54. __O uint32_t DIRCLR[32]; /*!< Offset 0x2400: Clear Direction */
  55. __O uint32_t DIRNOT[32]; /*!< Offset 0x2480: Toggle Dirction */
  56. } LPC_GPIO_T;
  57. /**
  58. * @brief Initialize GPIO block
  59. * @param pGPIO : The base of GPIO peripheral on the chip
  60. * @return Nothing
  61. */
  62. STATIC INLINE void Chip_GPIO_Init(LPC_GPIO_T *pGPIO)
  63. {
  64. LPC_SYSCTL->SYSAHBCLKCTRL = (1 << SYSCTL_CLOCK_GPIO) | (LPC_SYSCTL->SYSAHBCLKCTRL & ~SYSCTL_SYSAHBCLKCTRL_RESERVED);
  65. }
  66. /**
  67. * @brief De-Initialize GPIO block
  68. * @param pGPIO : The base of GPIO peripheral on the chip
  69. * @return Nothing
  70. */
  71. STATIC INLINE void Chip_GPIO_DeInit(LPC_GPIO_T *pGPIO)
  72. {
  73. LPC_SYSCTL->SYSAHBCLKCTRL &= ~((1 << SYSCTL_CLOCK_GPIO) | SYSCTL_SYSAHBCLKCTRL_RESERVED);
  74. }
  75. /**
  76. * @brief Set GPIO direction for a single GPIO pin
  77. * @param pGPIO : The base of GPIO peripheral on the chip
  78. * @param port : GPIO port to set (supports port 0 only)
  79. * @param pin : GPIO pin to set direction on as output
  80. * @param isOutput: If new direction is output
  81. * @return Nothing
  82. */
  83. STATIC INLINE void Chip_GPIO_PinSetDIR(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin, bool isOutput)
  84. {
  85. #ifdef CHIP_LPC82X
  86. if (isOutput)
  87. pGPIO->DIRSET[port] = 1UL << pin;
  88. else
  89. pGPIO->DIRCLR[port] = 1UL << pin;
  90. #else
  91. if (isOutput)
  92. pGPIO->DIR[port] |= 1UL << pin;
  93. else
  94. pGPIO->DIR[port] &= ~(1UL << pin);
  95. #endif
  96. }
  97. /**
  98. * @brief Get GPIO direction for a single GPIO pin
  99. * @param pGPIO : The base of GPIO peripheral on the chip
  100. * @param port : GPIO port to read (supports port 0 only)
  101. * @param pin : GPIO pin to get direction for
  102. * @return true if the GPIO is an output, false if input
  103. */
  104. STATIC INLINE bool Chip_GPIO_PinGetDIR(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin)
  105. {
  106. return (bool) (((pGPIO->DIR[port]) >> pin) & 1);
  107. }
  108. /**
  109. * @brief Toggle GPIO direction for a single GPIO pin
  110. * @param pGPIO : The base of GPIO peripheral on the chip
  111. * @param port : GPIO port to set (supports port 0 only)
  112. * @param pin : GPIO pin to toggle direction
  113. * @return Nothing
  114. */
  115. STATIC INLINE void Chip_GPIO_PinToggleDIR(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin)
  116. {
  117. #ifdef CHIP_LPC82X
  118. pGPIO->DIRNOT[port] = 1UL << pin;
  119. #else
  120. pGPIO->DIR[port] ^= 1UL << pin;
  121. #endif
  122. }
  123. /**
  124. * @brief Set a GPIO pin state via the GPIO byte register
  125. * @param pGPIO : The base of GPIO peripheral on the chip
  126. * @param port : GPIO port to set (supports port 0 only)
  127. * @param pin : GPIO pin to set
  128. * @param setting : true for high, false for low
  129. * @return Nothing
  130. * @note This function replaces Chip_GPIO_WritePortBit()
  131. */
  132. STATIC INLINE void Chip_GPIO_PinSetState(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin, bool setting)
  133. {
  134. pGPIO->B[port][pin] = setting;
  135. }
  136. /**
  137. * @brief Get a GPIO pin state via the GPIO byte register
  138. * @param pGPIO : The base of GPIO peripheral on the chip
  139. * @param port : GPIO port to read (supports port 0 only)
  140. * @param pin : GPIO pin to get state for
  141. * @return true if the GPIO is high, false if low
  142. * @note This function replaces Chip_GPIO_ReadPortBit()
  143. */
  144. STATIC INLINE bool Chip_GPIO_PinGetState(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin)
  145. {
  146. return (bool) pGPIO->B[port][pin];
  147. }
  148. /**
  149. * @brief Get a GPIO pin state via the GPIO byte register
  150. * @param pGPIO : The base of GPIO peripheral on the chip
  151. * @param port : GPIO port to read (supports port 0 only)
  152. * @param pin : GPIO pin to get state for
  153. * @return true if the GPIO is high, false if low
  154. * @note This function replaces Chip_GPIO_ReadPortBit()
  155. */
  156. STATIC INLINE void Chip_GPIO_PinToggleState(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin)
  157. {
  158. pGPIO->NOT[port] = 1UL << pin;
  159. }
  160. /**
  161. * @brief Set GPIO direction for a all selected GPIO pins
  162. * @param pGPIO : The base of GPIO peripheral on the chip
  163. * @param port : port Number (supports port 0 only)
  164. * @param pinMask : GPIO pin mask to set direction on as output (bits 0..b for pins 0..n)
  165. * @param isOutput: If new direction is output
  166. * @return Nothing
  167. * @note Sets multiple GPIO pins to the output direction, each bit's position that is
  168. * high sets the corresponding pin number for that bit to an output.
  169. */
  170. STATIC INLINE void Chip_GPIO_PortSetDIR(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pinMask, bool isOutput)
  171. {
  172. #ifdef CHIP_LPC82X
  173. if (isOutput)
  174. pGPIO->DIRSET[port] = pinMask;
  175. else
  176. pGPIO->DIRCLR[port] = pinMask;
  177. #else
  178. if (isOutput)
  179. pGPIO->DIR[port] |= pinMask;
  180. else
  181. pGPIO->DIR[port] &= ~pinMask;
  182. #endif
  183. }
  184. /**
  185. * @brief Get GPIO direction for a all GPIO pins
  186. * @param pGPIO : The base of GPIO peripheral on the chip
  187. * @param port : port Number (supports port 0 only)
  188. * @return a bitfield containing the input and output states for each pin
  189. * @note For pins 0..n, a high state in a bit corresponds to an output state for the
  190. * same pin, while a low state corresponds to an input state.
  191. */
  192. STATIC INLINE uint32_t Chip_GPIO_PortGetDIR(LPC_GPIO_T *pGPIO, uint8_t port)
  193. {
  194. return pGPIO->DIR[port];
  195. }
  196. /**
  197. * @brief Toggle GPIO direction for a all selected GPIO pins
  198. * @param pGPIO : The base of GPIO peripheral on the chip
  199. * @param port : port Number (supports port 0 only)
  200. * @param pinMask : GPIO pin mask Toggle direction (bits 0..n for pins 0..n)
  201. * @return Nothing
  202. * @note Toggles multiple GPIO pin's direction, each bit's position that is
  203. * high toggles direction of the corresponding pin number.
  204. */
  205. STATIC INLINE void Chip_GPIO_PortToggleDIR(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pinMask)
  206. {
  207. #ifdef CHIP_LPC82X
  208. pGPIO->DIRNOT[port] = pinMask;
  209. #else
  210. pGPIO->DIR[port] ^= pinMask;
  211. #endif
  212. }
  213. /**
  214. * @brief Set all GPIO raw pin states (regardless of masking)
  215. * @param pGPIO : The base of GPIO peripheral on the chip
  216. * @param port : port Number (supports port 0 only)
  217. * @param value : Value to set all GPIO pin states (0..n) to
  218. * @return Nothing
  219. */
  220. STATIC INLINE void Chip_GPIO_PortSetState(LPC_GPIO_T *pGPIO, uint8_t port, uint32_t value)
  221. {
  222. pGPIO->PIN[port] = value;
  223. }
  224. /**
  225. * @brief Get all GPIO raw pin states (regardless of masking)
  226. * @param pGPIO : The base of GPIO peripheral on the chip
  227. * @param port : port Number (supports port 0 only)
  228. * @return Current (raw) state of all GPIO pins
  229. */
  230. STATIC INLINE uint32_t Chip_GPIO_PortGetState(LPC_GPIO_T *pGPIO, uint8_t port)
  231. {
  232. return pGPIO->PIN[port];
  233. }
  234. /**
  235. * @brief Toggle selected GPIO output pins to the opposite state
  236. * @param pGPIO : The base of GPIO peripheral on the chip
  237. * @param port : port Number (supports port 0 only)
  238. * @param pins : pins (0..n) to toggle
  239. * @return None
  240. * @note Any bit set as a '0' will not have it's state changed. This only
  241. * applies to ports configured as an output.
  242. */
  243. STATIC INLINE void Chip_GPIO_PortToggleState(LPC_GPIO_T *pGPIO, uint8_t port, uint32_t pins)
  244. {
  245. pGPIO->NOT[port] = pins;
  246. }
  247. /**
  248. * @brief Set selected GPIO output pins to the high state
  249. * @param pGPIO : The base of GPIO peripheral on the chip
  250. * @param port : port Number (supports port 0 only)
  251. * @param pins : pins (0..n) to set high
  252. * @return None
  253. * @note Any bit set as a '0' will not have it's state changed. This only
  254. * applies to ports configured as an output.
  255. */
  256. STATIC INLINE void Chip_GPIO_PortSetOutHigh(LPC_GPIO_T *pGPIO, uint8_t port, uint32_t bmPins)
  257. {
  258. pGPIO->SET[port] = bmPins;
  259. }
  260. /**
  261. * @brief Set selected GPIO output pins to the low state
  262. * @param pGPIO : The base of GPIO peripheral on the chip
  263. * @param port : port Number (supports port 0 only)
  264. * @param pins : pins (0..n) to set low
  265. * @return None
  266. * @note Any bit set as a '0' will not have it's state changed. This only
  267. * applies to ports configured as an output.
  268. */
  269. STATIC INLINE void Chip_GPIO_PortSetOutLow(LPC_GPIO_T *pGPIO, uint8_t port, uint32_t pins)
  270. {
  271. pGPIO->CLR[port] = pins;
  272. }
  273. /**
  274. * @brief Set GPIO port mask value for GPIO masked read and write
  275. * @param pGPIO : The base of GPIO peripheral on the chip
  276. * @param port : port Number (supports port 0 only)
  277. * @param mask : Mask value for read and write (only low bits are enabled)
  278. * @return Nothing
  279. * @note Controls which bits are set or unset when using the masked
  280. * GPIO read and write functions. A low state indicates the pin is settable
  281. * and readable via the masked write and read functions.
  282. */
  283. STATIC INLINE void Chip_GPIO_PortSetMask(LPC_GPIO_T *pGPIO, uint8_t port, uint32_t mask)
  284. {
  285. pGPIO->MASK[port] = mask;
  286. }
  287. /**
  288. * @brief Get GPIO port mask value used for GPIO masked read and write
  289. * @param pGPIO : The base of GPIO peripheral on the chip
  290. * @param port : port Number (supports port 0 only)
  291. * @return Returns value set with the Chip_GPIO_PortSetMask() function.
  292. * @note A high bit in the return value indicates that that GPIO pin for the
  293. * port cannot be set using the masked write function.
  294. */
  295. STATIC INLINE uint32_t Chip_GPIO_PortGetMask(LPC_GPIO_T *pGPIO, uint8_t port)
  296. {
  297. return pGPIO->MASK[port];
  298. }
  299. /**
  300. * @brief Set all GPIO pin states, but mask via the MASKP0 register
  301. * @param pGPIO : The base of GPIO peripheral on the chip
  302. * @param port : port Number (supports port 0 only)
  303. * @param value : Value to set all GPIO pin states (0..n) to
  304. * @return Nothing
  305. */
  306. STATIC INLINE void Chip_GPIO_PortSetMaskedState(LPC_GPIO_T *pGPIO, uint8_t port, uint32_t value)
  307. {
  308. pGPIO->MPIN[port] = value;
  309. }
  310. /**
  311. * @brief Get all GPIO pin statesm but mask via the MASKP0 register
  312. * @param pGPIO : The base of GPIO peripheral on the chip
  313. * @param port : port Number (supports port 0 only)
  314. * @return Current (masked) state of all GPIO pins
  315. */
  316. STATIC INLINE uint32_t Chip_GPIO_PortGetMaskedState(LPC_GPIO_T *pGPIO, uint8_t port)
  317. {
  318. return pGPIO->MPIN[port];
  319. }
  320. /**
  321. * @}
  322. */
  323. #ifdef __cplusplus
  324. }
  325. #endif
  326. #endif /* __GPIO_8XX_H_ */