1 /**********************************************************************
\r
2 * $Id$ lpc18xx_gpio.c 2011-06-02
\r
4 * @file lpc18xx_gpio.c
\r
5 * @brief Contains all functions support for GPIO firmware library
\r
8 * @date 02. June. 2011
\r
9 * @author NXP MCU SW Application Team
\r
11 * Copyright(C) 2011, NXP Semiconductor
\r
12 * All rights reserved.
\r
14 ***********************************************************************
\r
15 * Software that is described herein is for illustrative purposes only
\r
16 * which provides customers with programming information regarding the
\r
17 * products. This software is supplied "AS IS" without any warranties.
\r
18 * NXP Semiconductors assumes no responsibility or liability for the
\r
19 * use of the software, conveys no license or title under any patent,
\r
20 * copyright, or mask work right to the product. NXP Semiconductors
\r
21 * reserves the right to make changes in the software without
\r
22 * notification. NXP Semiconductors also make no representation or
\r
23 * warranty that such application will be suitable for the specified
\r
24 * use without further testing or modification.
\r
25 **********************************************************************/
\r
27 /* Peripheral group ----------------------------------------------------------- */
\r
28 /** @addtogroup GPIO
\r
32 /* Includes ------------------------------------------------------------------- */
\r
33 #include "lpc18xx_gpio.h"
\r
34 #include "lpc_types.h"
\r
36 /* If this source file built with example, the LPC18xx FW library configuration
\r
37 * file in each example directory ("lpc18xx_libcfg.h") must be included,
\r
38 * otherwise the default FW library configuration file must be included instead
\r
40 #ifdef __BUILD_WITH_EXAMPLE__
\r
41 #include "lpc18xx_libcfg.h"
\r
43 #include "lpc18xx_libcfg_default.h"
\r
44 #endif /* __BUILD_WITH_EXAMPLE__ */
\r
49 /* Private Functions ---------------------------------------------------------- */
\r
51 //static LPC_GPIOn_Type *GPIO_GetPointer(uint8_t portNum);
\r
52 //static GPIO_HalfWord_TypeDef *FIO_HalfWordGetPointer(uint8_t portNum);
\r
53 //static GPIO_Byte_TypeDef *FIO_ByteGetPointer(uint8_t portNum);
\r
56 /*********************************************************************//**
\r
57 * @brief Get pointer to GPIO peripheral due to GPIO port
\r
58 * @param[in] portNum Port Number value, should be in range from 0 to 4.
\r
59 * @return Pointer to GPIO peripheral
\r
60 **********************************************************************/
\r
61 static LPC_GPIOn_Type *GPIO_GetPointer(uint8_t portNum)
\r
63 LPC_GPIOn_Type *pGPIO = NULL;
\r
94 /*********************************************************************//**
\r
95 * @brief Get pointer to FIO peripheral in halfword accessible style
\r
97 * @param[in] portNum Port Number value, should be in range from 0 to 4.
\r
98 * @return Pointer to FIO peripheral
\r
99 **********************************************************************/
\r
100 static GPIO_HalfWord_TypeDef *FIO_HalfWordGetPointer(uint8_t portNum)
\r
102 GPIO_HalfWord_TypeDef *pFIO = NULL;
\r
107 pFIO = GPIO0_HalfWord;
\r
111 pFIO = GPIO1_HalfWord;
\r
115 pFIO = GPIO2_HalfWord;
\r
119 pFIO = GPIO3_HalfWord;
\r
123 pFIO = GPIO4_HalfWord;
\r
132 /*********************************************************************//**
\r
133 * @brief Get pointer to FIO peripheral in byte accessible style
\r
135 * @param[in] portNum Port Number value, should be in range from 0 to 4.
\r
136 * @return Pointer to FIO peripheral
\r
137 **********************************************************************/
\r
138 static GPIO_Byte_TypeDef *FIO_ByteGetPointer(uint8_t portNum)
\r
140 GPIO_Byte_TypeDef *pFIO = NULL;
\r
172 /* End of Private Functions --------------------------------------------------- */
\r
175 /* Public Functions ----------------------------------------------------------- */
\r
176 /** @addtogroup GPIO_Public_Functions
\r
181 /* GPIO ------------------------------------------------------------------------------ */
\r
183 /*********************************************************************//**
\r
184 * @brief Set Direction for GPIO port.
\r
185 * @param[in] portNum Port Number value, should be in range from 0 to 4
\r
186 * @param[in] bitValue Value that contains all bits to set direction,
\r
187 * in range from 0 to 0xFFFFFFFF.
\r
188 * example: value 0x5 to set direction for bit 0 and bit 1.
\r
189 * @param[in] dir Direction value, should be:
\r
195 * All remaining bits that are not activated in bitValue (value '0')
\r
196 * will not be effected by this function.
\r
197 **********************************************************************/
\r
198 void GPIO_SetDir(uint8_t portNum, uint32_t bitValue, uint8_t dir)
\r
202 LPC_GPIO_PORT->DIR[portNum] |= bitValue;
\r
205 LPC_GPIO_PORT->DIR[portNum] &= ~bitValue;
\r
210 /*********************************************************************//**
\r
211 * @brief Set Value for bits that have output direction on GPIO port.
\r
212 * @param[in] portNum Port number value, should be in range from 0 to 4
\r
213 * @param[in] bitValue Value that contains all bits on GPIO to set, should
\r
214 * be in range from 0 to 0xFFFFFFFF.
\r
215 * example: value 0x5 to set bit 0 and bit 1.
\r
219 * - For all bits that has been set as input direction, this function will
\r
221 * - For all remaining bits that are not activated in bitValue (value '0')
\r
222 * will not be effected by this function.
\r
223 **********************************************************************/
\r
224 void GPIO_SetValue(uint8_t portNum, uint32_t bitValue)
\r
226 LPC_GPIO_PORT->SET[portNum] = bitValue;
\r
230 /*********************************************************************//**
\r
231 * @brief Clear Value for bits that have output direction on GPIO port.
\r
232 * @param[in] portNum Port number value, should be in range from 0 to 4
\r
233 * @param[in] bitValue Value that contains all bits on GPIO to clear, should
\r
234 * be in range from 0 to 0xFFFFFFFF.
\r
235 * example: value 0x5 to clear bit 0 and bit 1.
\r
239 * - For all bits that has been set as input direction, this function will
\r
241 * - For all remaining bits that are not activated in bitValue (value '0')
\r
242 * will not be effected by this function.
\r
243 **********************************************************************/
\r
244 void GPIO_ClearValue(uint8_t portNum, uint32_t bitValue)
\r
246 LPC_GPIO_PORT->CLR[portNum] = bitValue;
\r
250 /*********************************************************************//**
\r
251 * @brief Read Current state on port pin that have input direction of GPIO
\r
252 * @param[in] portNum Port number to read value, in range from 0 to 4
\r
253 * @return Current value of GPIO port.
\r
255 * Note: Return value contain state of each port pin (bit) on that GPIO regardless
\r
256 * its direction is input or output.
\r
257 **********************************************************************/
\r
258 uint32_t GPIO_ReadValue(uint8_t portNum)
\r
260 return LPC_GPIO_PORT->PIN[portNum];
\r
265 /*********************************************************************//**
\r
266 * @brief Enable GPIO interrupt (just used for P0.0-P0.30, P2.0-P2.13)
\r
267 * @param[in] portNum Port number to read value, should be: 0 or 2
\r
268 * @param[in] bitValue Value that contains all bits on GPIO to enable,
\r
269 * should be in range from 0 to 0xFFFFFFFF.
\r
270 * @param[in] edgeState state of edge, should be:
\r
272 * - 1: Falling edge
\r
274 **********************************************************************/
\r
275 void GPIO_IntCmd(uint8_t portNum, uint32_t bitValue, uint8_t edgeState)
\r
277 if((portNum == 0)&&(edgeState == 0))
\r
278 LPC_GPIOINT->IO0IntEnR = bitValue;
\r
279 else if ((portNum == 2)&&(edgeState == 0))
\r
280 LPC_GPIOINT->IO2IntEnR = bitValue;
\r
281 else if ((portNum == 0)&&(edgeState == 1))
\r
282 LPC_GPIOINT->IO0IntEnF = bitValue;
\r
283 else if ((portNum == 2)&&(edgeState == 1))
\r
284 LPC_GPIOINT->IO2IntEnF = bitValue;
\r
291 /*********************************************************************//**
\r
292 * @brief Get GPIO Interrupt Status (just used for P0.0-P0.30, P2.0-P2.13)
\r
293 * @param[in] portNum Port number to read value, should be: 0 or 2
\r
294 * @param[in] pinNum Pin number, should be: 0..30(with port 0) and 0..13
\r
296 * @param[in] edgeState state of edge, should be:
\r
298 * - 1 :Falling edge
\r
299 * @return Function status, could be:
\r
300 * - ENABLE :Interrupt has been generated due to a rising edge on P0.0
\r
301 * - DISABLE :A rising edge has not been detected on P0.0
\r
302 **********************************************************************/
\r
303 FunctionalState GPIO_GetIntStatus(uint8_t portNum, uint32_t pinNum, uint8_t edgeState)
\r
305 if((portNum == 0) && (edgeState == 0))//Rising Edge
\r
306 return (((LPC_GPIOINT->IO0IntStatR)>>pinNum)& 0x1);
\r
307 else if ((portNum == 2) && (edgeState == 0))
\r
308 return (((LPC_GPIOINT->IO2IntStatR)>>pinNum)& 0x1);
\r
309 else if ((portNum == 0) && (edgeState == 1))//Falling Edge
\r
310 return (((LPC_GPIOINT->IO0IntStatF)>>pinNum)& 0x1);
\r
311 else if ((portNum == 2) && (edgeState == 1))
\r
312 return (((LPC_GPIOINT->IO2IntStatF)>>pinNum)& 0x1);
\r
319 /*********************************************************************//**
\r
320 * @brief Clear GPIO interrupt (just used for P0.0-P0.30, P2.0-P2.13)
\r
321 * @param[in] portNum Port number to read value, should be: 0 or 2
\r
322 * @param[in] bitValue Value that contains all bits on GPIO to enable,
\r
323 * should be in range from 0 to 0xFFFFFFFF.
\r
325 **********************************************************************/
\r
326 void GPIO_ClearInt(uint8_t portNum, uint32_t bitValue)
\r
329 LPC_GPIOINT->IO0IntClr = bitValue;
\r
330 else if (portNum == 2)
\r
331 LPC_GPIOINT->IO2IntClr = bitValue;
\r
339 /* FIO word accessible ----------------------------------------------------------------- */
\r
340 /* Stub function for FIO (word-accessible) style */
\r
343 * @brief The same with GPIO_SetDir()
\r
345 void FIO_SetDir(uint8_t portNum, uint32_t bitValue, uint8_t dir)
\r
347 GPIO_SetDir(portNum, bitValue, dir);
\r
351 * @brief The same with GPIO_SetValue()
\r
353 void FIO_SetValue(uint8_t portNum, uint32_t bitValue)
\r
355 GPIO_SetValue(portNum, bitValue);
\r
359 * @brief The same with GPIO_ClearValue()
\r
361 void FIO_ClearValue(uint8_t portNum, uint32_t bitValue)
\r
363 GPIO_ClearValue(portNum, bitValue);
\r
367 * @brief The same with GPIO_ReadValue()
\r
369 uint32_t FIO_ReadValue(uint8_t portNum)
\r
371 return (GPIO_ReadValue(portNum));
\r
377 * @brief The same with GPIO_IntCmd()
\r
379 void FIO_IntCmd(uint8_t portNum, uint32_t bitValue, uint8_t edgeState)
\r
381 GPIO_IntCmd(portNum, bitValue, edgeState);
\r
385 * @brief The same with GPIO_GetIntStatus()
\r
387 FunctionalState FIO_GetIntStatus(uint8_t portNum, uint32_t pinNum, uint8_t edgeState)
\r
389 return (GPIO_GetIntStatus(portNum, pinNum, edgeState));
\r
393 * @brief The same with GPIO_ClearInt()
\r
395 void FIO_ClearInt(uint8_t portNum, uint32_t bitValue)
\r
397 GPIO_ClearInt(portNum, bitValue);
\r
402 /*********************************************************************//**
\r
403 * @brief Set mask value for bits in FIO port
\r
404 * @param[in] portNum Port number, in range from 0 to 4
\r
405 * @param[in] bitValue Value that contains all bits in to set, should be
\r
406 * in range from 0 to 0xFFFFFFFF.
\r
407 * @param[in] maskValue Mask value contains state value for each bit:
\r
413 * - All remaining bits that are not activated in bitValue (value '0')
\r
414 * will not be effected by this function.
\r
415 * - After executing this function, in mask register, value '0' on each bit
\r
416 * enables an access to the corresponding physical pin via a read or write access,
\r
417 * while value '1' on bit (masked) that corresponding pin will not be changed
\r
418 * with write access and if read, will not be reflected in the updated pin.
\r
419 **********************************************************************/
\r
420 void FIO_SetMask(uint8_t portNum, uint32_t bitValue, uint8_t maskValue)
\r
424 LPC_GPIO_PORT->MASK[portNum] |= bitValue;
\r
427 LPC_GPIO_PORT->MASK[portNum] &= ~bitValue;
\r
432 /* FIO halfword accessible ------------------------------------------------------------- */
\r
434 /*********************************************************************//**
\r
435 * @brief Set direction for FIO port in halfword accessible style
\r
436 * @param[in] portNum Port number, in range from 0 to 4
\r
437 * @param[in] halfwordNum HalfWord part number, should be 0 (lower) or 1(upper)
\r
438 * @param[in] bitValue Value that contains all bits in to set direction,
\r
439 * in range from 0 to 0xFFFF.
\r
440 * @param[in] dir Direction value, should be:
\r
445 * Note: All remaining bits that are not activated in bitValue (value '0')
\r
446 * will not be effected by this function.
\r
447 **********************************************************************/
\r
448 void FIO_HalfWordSetDir(uint8_t portNum, uint8_t halfwordNum, uint16_t bitValue, uint8_t dir)
\r
450 GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum);
\r
454 // Output direction
\r
460 pFIO->FIODIRU |= bitValue;
\r
465 pFIO->FIODIRL |= bitValue;
\r
474 pFIO->FIODIRU &= ~bitValue;
\r
479 pFIO->FIODIRL &= ~bitValue;
\r
486 /*********************************************************************//**
\r
487 * @brief Set mask value for bits in FIO port in halfword accessible style
\r
488 * @param[in] portNum Port number, in range from 0 to 4
\r
489 * @param[in] halfwordNum HalfWord part number, should be 0 (lower) or 1(upper)
\r
490 * @param[in] bitValue Value that contains all bits in to set,
\r
491 * in range from 0 to 0xFFFF.
\r
492 * @param[in] maskValue Mask value contains state value for each bit:
\r
498 * - All remaining bits that are not activated in bitValue (value '0')
\r
499 * will not be effected by this function.
\r
500 * - After executing this function, in mask register, value '0' on each bit
\r
501 * enables an access to the corresponding physical pin via a read or write access,
\r
502 * while value '1' on bit (masked) that corresponding pin will not be changed
\r
503 * with write access and if read, will not be reflected in the updated pin.
\r
504 **********************************************************************/
\r
505 void FIO_HalfWordSetMask(uint8_t portNum, uint8_t halfwordNum, uint16_t bitValue, uint8_t maskValue)
\r
507 GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum);
\r
517 pFIO->FIOMASKU |= bitValue;
\r
522 pFIO->FIOMASKL |= bitValue;
\r
531 pFIO->FIOMASKU &= ~bitValue;
\r
536 pFIO->FIOMASKL &= ~bitValue;
\r
543 /*********************************************************************//**
\r
544 * @brief Set bits for FIO port in halfword accessible style
\r
545 * @param[in] portNum Port number, in range from 0 to 4
\r
546 * @param[in] halfwordNum HalfWord part number, should be 0 (lower) or 1(upper)
\r
547 * @param[in] bitValue Value that contains all bits in to set, should be
\r
548 * in range from 0 to 0xFFFF.
\r
552 * - For all bits that has been set as input direction, this function will
\r
554 * - For all remaining bits that are not activated in bitValue (value '0')
\r
555 * will not be effected by this function.
\r
556 **********************************************************************/
\r
557 void FIO_HalfWordSetValue(uint8_t portNum, uint8_t halfwordNum, uint16_t bitValue)
\r
559 GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum);
\r
566 pFIO->FIOSETU = bitValue;
\r
571 pFIO->FIOSETL = bitValue;
\r
577 /*********************************************************************//**
\r
578 * @brief Clear bits for FIO port in halfword accessible style
\r
579 * @param[in] portNum Port number, in range from 0 to 4
\r
580 * @param[in] halfwordNum HalfWord part number, should be 0 (lower) or 1(upper)
\r
581 * @param[in] bitValue Value that contains all bits in to clear, should be
\r
582 * in range from 0 to 0xFFFF.
\r
586 * - For all bits that has been set as input direction, this function will
\r
588 * - For all remaining bits that are not activated in bitValue (value '0')
\r
589 * will not be effected by this function.
\r
590 **********************************************************************/
\r
591 void FIO_HalfWordClearValue(uint8_t portNum, uint8_t halfwordNum, uint16_t bitValue)
\r
593 GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum);
\r
600 pFIO->FIOCLRU = bitValue;
\r
605 pFIO->FIOCLRL = bitValue;
\r
611 /*********************************************************************//**
\r
612 * @brief Read Current state on port pin that have input direction of GPIO
\r
613 * in halfword accessible style.
\r
614 * @param[in] portNum Port number, in range from 0 to 4
\r
615 * @param[in] halfwordNum HalfWord part number, should be 0 (lower) or 1(upper)
\r
616 * @return Current value of FIO port pin of specified halfword.
\r
617 * Note: Return value contain state of each port pin (bit) on that FIO regardless
\r
618 * its direction is input or output.
\r
619 **********************************************************************/
\r
620 uint16_t FIO_HalfWordReadValue(uint8_t portNum, uint8_t halfwordNum)
\r
622 GPIO_HalfWord_TypeDef *pFIO = FIO_HalfWordGetPointer(portNum);
\r
629 return (pFIO->FIOPINU);
\r
634 return (pFIO->FIOPINL);
\r
642 /* FIO Byte accessible ------------------------------------------------------------ */
\r
644 /*********************************************************************//**
\r
645 * @brief Set direction for FIO port in byte accessible style
\r
646 * @param[in] portNum Port number, in range from 0 to 4
\r
647 * @param[in] byteNum Byte part number, should be in range from 0 to 3
\r
648 * @param[in] bitValue Value that contains all bits in to set direction,
\r
649 * in range from 0 to 0xFF.
\r
650 * @param[in] dir Direction value, should be:
\r
655 * Note: All remaining bits that are not activated in bitValue (value '0')
\r
656 * will not be effected by this function.
\r
657 **********************************************************************/
\r
658 void FIO_ByteSetDir(uint8_t portNum, uint8_t byteNum, uint8_t bitValue, uint8_t dir)
\r
660 GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum);
\r
664 // Output direction
\r
669 pFIO->FIODIR[byteNum] |= bitValue;
\r
677 pFIO->FIODIR[byteNum] &= ~bitValue;
\r
683 /*********************************************************************//**
\r
684 * @brief Set mask value for bits in FIO port in byte accessible style
\r
685 * @param[in] portNum Port number, in range from 0 to 4
\r
686 * @param[in] byteNum Byte part number, should be in range from 0 to 3
\r
687 * @param[in] bitValue Value that contains all bits in to set mask, should
\r
688 * be in range from 0 to 0xFF.
\r
689 * @param[in] maskValue Mask value contains state value for each bit:
\r
695 * - All remaining bits that are not activated in bitValue (value '0')
\r
696 * will not be effected by this function.
\r
697 * - After executing this function, in mask register, value '0' on each bit
\r
698 * enables an access to the corresponding physical pin via a read or write access,
\r
699 * while value '1' on bit (masked) that corresponding pin will not be changed
\r
700 * with write access and if read, will not be reflected in the updated pin.
\r
701 **********************************************************************/
\r
702 void FIO_ByteSetMask(uint8_t portNum, uint8_t byteNum, uint8_t bitValue, uint8_t maskValue)
\r
704 GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum);
\r
713 pFIO->FIOMASK[byteNum] |= bitValue;
\r
720 pFIO->FIOMASK[byteNum] &= ~bitValue;
\r
727 /*********************************************************************//**
\r
728 * @brief Set bits for FIO port in byte accessible style
\r
729 * @param[in] portNum Port number, in range from 0 to 4
\r
730 * @param[in] byteNum Byte part number, should be in range from 0 to 3
\r
731 * @param[in] bitValue Value that contains all bits in to set, should
\r
732 * be in range from 0 to 0xFF.
\r
736 * - For all bits that has been set as input direction, this function will
\r
738 * - For all remaining bits that are not activated in bitValue (value '0')
\r
739 * will not be effected by this function.
\r
740 **********************************************************************/
\r
741 void FIO_ByteSetValue(uint8_t portNum, uint8_t byteNum, uint8_t bitValue)
\r
743 GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum);
\r
745 if (pFIO != NULL) {
\r
748 pFIO->FIOSET[byteNum] = bitValue;
\r
754 /*********************************************************************//**
\r
755 * @brief Clear bits for FIO port in byte accessible style
\r
756 * @param[in] portNum Port number, in range from 0 to 4
\r
757 * @param[in] byteNum Byte part number, should be in range from 0 to 3
\r
758 * @param[in] bitValue Value that contains all bits in to clear, should
\r
759 * be in range from 0 to 0xFF.
\r
763 * - For all bits that has been set as input direction, this function will
\r
765 * - For all remaining bits that are not activated in bitValue (value '0')
\r
766 * will not be effected by this function.
\r
767 **********************************************************************/
\r
768 void FIO_ByteClearValue(uint8_t portNum, uint8_t byteNum, uint8_t bitValue)
\r
770 GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum);
\r
776 pFIO->FIOCLR[byteNum] = bitValue;
\r
782 /*********************************************************************//**
\r
783 * @brief Read Current state on port pin that have input direction of GPIO
\r
784 * in byte accessible style.
\r
785 * @param[in] portNum Port number, in range from 0 to 4
\r
786 * @param[in] byteNum Byte part number, should be in range from 0 to 3
\r
787 * @return Current value of FIO port pin of specified byte part.
\r
788 * Note: Return value contain state of each port pin (bit) on that FIO regardless
\r
789 * its direction is input or output.
\r
790 **********************************************************************/
\r
791 uint8_t FIO_ByteReadValue(uint8_t portNum, uint8_t byteNum)
\r
793 GPIO_Byte_TypeDef *pFIO = FIO_ByteGetPointer(portNum);
\r
799 return (pFIO->FIOPIN[byteNum]);
\r
816 /* --------------------------------- End Of File ------------------------------ */
\r