gd32f4xx_gpio.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*!
  2. \file gd32f4xx_gpio.h
  3. \brief definitions for the GPIO
  4. */
  5. /*
  6. Copyright (C) 2016 GigaDevice
  7. 2016-08-15, V1.0.0, firmware for GD32F4xx
  8. */
  9. #ifndef GD32F4XX_GPIO_H
  10. #define GD32F4XX_GPIO_H
  11. #include "gd32f4xx.h"
  12. /* GPIOx(x=A,B,C,D,E,F,G,H,I) definitions */
  13. #define GPIOA (GPIO_BASE + 0x00000000U)
  14. #define GPIOB (GPIO_BASE + 0x00000400U)
  15. #define GPIOC (GPIO_BASE + 0x00000800U)
  16. #define GPIOD (GPIO_BASE + 0x00000C00U)
  17. #define GPIOE (GPIO_BASE + 0x00001000U)
  18. #define GPIOF (GPIO_BASE + 0x00001400U)
  19. #define GPIOG (GPIO_BASE + 0x00001800U)
  20. #define GPIOH (GPIO_BASE + 0x00001C00U)
  21. #define GPIOI (GPIO_BASE + 0x00002000U)
  22. /* registers definitions */
  23. #define GPIO_CTL(gpiox) REG32((gpiox) + 0x00U) /*!< GPIO port control register */
  24. #define GPIO_OMODE(gpiox) REG32((gpiox) + 0x04U) /*!< GPIO port output mode register */
  25. #define GPIO_OSPD(gpiox) REG32((gpiox) + 0x08U) /*!< GPIO port output speed register */
  26. #define GPIO_PUD(gpiox) REG32((gpiox) + 0x0CU) /*!< GPIO port pull-up/pull-down register */
  27. #define GPIO_ISTAT(gpiox) REG32((gpiox) + 0x10U) /*!< GPIO port input status register */
  28. #define GPIO_OCTL(gpiox) REG32((gpiox) + 0x14U) /*!< GPIO port output control register */
  29. #define GPIO_BOP(gpiox) REG32((gpiox) + 0x18U) /*!< GPIO port bit operation register */
  30. #define GPIO_LOCK(gpiox) REG32((gpiox) + 0x1CU) /*!< GPIO port configuration lock register */
  31. #define GPIO_AFSEL0(gpiox) REG32((gpiox) + 0x20U) /*!< GPIO alternate function selected register 0 */
  32. #define GPIO_AFSEL1(gpiox) REG32((gpiox) + 0x24U) /*!< GPIO alternate function selected register 1 */
  33. #define GPIO_BC(gpiox) REG32((gpiox) + 0x28U) /*!< GPIO bit clear register */
  34. #define GPIO_TG(gpiox) REG32((gpiox) + 0x2CU) /*!< GPIO port bit toggle register */
  35. /* bits definitions */
  36. /* GPIO_CTL */
  37. #define GPIO_CTL_CTL0 BITS(0,1) /*!< pin 0 configuration bits */
  38. #define GPIO_CTL_CTL1 BITS(2,3) /*!< pin 1 configuration bits */
  39. #define GPIO_CTL_CTL2 BITS(4,5) /*!< pin 2 configuration bits */
  40. #define GPIO_CTL_CTL3 BITS(6,7) /*!< pin 3 configuration bits */
  41. #define GPIO_CTL_CTL4 BITS(8,9) /*!< pin 4 configuration bits */
  42. #define GPIO_CTL_CTL5 BITS(10,11) /*!< pin 5 configuration bits */
  43. #define GPIO_CTL_CTL6 BITS(12,13) /*!< pin 6 configuration bits */
  44. #define GPIO_CTL_CTL7 BITS(14,15) /*!< pin 7 configuration bits */
  45. #define GPIO_CTL_CTL8 BITS(16,17) /*!< pin 8 configuration bits */
  46. #define GPIO_CTL_CTL9 BITS(18,19) /*!< pin 9 configuration bits */
  47. #define GPIO_CTL_CTL10 BITS(20,21) /*!< pin 10 configuration bits */
  48. #define GPIO_CTL_CTL11 BITS(22,23) /*!< pin 11 configuration bits */
  49. #define GPIO_CTL_CTL12 BITS(24,25) /*!< pin 12 configuration bits */
  50. #define GPIO_CTL_CTL13 BITS(26,27) /*!< pin 13 configuration bits */
  51. #define GPIO_CTL_CTL14 BITS(28,29) /*!< pin 14 configuration bits */
  52. #define GPIO_CTL_CTL15 BITS(30,31) /*!< pin 15 configuration bits */
  53. /* GPIO_OMODE */
  54. #define GPIO_OMODE_OM0 BIT(0) /*!< pin 0 output mode bit */
  55. #define GPIO_OMODE_OM1 BIT(1) /*!< pin 1 output mode bit */
  56. #define GPIO_OMODE_OM2 BIT(2) /*!< pin 2 output mode bit */
  57. #define GPIO_OMODE_OM3 BIT(3) /*!< pin 3 output mode bit */
  58. #define GPIO_OMODE_OM4 BIT(4) /*!< pin 4 output mode bit */
  59. #define GPIO_OMODE_OM5 BIT(5) /*!< pin 5 output mode bit */
  60. #define GPIO_OMODE_OM6 BIT(6) /*!< pin 6 output mode bit */
  61. #define GPIO_OMODE_OM7 BIT(7) /*!< pin 7 output mode bit */
  62. #define GPIO_OMODE_OM8 BIT(8) /*!< pin 8 output mode bit */
  63. #define GPIO_OMODE_OM9 BIT(9) /*!< pin 9 output mode bit */
  64. #define GPIO_OMODE_OM10 BIT(10) /*!< pin 10 output mode bit */
  65. #define GPIO_OMODE_OM11 BIT(11) /*!< pin 11 output mode bit */
  66. #define GPIO_OMODE_OM12 BIT(12) /*!< pin 12 output mode bit */
  67. #define GPIO_OMODE_OM13 BIT(13) /*!< pin 13 output mode bit */
  68. #define GPIO_OMODE_OM14 BIT(14) /*!< pin 14 output mode bit */
  69. #define GPIO_OMODE_OM15 BIT(15) /*!< pin 15 output mode bit */
  70. /* GPIO_OSPD */
  71. #define GPIO_OSPD_OSPD0 BITS(0,1) /*!< pin 0 output max speed bits */
  72. #define GPIO_OSPD_OSPD1 BITS(2,3) /*!< pin 1 output max speed bits */
  73. #define GPIO_OSPD_OSPD2 BITS(4,5) /*!< pin 2 output max speed bits */
  74. #define GPIO_OSPD_OSPD3 BITS(6,7) /*!< pin 3 output max speed bits */
  75. #define GPIO_OSPD_OSPD4 BITS(8,9) /*!< pin 4 output max speed bits */
  76. #define GPIO_OSPD_OSPD5 BITS(10,11) /*!< pin 5 output max speed bits */
  77. #define GPIO_OSPD_OSPD6 BITS(12,13) /*!< pin 6 output max speed bits */
  78. #define GPIO_OSPD_OSPD7 BITS(14,15) /*!< pin 7 output max speed bits */
  79. #define GPIO_OSPD_OSPD8 BITS(16,17) /*!< pin 8 output max speed bits */
  80. #define GPIO_OSPD_OSPD9 BITS(18,19) /*!< pin 9 output max speed bits */
  81. #define GPIO_OSPD_OSPD10 BITS(20,21) /*!< pin 10 output max speed bits */
  82. #define GPIO_OSPD_OSPD11 BITS(22,23) /*!< pin 11 output max speed bits */
  83. #define GPIO_OSPD_OSPD12 BITS(24,25) /*!< pin 12 output max speed bits */
  84. #define GPIO_OSPD_OSPD13 BITS(26,27) /*!< pin 13 output max speed bits */
  85. #define GPIO_OSPD_OSPD14 BITS(28,29) /*!< pin 14 output max speed bits */
  86. #define GPIO_OSPD_OSPD15 BITS(30,31) /*!< pin 15 output max speed bits */
  87. /* GPIO_PUD */
  88. #define GPIO_PUD_PUD0 BITS(0,1) /*!< pin 0 pull-up or pull-down bits */
  89. #define GPIO_PUD_PUD1 BITS(2,3) /*!< pin 1 pull-up or pull-down bits */
  90. #define GPIO_PUD_PUD2 BITS(4,5) /*!< pin 2 pull-up or pull-down bits */
  91. #define GPIO_PUD_PUD3 BITS(6,7) /*!< pin 3 pull-up or pull-down bits */
  92. #define GPIO_PUD_PUD4 BITS(8,9) /*!< pin 4 pull-up or pull-down bits */
  93. #define GPIO_PUD_PUD5 BITS(10,11) /*!< pin 5 pull-up or pull-down bits */
  94. #define GPIO_PUD_PUD6 BITS(12,13) /*!< pin 6 pull-up or pull-down bits */
  95. #define GPIO_PUD_PUD7 BITS(14,15) /*!< pin 7 pull-up or pull-down bits */
  96. #define GPIO_PUD_PUD8 BITS(16,17) /*!< pin 8 pull-up or pull-down bits */
  97. #define GPIO_PUD_PUD9 BITS(18,19) /*!< pin 9 pull-up or pull-down bits */
  98. #define GPIO_PUD_PUD10 BITS(20,21) /*!< pin 10 pull-up or pull-down bits */
  99. #define GPIO_PUD_PUD11 BITS(22,23) /*!< pin 11 pull-up or pull-down bits */
  100. #define GPIO_PUD_PUD12 BITS(24,25) /*!< pin 12 pull-up or pull-down bits */
  101. #define GPIO_PUD_PUD13 BITS(26,27) /*!< pin 13 pull-up or pull-down bits */
  102. #define GPIO_PUD_PUD14 BITS(28,29) /*!< pin 14 pull-up or pull-down bits */
  103. #define GPIO_PUD_PUD15 BITS(30,31) /*!< pin 15 pull-up or pull-down bits */
  104. /* GPIO_ISTAT */
  105. #define GPIO_ISTAT_ISTAT0 BIT(0) /*!< pin 0 input status */
  106. #define GPIO_ISTAT_ISTAT1 BIT(1) /*!< pin 1 input status */
  107. #define GPIO_ISTAT_ISTAT2 BIT(2) /*!< pin 2 input status */
  108. #define GPIO_ISTAT_ISTAT3 BIT(3) /*!< pin 3 input status */
  109. #define GPIO_ISTAT_ISTAT4 BIT(4) /*!< pin 4 input status */
  110. #define GPIO_ISTAT_ISTAT5 BIT(5) /*!< pin 5 input status */
  111. #define GPIO_ISTAT_ISTAT6 BIT(6) /*!< pin 6 input status */
  112. #define GPIO_ISTAT_ISTAT7 BIT(7) /*!< pin 7 input status */
  113. #define GPIO_ISTAT_ISTAT8 BIT(8) /*!< pin 8 input status */
  114. #define GPIO_ISTAT_ISTAT9 BIT(9) /*!< pin 9 input status */
  115. #define GPIO_ISTAT_ISTAT10 BIT(10) /*!< pin 10 input status */
  116. #define GPIO_ISTAT_ISTAT11 BIT(11) /*!< pin 11 input status */
  117. #define GPIO_ISTAT_ISTAT12 BIT(12) /*!< pin 12 input status */
  118. #define GPIO_ISTAT_ISTAT13 BIT(13) /*!< pin 13 input status */
  119. #define GPIO_ISTAT_ISTAT14 BIT(14) /*!< pin 14 input status */
  120. #define GPIO_ISTAT_ISTAT15 BIT(15) /*!< pin 15 input status */
  121. /* GPIO_OCTL */
  122. #define GPIO_OCTL_OCTL0 BIT(0) /*!< pin 0 output bit */
  123. #define GPIO_OCTL_OCTL1 BIT(1) /*!< pin 1 output bit */
  124. #define GPIO_OCTL_OCTL2 BIT(2) /*!< pin 2 output bit */
  125. #define GPIO_OCTL_OCTL3 BIT(3) /*!< pin 3 output bit */
  126. #define GPIO_OCTL_OCTL4 BIT(4) /*!< pin 4 output bit */
  127. #define GPIO_OCTL_OCTL5 BIT(5) /*!< pin 5 output bit */
  128. #define GPIO_OCTL_OCTL6 BIT(6) /*!< pin 6 output bit */
  129. #define GPIO_OCTL_OCTL7 BIT(7) /*!< pin 7 output bit */
  130. #define GPIO_OCTL_OCTL8 BIT(8) /*!< pin 8 output bit */
  131. #define GPIO_OCTL_OCTL9 BIT(9) /*!< pin 9 output bit */
  132. #define GPIO_OCTL_OCTL10 BIT(10) /*!< pin 10 output bit */
  133. #define GPIO_OCTL_OCTL11 BIT(11) /*!< pin 11 output bit */
  134. #define GPIO_OCTL_OCTL12 BIT(12) /*!< pin 12 output bit */
  135. #define GPIO_OCTL_OCTL13 BIT(13) /*!< pin 13 output bit */
  136. #define GPIO_OCTL_OCTL14 BIT(14) /*!< pin 14 output bit */
  137. #define GPIO_OCTL_OCTL15 BIT(15) /*!< pin 15 output bit */
  138. /* GPIO_BOP */
  139. #define GPIO_BOP_BOP0 BIT(0) /*!< pin 0 set bit */
  140. #define GPIO_BOP_BOP1 BIT(1) /*!< pin 1 set bit */
  141. #define GPIO_BOP_BOP2 BIT(2) /*!< pin 2 set bit */
  142. #define GPIO_BOP_BOP3 BIT(3) /*!< pin 3 set bit */
  143. #define GPIO_BOP_BOP4 BIT(4) /*!< pin 4 set bit */
  144. #define GPIO_BOP_BOP5 BIT(5) /*!< pin 5 set bit */
  145. #define GPIO_BOP_BOP6 BIT(6) /*!< pin 6 set bit */
  146. #define GPIO_BOP_BOP7 BIT(7) /*!< pin 7 set bit */
  147. #define GPIO_BOP_BOP8 BIT(8) /*!< pin 8 set bit */
  148. #define GPIO_BOP_BOP9 BIT(9) /*!< pin 9 set bit */
  149. #define GPIO_BOP_BOP10 BIT(10) /*!< pin 10 set bit */
  150. #define GPIO_BOP_BOP11 BIT(11) /*!< pin 11 set bit */
  151. #define GPIO_BOP_BOP12 BIT(12) /*!< pin 12 set bit */
  152. #define GPIO_BOP_BOP13 BIT(13) /*!< pin 13 set bit */
  153. #define GPIO_BOP_BOP14 BIT(14) /*!< pin 14 set bit */
  154. #define GPIO_BOP_BOP15 BIT(15) /*!< pin 15 set bit */
  155. #define GPIO_BOP_CR0 BIT(16) /*!< pin 0 clear bit */
  156. #define GPIO_BOP_CR1 BIT(17) /*!< pin 1 clear bit */
  157. #define GPIO_BOP_CR2 BIT(18) /*!< pin 2 clear bit */
  158. #define GPIO_BOP_CR3 BIT(19) /*!< pin 3 clear bit */
  159. #define GPIO_BOP_CR4 BIT(20) /*!< pin 4 clear bit */
  160. #define GPIO_BOP_CR5 BIT(21) /*!< pin 5 clear bit */
  161. #define GPIO_BOP_CR6 BIT(22) /*!< pin 6 clear bit */
  162. #define GPIO_BOP_CR7 BIT(23) /*!< pin 7 clear bit */
  163. #define GPIO_BOP_CR8 BIT(24) /*!< pin 8 clear bit */
  164. #define GPIO_BOP_CR9 BIT(25) /*!< pin 9 clear bit */
  165. #define GPIO_BOP_CR10 BIT(26) /*!< pin 10 clear bit */
  166. #define GPIO_BOP_CR11 BIT(27) /*!< pin 11 clear bit */
  167. #define GPIO_BOP_CR12 BIT(28) /*!< pin 12 clear bit */
  168. #define GPIO_BOP_CR13 BIT(29) /*!< pin 13 clear bit */
  169. #define GPIO_BOP_CR14 BIT(30) /*!< pin 14 clear bit */
  170. #define GPIO_BOP_CR15 BIT(31) /*!< pin 15 clear bit */
  171. /* GPIO_LOCK */
  172. #define GPIO_LOCK_LK0 BIT(0) /*!< pin 0 lock bit */
  173. #define GPIO_LOCK_LK1 BIT(1) /*!< pin 1 lock bit */
  174. #define GPIO_LOCK_LK2 BIT(2) /*!< pin 2 lock bit */
  175. #define GPIO_LOCK_LK3 BIT(3) /*!< pin 3 lock bit */
  176. #define GPIO_LOCK_LK4 BIT(4) /*!< pin 4 lock bit */
  177. #define GPIO_LOCK_LK5 BIT(5) /*!< pin 5 lock bit */
  178. #define GPIO_LOCK_LK6 BIT(6) /*!< pin 6 lock bit */
  179. #define GPIO_LOCK_LK7 BIT(7) /*!< pin 7 lock bit */
  180. #define GPIO_LOCK_LK8 BIT(8) /*!< pin 8 lock bit */
  181. #define GPIO_LOCK_LK9 BIT(9) /*!< pin 9 lock bit */
  182. #define GPIO_LOCK_LK10 BIT(10) /*!< pin 10 lock bit */
  183. #define GPIO_LOCK_LK11 BIT(11) /*!< pin 11 lock bit */
  184. #define GPIO_LOCK_LK12 BIT(12) /*!< pin 12 lock bit */
  185. #define GPIO_LOCK_LK13 BIT(13) /*!< pin 13 lock bit */
  186. #define GPIO_LOCK_LK14 BIT(14) /*!< pin 14 lock bit */
  187. #define GPIO_LOCK_LK15 BIT(15) /*!< pin 15 lock bit */
  188. #define GPIO_LOCK_LKK BIT(16) /*!< pin sequence lock key */
  189. /* GPIO_AFSEL0 */
  190. #define GPIO_AFSEL0_SEL0 BITS(0,3) /*!< pin 0 alternate function selected */
  191. #define GPIO_AFSEL0_SEL1 BITS(4,7) /*!< pin 1 alternate function selected */
  192. #define GPIO_AFSEL0_SEL2 BITS(8,11) /*!< pin 2 alternate function selected */
  193. #define GPIO_AFSEL0_SEL3 BITS(12,15) /*!< pin 3 alternate function selected */
  194. #define GPIO_AFSEL0_SEL4 BITS(16,19) /*!< pin 4 alternate function selected */
  195. #define GPIO_AFSEL0_SEL5 BITS(20,23) /*!< pin 5 alternate function selected */
  196. #define GPIO_AFSEL0_SEL6 BITS(24,27) /*!< pin 6 alternate function selected */
  197. #define GPIO_AFSEL0_SEL7 BITS(28,31) /*!< pin 7 alternate function selected */
  198. /* GPIO_AFSEL1 */
  199. #define GPIO_AFSEL1_SEL8 BITS(0,3) /*!< pin 8 alternate function selected */
  200. #define GPIO_AFSEL1_SEL9 BITS(4,7) /*!< pin 9 alternate function selected */
  201. #define GPIO_AFSEL1_SEL10 BITS(8,11) /*!< pin 10 alternate function selected */
  202. #define GPIO_AFSEL1_SEL11 BITS(12,15) /*!< pin 11 alternate function selected */
  203. #define GPIO_AFSEL1_SEL12 BITS(16,19) /*!< pin 12 alternate function selected */
  204. #define GPIO_AFSEL1_SEL13 BITS(20,23) /*!< pin 13 alternate function selected */
  205. #define GPIO_AFSEL1_SEL14 BITS(24,27) /*!< pin 14 alternate function selected */
  206. #define GPIO_AFSEL1_SEL15 BITS(28,31) /*!< pin 15 alternate function selected */
  207. /* GPIO_BC */
  208. #define GPIO_BC_CR0 BIT(0) /*!< pin 0 clear bit */
  209. #define GPIO_BC_CR1 BIT(1) /*!< pin 1 clear bit */
  210. #define GPIO_BC_CR2 BIT(2) /*!< pin 2 clear bit */
  211. #define GPIO_BC_CR3 BIT(3) /*!< pin 3 clear bit */
  212. #define GPIO_BC_CR4 BIT(4) /*!< pin 4 clear bit */
  213. #define GPIO_BC_CR5 BIT(5) /*!< pin 5 clear bit */
  214. #define GPIO_BC_CR6 BIT(6) /*!< pin 6 clear bit */
  215. #define GPIO_BC_CR7 BIT(7) /*!< pin 7 clear bit */
  216. #define GPIO_BC_CR8 BIT(8) /*!< pin 8 clear bit */
  217. #define GPIO_BC_CR9 BIT(9) /*!< pin 9 clear bit */
  218. #define GPIO_BC_CR10 BIT(10) /*!< pin 10 clear bit */
  219. #define GPIO_BC_CR11 BIT(11) /*!< pin 11 clear bit */
  220. #define GPIO_BC_CR12 BIT(12) /*!< pin 12 clear bit */
  221. #define GPIO_BC_CR13 BIT(13) /*!< pin 13 clear bit */
  222. #define GPIO_BC_CR14 BIT(14) /*!< pin 14 clear bit */
  223. #define GPIO_BC_CR15 BIT(15) /*!< pin 15 clear bit */
  224. /* GPIO_TG */
  225. #define GPIO_TG_TG0 BIT(0) /*!< pin 0 toggle bit */
  226. #define GPIO_TG_TG1 BIT(1) /*!< pin 1 toggle bit */
  227. #define GPIO_TG_TG2 BIT(2) /*!< pin 2 toggle bit */
  228. #define GPIO_TG_TG3 BIT(3) /*!< pin 3 toggle bit */
  229. #define GPIO_TG_TG4 BIT(4) /*!< pin 4 toggle bit */
  230. #define GPIO_TG_TG5 BIT(5) /*!< pin 5 toggle bit */
  231. #define GPIO_TG_TG6 BIT(6) /*!< pin 6 toggle bit */
  232. #define GPIO_TG_TG7 BIT(7) /*!< pin 7 toggle bit */
  233. #define GPIO_TG_TG8 BIT(8) /*!< pin 8 toggle bit */
  234. #define GPIO_TG_TG9 BIT(9) /*!< pin 9 toggle bit */
  235. #define GPIO_TG_TG10 BIT(10) /*!< pin 10 toggle bit */
  236. #define GPIO_TG_TG11 BIT(11) /*!< pin 11 toggle bit */
  237. #define GPIO_TG_TG12 BIT(12) /*!< pin 12 toggle bit */
  238. #define GPIO_TG_TG13 BIT(13) /*!< pin 13 toggle bit */
  239. #define GPIO_TG_TG14 BIT(14) /*!< pin 14 toggle bit */
  240. #define GPIO_TG_TG15 BIT(15) /*!< pin 15 toggle bit */
  241. /* constants definitions */
  242. typedef FlagStatus bit_status;
  243. /* output mode definitions */
  244. #define CTL_CLTR(regval) (BITS(0,1) & ((uint32_t)(regval) << 0))
  245. #define GPIO_MODE_INPUT CTL_CLTR(0) /*!< input mode */
  246. #define GPIO_MODE_OUTPUT CTL_CLTR(1) /*!< output mode */
  247. #define GPIO_MODE_AF CTL_CLTR(2) /*!< alternate function mode */
  248. #define GPIO_MODE_ANALOG CTL_CLTR(3) /*!< analog mode */
  249. /* pull up pull down definitions */
  250. #define PUD_PUPD(regval) (BITS(0,1) & ((uint32_t)(regval) << 0))
  251. #define GPIO_PUPD_NONE PUD_PUPD(0) /*!< without weak pull-up and pull-down resistors */
  252. #define GPIO_PUPD_PULLUP PUD_PUPD(1) /*!< with weak pull-up resistor */
  253. #define GPIO_PUPD_PULLDOWN PUD_PUPD(2) /*!< with weak pull-down resistor */
  254. /* gpio pin definitions */
  255. #define GPIO_PIN_0 BIT(0) /*!< GPIO pin 0 */
  256. #define GPIO_PIN_1 BIT(1) /*!< GPIO pin 1 */
  257. #define GPIO_PIN_2 BIT(2) /*!< GPIO pin 2 */
  258. #define GPIO_PIN_3 BIT(3) /*!< GPIO pin 3 */
  259. #define GPIO_PIN_4 BIT(4) /*!< GPIO pin 4 */
  260. #define GPIO_PIN_5 BIT(5) /*!< GPIO pin 5 */
  261. #define GPIO_PIN_6 BIT(6) /*!< GPIO pin 6 */
  262. #define GPIO_PIN_7 BIT(7) /*!< GPIO pin 7 */
  263. #define GPIO_PIN_8 BIT(8) /*!< GPIO pin 8 */
  264. #define GPIO_PIN_9 BIT(9) /*!< GPIO pin 9 */
  265. #define GPIO_PIN_10 BIT(10) /*!< GPIO pin 10 */
  266. #define GPIO_PIN_11 BIT(11) /*!< GPIO pin 11 */
  267. #define GPIO_PIN_12 BIT(12) /*!< GPIO pin 12 */
  268. #define GPIO_PIN_13 BIT(13) /*!< GPIO pin 13 */
  269. #define GPIO_PIN_14 BIT(14) /*!< GPIO pin 14 */
  270. #define GPIO_PIN_15 BIT(15) /*!< GPIO pin 15 */
  271. #define GPIO_PIN_ALL ((uint32_t)(0xFFFF)) /*!< GPIO pin all */
  272. /* gpio ctlr values */
  273. #define GPIO_MODE_SET(n, mode) ((uint32_t)((uint32_t)(mode) << (2U * (n))))
  274. #define GPIO_MODE_MASK(n) (0x3U << (2U * (n)))
  275. /* gpio pull up pull down values */
  276. #define GPIO_PUPD_SET(n, pupd) ((uint32_t)((uint32_t)(pupd) << (2U * (n))))
  277. #define GPIO_PUPD_MASK(n) (0x3U << (2U * (n)))
  278. /* gpio output speed values */
  279. #define GPIO_OSPEED_SET(n, speed) ((uint32_t)((uint32_t)(speed) << (2U * (n))))
  280. #define GPIO_OSPEED_MASK(n) (0x3U << (2U * (n)))
  281. /* gpio output type */
  282. #define GPIO_OTYPE_PP ((uint8_t)(0x00)) /*!< push pull mode */
  283. #define GPIO_OTYPE_OD ((uint8_t)(0x01)) /*!< open drain mode */
  284. /* gpio output max speed level */
  285. #define OSPD_OSPD(regval) (BITS(0,1) & ((uint32_t)(regval) << 0))
  286. #define GPIO_OSPEED_LEVEL0 OSPD_OSPD(0) /*!< output max speed level 0 */
  287. #define GPIO_OSPEED_LEVEL1 OSPD_OSPD(1) /*!< output max speed level 1 */
  288. #define GPIO_OSPEED_LEVEL2 OSPD_OSPD(2) /*!< output max speed level 2 */
  289. #define GPIO_OSPEED_LEVEL3 OSPD_OSPD(3) /*!< output max speed level 3 */
  290. /* gpio output max speed value */
  291. #define GPIO_OSPEED_2MHZ GPIO_OSPEED_LEVEL0 /*!< output max speed 2M */
  292. #define GPIO_OSPEED_25MHZ GPIO_OSPEED_LEVEL1 /*!< output max speed 25M */
  293. #define GPIO_OSPEED_50MHZ GPIO_OSPEED_LEVEL2 /*!< output max speed 50M */
  294. #define GPIO_OSPEED_200MHZ GPIO_OSPEED_LEVEL3 /*!< output max speed 200M */
  295. /* gpio alternate function values */
  296. #define GPIO_AFR_SET(n, af) ((uint32_t)((uint32_t)(af) << (4U * (n))))
  297. #define GPIO_AFR_MASK(n) (0xFU << (4U * (n)))
  298. /* gpio alternate function */
  299. #define AF(regval) (BITS(0,3) & ((uint32_t)(regval) << 0))
  300. #define GPIO_AF_0 AF(0) /*!< alternate function selected 0 */
  301. #define GPIO_AF_1 AF(1) /*!< alternate function selected 1 */
  302. #define GPIO_AF_2 AF(2) /*!< alternate function selected 2 */
  303. #define GPIO_AF_3 AF(3) /*!< alternate function selected 3 */
  304. #define GPIO_AF_4 AF(4) /*!< alternate function selected 4 */
  305. #define GPIO_AF_5 AF(5) /*!< alternate function selected 5 */
  306. #define GPIO_AF_6 AF(6) /*!< alternate function selected 6 */
  307. #define GPIO_AF_7 AF(7) /*!< alternate function selected 7 */
  308. #define GPIO_AF_8 AF(8) /*!< alternate function selected 8 */
  309. #define GPIO_AF_9 AF(9) /*!< alternate function selected 9 */
  310. #define GPIO_AF_10 AF(10) /*!< alternate function selected 10 */
  311. #define GPIO_AF_11 AF(11) /*!< alternate function selected 11 */
  312. #define GPIO_AF_12 AF(12) /*!< alternate function selected 12 */
  313. #define GPIO_AF_13 AF(13) /*!< alternate function selected 13 */
  314. #define GPIO_AF_14 AF(14) /*!< alternate function selected 14 */
  315. #define GPIO_AF_15 AF(15) /*!< alternate function selected 15 */
  316. /* function declarations */
  317. /* reset gpio port */
  318. void gpio_deinit(uint32_t gpio_periph);
  319. /* set gpio mode */
  320. void gpio_mode_set(uint32_t gpio_periph,uint32_t mode,uint32_t pull_up_down,uint32_t pin);
  321. /* set gpio output type and speed */
  322. void gpio_output_options_set(uint32_t gpio_periph,uint8_t otype,uint32_t speed,uint32_t pin);
  323. /* set gpio pin bit */
  324. void gpio_bit_set(uint32_t gpio_periph,uint32_t pin);
  325. /* reset gpio pin bit */
  326. void gpio_bit_reset(uint32_t gpio_periph,uint32_t pin);
  327. /* write data to the specified gpio pin */
  328. void gpio_bit_write(uint32_t gpio_periph,uint32_t pin,bit_status bit_value);
  329. /* write data to the specified gpio port */
  330. void gpio_port_write(uint32_t gpio_periph,uint16_t data);
  331. /* get gpio pin input status */
  332. FlagStatus gpio_input_bit_get(uint32_t gpio_periph,uint32_t pin);
  333. /* get gpio port input status */
  334. uint16_t gpio_input_port_get(uint32_t gpio_periph);
  335. /* get gpio pin output status */
  336. FlagStatus gpio_output_bit_get(uint32_t gpio_periph,uint32_t pin);
  337. /* get gpio port output status */
  338. uint16_t gpio_output_port_get(uint32_t gpio_periph);
  339. /* set gpio alternate function */
  340. void gpio_af_set(uint32_t gpio_periph,uint32_t alt_func_num,uint32_t pin);
  341. /* lock gpio pin bit */
  342. void gpio_pin_lock(uint32_t gpio_periph,uint32_t pin);
  343. /* toggle gpio pin status */
  344. void gpio_bit_toggle(uint32_t gpio_periph,uint32_t pin);
  345. /* toggle gpio port status */
  346. void gpio_port_toggle(uint32_t gpio_periph);
  347. #endif /* GD32F4XX_GPIO_H */