gpio_5410x.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * @brief LPC5410X GPIO driver
  3. *
  4. * @note
  5. * Copyright(C) NXP Semiconductors, 2014
  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_5410X_H_
  32. #define __GPIO_5410X_H_
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /** @defgroup GPIO_5410X CHIP: LPC5410X GPIO driver
  37. * @ingroup CHIP_5410X_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. } LPC_GPIO_T;
  54. /**
  55. * @brief Initialize GPIO block
  56. * @param pGPIO : The base of GPIO peripheral on the chip
  57. * @return Nothing
  58. */
  59. void Chip_GPIO_Init(LPC_GPIO_T *pGPIO);
  60. /**
  61. * @brief De-Initialize GPIO block
  62. * @param pGPIO : The base of GPIO peripheral on the chip
  63. * @return Nothing
  64. */
  65. void Chip_GPIO_DeInit(LPC_GPIO_T *pGPIO);
  66. /**
  67. * @brief Set a GPIO port/pin state
  68. * @param pGPIO : The base of GPIO peripheral on the chip
  69. * @param port : GPIO port to set
  70. * @param pin : GPIO pin to set
  71. * @param setting : true for high, false for low
  72. * @return Nothing
  73. * @note It is recommended to use the Chip_GPIO_SetPinState() function instead.
  74. */
  75. STATIC INLINE void Chip_GPIO_WritePortBit(LPC_GPIO_T *pGPIO, uint32_t port, uint8_t pin, bool setting)
  76. {
  77. pGPIO->B[port][pin] = setting;
  78. }
  79. /**
  80. * @brief Set a GPIO pin state via the GPIO byte register
  81. * @param pGPIO : The base of GPIO peripheral on the chip
  82. * @param port : GPIO port to set
  83. * @param pin : GPIO pin to set
  84. * @param setting : true for high, false for low
  85. * @return Nothing
  86. * @note This function replaces Chip_GPIO_WritePortBit()
  87. */
  88. STATIC INLINE void Chip_GPIO_SetPinState(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin, bool setting)
  89. {
  90. pGPIO->B[port][pin] = setting;
  91. }
  92. /**
  93. * @brief Read a GPIO pin state via the GPIO byte register
  94. * @param pGPIO : The base of GPIO peripheral on the chip
  95. * @param port : GPIO port to read
  96. * @param pin : GPIO pin to read
  97. * @return true if the GPIO pin is high, false if low
  98. * @note It is recommended to use the Chip_GPIO_GetPinState() function instead.
  99. */
  100. STATIC INLINE bool Chip_GPIO_ReadPortBit(LPC_GPIO_T *pGPIO, uint32_t port, uint8_t pin)
  101. {
  102. return (bool) pGPIO->B[port][pin];
  103. }
  104. /**
  105. * @brief Get a GPIO pin state via the GPIO byte register
  106. * @param pGPIO : The base of GPIO peripheral on the chip
  107. * @param port : GPIO port to read
  108. * @param pin : GPIO pin to get state for
  109. * @return true if the GPIO is high, false if low
  110. * @note This function replaces Chip_GPIO_ReadPortBit()
  111. */
  112. STATIC INLINE bool Chip_GPIO_GetPinState(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin)
  113. {
  114. return (bool) pGPIO->B[port][pin];
  115. }
  116. /**
  117. * @brief Set GPIO direction for a single GPIO pin
  118. * @param pGPIO : The base of GPIO peripheral on the chip
  119. * @param port : GPIO port to set
  120. * @param pin : GPIO pin to set
  121. * @param setting : true for output, false for input
  122. * @return Nothing
  123. * @note It is recommended to use the Chip_GPIO_SetPinDIROutput(),
  124. * Chip_GPIO_SetPinDIRInput() or Chip_GPIO_SetPinDIR() functions instead
  125. * of this function.
  126. */
  127. void Chip_GPIO_WriteDirBit(LPC_GPIO_T *pGPIO, uint32_t port, uint8_t pin, bool setting);
  128. /**
  129. * @brief Set GPIO direction for a single GPIO pin to an output
  130. * @param pGPIO : The base of GPIO peripheral on the chip
  131. * @param port : GPIO port to set
  132. * @param pin : GPIO pin to set direction on as output
  133. * @return Nothing
  134. */
  135. STATIC INLINE void Chip_GPIO_SetPinDIROutput(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin)
  136. {
  137. pGPIO->DIR[port] |= 1UL << pin;
  138. }
  139. /**
  140. * @brief Set GPIO direction for a single GPIO pin to an input
  141. * @param pGPIO : The base of GPIO peripheral on the chip
  142. * @param port : GPIO port to set
  143. * @param pin : GPIO pin to set direction on as input
  144. * @return Nothing
  145. */
  146. STATIC INLINE void Chip_GPIO_SetPinDIRInput(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin)
  147. {
  148. pGPIO->DIR[port] &= ~(1UL << pin);
  149. }
  150. /**
  151. * @brief Set GPIO direction for a single GPIO pin
  152. * @param pGPIO : The base of GPIO peripheral on the chip
  153. * @param port : GPIO port to set
  154. * @param pin : GPIO pin to set direction for
  155. * @param output : true for output, false for input
  156. * @return Nothing
  157. */
  158. void Chip_GPIO_SetPinDIR(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin, bool output);
  159. /**
  160. * @brief Read a GPIO direction (out or in)
  161. * @param pGPIO : The base of GPIO peripheral on the chip
  162. * @param port : GPIO port to read
  163. * @param bit : GPIO bit direction to read
  164. * @return true if the GPIO is an output, false if input
  165. * @note It is recommended to use the Chip_GPIO_GetPinDIR() function instead.
  166. */
  167. STATIC INLINE bool Chip_GPIO_ReadDirBit(LPC_GPIO_T *pGPIO, uint32_t port, uint8_t bit)
  168. {
  169. return (bool) (((pGPIO->DIR[port]) >> bit) & 1);
  170. }
  171. /**
  172. * @brief Get GPIO direction for a single GPIO pin
  173. * @param pGPIO : The base of GPIO peripheral on the chip
  174. * @param port : GPIO port to read (supports port 0 only)
  175. * @param pin : GPIO pin to get direction for
  176. * @return true if the GPIO is an output, false if input
  177. */
  178. STATIC INLINE bool Chip_GPIO_GetPinDIR(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin)
  179. {
  180. return Chip_GPIO_ReadDirBit(pGPIO, port, pin);
  181. }
  182. /**
  183. * @brief Set Direction for a GPIO port
  184. * @param pGPIO : The base of GPIO peripheral on the chip
  185. * @param portNum : port Number
  186. * @param bitValue : GPIO bit to set
  187. * @param out : Direction value, 0 = input, !0 = output
  188. * @return None
  189. * @note Bits set to '0' are not altered. It is recommended to use the
  190. * Chip_GPIO_SetPortDIR() function instead.
  191. */
  192. void Chip_GPIO_SetDir(LPC_GPIO_T *pGPIO, uint8_t portNum, uint32_t bitValue, uint8_t out);
  193. /**
  194. * @brief Set GPIO direction for a all selected GPIO pins to an output
  195. * @param pGPIO : The base of GPIO peripheral on the chip
  196. * @param port : port Number
  197. * @param pinMask : GPIO pin mask to set direction on as output (bits 0..b for pins 0..n)
  198. * @return Nothing
  199. * @note Sets multiple GPIO pins to the output direction, each bit's position that is
  200. * high sets the corresponding pin number for that bit to an output.
  201. */
  202. STATIC INLINE void Chip_GPIO_SetPortDIROutput(LPC_GPIO_T *pGPIO, uint8_t port, uint32_t pinMask)
  203. {
  204. pGPIO->DIR[port] |= pinMask;
  205. }
  206. /**
  207. * @brief Set GPIO direction for a all selected GPIO pins to an input
  208. * @param pGPIO : The base of GPIO peripheral on the chip
  209. * @param port : port Number
  210. * @param pinMask : GPIO pin mask to set direction on as input (bits 0..b for pins 0..n)
  211. * @return Nothing
  212. * @note Sets multiple GPIO pins to the input direction, each bit's position that is
  213. * high sets the corresponding pin number for that bit to an input.
  214. */
  215. STATIC INLINE void Chip_GPIO_SetPortDIRInput(LPC_GPIO_T *pGPIO, uint8_t port, uint32_t pinMask)
  216. {
  217. pGPIO->DIR[port] &= ~pinMask;
  218. }
  219. /**
  220. * @brief Set GPIO direction for a all selected GPIO pins to an input or output
  221. * @param pGPIO : The base of GPIO peripheral on the chip
  222. * @param port : port Number
  223. * @param pinMask : GPIO pin mask to set direction on (bits 0..b for pins 0..n)
  224. * @param outSet : Direction value, false = set as inputs, true = set as outputs
  225. * @return Nothing
  226. * @note Sets multiple GPIO pins to the input direction, each bit's position that is
  227. * high sets the corresponding pin number for that bit to an input.
  228. */
  229. void Chip_GPIO_SetPortDIR(LPC_GPIO_T *pGPIO, uint8_t port, uint32_t pinMask, bool outSet);
  230. /**
  231. * @brief Get GPIO direction for a all GPIO pins
  232. * @param pGPIO : The base of GPIO peripheral on the chip
  233. * @param port : port Number
  234. * @return a bitfield containing the input and output states for each pin
  235. * @note For pins 0..n, a high state in a bit corresponds to an output state for the
  236. * same pin, while a low state corresponds to an input state.
  237. */
  238. STATIC INLINE uint32_t Chip_GPIO_GetPortDIR(LPC_GPIO_T *pGPIO, uint8_t port)
  239. {
  240. return pGPIO->DIR[port];
  241. }
  242. /**
  243. * @brief Set GPIO port mask value for GPIO masked read and write
  244. * @param pGPIO : The base of GPIO peripheral on the chip
  245. * @param port : port Number (supports port 0 only)
  246. * @param mask : Mask value for read and write
  247. * @return Nothing
  248. * @note Controls which bits corresponding to PIO0_n are active in the P0MPORT
  249. * register (bit 0 = PIO0_0, bit 1 = PIO0_1, ..., bit 17 = PIO0_17).
  250. */
  251. STATIC INLINE void Chip_GPIO_SetPortMask(LPC_GPIO_T *pGPIO, uint8_t port, uint32_t mask)
  252. {
  253. pGPIO->MASK[port] = mask;
  254. }
  255. /**
  256. * @brief Get GPIO port mask value used for GPIO masked read and write
  257. * @param pGPIO : The base of GPIO peripheral on the chip
  258. * @param port : port Number (supports port 0 only)
  259. * @return Returns value set with the Chip_GPIO_SetPortMask() function.
  260. */
  261. STATIC INLINE uint32_t Chip_GPIO_GetPortMask(LPC_GPIO_T *pGPIO, uint8_t port)
  262. {
  263. return pGPIO->MASK[port];
  264. }
  265. /**
  266. * @brief Set all GPIO raw pin states (regardless of masking)
  267. * @param pGPIO : The base of GPIO peripheral on the chip
  268. * @param port : port Number (supports port 0 only)
  269. * @param value : Value to set all GPIO pin states (0..n) to
  270. * @return Nothing
  271. */
  272. STATIC INLINE void Chip_GPIO_SetPortValue(LPC_GPIO_T *pGPIO, uint8_t port, uint32_t value)
  273. {
  274. pGPIO->PIN[port] = value;
  275. }
  276. /**
  277. * @brief Get all GPIO raw pin states (regardless of masking)
  278. * @param pGPIO : The base of GPIO peripheral on the chip
  279. * @param port : port Number (supports port 0 only)
  280. * @return Current (raw) state of all GPIO pins
  281. */
  282. STATIC INLINE uint32_t Chip_GPIO_GetPortValue(LPC_GPIO_T *pGPIO, uint8_t port)
  283. {
  284. return pGPIO->PIN[port];
  285. }
  286. /**
  287. * @brief Set all GPIO pin states, but mask via the MASKP0 register
  288. * @param pGPIO : The base of GPIO peripheral on the chip
  289. * @param port : port Number (supports port 0 only)
  290. * @param value : Value to set all GPIO pin states (0..n) to
  291. * @return Nothing
  292. */
  293. STATIC INLINE void Chip_GPIO_SetMaskedPortValue(LPC_GPIO_T *pGPIO, uint8_t port, uint32_t value)
  294. {
  295. pGPIO->MPIN[port] = value;
  296. }
  297. /**
  298. * @brief Get all GPIO pin statesm but mask via the MASKP0 register
  299. * @param pGPIO : The base of GPIO peripheral on the chip
  300. * @param port : port Number (supports port 0 only)
  301. * @return Current (masked) state of all GPIO pins
  302. */
  303. STATIC INLINE uint32_t Chip_GPIO_GetMaskedPortValue(LPC_GPIO_T *pGPIO, uint8_t port)
  304. {
  305. return pGPIO->MPIN[port];
  306. }
  307. /**
  308. * @brief Set a GPIO port/bit to the high state
  309. * @param pGPIO : The base of GPIO peripheral on the chip
  310. * @param portNum : port number (supports port 0 only)
  311. * @param bitValue : bit(s) in the port to set high
  312. * @return None
  313. * @note Any bit set as a '0' will not have it's state changed. This only
  314. * applies to ports configured as an output. It is recommended to use the
  315. * Chip_GPIO_SetPortOutHigh() function instead.
  316. */
  317. STATIC INLINE void Chip_GPIO_SetValue(LPC_GPIO_T *pGPIO, uint8_t portNum, uint32_t bitValue)
  318. {
  319. pGPIO->SET[portNum] = bitValue;
  320. }
  321. /**
  322. * @brief Set selected GPIO output pins to the high state
  323. * @param pGPIO : The base of GPIO peripheral on the chip
  324. * @param port : port Number (supports port 0 only)
  325. * @param pins : pins (0..n) to set high
  326. * @return None
  327. * @note Any bit set as a '0' will not have it's state changed. This only
  328. * applies to ports configured as an output.
  329. */
  330. STATIC INLINE void Chip_GPIO_SetPortOutHigh(LPC_GPIO_T *pGPIO, uint8_t port, uint32_t pins)
  331. {
  332. pGPIO->SET[port] = pins;
  333. }
  334. /**
  335. * @brief Set an individual GPIO output pin to the high state
  336. * @param pGPIO : The base of GPIO peripheral on the chip
  337. * @param port : port Number (supports port 0 only)
  338. * @param pin : pin number (0..n) to set high
  339. * @return None
  340. * @note Any bit set as a '0' will not have it's state changed. This only
  341. * applies to ports configured as an output.
  342. */
  343. STATIC INLINE void Chip_GPIO_SetPinOutHigh(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin)
  344. {
  345. pGPIO->SET[port] = (1 << pin);
  346. }
  347. /**
  348. * @brief Set a GPIO port/bit to the low state
  349. * @param pGPIO : The base of GPIO peripheral on the chip
  350. * @param portNum : port number (support port 0 only)
  351. * @param bitValue : bit(s) in the port to set low
  352. * @return None
  353. * @note Any bit set as a '0' will not have it's state changed. This only
  354. * applies to ports configured as an output. It is recommended to use the
  355. * Chip_GPIO_SetPortOutLow() function instead.
  356. */
  357. STATIC INLINE void Chip_GPIO_ClearValue(LPC_GPIO_T *pGPIO, uint8_t portNum, uint32_t bitValue)
  358. {
  359. pGPIO->CLR[portNum] = bitValue;
  360. }
  361. /**
  362. * @brief Set selected GPIO output pins to the low state
  363. * @param pGPIO : The base of GPIO peripheral on the chip
  364. * @param port : port Number (supports port 0 only)
  365. * @param pins : pins (0..n) to set low
  366. * @return None
  367. * @note Any bit set as a '0' will not have it's state changed. This only
  368. * applies to ports configured as an output.
  369. */
  370. STATIC INLINE void Chip_GPIO_SetPortOutLow(LPC_GPIO_T *pGPIO, uint8_t port, uint32_t pins)
  371. {
  372. pGPIO->CLR[port] = pins;
  373. }
  374. /**
  375. * @brief Set an individual GPIO output pin to the low state
  376. * @param pGPIO : The base of GPIO peripheral on the chip
  377. * @param port : port Number (supports port 0 only)
  378. * @param pin : pin number (0..n) to set low
  379. * @return None
  380. * @note Any bit set as a '0' will not have it's state changed. This only
  381. * applies to ports configured as an output.
  382. */
  383. STATIC INLINE void Chip_GPIO_SetPinOutLow(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin)
  384. {
  385. pGPIO->CLR[port] = (1 << pin);
  386. }
  387. /**
  388. * @brief Toggle selected GPIO output pins to the opposite state
  389. * @param pGPIO : The base of GPIO peripheral on the chip
  390. * @param port : port Number (supports port 0 only)
  391. * @param pins : pins (0..n) to toggle
  392. * @return None
  393. * @note Any bit set as a '0' will not have it's state changed. This only
  394. * applies to ports configured as an output.
  395. */
  396. STATIC INLINE void Chip_GPIO_SetPortToggle(LPC_GPIO_T *pGPIO, uint8_t port, uint32_t pins)
  397. {
  398. pGPIO->NOT[port] = pins;
  399. }
  400. /**
  401. * @brief Toggle an individual GPIO output pin to the opposite state
  402. * @param pGPIO : The base of GPIO peripheral on the chip
  403. * @param port : port Number (supports port 0 only)
  404. * @param pin : pin number (0..n) to toggle
  405. * @return None
  406. * @note Any bit set as a '0' will not have it's state changed. This only
  407. * applies to ports configured as an output.
  408. */
  409. STATIC INLINE void Chip_GPIO_SetPinToggle(LPC_GPIO_T *pGPIO, uint8_t port, uint8_t pin)
  410. {
  411. pGPIO->NOT[port] = (1 << pin);
  412. }
  413. /**
  414. * @brief Read current bit states for the selected port
  415. * @param pGPIO : The base of GPIO peripheral on the chip
  416. * @param portNum : port number to read (supports port 0 only)
  417. * @return Current value of GPIO port
  418. * @note The current states of the bits for the port are read, regardless of
  419. * whether the GPIO port bits are input or output. It is recommended to use the
  420. * Chip_GPIO_GetPortValue() function instead.
  421. */
  422. STATIC INLINE uint32_t Chip_GPIO_ReadValue(LPC_GPIO_T *pGPIO, uint8_t portNum)
  423. {
  424. return pGPIO->PIN[portNum];
  425. }
  426. /**
  427. * @}
  428. */
  429. #ifdef __cplusplus
  430. }
  431. #endif
  432. #endif /* __GPIO_5410X_H_ */