gpio_lpc17xx.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (C) 2012 by Ole Reinhardt (ole.reinhardt@embedded-it.de)
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*
  35. * \verbatim
  36. * $Id: gpio_lpc17xx.h $
  37. * \endverbatim
  38. */
  39. typedef uint32_t nutgpio_port_t;
  40. typedef uint32_t nutgpio_pin_t;
  41. #include <cfg/arch.h>
  42. #include <cfg/arch/gpio.h>
  43. #include <dev/irqreg.h>
  44. #if defined(MCU_LPC176x)
  45. #include <arch/cm3/nxp/lpc176x.h>
  46. #elif defined(MCU_LPC177x_8x)
  47. #include <arch/cm3/nxp/lpc177x_8x.h>
  48. #elif defined(MCU_LPC407x_8x)
  49. #include <arch/cm3/nxp/lpc407x_8x.h>
  50. #else
  51. #warning "Unknown LPC family"
  52. #endif
  53. #define NUTGPIO_PORT 0
  54. #define NUTGPIO_PORT0 0
  55. #define NUTGPIO_PORT1 1
  56. #define NUTGPIO_PORT2 2
  57. #define NUTGPIO_PORT3 3
  58. #define NUTGPIO_PORT4 4
  59. #define NUTGPIO_PORT5 5
  60. #define NUTGPIO_PORTA NUTGPIO_PORT0
  61. #define NUTGPIO_PORTB NUTGPIO_PORT1
  62. #define NUTGPIO_PORTC NUTGPIO_PORT2
  63. #define NUTGPIO_PORTD NUTGPIO_PORT3
  64. #define NUTGPIO_PORTE NUTGPIO_PORT4
  65. #define NUTGPIO_PORTF NUTGPIO_PORT5
  66. #define NUTGPIO_EXTINT0 1
  67. #define NUTGPIO_EXTINT1 2
  68. #define NUTGPIO_EXTINT2 3
  69. #define NUTGPIO_EXTINT3 4
  70. #define NUTGPIO_EXTINT4 5
  71. /*!
  72. * \brief GPIO input.
  73. *
  74. * Will configure the pin as input. This is the default state, when no other
  75. * config option is given.
  76. */
  77. #define GPIO_CFG_INPUT 0x00000000
  78. /*!
  79. * \brief GPIO disabled.
  80. *
  81. * Will activate the pins alternate function if set. This may not work
  82. * on all platforms.
  83. */
  84. #define GPIO_CFG_DISABLED 0x00000001
  85. /*!
  86. * \brief GPIO output direction enabled.
  87. *
  88. * If set, the pin is configured as an output. Otherwise it is in
  89. * input mode or z-state.
  90. */
  91. #define GPIO_CFG_OUTPUT 0x00000002
  92. /*!
  93. * \brief GPIO pull-up enabled.
  94. */
  95. #define GPIO_CFG_PULLUP 0x00000004
  96. /*!
  97. * \brief GPIO pull-down enabled.
  98. */
  99. #define GPIO_CFG_PULLDOWN 0x00000008
  100. /*!
  101. * \brief GPIO repeater-mode enabled.
  102. */
  103. #define GPIO_CFG_REPEATER 0x00000010
  104. /*!
  105. * \brief GPIO open drain output feature enabled.
  106. *
  107. * If not set, the output will use push pull mode.
  108. */
  109. #define GPIO_CFG_MULTIDRIVE 0x00000020
  110. /*!
  111. * \brief GPIO input glitch filter enabled.
  112. *
  113. */
  114. #if defined(MCU_LPC176x)
  115. /*Not supported with the LPC176x family */
  116. #define GPIO_CFG_DEBOUNCE 0x00000000
  117. #elif defined(MCU_LPC177x_8x) || defined(MCU_LPC407x_8x)
  118. #define GPIO_CFG_DEBOUNCE 0x00000040
  119. #define GPIO_CFG_HYSTERESIS 0x00000080
  120. #define GPIO_CFG_INVERT 0x00000100
  121. #define GPIO_CFG_SLEWCTRL 0x00000200
  122. #define GPIO_CFG_ADMODE 0x00000400
  123. #define GPIO_CFG_DAC_ENABLE 0x00000800
  124. #endif
  125. /*!
  126. * \brief GPIO set to alternate function 0.
  127. *
  128. * LPC176x specific:
  129. * Enables alternate function 0..3 of the pin.
  130. * Function 0 is the GPIO function
  131. */
  132. #define GPIO_CFG_PERIPHERAL_MASK 0x07000000
  133. #define GPIO_CFG_PERIPHERAL_POS 24
  134. #define GPIO_CFG_PERIPHERAL0 0x00000000
  135. #define GPIO_CFG_PERIPHERAL1 0x01000000
  136. #define GPIO_CFG_PERIPHERAL2 0x02000000
  137. #define GPIO_CFG_PERIPHERAL3 0x03000000
  138. #if defined(MCU_LPC177x_8x) || defined(MCU_LPC407x_8x)
  139. #define GPIO_CFG_PERIPHERAL4 0x04000000
  140. #define GPIO_CFG_PERIPHERAL5 0x05000000
  141. #define GPIO_CFG_PERIPHERAL6 0x06000000
  142. #define GPIO_CFG_PERIPHERAL7 0x07000000
  143. #endif
  144. #define GPIO_BANKID2BASE(bank) (LPC_GPIO0_BASE + (bank << 5))
  145. typedef struct {
  146. void (*iov_handler) (void *);
  147. void *iov_arg;
  148. } GPIO_VECTOR;
  149. typedef struct _gpio_signal GPIO_SIGNAL;
  150. struct _gpio_signal {
  151. int ios_port;
  152. void (*ios_handler) (void *);
  153. int (*ios_ctl) (GPIO_SIGNAL * sig, int cmd, void *param, int bit);
  154. GPIO_VECTOR *ios_vector;
  155. uint32_t enabled;
  156. uint32_t mode_rising_enabled;
  157. uint32_t mode_falling_enabled;
  158. };
  159. extern GPIO_SIGNAL sig_GPIO0;
  160. extern GPIO_SIGNAL sig_GPIO2;
  161. extern uint32_t GpioPinConfigGet(int bank, int bit);
  162. extern int GpioPinConfigSet(int bank, int bit, uint32_t flags);
  163. extern int GpioPortConfigSet(int bank, uint32_t mask, uint32_t flags);
  164. #define GpioPinGet(bank, bit) CM3BBGET(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIOPIN, (bit))
  165. //#define GpioPinSet(bank, bit, value) CM3BBSETVAL(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIOPIN, bit, value)
  166. #define GpioPinSet(bank, bit, value) do { \
  167. if(value) CM3BBSET(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIOSET, (bit)); else \
  168. CM3BBSET(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIOCLR, (bit)); \
  169. } while (0)
  170. #define GpioPinMaskSet(bank, bit, value) CM3BBSETVAL(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIOMASK, (bit), value)
  171. #define GpioPinSetHigh(bank, bit) CM3BBSET(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIOSET, (bit))
  172. #define GpioPinSetLow(bank, bit) CM3BBSET(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIOCLR, (bit))
  173. #define GpioPortGet(bank) CM3REG(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIOPIN)
  174. #define GpioPortSet(bank, value) CM3REG(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIOPIN) = (value)
  175. #define GpioPortSetHigh(bank, mask) CM3REG(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIOSET) = (mask)
  176. #define GpioPortSetLow(bank, mask) CM3REG(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIOCLR) = (mask)
  177. #define GpioPortMaskSet(bank, mask) CM3REG(GPIO_BANKID2BASE(bank), LPC_GPIO_TypeDef, FIOMASK) = (mask)
  178. extern int GpioRegisterIrqHandler(GPIO_SIGNAL * sig, int bit, void (*handler) (void *), void *arg);
  179. extern int GpioIrqEnable(GPIO_SIGNAL * sig, int bit);
  180. extern int GpioIrqStatus(GPIO_SIGNAL * sig, int bit);
  181. extern int GpioIrqDisable(GPIO_SIGNAL * sig, int bit);
  182. extern int GpioIrqSetMode(GPIO_SIGNAL * sig, int bit, int mode);