PageRenderTime 61ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/libmaple/stm32f2/include/series/gpio.h

https://github.com/torfbolt/libmaple
C Header | 264 lines | 123 code | 32 blank | 109 comment | 0 complexity | e6a21f71c48e13d8182b29d44fd33704 MD5 | raw file
  1. /******************************************************************************
  2. * The MIT License
  3. *
  4. * Copyright (c) 2011, 2012 LeafLabs, LLC.
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  21. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  22. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. *****************************************************************************/
  26. /**
  27. * @file libmaple/stm32f2/include/series/gpio.h
  28. * @brief STM32F2 GPIO support.
  29. */
  30. #ifndef _LIBMAPLE_STM32F2_GPIO_H_
  31. #define _LIBMAPLE_STM32F2_GPIO_H_
  32. #ifdef __cplusplus
  33. extern "C"{
  34. #endif
  35. #include <libmaple/libmaple_types.h>
  36. /*
  37. * GPIO register maps and devices
  38. */
  39. /** GPIO register map type */
  40. typedef struct gpio_reg_map {
  41. __io uint32 MODER; /**< Mode register */
  42. __io uint32 OTYPER; /**< Output type register */
  43. __io uint32 OSPEEDR; /**< Output speed register */
  44. __io uint32 PUPDR; /**< Pull-up/pull-down register */
  45. __io uint32 IDR; /**< Input data register */
  46. __io uint32 ODR; /**< Output data register */
  47. __io uint32 BSRR; /**< Bit set/reset register */
  48. __io uint32 LCKR; /**< Configuration lock register */
  49. __io uint32 AFRL; /**< Alternate function low register */
  50. __io uint32 AFRH; /**< Alternate function high register */
  51. } gpio_reg_map;
  52. /** GPIO port A register map base pointer */
  53. #define GPIOA_BASE ((struct gpio_reg_map*)0x40020000)
  54. /** GPIO port B register map base pointer */
  55. #define GPIOB_BASE ((struct gpio_reg_map*)0x40020400)
  56. /** GPIO port C register map base pointer */
  57. #define GPIOC_BASE ((struct gpio_reg_map*)0x40020800)
  58. /** GPIO port D register map base pointer */
  59. #define GPIOD_BASE ((struct gpio_reg_map*)0x40020C00)
  60. /** GPIO port E register map base pointer */
  61. #define GPIOE_BASE ((struct gpio_reg_map*)0x40021000)
  62. /** GPIO port F register map base pointer */
  63. #define GPIOF_BASE ((struct gpio_reg_map*)0x40021400)
  64. /** GPIO port G register map base pointer */
  65. #define GPIOG_BASE ((struct gpio_reg_map*)0x40021800)
  66. /** GPIO port H register map base pointer */
  67. #define GPIOH_BASE ((struct gpio_reg_map*)0x40021C00)
  68. /** GPIO port I register map base pointer */
  69. #define GPIOI_BASE ((struct gpio_reg_map*)0x40022000)
  70. struct gpio_dev;
  71. extern struct gpio_dev* const GPIOA;
  72. extern struct gpio_dev gpioa;
  73. extern struct gpio_dev* const GPIOB;
  74. extern struct gpio_dev gpiob;
  75. extern struct gpio_dev* const GPIOC;
  76. extern struct gpio_dev gpioc;
  77. extern struct gpio_dev* const GPIOD;
  78. extern struct gpio_dev gpiod;
  79. extern struct gpio_dev* const GPIOE;
  80. extern struct gpio_dev gpioe;
  81. extern struct gpio_dev* const GPIOF;
  82. extern struct gpio_dev gpiof;
  83. extern struct gpio_dev* const GPIOG;
  84. extern struct gpio_dev gpiog;
  85. extern struct gpio_dev* const GPIOH;
  86. extern struct gpio_dev gpioh;
  87. extern struct gpio_dev* const GPIOI;
  88. extern struct gpio_dev gpioi;
  89. /*
  90. * Register bit definitions
  91. *
  92. * Currently, we only provide masks to be used for shifting for some
  93. * registers, rather than repeating the same values 16 times.
  94. */
  95. /* Mode register */
  96. #define GPIO_MODER_INPUT 0x0
  97. #define GPIO_MODER_OUTPUT 0x1
  98. #define GPIO_MODER_AF 0x2
  99. #define GPIO_MODER_ANALOG 0x3
  100. /* Output type register */
  101. #define GPIO_OTYPER_PP 0x0
  102. #define GPIO_OTYPER_OD 0x1
  103. /* Output speed register */
  104. #define GPIO_OSPEEDR_LOW 0x0
  105. #define GPIO_OSPEEDR_MED 0x1
  106. #define GPIO_OSPEEDR_FAST 0x2
  107. #define GPIO_OSPEEDR_HIGH 0x3
  108. /* Pull-up/pull-down register */
  109. #define GPIO_PUPDR_NOPUPD 0x0
  110. #define GPIO_PUPDR_PU 0x1
  111. #define GPIO_PUPDR_PD 0x2
  112. /* Alternate function register low */
  113. #define GPIO_AFRL_AF0 (0xFU << 0)
  114. #define GPIO_AFRL_AF1 (0xFU << 4)
  115. #define GPIO_AFRL_AF2 (0xFU << 8)
  116. #define GPIO_AFRL_AF3 (0xFU << 12)
  117. #define GPIO_AFRL_AF4 (0xFU << 16)
  118. #define GPIO_AFRL_AF5 (0xFU << 20)
  119. #define GPIO_AFRL_AF6 (0xFU << 24)
  120. #define GPIO_AFRL_AF7 (0xFU << 28)
  121. /* Alternate function register high */
  122. #define GPIO_AFRH_AF8 (0xFU << 0)
  123. #define GPIO_AFRH_AF9 (0xFU << 4)
  124. #define GPIO_AFRH_AF10 (0xFU << 8)
  125. #define GPIO_AFRH_AF11 (0xFU << 12)
  126. #define GPIO_AFRH_AF12 (0xFU << 16)
  127. #define GPIO_AFRH_AF13 (0xFU << 20)
  128. #define GPIO_AFRH_AF14 (0xFU << 24)
  129. #define GPIO_AFRH_AF15 (0xFU << 28)
  130. /*
  131. * GPIO routines
  132. */
  133. /**
  134. * @brief GPIO pin modes
  135. */
  136. typedef enum gpio_pin_mode {
  137. GPIO_MODE_INPUT = GPIO_MODER_INPUT, /**< Input mode */
  138. GPIO_MODE_OUTPUT = GPIO_MODER_OUTPUT, /**< Output mode */
  139. GPIO_MODE_AF = GPIO_MODER_AF, /**< Alternate function mode */
  140. GPIO_MODE_ANALOG = GPIO_MODER_ANALOG, /**< Analog mode */
  141. } gpio_pin_mode;
  142. /**
  143. * @brief Additional flags to be used when setting a pin's mode.
  144. *
  145. * Beyond the basic modes (input, general purpose output, alternate
  146. * function, and analog), there are three parameters that can affect a
  147. * pin's mode:
  148. *
  149. * 1. Output type: push/pull or open-drain. This only has an effect
  150. * for output modes. Choices are: GPIO_MODEF_TYPE_PP (the default)
  151. * and GPIO_MODEF_TYPE_OD.
  152. *
  153. * 2. Output speed: specifies the frequency at which a pin changes
  154. * state. This only has an effect for output modes. Choices are:
  155. * GPIO_MODEF_SPEED_LOW (default), GPIO_MODEF_SPEED_MED,
  156. * GPIO_MODEF_SPEED_FAST, and GPIO_MODEF_SPEED_HIGH.
  157. *
  158. * 3. Push/pull setting: All GPIO pins have weak pull-up and pull-down
  159. * resistors that can be enabled when the pin's mode is
  160. * set. Choices are: GPIO_MODEF_PUPD_NONE (default),
  161. * GPIO_MODEF_PUPD_PU, and GPIO_MODEF_PUPD_PD.
  162. */
  163. typedef enum gpio_mode_flags {
  164. /* Output type in bit 0 */
  165. GPIO_MODEF_TYPE_PP = GPIO_OTYPER_PP, /**< Output push/pull (default).
  166. Applies only when the mode
  167. specifies output. */
  168. GPIO_MODEF_TYPE_OD = GPIO_OTYPER_OD, /**< Output open drain.
  169. Applies only when the mode
  170. specifies output. */
  171. /* Speed in bits 2:1 */
  172. GPIO_MODEF_SPEED_LOW = GPIO_OSPEEDR_LOW << 1, /**< Low speed (default):
  173. 2 MHz. */
  174. GPIO_MODEF_SPEED_MED = GPIO_OSPEEDR_MED << 1, /**< Medium speed: 25 MHz. */
  175. GPIO_MODEF_SPEED_FAST = GPIO_OSPEEDR_FAST << 1, /**< Fast speed: 50 MHz. */
  176. GPIO_MODEF_SPEED_HIGH = GPIO_OSPEEDR_HIGH << 1, /**< High speed:
  177. 100 MHz on 30 pF,
  178. 80 MHz on 15 pF. */
  179. /* Pull-up/pull-down in bits 4:3 */
  180. GPIO_MODEF_PUPD_NONE = GPIO_PUPDR_NOPUPD << 3, /**< No pull-up/pull-down
  181. (default). */
  182. GPIO_MODEF_PUPD_PU = GPIO_PUPDR_PU << 3, /**< Pull-up */
  183. GPIO_MODEF_PUPD_PD = GPIO_PUPDR_PD << 3, /**< Pull-down */
  184. } gpio_mode_flags;
  185. void gpio_set_modef(struct gpio_dev *dev,
  186. uint8 bit,
  187. gpio_pin_mode mode,
  188. unsigned flags);
  189. /**
  190. * @brief Set the mode of a GPIO pin.
  191. *
  192. * Calling this function is equivalent to calling gpio_set_modef(dev,
  193. * pin, mode, GPIO_MODE_SPEED_HIGH). Note that this overrides the
  194. * default speed.
  195. *
  196. * @param dev GPIO device.
  197. * @param bit Bit on the device whose mode to set, 0--15.
  198. * @param mode Mode to set the pin to.
  199. */
  200. static inline void gpio_set_mode(struct gpio_dev *dev,
  201. uint8 bit,
  202. gpio_pin_mode mode) {
  203. gpio_set_modef(dev, bit, mode, GPIO_MODEF_SPEED_HIGH);
  204. }
  205. /**
  206. * @brief GPIO alternate functions.
  207. * Use these to select an alternate function for a pin.
  208. * @see gpio_set_af()
  209. */
  210. typedef enum gpio_af {
  211. GPIO_AF_SYS = 0, /**< System. */
  212. GPIO_AF_TIM_1_2 = 1, /**< Timers 1 and 2. */
  213. GPIO_AF_TIM_3_4_5 = 2, /**< Timers 3, 4, and 5. */
  214. GPIO_AF_TIM_8_9_10_11 = 3, /**< Timers 8 through 11. */
  215. GPIO_AF_I2C = 4, /**< I2C 1, 2, and 3. */
  216. GPIO_AF_SPI_1_2 = 5, /**< SPI1, SPI2/I2S2. */
  217. GPIO_AF_SPI3 = 6, /**< SPI3/I2S3. */
  218. GPIO_AF_USART_1_2_3 = 7, /**< USART 1, 2, and 3. */
  219. GPIO_AF_USART_4_5_6 = 8, /**< UART 4 and 5, USART 6. */
  220. GPIO_AF_CAN_1_2_TIM_12_13_14 = 9, /**<
  221. * CAN 1 and 2, timers 12, 13, and 14. */
  222. GPIO_AF_USB_OTG_FS_HS = 10, /**< USB OTG HS and FS. */
  223. GPIO_AF_ETH = 11, /**< Ethernet MII and RMII. */
  224. GPIO_AF_FSMC_SDIO_OTG_FS = 12, /**< FSMC, SDIO, and USB OTG FS. */
  225. GPIO_AF_DCMI = 13, /**< DCMI. */
  226. GPIO_AF_EVENTOUT = 15, /**< EVENTOUT. */
  227. } gpio_af;
  228. void gpio_set_af(struct gpio_dev *dev, uint8 bit, gpio_af af);
  229. #ifdef __cplusplus
  230. }
  231. #endif
  232. #endif