gpio_stm32.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (C) 2010 by Ulrich Prinz (uprinz2@netscape.net)
  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: stm32_can2.c 3108 2010-09-15 21:11:15Z Astralix $
  37. * \endverbatim
  38. */
  39. #include <cfg/arch.h>
  40. #include <cfg/arch/gpio.h>
  41. #include <dev/irqreg.h>
  42. #include <arch/cm3/stm/stm32xxxx.h>
  43. typedef uint32_t nutgpio_port_t;
  44. typedef uint_fast16_t nutgpio_pin_t;
  45. #define NUTGPIO_PORT GPIOA_BASE
  46. #define NUTGPIO_PORTA GPIOA_BASE
  47. #define NUTGPIO_PORTB GPIOB_BASE
  48. #define NUTGPIO_PORTC GPIOC_BASE
  49. #define NUTGPIO_PORTD GPIOD_BASE
  50. #define NUTGPIO_PORTE GPIOE_BASE
  51. #define NUTGPIO_PORTF GPIOF_BASE
  52. #define NUTGPIO_PORTG GPIOG_BASE
  53. #define NUTGPIO_PORTH GPIOH_BASE
  54. #define NUTGPIO_PORTI GPIOI_BASE
  55. #define NUTGPIO_EXTINT0 1
  56. #define NUTGPIO_EXTINT1 2
  57. #define NUTGPIO_EXTINT2 3
  58. #define NUTGPIO_EXTINT3 4
  59. #define NUTGPIO_EXTINT4 5
  60. /*!
  61. * \brief GPIO input.
  62. *
  63. * Will configure the pin as input. This is the default state, when no other
  64. * config option is given.
  65. */
  66. #define GPIO_CFG_INPUT 0x00000000
  67. /*!
  68. * \brief GPIO disabled.
  69. *
  70. * STM32 Specific:
  71. * If combined with GPIO_CFG_OUTPUT it enables the alternate function.
  72. * If used without, it sets the analog input mode of the pin.
  73. */
  74. #define GPIO_CFG_DISABLED 0x00000001
  75. /*!
  76. * \brief GPIO output direction enabled.
  77. *
  78. * If set, the pin is configured as an output. Otherwise it is in
  79. * input mode or z-state.
  80. * For STM32F it is configured as output 50MHz.
  81. */
  82. #define GPIO_CFG_OUTPUT 0x00000002
  83. /*!
  84. * \brief GPIO pull-up enabled.
  85. */
  86. #define GPIO_CFG_PULLUP 0x00000004
  87. /*!
  88. * \brief GPIO pull-down enabled.
  89. */
  90. #define GPIO_CFG_PULLDOWN 0x00000100
  91. /*!
  92. * \brief GPIO open drain output feature enabled.
  93. *
  94. * If not set, the output will use push pull mode.
  95. */
  96. #define GPIO_CFG_MULTIDRIVE 0x00000008
  97. /*!
  98. * \brief GPIO input glitch filter enabled.
  99. *
  100. * Not supported with STM32F
  101. */
  102. #define GPIO_CFG_DEBOUNCE 0x00000000
  103. /*!
  104. * \brief GPIO set to alternate function.
  105. *
  106. * STM32F specific:
  107. * Enables alternate function of the pin.
  108. */
  109. #define GPIO_CFG_PERIPHAL 0x00000020
  110. /*!
  111. * \brief GPIO pin speed
  112. *
  113. * Speed L1 F2/F2
  114. * SLOW 400 kHz 2 MHz
  115. * MED 2 MHz 25 MHz
  116. * FAST 10 MHz 50 MHz
  117. * HIGH 40 MHz 100 MHz
  118. *
  119. * As default, we set GPIO_CFG_SPEED_MED
  120. */
  121. #define GPIO_CFG_SPEED 0x000000C0
  122. #define GPIO_CFG_SPEED_SLOW 0x00000040
  123. #define GPIO_CFG_SPEED_MED 0x00000000
  124. #define GPIO_CFG_SPEED_FAST 0x00000080
  125. #define GPIO_CFG_SPEED_HIGH 0x000000C0
  126. /*!
  127. * \brief GPIO Output Register inital value Low
  128. */
  129. #define GPIO_CFG_INIT_LOW 0x40000000
  130. /*!
  131. * \brief GPIO Output Register inital value High
  132. */
  133. #define GPIO_CFG_INIT_HIGH 0x80000000
  134. typedef struct {
  135. void (*iov_handler) (void *);
  136. void *iov_arg;
  137. } GPIO_VECTOR;
  138. typedef struct {
  139. IRQ_HANDLER *ios_sig;
  140. void (*ios_handler) (void *);
  141. int (*ios_ctl) (int cmd, void *param, int bit);
  142. GPIO_VECTOR *ios_vector;
  143. } GPIO_SIGNAL;
  144. #if defined(PIO_ISR)
  145. extern GPIO_SIGNAL sig_GPIO;
  146. #endif
  147. #if defined(PIOA_ISR)
  148. extern GPIO_SIGNAL sig_GPIO1;
  149. #endif
  150. #if defined(PIOB_ISR)
  151. extern GPIO_SIGNAL sig_GPIO2;
  152. #endif
  153. #if defined(PIOC_ISR)
  154. extern GPIO_SIGNAL sig_GPIO3;
  155. #endif
  156. extern uint32_t GpioPinConfigGet(int bank, int bit);
  157. extern int GpioPinConfigSet(int bank, int bit, uint32_t flags);
  158. extern int GpioPortConfigSet(int bank, uint32_t mask, uint32_t flags);
  159. #if defined(MCU_STM32F0) ||defined(MCU_STM32F3)
  160. /* GPIO on AHB2 is outside of bitband region */
  161. #define GpioPinGet(bank, bit) ((CM3REG((bank), GPIO_TypeDef, IDR ) & (1<<(bit)))?1:0)
  162. #define GpioPinSet(bank, bit, value) (((volatile uint16_t*)((bank) + offsetof(GPIO_TypeDef, BSRR)))[(value)?0:1] = (1<<(bit)))
  163. #define GpioPinSetHigh(bank, bit) (CM3REG((bank), GPIO_TypeDef, BSRR) = (1<<(bit)))
  164. #define GpioPinSetLow(bank, bit) (((volatile uint16_t*)((bank) + offsetof(GPIO_TypeDef, BSRR)))[1] = (1<<(bit)))
  165. #define GpioPinDrive(bank, bit) (CM3REG((bank), GPIO_TypeDef, MODER) |= (1<<((bit)<<1)))
  166. #define GpioPinRelease(bank, bit) (CM3REG((bank), GPIO_TypeDef, MODER) &= ~(1<<((bit)<<1)))
  167. #else
  168. #define GpioPinGet(bank, bit) CM3BBGET((bank), GPIO_TypeDef, IDR, (bit))
  169. #define GpioPinSet(bank, bit, value) CM3BBSETVAL((bank), GPIO_TypeDef, ODR, bit, value)
  170. #if defined(MCU_STM32F1)
  171. #define GpioPinSetHigh(bank, bit) CM3BBSET((bank), GPIO_TypeDef, BSRR, (bit))
  172. #define GpioPinSetLow(bank, bit) CM3BBSET((bank), GPIO_TypeDef, BRR , (bit))
  173. /* We unconditionally switch back to 2 Mhz output speed after we released the pin at least once*/
  174. #define GpioPinDrive(bank, bit) do { \
  175. __IO uint32_t *cr_bb = &CM3BBREG((bank), GPIO_TypeDef, CRL, ((bit)<<2)); \
  176. cr_bb[1] = 1; cr_bb[2] = 0; } while (0)
  177. #define GpioPinRelease(bank, bit) do { \
  178. __IO uint32_t *cr_bb = &CM3BBREG((bank), GPIO_TypeDef, CRL, ((bit)<<2)); \
  179. cr_bb[1] = 0; cr_bb[2] = 1; } while (0)
  180. #else
  181. #define GpioPinSetHigh(bank, bit) CM3BBSET((bank), GPIO_TypeDef, BSRRL, (bit))
  182. #define GpioPinSetLow(bank, bit) CM3BBSET((bank), GPIO_TypeDef, BSRRH, (bit))
  183. #define GpioPinDrive(bank, bit) CM3BBSET((bank), GPIO_TypeDef, MODER, (bit)<<1)
  184. #define GpioPinRelease(bank, bit) CM3BBCLR((bank), GPIO_TypeDef, MODER, (bit)<<1)
  185. #endif
  186. #endif
  187. #define GpioPortGet(bank) CM3REG((bank), GPIO_TypeDef, IDR )
  188. #define GpioPortSet(bank, value) (CM3REG((bank), GPIO_TypeDef, ODR ) = value)
  189. #define GpioPortSetHigh(bank, mask) (CM3REG((bank), GPIO_TypeDef, BSRR) = mask)
  190. #define GpioPortSetLow(bank, mask) (CM3REG((bank), GPIO_TypeDef, BRR ) = mask)
  191. #if defined(MCU_STM32L1)
  192. #define GpioClkEnable(bank) CM3BBSET(RCC_BASE, RCC_TypeDef, AHBENR, ( bank-GPIOA_BASE)>>10)
  193. #elif defined(MCU_STM32F0) ||defined(MCU_STM32F3)
  194. #define GpioClkEnable(bank) CM3BBSET(RCC_BASE, RCC_TypeDef, AHBENR, (((bank-GPIOA_BASE)>>10) +17))
  195. #else
  196. #define GpioClkEnable(bank) CM3BBSET(RCC_BASE, RCC_TypeDef, AHB1ENR,( bank-GPIOA_BASE)>>10)
  197. #endif
  198. extern int GpioRegisterIrqHandler(GPIO_SIGNAL * sig, uint8_t bit, void (*handler) (void *), void *arg);
  199. extern int GpioIrqEnable(GPIO_SIGNAL * sig, uint8_t bit);
  200. extern int GpioIrqDisable(GPIO_SIGNAL * sig, uint8_t bit);