gd32f10x_gpio.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*!
  2. \file gd32f10x_gpio.h
  3. \brief definitions for the GPIO
  4. \version 2014-12-26, V1.0.0, firmware for GD32F10x
  5. \version 2017-06-20, V2.0.0, firmware for GD32F10x
  6. \version 2018-07-31, V2.1.0, firmware for GD32F10x
  7. \version 2020-09-30, V2.2.0, firmware for GD32F10x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #ifndef GD32F10x_GPIO_H
  33. #define GD32F10x_GPIO_H
  34. #include "gd32f10x.h"
  35. /* GPIOx(x=A,B,C,D,E,F,G) definitions */
  36. #define GPIOA (GPIO_BASE + 0x00000000U)
  37. #define GPIOB (GPIO_BASE + 0x00000400U)
  38. #define GPIOC (GPIO_BASE + 0x00000800U)
  39. #define GPIOD (GPIO_BASE + 0x00000C00U)
  40. #define GPIOE (GPIO_BASE + 0x00001000U)
  41. #define GPIOF (GPIO_BASE + 0x00001400U)
  42. #define GPIOG (GPIO_BASE + 0x00001800U)
  43. /* AFIO definitions */
  44. #define AFIO AFIO_BASE
  45. /* registers definitions */
  46. /* GPIO registers definitions */
  47. #define GPIO_CTL0(gpiox) REG32((gpiox) + 0x00U) /*!< GPIO port control register 0 */
  48. #define GPIO_CTL1(gpiox) REG32((gpiox) + 0x04U) /*!< GPIO port control register 1 */
  49. #define GPIO_ISTAT(gpiox) REG32((gpiox) + 0x08U) /*!< GPIO port input status register */
  50. #define GPIO_OCTL(gpiox) REG32((gpiox) + 0x0CU) /*!< GPIO port output control register */
  51. #define GPIO_BOP(gpiox) REG32((gpiox) + 0x10U) /*!< GPIO port bit operation register */
  52. #define GPIO_BC(gpiox) REG32((gpiox) + 0x14U) /*!< GPIO bit clear register */
  53. #define GPIO_LOCK(gpiox) REG32((gpiox) + 0x18U) /*!< GPIO port configuration lock register */
  54. /* AFIO registers definitions */
  55. #define AFIO_EC REG32(AFIO + 0x00U) /*!< AFIO event control register */
  56. #define AFIO_PCF0 REG32(AFIO + 0x04U) /*!< AFIO port configuration register 0 */
  57. #define AFIO_EXTISS0 REG32(AFIO + 0x08U) /*!< AFIO port EXTI sources selection register 0 */
  58. #define AFIO_EXTISS1 REG32(AFIO + 0x0CU) /*!< AFIO port EXTI sources selection register 1 */
  59. #define AFIO_EXTISS2 REG32(AFIO + 0x10U) /*!< AFIO port EXTI sources selection register 2 */
  60. #define AFIO_EXTISS3 REG32(AFIO + 0x14U) /*!< AFIO port EXTI sources selection register 3 */
  61. #define AFIO_PCF1 REG32(AFIO + 0x1CU) /*!< AFIO port configuration register 1 */
  62. /* bits definitions */
  63. /* GPIO_CTL0 */
  64. #define GPIO_CTL0_MD0 BITS(0,1) /*!< port 0 mode bits */
  65. #define GPIO_CTL0_CTL0 BITS(2,3) /*!< pin 0 configuration bits */
  66. #define GPIO_CTL0_MD1 BITS(4,5) /*!< port 1 mode bits */
  67. #define GPIO_CTL0_CTL1 BITS(6,7) /*!< pin 1 configuration bits */
  68. #define GPIO_CTL0_MD2 BITS(8,9) /*!< port 2 mode bits */
  69. #define GPIO_CTL0_CTL2 BITS(10,11) /*!< pin 2 configuration bits */
  70. #define GPIO_CTL0_MD3 BITS(12,13) /*!< port 3 mode bits */
  71. #define GPIO_CTL0_CTL3 BITS(14,15) /*!< pin 3 configuration bits */
  72. #define GPIO_CTL0_MD4 BITS(16,17) /*!< port 4 mode bits */
  73. #define GPIO_CTL0_CTL4 BITS(18,19) /*!< pin 4 configuration bits */
  74. #define GPIO_CTL0_MD5 BITS(20,21) /*!< port 5 mode bits */
  75. #define GPIO_CTL0_CTL5 BITS(22,23) /*!< pin 5 configuration bits */
  76. #define GPIO_CTL0_MD6 BITS(24,25) /*!< port 6 mode bits */
  77. #define GPIO_CTL0_CTL6 BITS(26,27) /*!< pin 6 configuration bits */
  78. #define GPIO_CTL0_MD7 BITS(28,29) /*!< port 7 mode bits */
  79. #define GPIO_CTL0_CTL7 BITS(30,31) /*!< pin 7 configuration bits */
  80. /* GPIO_CTL1 */
  81. #define GPIO_CTL1_MD8 BITS(0,1) /*!< port 8 mode bits */
  82. #define GPIO_CTL1_CTL8 BITS(2,3) /*!< pin 8 configuration bits */
  83. #define GPIO_CTL1_MD9 BITS(4,5) /*!< port 9 mode bits */
  84. #define GPIO_CTL1_CTL9 BITS(6,7) /*!< pin 9 configuration bits */
  85. #define GPIO_CTL1_MD10 BITS(8,9) /*!< port 10 mode bits */
  86. #define GPIO_CTL1_CTL10 BITS(10,11) /*!< pin 10 configuration bits */
  87. #define GPIO_CTL1_MD11 BITS(12,13) /*!< port 11 mode bits */
  88. #define GPIO_CTL1_CTL11 BITS(14,15) /*!< pin 11 configuration bits */
  89. #define GPIO_CTL1_MD12 BITS(16,17) /*!< port 12 mode bits */
  90. #define GPIO_CTL1_CTL12 BITS(18,19) /*!< pin 12 configuration bits */
  91. #define GPIO_CTL1_MD13 BITS(20,21) /*!< port 13 mode bits */
  92. #define GPIO_CTL1_CTL13 BITS(22,23) /*!< pin 13 configuration bits */
  93. #define GPIO_CTL1_MD14 BITS(24,25) /*!< port 14 mode bits */
  94. #define GPIO_CTL1_CTL14 BITS(26,27) /*!< pin 14 configuration bits */
  95. #define GPIO_CTL1_MD15 BITS(28,29) /*!< port 15 mode bits */
  96. #define GPIO_CTL1_CTL15 BITS(30,31) /*!< pin 15 configuration bits */
  97. /* GPIO_ISTAT */
  98. #define GPIO_ISTAT_ISTAT0 BIT(0) /*!< pin 0 input status */
  99. #define GPIO_ISTAT_ISTAT1 BIT(1) /*!< pin 1 input status */
  100. #define GPIO_ISTAT_ISTAT2 BIT(2) /*!< pin 2 input status */
  101. #define GPIO_ISTAT_ISTAT3 BIT(3) /*!< pin 3 input status */
  102. #define GPIO_ISTAT_ISTAT4 BIT(4) /*!< pin 4 input status */
  103. #define GPIO_ISTAT_ISTAT5 BIT(5) /*!< pin 5 input status */
  104. #define GPIO_ISTAT_ISTAT6 BIT(6) /*!< pin 6 input status */
  105. #define GPIO_ISTAT_ISTAT7 BIT(7) /*!< pin 7 input status */
  106. #define GPIO_ISTAT_ISTAT8 BIT(8) /*!< pin 8 input status */
  107. #define GPIO_ISTAT_ISTAT9 BIT(9) /*!< pin 9 input status */
  108. #define GPIO_ISTAT_ISTAT10 BIT(10) /*!< pin 10 input status */
  109. #define GPIO_ISTAT_ISTAT11 BIT(11) /*!< pin 11 input status */
  110. #define GPIO_ISTAT_ISTAT12 BIT(12) /*!< pin 12 input status */
  111. #define GPIO_ISTAT_ISTAT13 BIT(13) /*!< pin 13 input status */
  112. #define GPIO_ISTAT_ISTAT14 BIT(14) /*!< pin 14 input status */
  113. #define GPIO_ISTAT_ISTAT15 BIT(15) /*!< pin 15 input status */
  114. /* GPIO_OCTL */
  115. #define GPIO_OCTL_OCTL0 BIT(0) /*!< pin 0 output bit */
  116. #define GPIO_OCTL_OCTL1 BIT(1) /*!< pin 1 output bit */
  117. #define GPIO_OCTL_OCTL2 BIT(2) /*!< pin 2 output bit */
  118. #define GPIO_OCTL_OCTL3 BIT(3) /*!< pin 3 output bit */
  119. #define GPIO_OCTL_OCTL4 BIT(4) /*!< pin 4 output bit */
  120. #define GPIO_OCTL_OCTL5 BIT(5) /*!< pin 5 output bit */
  121. #define GPIO_OCTL_OCTL6 BIT(6) /*!< pin 6 output bit */
  122. #define GPIO_OCTL_OCTL7 BIT(7) /*!< pin 7 output bit */
  123. #define GPIO_OCTL_OCTL8 BIT(8) /*!< pin 8 output bit */
  124. #define GPIO_OCTL_OCTL9 BIT(9) /*!< pin 9 output bit */
  125. #define GPIO_OCTL_OCTL10 BIT(10) /*!< pin 10 output bit */
  126. #define GPIO_OCTL_OCTL11 BIT(11) /*!< pin 11 output bit */
  127. #define GPIO_OCTL_OCTL12 BIT(12) /*!< pin 12 output bit */
  128. #define GPIO_OCTL_OCTL13 BIT(13) /*!< pin 13 output bit */
  129. #define GPIO_OCTL_OCTL14 BIT(14) /*!< pin 14 output bit */
  130. #define GPIO_OCTL_OCTL15 BIT(15) /*!< pin 15 output bit */
  131. /* GPIO_BOP */
  132. #define GPIO_BOP_BOP0 BIT(0) /*!< pin 0 set bit */
  133. #define GPIO_BOP_BOP1 BIT(1) /*!< pin 1 set bit */
  134. #define GPIO_BOP_BOP2 BIT(2) /*!< pin 2 set bit */
  135. #define GPIO_BOP_BOP3 BIT(3) /*!< pin 3 set bit */
  136. #define GPIO_BOP_BOP4 BIT(4) /*!< pin 4 set bit */
  137. #define GPIO_BOP_BOP5 BIT(5) /*!< pin 5 set bit */
  138. #define GPIO_BOP_BOP6 BIT(6) /*!< pin 6 set bit */
  139. #define GPIO_BOP_BOP7 BIT(7) /*!< pin 7 set bit */
  140. #define GPIO_BOP_BOP8 BIT(8) /*!< pin 8 set bit */
  141. #define GPIO_BOP_BOP9 BIT(9) /*!< pin 9 set bit */
  142. #define GPIO_BOP_BOP10 BIT(10) /*!< pin 10 set bit */
  143. #define GPIO_BOP_BOP11 BIT(11) /*!< pin 11 set bit */
  144. #define GPIO_BOP_BOP12 BIT(12) /*!< pin 12 set bit */
  145. #define GPIO_BOP_BOP13 BIT(13) /*!< pin 13 set bit */
  146. #define GPIO_BOP_BOP14 BIT(14) /*!< pin 14 set bit */
  147. #define GPIO_BOP_BOP15 BIT(15) /*!< pin 15 set bit */
  148. #define GPIO_BOP_CR0 BIT(16) /*!< pin 0 clear bit */
  149. #define GPIO_BOP_CR1 BIT(17) /*!< pin 1 clear bit */
  150. #define GPIO_BOP_CR2 BIT(18) /*!< pin 2 clear bit */
  151. #define GPIO_BOP_CR3 BIT(19) /*!< pin 3 clear bit */
  152. #define GPIO_BOP_CR4 BIT(20) /*!< pin 4 clear bit */
  153. #define GPIO_BOP_CR5 BIT(21) /*!< pin 5 clear bit */
  154. #define GPIO_BOP_CR6 BIT(22) /*!< pin 6 clear bit */
  155. #define GPIO_BOP_CR7 BIT(23) /*!< pin 7 clear bit */
  156. #define GPIO_BOP_CR8 BIT(24) /*!< pin 8 clear bit */
  157. #define GPIO_BOP_CR9 BIT(25) /*!< pin 9 clear bit */
  158. #define GPIO_BOP_CR10 BIT(26) /*!< pin 10 clear bit */
  159. #define GPIO_BOP_CR11 BIT(27) /*!< pin 11 clear bit */
  160. #define GPIO_BOP_CR12 BIT(28) /*!< pin 12 clear bit */
  161. #define GPIO_BOP_CR13 BIT(29) /*!< pin 13 clear bit */
  162. #define GPIO_BOP_CR14 BIT(30) /*!< pin 14 clear bit */
  163. #define GPIO_BOP_CR15 BIT(31) /*!< pin 15 clear bit */
  164. /* GPIO_BC */
  165. #define GPIO_BC_CR0 BIT(0) /*!< pin 0 clear bit */
  166. #define GPIO_BC_CR1 BIT(1) /*!< pin 1 clear bit */
  167. #define GPIO_BC_CR2 BIT(2) /*!< pin 2 clear bit */
  168. #define GPIO_BC_CR3 BIT(3) /*!< pin 3 clear bit */
  169. #define GPIO_BC_CR4 BIT(4) /*!< pin 4 clear bit */
  170. #define GPIO_BC_CR5 BIT(5) /*!< pin 5 clear bit */
  171. #define GPIO_BC_CR6 BIT(6) /*!< pin 6 clear bit */
  172. #define GPIO_BC_CR7 BIT(7) /*!< pin 7 clear bit */
  173. #define GPIO_BC_CR8 BIT(8) /*!< pin 8 clear bit */
  174. #define GPIO_BC_CR9 BIT(9) /*!< pin 9 clear bit */
  175. #define GPIO_BC_CR10 BIT(10) /*!< pin 10 clear bit */
  176. #define GPIO_BC_CR11 BIT(11) /*!< pin 11 clear bit */
  177. #define GPIO_BC_CR12 BIT(12) /*!< pin 12 clear bit */
  178. #define GPIO_BC_CR13 BIT(13) /*!< pin 13 clear bit */
  179. #define GPIO_BC_CR14 BIT(14) /*!< pin 14 clear bit */
  180. #define GPIO_BC_CR15 BIT(15) /*!< pin 15 clear bit */
  181. /* GPIO_LOCK */
  182. #define GPIO_LOCK_LK0 BIT(0) /*!< pin 0 lock bit */
  183. #define GPIO_LOCK_LK1 BIT(1) /*!< pin 1 lock bit */
  184. #define GPIO_LOCK_LK2 BIT(2) /*!< pin 2 lock bit */
  185. #define GPIO_LOCK_LK3 BIT(3) /*!< pin 3 lock bit */
  186. #define GPIO_LOCK_LK4 BIT(4) /*!< pin 4 lock bit */
  187. #define GPIO_LOCK_LK5 BIT(5) /*!< pin 5 lock bit */
  188. #define GPIO_LOCK_LK6 BIT(6) /*!< pin 6 lock bit */
  189. #define GPIO_LOCK_LK7 BIT(7) /*!< pin 7 lock bit */
  190. #define GPIO_LOCK_LK8 BIT(8) /*!< pin 8 lock bit */
  191. #define GPIO_LOCK_LK9 BIT(9) /*!< pin 9 lock bit */
  192. #define GPIO_LOCK_LK10 BIT(10) /*!< pin 10 lock bit */
  193. #define GPIO_LOCK_LK11 BIT(11) /*!< pin 11 lock bit */
  194. #define GPIO_LOCK_LK12 BIT(12) /*!< pin 12 lock bit */
  195. #define GPIO_LOCK_LK13 BIT(13) /*!< pin 13 lock bit */
  196. #define GPIO_LOCK_LK14 BIT(14) /*!< pin 14 lock bit */
  197. #define GPIO_LOCK_LK15 BIT(15) /*!< pin 15 lock bit */
  198. #define GPIO_LOCK_LKK BIT(16) /*!< pin sequence lock key */
  199. /* AFIO_EC */
  200. #define AFIO_EC_PIN BITS(0,3) /*!< event output pin selection */
  201. #define AFIO_EC_PORT BITS(4,6) /*!< event output port selection */
  202. #define AFIO_EC_EOE BIT(7) /*!< event output enable */
  203. /* AFIO_PCF0 */
  204. #ifdef GD32F10X_CL
  205. /* memory map and bit definitions for GD32F10X_CL devices */
  206. #define AFIO_PCF0_SPI0_REMAP BIT(0) /*!< SPI0 remapping */
  207. #define AFIO_PCF0_I2C0_REMAP BIT(1) /*!< I2C0 remapping */
  208. #define AFIO_PCF0_USART0_REMAP BIT(2) /*!< USART0 remapping */
  209. #define AFIO_PCF0_USART1_REMAP BIT(3) /*!< USART1 remapping */
  210. #define AFIO_PCF0_USART2_REMAP BITS(4,5) /*!< USART2 remapping */
  211. #define AFIO_PCF0_TIMER0_REMAP BITS(6,7) /*!< TIMER0 remapping */
  212. #define AFIO_PCF0_TIMER1_REMAP BITS(8,9) /*!< TIMER1 remapping */
  213. #define AFIO_PCF0_TIMER2_REMAP BITS(10,11) /*!< TIMER2 remapping */
  214. #define AFIO_PCF0_TIMER3_REMAP BIT(12) /*!< TIMER3 remapping */
  215. #define AFIO_PCF0_CAN0_REMAP BITS(13,14) /*!< CAN0 remapping */
  216. #define AFIO_PCF0_PD01_REMAP BIT(15) /*!< port D0/port D1 mapping on OSC_IN/OSC_OUT */
  217. #define AFIO_PCF0_TIMER4CH3_IREMAP BIT(16) /*!< TIMER3 channel3 internal remapping */
  218. #define AFIO_PCF0_ENET_REMAP BIT(21) /*!< ethernet MAC I/O remapping */
  219. #define AFIO_PCF0_CAN1_REMAP BIT(22) /*!< CAN1 remapping */
  220. #define AFIO_PCF0_ENET_PHY_SEL BIT(23) /*!< ethernet MII or RMII PHY selection */
  221. #define AFIO_PCF0_SWJ_CFG BITS(24,26) /*!< serial wire JTAG configuration */
  222. #define AFIO_PCF0_SPI2_REMAP BIT(28) /*!< SPI2/I2S2 remapping */
  223. #define AFIO_PCF0_TIMER1ITI1_REMAP BIT(29) /*!< TIMER1 internal trigger 1 remapping */
  224. #define AFIO_PCF0_PTP_PPS_REMAP BIT(30) /*!< ethernet PTP PPS remapping */
  225. #else
  226. /* memory map and bit definitions for GD32F10X_MD, GD32F10X_HD devices and GD32F10X_XD devices */
  227. #define AFIO_PCF0_SPI0_REMAP BIT(0) /*!< SPI0 remapping */
  228. #define AFIO_PCF0_I2C0_REMAP BIT(1) /*!< I2C0 remapping */
  229. #define AFIO_PCF0_USART0_REMAP BIT(2) /*!< USART0 remapping */
  230. #define AFIO_PCF0_USART1_REMAP BIT(3) /*!< USART1 remapping */
  231. #define AFIO_PCF0_USART2_REMAP BITS(4,5) /*!< USART2 remapping */
  232. #define AFIO_PCF0_TIMER0_REMAP BITS(6,7) /*!< TIMER0 remapping */
  233. #define AFIO_PCF0_TIMER1_REMAP BITS(8,9) /*!< TIMER1 remapping */
  234. #define AFIO_PCF0_TIMER2_REMAP BITS(10,11) /*!< TIMER2 remapping */
  235. #define AFIO_PCF0_TIMER3_REMAP BIT(12) /*!< TIMER3 remapping */
  236. #define AFIO_PCF0_CAN_REMAP BITS(13,14) /*!< CAN remapping */
  237. #define AFIO_PCF0_PD01_REMAP BIT(15) /*!< port D0/port D1 mapping on OSC_IN/OSC_OUT */
  238. #define AFIO_PCF0_TIMER4CH3_REMAP BIT(16) /*!< TIMER4 channel3 internal remapping */
  239. #define AFIO_PCF0_ADC0_ETRGRT_REMAP BIT(18) /*!< ADC 0 external trigger routine conversion remapping */
  240. #define AFIO_PCF0_ADC1_ETRGRT_REMAP BIT(20) /*!< ADC 1 external trigger routine conversion remapping */
  241. #define AFIO_PCF0_SWJ_CFG BITS(24,26) /*!< serial wire JTAG configuration */
  242. #endif /* GD32F10X_CL */
  243. /* AFIO_EXTISS0 */
  244. #define AFIO_EXTI0_SS BITS(0,3) /*!< EXTI 0 sources selection */
  245. #define AFIO_EXTI1_SS BITS(4,7) /*!< EXTI 1 sources selection */
  246. #define AFIO_EXTI2_SS BITS(8,11) /*!< EXTI 2 sources selection */
  247. #define AFIO_EXTI3_SS BITS(12,15) /*!< EXTI 3 sources selection */
  248. /* AFIO_EXTISS1 */
  249. #define AFIO_EXTI4_SS BITS(0,3) /*!< EXTI 4 sources selection */
  250. #define AFIO_EXTI5_SS BITS(4,7) /*!< EXTI 5 sources selection */
  251. #define AFIO_EXTI6_SS BITS(8,11) /*!< EXTI 6 sources selection */
  252. #define AFIO_EXTI7_SS BITS(12,15) /*!< EXTI 7 sources selection */
  253. /* AFIO_EXTISS2 */
  254. #define AFIO_EXTI8_SS BITS(0,3) /*!< EXTI 8 sources selection */
  255. #define AFIO_EXTI9_SS BITS(4,7) /*!< EXTI 9 sources selection */
  256. #define AFIO_EXTI10_SS BITS(8,11) /*!< EXTI 10 sources selection */
  257. #define AFIO_EXTI11_SS BITS(12,15) /*!< EXTI 11 sources selection */
  258. /* AFIO_EXTISS3 */
  259. #define AFIO_EXTI12_SS BITS(0,3) /*!< EXTI 12 sources selection */
  260. #define AFIO_EXTI13_SS BITS(4,7) /*!< EXTI 13 sources selection */
  261. #define AFIO_EXTI14_SS BITS(8,11) /*!< EXTI 14 sources selection */
  262. #define AFIO_EXTI15_SS BITS(12,15) /*!< EXTI 15 sources selection */
  263. /* AFIO_PCF1 */
  264. #define AFIO_PCF1_TIMER8_REMAP BIT(5) /*!< TIMER8 remapping */
  265. #define AFIO_PCF1_TIMER9_REMAP BIT(6) /*!< TIMER9 remapping */
  266. #define AFIO_PCF1_TIMER10_REMAP BIT(7) /*!< TIMER10 remapping */
  267. #define AFIO_PCF1_TIMER12_REMAP BIT(8) /*!< TIMER12 remapping */
  268. #define AFIO_PCF1_TIMER13_REMAP BIT(9) /*!< TIMER13 remapping */
  269. #define AFIO_PCF1_EXMC_NADV BIT(10) /*!< EXMC_NADV connect/disconnect */
  270. /* constants definitions */
  271. typedef FlagStatus bit_status;
  272. /* GPIO mode values set */
  273. #define GPIO_MODE_SET(n, mode) ((uint32_t)((uint32_t)(mode) << (4U * (n))))
  274. #define GPIO_MODE_MASK(n) (0xFU << (4U * (n)))
  275. /* GPIO mode definitions */
  276. #define GPIO_MODE_AIN ((uint8_t)0x00U) /*!< analog input mode */
  277. #define GPIO_MODE_IN_FLOATING ((uint8_t)0x04U) /*!< floating input mode */
  278. #define GPIO_MODE_IPD ((uint8_t)0x28U) /*!< pull-down input mode */
  279. #define GPIO_MODE_IPU ((uint8_t)0x48U) /*!< pull-up input mode */
  280. #define GPIO_MODE_OUT_OD ((uint8_t)0x14U) /*!< GPIO output with open-drain */
  281. #define GPIO_MODE_OUT_PP ((uint8_t)0x10U) /*!< GPIO output with push-pull */
  282. #define GPIO_MODE_AF_OD ((uint8_t)0x1CU) /*!< AFIO output with open-drain */
  283. #define GPIO_MODE_AF_PP ((uint8_t)0x18U) /*!< AFIO output with push-pull */
  284. /* GPIO output max speed value */
  285. #define GPIO_OSPEED_10MHZ ((uint8_t)0x01U) /*!< output max speed 10MHz */
  286. #define GPIO_OSPEED_2MHZ ((uint8_t)0x02U) /*!< output max speed 2MHz */
  287. #define GPIO_OSPEED_50MHZ ((uint8_t)0x03U) /*!< output max speed 50MHz */
  288. /* GPIO event output port definitions */
  289. #define GPIO_EVENT_PORT_GPIOA ((uint8_t)0x00U) /*!< event output port A */
  290. #define GPIO_EVENT_PORT_GPIOB ((uint8_t)0x01U) /*!< event output port B */
  291. #define GPIO_EVENT_PORT_GPIOC ((uint8_t)0x02U) /*!< event output port C */
  292. #define GPIO_EVENT_PORT_GPIOD ((uint8_t)0x03U) /*!< event output port D */
  293. #define GPIO_EVENT_PORT_GPIOE ((uint8_t)0x04U) /*!< event output port E */
  294. /* GPIO output port source definitions */
  295. #define GPIO_PORT_SOURCE_GPIOA ((uint8_t)0x00U) /*!< output port source A */
  296. #define GPIO_PORT_SOURCE_GPIOB ((uint8_t)0x01U) /*!< output port source B */
  297. #define GPIO_PORT_SOURCE_GPIOC ((uint8_t)0x02U) /*!< output port source C */
  298. #define GPIO_PORT_SOURCE_GPIOD ((uint8_t)0x03U) /*!< output port source D */
  299. #define GPIO_PORT_SOURCE_GPIOE ((uint8_t)0x04U) /*!< output port source E */
  300. #define GPIO_PORT_SOURCE_GPIOF ((uint8_t)0x05U) /*!< output port source F */
  301. #define GPIO_PORT_SOURCE_GPIOG ((uint8_t)0x06U) /*!< output port source G */
  302. /* GPIO event output pin definitions */
  303. #define GPIO_EVENT_PIN_0 ((uint8_t)0x00U) /*!< GPIO event pin 0 */
  304. #define GPIO_EVENT_PIN_1 ((uint8_t)0x01U) /*!< GPIO event pin 1 */
  305. #define GPIO_EVENT_PIN_2 ((uint8_t)0x02U) /*!< GPIO event pin 2 */
  306. #define GPIO_EVENT_PIN_3 ((uint8_t)0x03U) /*!< GPIO event pin 3 */
  307. #define GPIO_EVENT_PIN_4 ((uint8_t)0x04U) /*!< GPIO event pin 4 */
  308. #define GPIO_EVENT_PIN_5 ((uint8_t)0x05U) /*!< GPIO event pin 5 */
  309. #define GPIO_EVENT_PIN_6 ((uint8_t)0x06U) /*!< GPIO event pin 6 */
  310. #define GPIO_EVENT_PIN_7 ((uint8_t)0x07U) /*!< GPIO event pin 7 */
  311. #define GPIO_EVENT_PIN_8 ((uint8_t)0x08U) /*!< GPIO event pin 8 */
  312. #define GPIO_EVENT_PIN_9 ((uint8_t)0x09U) /*!< GPIO event pin 9 */
  313. #define GPIO_EVENT_PIN_10 ((uint8_t)0x0AU) /*!< GPIO event pin 10 */
  314. #define GPIO_EVENT_PIN_11 ((uint8_t)0x0BU) /*!< GPIO event pin 11 */
  315. #define GPIO_EVENT_PIN_12 ((uint8_t)0x0CU) /*!< GPIO event pin 12 */
  316. #define GPIO_EVENT_PIN_13 ((uint8_t)0x0DU) /*!< GPIO event pin 13 */
  317. #define GPIO_EVENT_PIN_14 ((uint8_t)0x0EU) /*!< GPIO event pin 14 */
  318. #define GPIO_EVENT_PIN_15 ((uint8_t)0x0FU) /*!< GPIO event pin 15 */
  319. /* GPIO output pin source definitions */
  320. #define GPIO_PIN_SOURCE_0 ((uint8_t)0x00U) /*!< GPIO pin source 0 */
  321. #define GPIO_PIN_SOURCE_1 ((uint8_t)0x01U) /*!< GPIO pin source 1 */
  322. #define GPIO_PIN_SOURCE_2 ((uint8_t)0x02U) /*!< GPIO pin source 2 */
  323. #define GPIO_PIN_SOURCE_3 ((uint8_t)0x03U) /*!< GPIO pin source 3 */
  324. #define GPIO_PIN_SOURCE_4 ((uint8_t)0x04U) /*!< GPIO pin source 4 */
  325. #define GPIO_PIN_SOURCE_5 ((uint8_t)0x05U) /*!< GPIO pin source 5 */
  326. #define GPIO_PIN_SOURCE_6 ((uint8_t)0x06U) /*!< GPIO pin source 6 */
  327. #define GPIO_PIN_SOURCE_7 ((uint8_t)0x07U) /*!< GPIO pin source 7 */
  328. #define GPIO_PIN_SOURCE_8 ((uint8_t)0x08U) /*!< GPIO pin source 8 */
  329. #define GPIO_PIN_SOURCE_9 ((uint8_t)0x09U) /*!< GPIO pin source 9 */
  330. #define GPIO_PIN_SOURCE_10 ((uint8_t)0x0AU) /*!< GPIO pin source 10 */
  331. #define GPIO_PIN_SOURCE_11 ((uint8_t)0x0BU) /*!< GPIO pin source 11 */
  332. #define GPIO_PIN_SOURCE_12 ((uint8_t)0x0CU) /*!< GPIO pin source 12 */
  333. #define GPIO_PIN_SOURCE_13 ((uint8_t)0x0DU) /*!< GPIO pin source 13 */
  334. #define GPIO_PIN_SOURCE_14 ((uint8_t)0x0EU) /*!< GPIO pin source 14 */
  335. #define GPIO_PIN_SOURCE_15 ((uint8_t)0x0FU) /*!< GPIO pin source 15 */
  336. /* GPIO pin definitions */
  337. #define GPIO_PIN_0 BIT(0) /*!< GPIO pin 0 */
  338. #define GPIO_PIN_1 BIT(1) /*!< GPIO pin 1 */
  339. #define GPIO_PIN_2 BIT(2) /*!< GPIO pin 2 */
  340. #define GPIO_PIN_3 BIT(3) /*!< GPIO pin 3 */
  341. #define GPIO_PIN_4 BIT(4) /*!< GPIO pin 4 */
  342. #define GPIO_PIN_5 BIT(5) /*!< GPIO pin 5 */
  343. #define GPIO_PIN_6 BIT(6) /*!< GPIO pin 6 */
  344. #define GPIO_PIN_7 BIT(7) /*!< GPIO pin 7 */
  345. #define GPIO_PIN_8 BIT(8) /*!< GPIO pin 8 */
  346. #define GPIO_PIN_9 BIT(9) /*!< GPIO pin 9 */
  347. #define GPIO_PIN_10 BIT(10) /*!< GPIO pin 10 */
  348. #define GPIO_PIN_11 BIT(11) /*!< GPIO pin 11 */
  349. #define GPIO_PIN_12 BIT(12) /*!< GPIO pin 12 */
  350. #define GPIO_PIN_13 BIT(13) /*!< GPIO pin 13 */
  351. #define GPIO_PIN_14 BIT(14) /*!< GPIO pin 14 */
  352. #define GPIO_PIN_15 BIT(15) /*!< GPIO pin 15 */
  353. #define GPIO_PIN_ALL BITS(0,15) /*!< GPIO pin all */
  354. /* GPIO remap definitions */
  355. #define GPIO_SPI0_REMAP ((uint32_t)0x00000001U) /*!< SPI0 remapping */
  356. #define GPIO_I2C0_REMAP ((uint32_t)0x00000002U) /*!< I2C0 remapping */
  357. #define GPIO_USART0_REMAP ((uint32_t)0x00000004U) /*!< USART0 remapping */
  358. #define GPIO_USART1_REMAP ((uint32_t)0x00000008U) /*!< USART1 remapping */
  359. #define GPIO_USART2_PARTIAL_REMAP ((uint32_t)0x00140010U) /*!< USART2 partial remapping */
  360. #define GPIO_USART2_FULL_REMAP ((uint32_t)0x00140030U) /*!< USART2 full remapping */
  361. #define GPIO_TIMER0_PARTIAL_REMAP ((uint32_t)0x00160040U) /*!< TIMER0 partial remapping */
  362. #define GPIO_TIMER0_FULL_REMAP ((uint32_t)0x001600C0U) /*!< TIMER0 full remapping */
  363. #define GPIO_TIMER1_PARTIAL_REMAP0 ((uint32_t)0x00180100U) /*!< TIMER1 partial remapping */
  364. #define GPIO_TIMER1_PARTIAL_REMAP1 ((uint32_t)0x00180200U) /*!< TIMER1 partial remapping */
  365. #define GPIO_TIMER1_FULL_REMAP ((uint32_t)0x00180300U) /*!< TIMER1 full remapping */
  366. #define GPIO_TIMER2_PARTIAL_REMAP ((uint32_t)0x001A0800U) /*!< TIMER2 partial remapping */
  367. #define GPIO_TIMER2_FULL_REMAP ((uint32_t)0x001A0C00U) /*!< TIMER2 full remapping */
  368. #define GPIO_TIMER3_REMAP ((uint32_t)0x00001000U) /*!< TIMER3 remapping */
  369. #define GPIO_PD01_REMAP ((uint32_t)0x00008000U) /*!< PD01 remapping */
  370. #if (defined(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
  371. #define GPIO_CAN_PARTIAL_REMAP ((uint32_t)0x001D4000U) /*!< CAN partial remapping(only for GD32F10X_MD devices, GD32F10X_HD devices and GD32F10X_XD devices) */
  372. #define GPIO_CAN_FULL_REMAP ((uint32_t)0x001D6000U) /*!< CAN full remapping(only for GD32F10X_MD devices, GD32F10X_HD devices and GD32F10X_XD devices) */
  373. #endif /* GD32F10X_MD||GD32F10X_HD||GD32F10X_XD */
  374. #if (defined(GD32F10X_CL) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
  375. #define GPIO_SPI2_REMAP ((uint32_t)0x00201100U) /*!< SPI2 remapping(only for GD32F10X_CL devices) */
  376. #endif /* GD32F10X_CL||GD32F10X_HD */
  377. #if (defined(GD32F10X_CL) || defined(GD32F10X_HD))
  378. #define GPIO_TIMER4CH3_IREMAP ((uint32_t)0x00200001U) /*!< TIMER4 channel3 internal remapping(only for GD32F10X_CL devices and GD32F10X_HD devices) */
  379. #endif /* GD32F10X_CL||GD32F10X_HD */
  380. #if (defined(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
  381. #define GPIO_ADC0_ETRGRT_REMAP ((uint32_t)0x00200004U) /*!< ADC0 external trigger routine conversion remapping(only for GD32F10X_MD devices, GD32F10X_HD devices and GD32F10X_XD devices) */
  382. #define GPIO_ADC1_ETRGRT_REMAP ((uint32_t)0x00200010U) /*!< ADC1 external trigger routine conversion remapping(only for GD32F10X_MD devices, GD32F10X_HD devices and GD32F10X_XD devices) */
  383. #endif /* GD32F10X_MD||GD32F10X_HD||GD32F10X_XD */
  384. #define GPIO_SWJ_NONJTRST_REMAP ((uint32_t)0x00300100U) /*!< full SWJ(JTAG-DP + SW-DP),but without NJTRST */
  385. #define GPIO_SWJ_SWDPENABLE_REMAP ((uint32_t)0x00300200U) /*!< JTAG-DP disabled and SW-DP enabled */
  386. #define GPIO_SWJ_DISABLE_REMAP ((uint32_t)0x00300400U) /*!< JTAG-DP disabled and SW-DP disabled */
  387. #ifdef GD32F10X_CL
  388. #define GPIO_CAN0_PARTIAL_REMAP ((uint32_t)0x001D4000U) /*!< CAN0 partial remapping(only for GD32F10X_CL devices) */
  389. #define GPIO_CAN0_FULL_REMAP ((uint32_t)0x001D6000U) /*!< CAN0 full remapping(only for GD32F10X_CL devices) */
  390. #define GPIO_ENET_REMAP ((uint32_t)0x00200020U) /*!< ENET remapping(only for GD32F10X_CL devices) */
  391. #define GPIO_CAN1_REMAP ((uint32_t)0x00200040U) /*!< CAN1 remapping(only for GD32F10X_CL devices) */
  392. #define GPIO_TIMER1ITI1_REMAP ((uint32_t)0x00202000U) /*!< TIMER1 internal trigger 1 remapping(only for GD32F10X_CL devices) */
  393. #define GPIO_PTP_PPS_REMAP ((uint32_t)0x00204000U) /*!< ethernet PTP PPS remapping(only for GD32F10X_CL devices) */
  394. #endif /* GD32F10X_CL */
  395. #ifdef GD32F10X_XD
  396. #define GPIO_TIMER8_REMAP ((uint32_t)0x80000020U) /*!< TIMER8 remapping */
  397. #define GPIO_TIMER9_REMAP ((uint32_t)0x80000040U) /*!< TIMER9 remapping */
  398. #define GPIO_TIMER10_REMAP ((uint32_t)0x80000080U) /*!< TIMER10 remapping */
  399. #define GPIO_TIMER12_REMAP ((uint32_t)0x80000100U) /*!< TIMER12 remapping */
  400. #define GPIO_TIMER13_REMAP ((uint32_t)0x80000200U) /*!< TIMER13 remapping */
  401. #define GPIO_EXMC_NADV_REMAP ((uint32_t)0x80000400U) /*!< EXMC_NADV connect/disconnect */
  402. #endif /* GD32F10X_XD */
  403. #ifdef GD32F10X_CL
  404. /* ethernet MII or RMII PHY selection */
  405. #define GPIO_ENET_PHY_MII ((uint32_t)0x00000000U) /*!< configure ethernet MAC for connection with an MII PHY */
  406. #define GPIO_ENET_PHY_RMII AFIO_PCF0_ENET_PHY_SEL /*!< configure ethernet MAC for connection with an RMII PHY */
  407. #endif /* GD32F10X_CL */
  408. /* function declarations */
  409. /* reset GPIO port */
  410. void gpio_deinit(uint32_t gpio_periph);
  411. /* reset alternate function I/O(AFIO) */
  412. void gpio_afio_deinit(void);
  413. /* GPIO parameter initialization */
  414. void gpio_init(uint32_t gpio_periph,uint32_t mode,uint32_t speed,uint32_t pin);
  415. /* set GPIO pin bit */
  416. void gpio_bit_set(uint32_t gpio_periph, uint32_t pin);
  417. /* reset GPIO pin bit */
  418. void gpio_bit_reset(uint32_t gpio_periph, uint32_t pin);
  419. /* write data to the specified GPIO pin */
  420. void gpio_bit_write(uint32_t gpio_periph, uint32_t pin, bit_status bit_value);
  421. /* write data to the specified GPIO port */
  422. void gpio_port_write(uint32_t gpio_periph, uint16_t data);
  423. /* get GPIO pin input status */
  424. FlagStatus gpio_input_bit_get(uint32_t gpio_periph, uint32_t pin);
  425. /* get GPIO port input status */
  426. uint16_t gpio_input_port_get(uint32_t gpio_periph);
  427. /* get GPIO pin output status */
  428. FlagStatus gpio_output_bit_get(uint32_t gpio_periph, uint32_t pin);
  429. /* get GPIO port output status */
  430. uint16_t gpio_output_port_get(uint32_t gpio_periph);
  431. /* configure GPIO pin remap */
  432. void gpio_pin_remap_config(uint32_t remap, ControlStatus newvalue);
  433. /* select GPIO pin exti sources */
  434. void gpio_exti_source_select(uint8_t output_port, uint8_t output_pin);
  435. /* configure GPIO pin event output */
  436. void gpio_event_output_config(uint8_t output_port, uint8_t output_pin);
  437. /* enable GPIO pin event output */
  438. void gpio_event_output_enable(void);
  439. /* disable GPIO pin event output */
  440. void gpio_event_output_disable(void);
  441. /* lock GPIO pin bit */
  442. void gpio_pin_lock(uint32_t gpio_periph, uint32_t pin);
  443. #ifdef GD32F10X_CL
  444. /* select ethernet MII or RMII PHY */
  445. void gpio_ethernet_phy_select(uint32_t gpio_enetsel);
  446. #endif /* GD32F10X_CL */
  447. #endif /* GD32F10x_GPIO_H */