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