1 /*!
2     \file    gd32f4xx_gpio.h
3     \brief   definitions for the GPIO
4 
5     \version 2016-08-15, V1.0.0, firmware for GD32F4xx
6     \version 2018-12-12, V2.0.0, firmware for GD32F4xx
7     \version 2020-09-30, V2.1.0, firmware for GD32F4xx
8     \version 2022-03-09, V3.0.0, firmware for GD32F4xx
9 */
10 
11 /*
12     Copyright (c) 2022, GigaDevice Semiconductor Inc.
13 
14     Redistribution and use in source and binary forms, with or without modification,
15 are permitted provided that the following conditions are met:
16 
17     1. Redistributions of source code must retain the above copyright notice, this
18        list of conditions and the following disclaimer.
19     2. Redistributions in binary form must reproduce the above copyright notice,
20        this list of conditions and the following disclaimer in the documentation
21        and/or other materials provided with the distribution.
22     3. Neither the name of the copyright holder nor the names of its contributors
23        may be used to endorse or promote products derived from this software without
24        specific prior written permission.
25 
26     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 OF SUCH DAMAGE.
36 */
37 
38 #ifndef GD32F4XX_GPIO_H
39 #define GD32F4XX_GPIO_H
40 
41 #include "gd32f4xx.h"
42 
43 /* GPIOx(x=A,B,C,D,E,F,G,H,I) definitions */
44 #define GPIOA                      (GPIO_BASE + 0x00000000U)
45 #define GPIOB                      (GPIO_BASE + 0x00000400U)
46 #define GPIOC                      (GPIO_BASE + 0x00000800U)
47 #define GPIOD                      (GPIO_BASE + 0x00000C00U)
48 #define GPIOE                      (GPIO_BASE + 0x00001000U)
49 #define GPIOF                      (GPIO_BASE + 0x00001400U)
50 #define GPIOG                      (GPIO_BASE + 0x00001800U)
51 #define GPIOH                      (GPIO_BASE + 0x00001C00U)
52 #define GPIOI                      (GPIO_BASE + 0x00002000U)
53 
54 /* registers definitions */
55 #define GPIO_CTL(gpiox)            REG32((gpiox) + 0x00U)    /*!< GPIO port control register */
56 #define GPIO_OMODE(gpiox)          REG32((gpiox) + 0x04U)    /*!< GPIO port output mode register */
57 #define GPIO_OSPD(gpiox)           REG32((gpiox) + 0x08U)    /*!< GPIO port output speed register */
58 #define GPIO_PUD(gpiox)            REG32((gpiox) + 0x0CU)    /*!< GPIO port pull-up/pull-down register */
59 #define GPIO_ISTAT(gpiox)          REG32((gpiox) + 0x10U)    /*!< GPIO port input status register */
60 #define GPIO_OCTL(gpiox)           REG32((gpiox) + 0x14U)    /*!< GPIO port output control register */
61 #define GPIO_BOP(gpiox)            REG32((gpiox) + 0x18U)    /*!< GPIO port bit operate register */
62 #define GPIO_LOCK(gpiox)           REG32((gpiox) + 0x1CU)    /*!< GPIO port configuration lock register */
63 #define GPIO_AFSEL0(gpiox)         REG32((gpiox) + 0x20U)    /*!< GPIO alternate function selected register 0 */
64 #define GPIO_AFSEL1(gpiox)         REG32((gpiox) + 0x24U)    /*!< GPIO alternate function selected register 1 */
65 #define GPIO_BC(gpiox)             REG32((gpiox) + 0x28U)    /*!< GPIO bit clear register */
66 #define GPIO_TG(gpiox)             REG32((gpiox) + 0x2CU)    /*!< GPIO port bit toggle register */
67 
68 /* bits definitions */
69 /* GPIO_CTL */
70 #define GPIO_CTL_CTL0              BITS(0,1)                 /*!< pin 0 configuration bits */
71 #define GPIO_CTL_CTL1              BITS(2,3)                 /*!< pin 1 configuration bits */
72 #define GPIO_CTL_CTL2              BITS(4,5)                 /*!< pin 2 configuration bits */
73 #define GPIO_CTL_CTL3              BITS(6,7)                 /*!< pin 3 configuration bits */
74 #define GPIO_CTL_CTL4              BITS(8,9)                 /*!< pin 4 configuration bits */
75 #define GPIO_CTL_CTL5              BITS(10,11)               /*!< pin 5 configuration bits */
76 #define GPIO_CTL_CTL6              BITS(12,13)               /*!< pin 6 configuration bits */
77 #define GPIO_CTL_CTL7              BITS(14,15)               /*!< pin 7 configuration bits */
78 #define GPIO_CTL_CTL8              BITS(16,17)               /*!< pin 8 configuration bits */
79 #define GPIO_CTL_CTL9              BITS(18,19)               /*!< pin 9 configuration bits */
80 #define GPIO_CTL_CTL10             BITS(20,21)               /*!< pin 10 configuration bits */
81 #define GPIO_CTL_CTL11             BITS(22,23)               /*!< pin 11 configuration bits */
82 #define GPIO_CTL_CTL12             BITS(24,25)               /*!< pin 12 configuration bits */
83 #define GPIO_CTL_CTL13             BITS(26,27)               /*!< pin 13 configuration bits */
84 #define GPIO_CTL_CTL14             BITS(28,29)               /*!< pin 14 configuration bits */
85 #define GPIO_CTL_CTL15             BITS(30,31)               /*!< pin 15 configuration bits */
86 
87 /* GPIO_OMODE */
88 #define GPIO_OMODE_OM0             BIT(0)                    /*!< pin 0 output mode bit */
89 #define GPIO_OMODE_OM1             BIT(1)                    /*!< pin 1 output mode bit */
90 #define GPIO_OMODE_OM2             BIT(2)                    /*!< pin 2 output mode bit */
91 #define GPIO_OMODE_OM3             BIT(3)                    /*!< pin 3 output mode bit */
92 #define GPIO_OMODE_OM4             BIT(4)                    /*!< pin 4 output mode bit */
93 #define GPIO_OMODE_OM5             BIT(5)                    /*!< pin 5 output mode bit */
94 #define GPIO_OMODE_OM6             BIT(6)                    /*!< pin 6 output mode bit */
95 #define GPIO_OMODE_OM7             BIT(7)                    /*!< pin 7 output mode bit */
96 #define GPIO_OMODE_OM8             BIT(8)                    /*!< pin 8 output mode bit */
97 #define GPIO_OMODE_OM9             BIT(9)                    /*!< pin 9 output mode bit */
98 #define GPIO_OMODE_OM10            BIT(10)                   /*!< pin 10 output mode bit */
99 #define GPIO_OMODE_OM11            BIT(11)                   /*!< pin 11 output mode bit */
100 #define GPIO_OMODE_OM12            BIT(12)                   /*!< pin 12 output mode bit */
101 #define GPIO_OMODE_OM13            BIT(13)                   /*!< pin 13 output mode bit */
102 #define GPIO_OMODE_OM14            BIT(14)                   /*!< pin 14 output mode bit */
103 #define GPIO_OMODE_OM15            BIT(15)                   /*!< pin 15 output mode bit */
104 
105 /* GPIO_OSPD */
106 #define GPIO_OSPD_OSPD0            BITS(0,1)                 /*!< pin 0 output max speed bits */
107 #define GPIO_OSPD_OSPD1            BITS(2,3)                 /*!< pin 1 output max speed bits */
108 #define GPIO_OSPD_OSPD2            BITS(4,5)                 /*!< pin 2 output max speed bits */
109 #define GPIO_OSPD_OSPD3            BITS(6,7)                 /*!< pin 3 output max speed bits */
110 #define GPIO_OSPD_OSPD4            BITS(8,9)                 /*!< pin 4 output max speed bits */
111 #define GPIO_OSPD_OSPD5            BITS(10,11)               /*!< pin 5 output max speed bits */
112 #define GPIO_OSPD_OSPD6            BITS(12,13)               /*!< pin 6 output max speed bits */
113 #define GPIO_OSPD_OSPD7            BITS(14,15)               /*!< pin 7 output max speed bits */
114 #define GPIO_OSPD_OSPD8            BITS(16,17)               /*!< pin 8 output max speed bits */
115 #define GPIO_OSPD_OSPD9            BITS(18,19)               /*!< pin 9 output max speed bits */
116 #define GPIO_OSPD_OSPD10           BITS(20,21)               /*!< pin 10 output max speed bits */
117 #define GPIO_OSPD_OSPD11           BITS(22,23)               /*!< pin 11 output max speed bits */
118 #define GPIO_OSPD_OSPD12           BITS(24,25)               /*!< pin 12 output max speed bits */
119 #define GPIO_OSPD_OSPD13           BITS(26,27)               /*!< pin 13 output max speed bits */
120 #define GPIO_OSPD_OSPD14           BITS(28,29)               /*!< pin 14 output max speed bits */
121 #define GPIO_OSPD_OSPD15           BITS(30,31)               /*!< pin 15 output max speed bits */
122 
123 /* GPIO_PUD */
124 #define GPIO_PUD_PUD0              BITS(0,1)                 /*!< pin 0 pull-up or pull-down bits */
125 #define GPIO_PUD_PUD1              BITS(2,3)                 /*!< pin 1 pull-up or pull-down bits */
126 #define GPIO_PUD_PUD2              BITS(4,5)                 /*!< pin 2 pull-up or pull-down bits */
127 #define GPIO_PUD_PUD3              BITS(6,7)                 /*!< pin 3 pull-up or pull-down bits */
128 #define GPIO_PUD_PUD4              BITS(8,9)                 /*!< pin 4 pull-up or pull-down bits */
129 #define GPIO_PUD_PUD5              BITS(10,11)               /*!< pin 5 pull-up or pull-down bits */
130 #define GPIO_PUD_PUD6              BITS(12,13)               /*!< pin 6 pull-up or pull-down bits */
131 #define GPIO_PUD_PUD7              BITS(14,15)               /*!< pin 7 pull-up or pull-down bits */
132 #define GPIO_PUD_PUD8              BITS(16,17)               /*!< pin 8 pull-up or pull-down bits */
133 #define GPIO_PUD_PUD9              BITS(18,19)               /*!< pin 9 pull-up or pull-down bits */
134 #define GPIO_PUD_PUD10             BITS(20,21)               /*!< pin 10 pull-up or pull-down bits */
135 #define GPIO_PUD_PUD11             BITS(22,23)               /*!< pin 11 pull-up or pull-down bits */
136 #define GPIO_PUD_PUD12             BITS(24,25)               /*!< pin 12 pull-up or pull-down bits */
137 #define GPIO_PUD_PUD13             BITS(26,27)               /*!< pin 13 pull-up or pull-down bits */
138 #define GPIO_PUD_PUD14             BITS(28,29)               /*!< pin 14 pull-up or pull-down bits */
139 #define GPIO_PUD_PUD15             BITS(30,31)               /*!< pin 15 pull-up or pull-down bits */
140 
141 /* GPIO_ISTAT */
142 #define GPIO_ISTAT_ISTAT0          BIT(0)                    /*!< pin 0 input status */
143 #define GPIO_ISTAT_ISTAT1          BIT(1)                    /*!< pin 1 input status */
144 #define GPIO_ISTAT_ISTAT2          BIT(2)                    /*!< pin 2 input status */
145 #define GPIO_ISTAT_ISTAT3          BIT(3)                    /*!< pin 3 input status */
146 #define GPIO_ISTAT_ISTAT4          BIT(4)                    /*!< pin 4 input status */
147 #define GPIO_ISTAT_ISTAT5          BIT(5)                    /*!< pin 5 input status */
148 #define GPIO_ISTAT_ISTAT6          BIT(6)                    /*!< pin 6 input status */
149 #define GPIO_ISTAT_ISTAT7          BIT(7)                    /*!< pin 7 input status */
150 #define GPIO_ISTAT_ISTAT8          BIT(8)                    /*!< pin 8 input status */
151 #define GPIO_ISTAT_ISTAT9          BIT(9)                    /*!< pin 9 input status */
152 #define GPIO_ISTAT_ISTAT10         BIT(10)                   /*!< pin 10 input status */
153 #define GPIO_ISTAT_ISTAT11         BIT(11)                   /*!< pin 11 input status */
154 #define GPIO_ISTAT_ISTAT12         BIT(12)                   /*!< pin 12 input status */
155 #define GPIO_ISTAT_ISTAT13         BIT(13)                   /*!< pin 13 input status */
156 #define GPIO_ISTAT_ISTAT14         BIT(14)                   /*!< pin 14 input status */
157 #define GPIO_ISTAT_ISTAT15         BIT(15)                   /*!< pin 15 input status */
158 
159 /* GPIO_OCTL */
160 #define GPIO_OCTL_OCTL0            BIT(0)                    /*!< pin 0 output control bit */
161 #define GPIO_OCTL_OCTL1            BIT(1)                    /*!< pin 1 output control bit */
162 #define GPIO_OCTL_OCTL2            BIT(2)                    /*!< pin 2 output control bit */
163 #define GPIO_OCTL_OCTL3            BIT(3)                    /*!< pin 3 output control bit */
164 #define GPIO_OCTL_OCTL4            BIT(4)                    /*!< pin 4 output control bit */
165 #define GPIO_OCTL_OCTL5            BIT(5)                    /*!< pin 5 output control bit */
166 #define GPIO_OCTL_OCTL6            BIT(6)                    /*!< pin 6 output control bit */
167 #define GPIO_OCTL_OCTL7            BIT(7)                    /*!< pin 7 output control bit */
168 #define GPIO_OCTL_OCTL8            BIT(8)                    /*!< pin 8 output control bit */
169 #define GPIO_OCTL_OCTL9            BIT(9)                    /*!< pin 9 output control bit */
170 #define GPIO_OCTL_OCTL10           BIT(10)                   /*!< pin 10 output control bit */
171 #define GPIO_OCTL_OCTL11           BIT(11)                   /*!< pin 11 output control bit */
172 #define GPIO_OCTL_OCTL12           BIT(12)                   /*!< pin 12 output control bit */
173 #define GPIO_OCTL_OCTL13           BIT(13)                   /*!< pin 13 output control bit */
174 #define GPIO_OCTL_OCTL14           BIT(14)                   /*!< pin 14 output control bit */
175 #define GPIO_OCTL_OCTL15           BIT(15)                   /*!< pin 15 output control bit */
176 
177 /* GPIO_BOP */
178 #define GPIO_BOP_BOP0              BIT(0)                    /*!< pin 0 set bit */
179 #define GPIO_BOP_BOP1              BIT(1)                    /*!< pin 1 set bit */
180 #define GPIO_BOP_BOP2              BIT(2)                    /*!< pin 2 set bit */
181 #define GPIO_BOP_BOP3              BIT(3)                    /*!< pin 3 set bit */
182 #define GPIO_BOP_BOP4              BIT(4)                    /*!< pin 4 set bit */
183 #define GPIO_BOP_BOP5              BIT(5)                    /*!< pin 5 set bit */
184 #define GPIO_BOP_BOP6              BIT(6)                    /*!< pin 6 set bit */
185 #define GPIO_BOP_BOP7              BIT(7)                    /*!< pin 7 set bit */
186 #define GPIO_BOP_BOP8              BIT(8)                    /*!< pin 8 set bit */
187 #define GPIO_BOP_BOP9              BIT(9)                    /*!< pin 9 set bit */
188 #define GPIO_BOP_BOP10             BIT(10)                   /*!< pin 10 set bit */
189 #define GPIO_BOP_BOP11             BIT(11)                   /*!< pin 11 set bit */
190 #define GPIO_BOP_BOP12             BIT(12)                   /*!< pin 12 set bit */
191 #define GPIO_BOP_BOP13             BIT(13)                   /*!< pin 13 set bit */
192 #define GPIO_BOP_BOP14             BIT(14)                   /*!< pin 14 set bit */
193 #define GPIO_BOP_BOP15             BIT(15)                   /*!< pin 15 set bit */
194 #define GPIO_BOP_CR0               BIT(16)                   /*!< pin 0 clear bit */
195 #define GPIO_BOP_CR1               BIT(17)                   /*!< pin 1 clear bit */
196 #define GPIO_BOP_CR2               BIT(18)                   /*!< pin 2 clear bit */
197 #define GPIO_BOP_CR3               BIT(19)                   /*!< pin 3 clear bit */
198 #define GPIO_BOP_CR4               BIT(20)                   /*!< pin 4 clear bit */
199 #define GPIO_BOP_CR5               BIT(21)                   /*!< pin 5 clear bit */
200 #define GPIO_BOP_CR6               BIT(22)                   /*!< pin 6 clear bit */
201 #define GPIO_BOP_CR7               BIT(23)                   /*!< pin 7 clear bit */
202 #define GPIO_BOP_CR8               BIT(24)                   /*!< pin 8 clear bit */
203 #define GPIO_BOP_CR9               BIT(25)                   /*!< pin 9 clear bit */
204 #define GPIO_BOP_CR10              BIT(26)                   /*!< pin 10 clear bit */
205 #define GPIO_BOP_CR11              BIT(27)                   /*!< pin 11 clear bit */
206 #define GPIO_BOP_CR12              BIT(28)                   /*!< pin 12 clear bit */
207 #define GPIO_BOP_CR13              BIT(29)                   /*!< pin 13 clear bit */
208 #define GPIO_BOP_CR14              BIT(30)                   /*!< pin 14 clear bit */
209 #define GPIO_BOP_CR15              BIT(31)                   /*!< pin 15 clear bit */
210 
211 /* GPIO_LOCK */
212 #define GPIO_LOCK_LK0              BIT(0)                    /*!< pin 0 lock bit */
213 #define GPIO_LOCK_LK1              BIT(1)                    /*!< pin 1 lock bit */
214 #define GPIO_LOCK_LK2              BIT(2)                    /*!< pin 2 lock bit */
215 #define GPIO_LOCK_LK3              BIT(3)                    /*!< pin 3 lock bit */
216 #define GPIO_LOCK_LK4              BIT(4)                    /*!< pin 4 lock bit */
217 #define GPIO_LOCK_LK5              BIT(5)                    /*!< pin 5 lock bit */
218 #define GPIO_LOCK_LK6              BIT(6)                    /*!< pin 6 lock bit */
219 #define GPIO_LOCK_LK7              BIT(7)                    /*!< pin 7 lock bit */
220 #define GPIO_LOCK_LK8              BIT(8)                    /*!< pin 8 lock bit */
221 #define GPIO_LOCK_LK9              BIT(9)                    /*!< pin 9 lock bit */
222 #define GPIO_LOCK_LK10             BIT(10)                   /*!< pin 10 lock bit */
223 #define GPIO_LOCK_LK11             BIT(11)                   /*!< pin 11 lock bit */
224 #define GPIO_LOCK_LK12             BIT(12)                   /*!< pin 12 lock bit */
225 #define GPIO_LOCK_LK13             BIT(13)                   /*!< pin 13 lock bit */
226 #define GPIO_LOCK_LK14             BIT(14)                   /*!< pin 14 lock bit */
227 #define GPIO_LOCK_LK15             BIT(15)                   /*!< pin 15 lock bit */
228 #define GPIO_LOCK_LKK              BIT(16)                   /*!< pin lock sequence key */
229 
230 /* GPIO_AFSEL0 */
231 #define GPIO_AFSEL0_SEL0           BITS(0,3)                 /*!< pin 0 alternate function selected */
232 #define GPIO_AFSEL0_SEL1           BITS(4,7)                 /*!< pin 1 alternate function selected */
233 #define GPIO_AFSEL0_SEL2           BITS(8,11)                /*!< pin 2 alternate function selected */
234 #define GPIO_AFSEL0_SEL3           BITS(12,15)               /*!< pin 3 alternate function selected */
235 #define GPIO_AFSEL0_SEL4           BITS(16,19)               /*!< pin 4 alternate function selected */
236 #define GPIO_AFSEL0_SEL5           BITS(20,23)               /*!< pin 5 alternate function selected */
237 #define GPIO_AFSEL0_SEL6           BITS(24,27)               /*!< pin 6 alternate function selected */
238 #define GPIO_AFSEL0_SEL7           BITS(28,31)               /*!< pin 7 alternate function selected */
239 
240 /* GPIO_AFSEL1 */
241 #define GPIO_AFSEL1_SEL8           BITS(0,3)                 /*!< pin 8 alternate function selected */
242 #define GPIO_AFSEL1_SEL9           BITS(4,7)                 /*!< pin 9 alternate function selected */
243 #define GPIO_AFSEL1_SEL10          BITS(8,11)                /*!< pin 10 alternate function selected */
244 #define GPIO_AFSEL1_SEL11          BITS(12,15)               /*!< pin 11 alternate function selected */
245 #define GPIO_AFSEL1_SEL12          BITS(16,19)               /*!< pin 12 alternate function selected */
246 #define GPIO_AFSEL1_SEL13          BITS(20,23)               /*!< pin 13 alternate function selected */
247 #define GPIO_AFSEL1_SEL14          BITS(24,27)               /*!< pin 14 alternate function selected */
248 #define GPIO_AFSEL1_SEL15          BITS(28,31)               /*!< pin 15 alternate function selected */
249 
250 /* GPIO_BC */
251 #define GPIO_BC_CR0                BIT(0)                    /*!< pin 0 clear bit */
252 #define GPIO_BC_CR1                BIT(1)                    /*!< pin 1 clear bit */
253 #define GPIO_BC_CR2                BIT(2)                    /*!< pin 2 clear bit */
254 #define GPIO_BC_CR3                BIT(3)                    /*!< pin 3 clear bit */
255 #define GPIO_BC_CR4                BIT(4)                    /*!< pin 4 clear bit */
256 #define GPIO_BC_CR5                BIT(5)                    /*!< pin 5 clear bit */
257 #define GPIO_BC_CR6                BIT(6)                    /*!< pin 6 clear bit */
258 #define GPIO_BC_CR7                BIT(7)                    /*!< pin 7 clear bit */
259 #define GPIO_BC_CR8                BIT(8)                    /*!< pin 8 clear bit */
260 #define GPIO_BC_CR9                BIT(9)                    /*!< pin 9 clear bit */
261 #define GPIO_BC_CR10               BIT(10)                   /*!< pin 10 clear bit */
262 #define GPIO_BC_CR11               BIT(11)                   /*!< pin 11 clear bit */
263 #define GPIO_BC_CR12               BIT(12)                   /*!< pin 12 clear bit */
264 #define GPIO_BC_CR13               BIT(13)                   /*!< pin 13 clear bit */
265 #define GPIO_BC_CR14               BIT(14)                   /*!< pin 14 clear bit */
266 #define GPIO_BC_CR15               BIT(15)                   /*!< pin 15 clear bit */
267 
268 /* GPIO_TG */
269 #define GPIO_TG_TG0                BIT(0)                    /*!< pin 0 toggle bit */
270 #define GPIO_TG_TG1                BIT(1)                    /*!< pin 1 toggle bit */
271 #define GPIO_TG_TG2                BIT(2)                    /*!< pin 2 toggle bit */
272 #define GPIO_TG_TG3                BIT(3)                    /*!< pin 3 toggle bit */
273 #define GPIO_TG_TG4                BIT(4)                    /*!< pin 4 toggle bit */
274 #define GPIO_TG_TG5                BIT(5)                    /*!< pin 5 toggle bit */
275 #define GPIO_TG_TG6                BIT(6)                    /*!< pin 6 toggle bit */
276 #define GPIO_TG_TG7                BIT(7)                    /*!< pin 7 toggle bit */
277 #define GPIO_TG_TG8                BIT(8)                    /*!< pin 8 toggle bit */
278 #define GPIO_TG_TG9                BIT(9)                    /*!< pin 9 toggle bit */
279 #define GPIO_TG_TG10               BIT(10)                   /*!< pin 10 toggle bit */
280 #define GPIO_TG_TG11               BIT(11)                   /*!< pin 11 toggle bit */
281 #define GPIO_TG_TG12               BIT(12)                   /*!< pin 12 toggle bit */
282 #define GPIO_TG_TG13               BIT(13)                   /*!< pin 13 toggle bit */
283 #define GPIO_TG_TG14               BIT(14)                   /*!< pin 14 toggle bit */
284 #define GPIO_TG_TG15               BIT(15)                   /*!< pin 15 toggle bit */
285 
286 /* constants definitions */
287 typedef FlagStatus bit_status;
288 
289 /* output mode definitions */
290 #define CTL_CLTR(regval)           (BITS(0,1) & ((uint32_t)(regval) << 0))
291 #define GPIO_MODE_INPUT            CTL_CLTR(0)               /*!< input mode */
292 #define GPIO_MODE_OUTPUT           CTL_CLTR(1)               /*!< output mode */
293 #define GPIO_MODE_AF               CTL_CLTR(2)               /*!< alternate function mode */
294 #define GPIO_MODE_ANALOG           CTL_CLTR(3)               /*!< analog mode */
295 
296 /* pull-up/ pull-down definitions */
297 #define PUD_PUPD(regval)           (BITS(0,1) & ((uint32_t)(regval) << 0))
298 #define GPIO_PUPD_NONE             PUD_PUPD(0)               /*!< floating mode, no pull-up and pull-down resistors */
299 #define GPIO_PUPD_PULLUP           PUD_PUPD(1)               /*!< with pull-up resistor */
300 #define GPIO_PUPD_PULLDOWN         PUD_PUPD(2)               /*!< with pull-down resistor */
301 
302 /* GPIO pin definitions */
303 #define GPIO_PIN_0                 BIT(0)                    /*!< GPIO pin 0 */
304 #define GPIO_PIN_1                 BIT(1)                    /*!< GPIO pin 1 */
305 #define GPIO_PIN_2                 BIT(2)                    /*!< GPIO pin 2 */
306 #define GPIO_PIN_3                 BIT(3)                    /*!< GPIO pin 3 */
307 #define GPIO_PIN_4                 BIT(4)                    /*!< GPIO pin 4 */
308 #define GPIO_PIN_5                 BIT(5)                    /*!< GPIO pin 5 */
309 #define GPIO_PIN_6                 BIT(6)                    /*!< GPIO pin 6 */
310 #define GPIO_PIN_7                 BIT(7)                    /*!< GPIO pin 7 */
311 #define GPIO_PIN_8                 BIT(8)                    /*!< GPIO pin 8 */
312 #define GPIO_PIN_9                 BIT(9)                    /*!< GPIO pin 9 */
313 #define GPIO_PIN_10                BIT(10)                   /*!< GPIO pin 10 */
314 #define GPIO_PIN_11                BIT(11)                   /*!< GPIO pin 11 */
315 #define GPIO_PIN_12                BIT(12)                   /*!< GPIO pin 12 */
316 #define GPIO_PIN_13                BIT(13)                   /*!< GPIO pin 13 */
317 #define GPIO_PIN_14                BIT(14)                   /*!< GPIO pin 14 */
318 #define GPIO_PIN_15                BIT(15)                   /*!< GPIO pin 15 */
319 #define GPIO_PIN_ALL               BITS(0,15)                /*!< GPIO pin all */
320 
321 /* GPIO mode configuration values */
322 #define GPIO_MODE_SET(n, mode)     ((uint32_t)((uint32_t)(mode) << (2U * (n))))
323 #define GPIO_MODE_MASK(n)          (0x3U << (2U * (n)))
324 
325 /* GPIO pull-up/ pull-down values */
326 #define GPIO_PUPD_SET(n, pupd)     ((uint32_t)((uint32_t)(pupd) << (2U * (n))))
327 #define GPIO_PUPD_MASK(n)          (0x3U << (2U * (n)))
328 
329 /* GPIO output speed values */
330 #define GPIO_OSPEED_SET(n, speed)  ((uint32_t)((uint32_t)(speed) << (2U * (n))))
331 #define GPIO_OSPEED_MASK(n)        (0x3U << (2U * (n)))
332 
333 /* GPIO output type */
334 #define GPIO_OTYPE_PP              ((uint8_t)(0x00U))        /*!< push pull mode */
335 #define GPIO_OTYPE_OD              ((uint8_t)(0x01U))        /*!< open drain mode */
336 
337 /* GPIO output max speed level */
338 #define OSPD_OSPD(regval)          (BITS(0,1) & ((uint32_t)(regval) << 0))
339 #define GPIO_OSPEED_LEVEL0         OSPD_OSPD(0)              /*!< output max speed level 0 */
340 #define GPIO_OSPEED_LEVEL1         OSPD_OSPD(1)              /*!< output max speed level 1 */
341 #define GPIO_OSPEED_LEVEL2         OSPD_OSPD(2)              /*!< output max speed level 2 */
342 #define GPIO_OSPEED_LEVEL3         OSPD_OSPD(3)              /*!< output max speed level 3 */
343 
344 /* GPIO output max speed value */
345 #define GPIO_OSPEED_2MHZ           GPIO_OSPEED_LEVEL0        /*!< output max speed 2MHz */
346 #define GPIO_OSPEED_25MHZ          GPIO_OSPEED_LEVEL1        /*!< output max speed 25MHz */
347 #define GPIO_OSPEED_50MHZ          GPIO_OSPEED_LEVEL2        /*!< output max speed 50MHz */
348 #define GPIO_OSPEED_MAX            GPIO_OSPEED_LEVEL3        /*!< GPIO very high output speed, max speed more than 50MHz */
349 
350 /* GPIO alternate function values */
351 #define GPIO_AFR_SET(n, af)        ((uint32_t)((uint32_t)(af) << (4U * (n))))
352 #define GPIO_AFR_MASK(n)           (0xFU << (4U * (n)))
353 
354 /* GPIO alternate function */
355 #define AF(regval)                 (BITS(0,3) & ((uint32_t)(regval) << 0))
356 #define GPIO_AF_0                   AF(0)                    /*!< alternate function 0 selected */
357 #define GPIO_AF_1                   AF(1)                    /*!< alternate function 1 selected */
358 #define GPIO_AF_2                   AF(2)                    /*!< alternate function 2 selected */
359 #define GPIO_AF_3                   AF(3)                    /*!< alternate function 3 selected */
360 #define GPIO_AF_4                   AF(4)                    /*!< alternate function 4 selected */
361 #define GPIO_AF_5                   AF(5)                    /*!< alternate function 5 selected */
362 #define GPIO_AF_6                   AF(6)                    /*!< alternate function 6 selected */
363 #define GPIO_AF_7                   AF(7)                    /*!< alternate function 7 selected */
364 #define GPIO_AF_8                   AF(8)                    /*!< alternate function 8 selected */
365 #define GPIO_AF_9                   AF(9)                    /*!< alternate function 9 selected */
366 #define GPIO_AF_10                  AF(10)                   /*!< alternate function 10 selected */
367 #define GPIO_AF_11                  AF(11)                   /*!< alternate function 11 selected */
368 #define GPIO_AF_12                  AF(12)                   /*!< alternate function 12 selected */
369 #define GPIO_AF_13                  AF(13)                   /*!< alternate function 13 selected */
370 #define GPIO_AF_14                  AF(14)                   /*!< alternate function 14 selected */
371 #define GPIO_AF_15                  AF(15)                   /*!< alternate function 15 selected */
372 
373 /* function declarations */
374 /* reset GPIO port */
375 void gpio_deinit(uint32_t gpio_periph);
376 /* set GPIO mode */
377 void gpio_mode_set(uint32_t gpio_periph, uint32_t mode, uint32_t pull_up_down, uint32_t pin);
378 /* set GPIO output type and speed */
379 void gpio_output_options_set(uint32_t gpio_periph, uint8_t otype, uint32_t speed, uint32_t pin);
380 
381 /* set GPIO pin bit */
382 void gpio_bit_set(uint32_t gpio_periph, uint32_t pin);
383 /* reset GPIO pin bit */
384 void gpio_bit_reset(uint32_t gpio_periph, uint32_t pin);
385 /* write data to the specified GPIO pin */
386 void gpio_bit_write(uint32_t gpio_periph, uint32_t pin, bit_status bit_value);
387 /* write data to the specified GPIO port */
388 void gpio_port_write(uint32_t gpio_periph, uint16_t data);
389 
390 /* get GPIO pin input status */
391 FlagStatus gpio_input_bit_get(uint32_t gpio_periph, uint32_t pin);
392 /* get GPIO port input status */
393 uint16_t gpio_input_port_get(uint32_t gpio_periph);
394 /* get GPIO pin output status */
395 FlagStatus gpio_output_bit_get(uint32_t gpio_periph, uint32_t pin);
396 /* get GPIO port output status */
397 uint16_t gpio_output_port_get(uint32_t gpio_periph);
398 
399 /* set GPIO alternate function */
400 void gpio_af_set(uint32_t gpio_periph, uint32_t alt_func_num, uint32_t pin);
401 /* lock GPIO pin bit */
402 void gpio_pin_lock(uint32_t gpio_periph, uint32_t pin);
403 
404 /* toggle GPIO pin status */
405 void gpio_bit_toggle(uint32_t gpio_periph, uint32_t pin);
406 /* toggle GPIO port status */
407 void gpio_port_toggle(uint32_t gpio_periph);
408 
409 #endif /* GD32F4XX_GPIO_H */
410