1 /*!
2     \file    gd32f3x0_gpio.h
3     \brief   definitions for the GPIO
4 
5     \version 2017-06-06, V1.0.0, firmware for GD32F3x0
6     \version 2019-06-01, V2.0.0, firmware for GD32F3x0
7     \version 2020-09-30, V2.1.0, firmware for GD32F3x0
8 */
9 
10 /*
11     Copyright (c) 2020, GigaDevice Semiconductor Inc.
12 
13     Redistribution and use in source and binary forms, with or without modification,
14 are permitted provided that the following conditions are met:
15 
16     1. Redistributions of source code must retain the above copyright notice, this
17        list of conditions and the following disclaimer.
18     2. Redistributions in binary form must reproduce the above copyright notice,
19        this list of conditions and the following disclaimer in the documentation
20        and/or other materials provided with the distribution.
21     3. Neither the name of the copyright holder nor the names of its contributors
22        may be used to endorse or promote products derived from this software without
23        specific prior written permission.
24 
25     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 OF SUCH DAMAGE.
35 */
36 
37 #ifndef GD32F3X0_GPIO_H
38 #define GD32F3X0_GPIO_H
39 
40 #include "gd32f3x0.h"
41 
42 /* GPIOx(x=A,B,C,D,F) definitions */
43 #define GPIOA                      (GPIO_BASE + 0x00000000U)
44 #define GPIOB                      (GPIO_BASE + 0x00000400U)
45 #define GPIOC                      (GPIO_BASE + 0x00000800U)
46 #define GPIOD                      (GPIO_BASE + 0x00000C00U)
47 #define GPIOF                      (GPIO_BASE + 0x00001400U)
48 
49 /* registers definitions */
50 #define GPIO_CTL(gpiox)            REG32((gpiox) + 0x00000000U)    /*!< GPIO port control register */
51 #define GPIO_OMODE(gpiox)          REG32((gpiox) + 0x00000004U)    /*!< GPIO port output mode register */
52 #define GPIO_OSPD0(gpiox)          REG32((gpiox) + 0x00000008U)    /*!< GPIO port output speed register 0 */
53 #define GPIO_PUD(gpiox)            REG32((gpiox) + 0x0000000CU)    /*!< GPIO port pull-up/pull-down register */
54 #define GPIO_ISTAT(gpiox)          REG32((gpiox) + 0x00000010U)    /*!< GPIO port input status register */
55 #define GPIO_OCTL(gpiox)           REG32((gpiox) + 0x00000014U)    /*!< GPIO port output control register */
56 #define GPIO_BOP(gpiox)            REG32((gpiox) + 0x00000018U)    /*!< GPIO port bit operation register */
57 #define GPIO_LOCK(gpiox)           REG32((gpiox) + 0x0000001CU)    /*!< GPIO port configuration lock register */
58 #define GPIO_AFSEL0(gpiox)         REG32((gpiox) + 0x00000020U)    /*!< GPIO alternate function selected register 0 */
59 #define GPIO_AFSEL1(gpiox)         REG32((gpiox) + 0x00000024U)    /*!< GPIO alternate function selected register 1 */
60 #define GPIO_BC(gpiox)             REG32((gpiox) + 0x00000028U)    /*!< GPIO bit clear register */
61 #define GPIO_TG(gpiox)             REG32((gpiox) + 0x0000002CU)    /*!< GPIO port bit toggle register */
62 #define GPIO_OSPD1(gpiox)          REG32((gpiox) + 0x0000003CU)    /*!< GPIO port output speed register 1 */
63 
64 /* bits definitions */
65 /* GPIO_CTL */
66 #define GPIO_CTL_CTL0              BITS(0,1)             /*!< pin 0 configuration bits */
67 #define GPIO_CTL_CTL1              BITS(2,3)             /*!< pin 1 configuration bits */
68 #define GPIO_CTL_CTL2              BITS(4,5)             /*!< pin 2 configuration bits */
69 #define GPIO_CTL_CTL3              BITS(6,7)             /*!< pin 3 configuration bits */
70 #define GPIO_CTL_CTL4              BITS(8,9)             /*!< pin 4 configuration bits */
71 #define GPIO_CTL_CTL5              BITS(10,11)           /*!< pin 5 configuration bits */
72 #define GPIO_CTL_CTL6              BITS(12,13)           /*!< pin 6 configuration bits */
73 #define GPIO_CTL_CTL7              BITS(14,15)           /*!< pin 7 configuration bits */
74 #define GPIO_CTL_CTL8              BITS(16,17)           /*!< pin 8 configuration bits */
75 #define GPIO_CTL_CTL9              BITS(18,19)           /*!< pin 9 configuration bits */
76 #define GPIO_CTL_CTL10             BITS(20,21)           /*!< pin 10 configuration bits */
77 #define GPIO_CTL_CTL11             BITS(22,23)           /*!< pin 11 configuration bits */
78 #define GPIO_CTL_CTL12             BITS(24,25)           /*!< pin 12 configuration bits */
79 #define GPIO_CTL_CTL13             BITS(26,27)           /*!< pin 13 configuration bits */
80 #define GPIO_CTL_CTL14             BITS(28,29)           /*!< pin 14 configuration bits */
81 #define GPIO_CTL_CTL15             BITS(30,31)           /*!< pin 15 configuration bits */
82 
83 /* GPIO_OMODE */
84 #define GPIO_OMODE_OM0             BIT(0)                /*!< pin 0 output mode bit */
85 #define GPIO_OMODE_OM1             BIT(1)                /*!< pin 1 output mode bit */
86 #define GPIO_OMODE_OM2             BIT(2)                /*!< pin 2 output mode bit */
87 #define GPIO_OMODE_OM3             BIT(3)                /*!< pin 3 output mode bit */
88 #define GPIO_OMODE_OM4             BIT(4)                /*!< pin 4 output mode bit */
89 #define GPIO_OMODE_OM5             BIT(5)                /*!< pin 5 output mode bit */
90 #define GPIO_OMODE_OM6             BIT(6)                /*!< pin 6 output mode bit */
91 #define GPIO_OMODE_OM7             BIT(7)                /*!< pin 7 output mode bit */
92 #define GPIO_OMODE_OM8             BIT(8)                /*!< pin 8 output mode bit */
93 #define GPIO_OMODE_OM9             BIT(9)                /*!< pin 9 output mode bit */
94 #define GPIO_OMODE_OM10            BIT(10)               /*!< pin 10 output mode bit */
95 #define GPIO_OMODE_OM11            BIT(11)               /*!< pin 11 output mode bit */
96 #define GPIO_OMODE_OM12            BIT(12)               /*!< pin 12 output mode bit */
97 #define GPIO_OMODE_OM13            BIT(13)               /*!< pin 13 output mode bit */
98 #define GPIO_OMODE_OM14            BIT(14)               /*!< pin 14 output mode bit */
99 #define GPIO_OMODE_OM15            BIT(15)               /*!< pin 15 output mode bit */
100 
101 /* GPIO_OSPD0 */
102 #define GPIO_OSPD0_OSPD0           BITS(0,1)             /*!< pin 0 output max speed bits */
103 #define GPIO_OSPD0_OSPD1           BITS(2,3)             /*!< pin 1 output max speed bits */
104 #define GPIO_OSPD0_OSPD2           BITS(4,5)             /*!< pin 2 output max speed bits */
105 #define GPIO_OSPD0_OSPD3           BITS(6,7)             /*!< pin 3 output max speed bits */
106 #define GPIO_OSPD0_OSPD4           BITS(8,9)             /*!< pin 4 output max speed bits */
107 #define GPIO_OSPD0_OSPD5           BITS(10,11)           /*!< pin 5 output max speed bits */
108 #define GPIO_OSPD0_OSPD6           BITS(12,13)           /*!< pin 6 output max speed bits */
109 #define GPIO_OSPD0_OSPD7           BITS(14,15)           /*!< pin 7 output max speed bits */
110 #define GPIO_OSPD0_OSPD8           BITS(16,17)           /*!< pin 8 output max speed bits */
111 #define GPIO_OSPD0_OSPD9           BITS(18,19)           /*!< pin 9 output max speed bits */
112 #define GPIO_OSPD0_OSPD10          BITS(20,21)           /*!< pin 10 output max speed bits */
113 #define GPIO_OSPD0_OSPD11          BITS(22,23)           /*!< pin 11 output max speed bits */
114 #define GPIO_OSPD0_OSPD12          BITS(24,25)           /*!< pin 12 output max speed bits */
115 #define GPIO_OSPD0_OSPD13          BITS(26,27)           /*!< pin 13 output max speed bits */
116 #define GPIO_OSPD0_OSPD14          BITS(28,29)           /*!< pin 14 output max speed bits */
117 #define GPIO_OSPD0_OSPD15          BITS(30,31)           /*!< pin 15 output max speed bits */
118 
119 /* GPIO_PUD */
120 #define GPIO_PUD_PUD0              BITS(0,1)             /*!< pin 0 pull-up or pull-down bits */
121 #define GPIO_PUD_PUD1              BITS(2,3)             /*!< pin 1 pull-up or pull-down bits */
122 #define GPIO_PUD_PUD2              BITS(4,5)             /*!< pin 2 pull-up or pull-down bits */
123 #define GPIO_PUD_PUD3              BITS(6,7)             /*!< pin 3 pull-up or pull-down bits */
124 #define GPIO_PUD_PUD4              BITS(8,9)             /*!< pin 4 pull-up or pull-down bits */
125 #define GPIO_PUD_PUD5              BITS(10,11)           /*!< pin 5 pull-up or pull-down bits */
126 #define GPIO_PUD_PUD6              BITS(12,13)           /*!< pin 6 pull-up or pull-down bits */
127 #define GPIO_PUD_PUD7              BITS(14,15)           /*!< pin 7 pull-up or pull-down bits */
128 #define GPIO_PUD_PUD8              BITS(16,17)           /*!< pin 8 pull-up or pull-down bits */
129 #define GPIO_PUD_PUD9              BITS(18,19)           /*!< pin 9 pull-up or pull-down bits */
130 #define GPIO_PUD_PUD10             BITS(20,21)           /*!< pin 10 pull-up or pull-down bits */
131 #define GPIO_PUD_PUD11             BITS(22,23)           /*!< pin 11 pull-up or pull-down bits */
132 #define GPIO_PUD_PUD12             BITS(24,25)           /*!< pin 12 pull-up or pull-down bits */
133 #define GPIO_PUD_PUD13             BITS(26,27)           /*!< pin 13 pull-up or pull-down bits */
134 #define GPIO_PUD_PUD14             BITS(28,29)           /*!< pin 14 pull-up or pull-down bits */
135 #define GPIO_PUD_PUD15             BITS(30,31)           /*!< pin 15 pull-up or pull-down bits */
136 
137 /* GPIO_ISTAT */
138 #define GPIO_ISTAT_ISTAT0          BIT(0)                /*!< pin 0 input status */
139 #define GPIO_ISTAT_ISTAT1          BIT(1)                /*!< pin 1 input status */
140 #define GPIO_ISTAT_ISTAT2          BIT(2)                /*!< pin 2 input status */
141 #define GPIO_ISTAT_ISTAT3          BIT(3)                /*!< pin 3 input status */
142 #define GPIO_ISTAT_ISTAT4          BIT(4)                /*!< pin 4 input status */
143 #define GPIO_ISTAT_ISTAT5          BIT(5)                /*!< pin 5 input status */
144 #define GPIO_ISTAT_ISTAT6          BIT(6)                /*!< pin 6 input status */
145 #define GPIO_ISTAT_ISTAT7          BIT(7)                /*!< pin 7 input status */
146 #define GPIO_ISTAT_ISTAT8          BIT(8)                /*!< pin 8 input status */
147 #define GPIO_ISTAT_ISTAT9          BIT(9)                /*!< pin 9 input status */
148 #define GPIO_ISTAT_ISTAT10         BIT(10)               /*!< pin 10 input status */
149 #define GPIO_ISTAT_ISTAT11         BIT(11)               /*!< pin 11 input status */
150 #define GPIO_ISTAT_ISTAT12         BIT(12)               /*!< pin 12 input status */
151 #define GPIO_ISTAT_ISTAT13         BIT(13)               /*!< pin 13 input status */
152 #define GPIO_ISTAT_ISTAT14         BIT(14)               /*!< pin 14 input status */
153 #define GPIO_ISTAT_ISTAT15         BIT(15)               /*!< pin 15 input status */
154 
155 /* GPIO_OCTL */
156 #define GPIO_OCTL_OCTL0            BIT(0)                /*!< pin 0 output bit */
157 #define GPIO_OCTL_OCTL1            BIT(1)                /*!< pin 1 output bit */
158 #define GPIO_OCTL_OCTL2            BIT(2)                /*!< pin 2 output bit */
159 #define GPIO_OCTL_OCTL3            BIT(3)                /*!< pin 3 output bit */
160 #define GPIO_OCTL_OCTL4            BIT(4)                /*!< pin 4 output bit */
161 #define GPIO_OCTL_OCTL5            BIT(5)                /*!< pin 5 output bit */
162 #define GPIO_OCTL_OCTL6            BIT(6)                /*!< pin 6 output bit */
163 #define GPIO_OCTL_OCTL7            BIT(7)                /*!< pin 7 output bit */
164 #define GPIO_OCTL_OCTL8            BIT(8)                /*!< pin 8 output bit */
165 #define GPIO_OCTL_OCTL9            BIT(9)                /*!< pin 9 output bit */
166 #define GPIO_OCTL_OCTL10           BIT(10)               /*!< pin 10 output bit */
167 #define GPIO_OCTL_OCTL11           BIT(11)               /*!< pin 11 output bit */
168 #define GPIO_OCTL_OCTL12           BIT(12)               /*!< pin 12 output bit */
169 #define GPIO_OCTL_OCTL13           BIT(13)               /*!< pin 13 output bit */
170 #define GPIO_OCTL_OCTL14           BIT(14)               /*!< pin 14 output bit */
171 #define GPIO_OCTL_OCTL15           BIT(15)               /*!< pin 15 output bit */
172 
173 /* GPIO_BOP */
174 #define GPIO_BOP_BOP0              BIT(0)                /*!< pin 0 set bit */
175 #define GPIO_BOP_BOP1              BIT(1)                /*!< pin 1 set bit */
176 #define GPIO_BOP_BOP2              BIT(2)                /*!< pin 2 set bit */
177 #define GPIO_BOP_BOP3              BIT(3)                /*!< pin 3 set bit */
178 #define GPIO_BOP_BOP4              BIT(4)                /*!< pin 4 set bit */
179 #define GPIO_BOP_BOP5              BIT(5)                /*!< pin 5 set bit */
180 #define GPIO_BOP_BOP6              BIT(6)                /*!< pin 6 set bit */
181 #define GPIO_BOP_BOP7              BIT(7)                /*!< pin 7 set bit */
182 #define GPIO_BOP_BOP8              BIT(8)                /*!< pin 8 set bit */
183 #define GPIO_BOP_BOP9              BIT(9)                /*!< pin 9 set bit */
184 #define GPIO_BOP_BOP10             BIT(10)               /*!< pin 10 set bit */
185 #define GPIO_BOP_BOP11             BIT(11)               /*!< pin 11 set bit */
186 #define GPIO_BOP_BOP12             BIT(12)               /*!< pin 12 set bit */
187 #define GPIO_BOP_BOP13             BIT(13)               /*!< pin 13 set bit */
188 #define GPIO_BOP_BOP14             BIT(14)               /*!< pin 14 set bit */
189 #define GPIO_BOP_BOP15             BIT(15)               /*!< pin 15 set bit */
190 #define GPIO_BOP_CR0               BIT(16)               /*!< pin 0 clear bit */
191 #define GPIO_BOP_CR1               BIT(17)               /*!< pin 1 clear bit */
192 #define GPIO_BOP_CR2               BIT(18)               /*!< pin 2 clear bit */
193 #define GPIO_BOP_CR3               BIT(19)               /*!< pin 3 clear bit */
194 #define GPIO_BOP_CR4               BIT(20)               /*!< pin 4 clear bit */
195 #define GPIO_BOP_CR5               BIT(21)               /*!< pin 5 clear bit */
196 #define GPIO_BOP_CR6               BIT(22)               /*!< pin 6 clear bit */
197 #define GPIO_BOP_CR7               BIT(23)               /*!< pin 7 clear bit */
198 #define GPIO_BOP_CR8               BIT(24)               /*!< pin 8 clear bit */
199 #define GPIO_BOP_CR9               BIT(25)               /*!< pin 9 clear bit */
200 #define GPIO_BOP_CR10              BIT(26)               /*!< pin 10 clear bit */
201 #define GPIO_BOP_CR11              BIT(27)               /*!< pin 11 clear bit */
202 #define GPIO_BOP_CR12              BIT(28)               /*!< pin 12 clear bit */
203 #define GPIO_BOP_CR13              BIT(29)               /*!< pin 13 clear bit */
204 #define GPIO_BOP_CR14              BIT(30)               /*!< pin 14 clear bit */
205 #define GPIO_BOP_CR15              BIT(31)               /*!< pin 15 clear bit */
206 
207 /* GPIO_LOCK */
208 #define GPIO_LOCK_LK0              BIT(0)                /*!< pin 0 lock bit */
209 #define GPIO_LOCK_LK1              BIT(1)                /*!< pin 1 lock bit */
210 #define GPIO_LOCK_LK2              BIT(2)                /*!< pin 2 lock bit */
211 #define GPIO_LOCK_LK3              BIT(3)                /*!< pin 3 lock bit */
212 #define GPIO_LOCK_LK4              BIT(4)                /*!< pin 4 lock bit */
213 #define GPIO_LOCK_LK5              BIT(5)                /*!< pin 5 lock bit */
214 #define GPIO_LOCK_LK6              BIT(6)                /*!< pin 6 lock bit */
215 #define GPIO_LOCK_LK7              BIT(7)                /*!< pin 7 lock bit */
216 #define GPIO_LOCK_LK8              BIT(8)                /*!< pin 8 lock bit */
217 #define GPIO_LOCK_LK9              BIT(9)                /*!< pin 9 lock bit */
218 #define GPIO_LOCK_LK10             BIT(10)               /*!< pin 10 lock bit */
219 #define GPIO_LOCK_LK11             BIT(11)               /*!< pin 11 lock bit */
220 #define GPIO_LOCK_LK12             BIT(12)               /*!< pin 12 lock bit */
221 #define GPIO_LOCK_LK13             BIT(13)               /*!< pin 13 lock bit */
222 #define GPIO_LOCK_LK14             BIT(14)               /*!< pin 14 lock bit */
223 #define GPIO_LOCK_LK15             BIT(15)               /*!< pin 15 lock bit */
224 #define GPIO_LOCK_LKK              BIT(16)               /*!< pin sequence lock key */
225 
226 /* GPIO_AFSEL0 */
227 #define GPIO_AFSEL0_SEL0           BITS(0,3)             /*!< pin 0 alternate function selected */
228 #define GPIO_AFSEL0_SEL1           BITS(4,7)             /*!< pin 1 alternate function selected */
229 #define GPIO_AFSEL0_SEL2           BITS(8,11)            /*!< pin 2 alternate function selected */
230 #define GPIO_AFSEL0_SEL3           BITS(12,15)           /*!< pin 3 alternate function selected */
231 #define GPIO_AFSEL0_SEL4           BITS(16,19)           /*!< pin 4 alternate function selected */
232 #define GPIO_AFSEL0_SEL5           BITS(20,23)           /*!< pin 5 alternate function selected */
233 #define GPIO_AFSEL0_SEL6           BITS(24,27)           /*!< pin 6 alternate function selected */
234 #define GPIO_AFSEL0_SEL7           BITS(28,31)           /*!< pin 7 alternate function selected */
235 
236 /* GPIO_AFSEL1 */
237 #define GPIO_AFSEL1_SEL8           BITS(0,3)             /*!< pin 8 alternate function selected */
238 #define GPIO_AFSEL1_SEL9           BITS(4,7)             /*!< pin 9 alternate function selected */
239 #define GPIO_AFSEL1_SEL10          BITS(8,11)            /*!< pin 10 alternate function selected */
240 #define GPIO_AFSEL1_SEL11          BITS(12,15)           /*!< pin 11 alternate function selected */
241 #define GPIO_AFSEL1_SEL12          BITS(16,19)           /*!< pin 12 alternate function selected */
242 #define GPIO_AFSEL1_SEL13          BITS(20,23)           /*!< pin 13 alternate function selected */
243 #define GPIO_AFSEL1_SEL14          BITS(24,27)           /*!< pin 14 alternate function selected */
244 #define GPIO_AFSEL1_SEL15          BITS(28,31)           /*!< pin 15 alternate function selected */
245 
246 /* GPIO_BC */
247 #define GPIO_BC_CR0                BIT(0)                /*!< pin 0 clear bit */
248 #define GPIO_BC_CR1                BIT(1)                /*!< pin 1 clear bit */
249 #define GPIO_BC_CR2                BIT(2)                /*!< pin 2 clear bit */
250 #define GPIO_BC_CR3                BIT(3)                /*!< pin 3 clear bit */
251 #define GPIO_BC_CR4                BIT(4)                /*!< pin 4 clear bit */
252 #define GPIO_BC_CR5                BIT(5)                /*!< pin 5 clear bit */
253 #define GPIO_BC_CR6                BIT(6)                /*!< pin 6 clear bit */
254 #define GPIO_BC_CR7                BIT(7)                /*!< pin 7 clear bit */
255 #define GPIO_BC_CR8                BIT(8)                /*!< pin 8 clear bit */
256 #define GPIO_BC_CR9                BIT(9)                /*!< pin 9 clear bit */
257 #define GPIO_BC_CR10               BIT(10)               /*!< pin 10 clear bit */
258 #define GPIO_BC_CR11               BIT(11)               /*!< pin 11 clear bit */
259 #define GPIO_BC_CR12               BIT(12)               /*!< pin 12 clear bit */
260 #define GPIO_BC_CR13               BIT(13)               /*!< pin 13 clear bit */
261 #define GPIO_BC_CR14               BIT(14)               /*!< pin 14 clear bit */
262 #define GPIO_BC_CR15               BIT(15)               /*!< pin 15 clear bit */
263 
264 /* GPIO_TG */
265 #define GPIO_TG_TG0                BIT(0)                /*!< pin 0 toggle bit */
266 #define GPIO_TG_TG1                BIT(1)                /*!< pin 1 toggle bit */
267 #define GPIO_TG_TG2                BIT(2)                /*!< pin 2 toggle bit */
268 #define GPIO_TG_TG3                BIT(3)                /*!< pin 3 toggle bit */
269 #define GPIO_TG_TG4                BIT(4)                /*!< pin 4 toggle bit */
270 #define GPIO_TG_TG5                BIT(5)                /*!< pin 5 toggle bit */
271 #define GPIO_TG_TG6                BIT(6)                /*!< pin 6 toggle bit */
272 #define GPIO_TG_TG7                BIT(7)                /*!< pin 7 toggle bit */
273 #define GPIO_TG_TG8                BIT(8)                /*!< pin 8 toggle bit */
274 #define GPIO_TG_TG9                BIT(9)                /*!< pin 9 toggle bit */
275 #define GPIO_TG_TG10               BIT(10)               /*!< pin 10 toggle bit */
276 #define GPIO_TG_TG11               BIT(11)               /*!< pin 11 toggle bit */
277 #define GPIO_TG_TG12               BIT(12)               /*!< pin 12 toggle bit */
278 #define GPIO_TG_TG13               BIT(13)               /*!< pin 13 toggle bit */
279 #define GPIO_TG_TG14               BIT(14)               /*!< pin 14 toggle bit */
280 #define GPIO_TG_TG15               BIT(15)               /*!< pin 15 toggle bit */
281 
282 /* GPIO_OSPD1 */
283 #define GPIO_OSPD1_SPD0            BIT(0)                /*!< set pin 0 very high output speed when OSPD0 is "11" */
284 #define GPIO_OSPD1_SPD1            BIT(1)                /*!< set pin 1 very high output speed when OSPD1 is "11" */
285 #define GPIO_OSPD1_SPD2            BIT(2)                /*!< set pin 2 very high output speed when OSPD2 is "11" */
286 #define GPIO_OSPD1_SPD3            BIT(3)                /*!< set pin 3 very high output speed when OSPD3 is "11" */
287 #define GPIO_OSPD1_SPD4            BIT(4)                /*!< set pin 4 very high output speed when OSPD4 is "11" */
288 #define GPIO_OSPD1_SPD5            BIT(5)                /*!< set pin 5 very high output speed when OSPD5 is "11" */
289 #define GPIO_OSPD1_SPD6            BIT(6)                /*!< set pin 6 very high output speed when OSPD6 is "11" */
290 #define GPIO_OSPD1_SPD7            BIT(7)                /*!< set pin 7 very high output speed when OSPD7 is "11" */
291 #define GPIO_OSPD1_SPD8            BIT(8)                /*!< set pin 8 very high output speed when OSPD8 is "11" */
292 #define GPIO_OSPD1_SPD9            BIT(9)                /*!< set pin 9 very high output speed when OSPD9 is "11" */
293 #define GPIO_OSPD1_SPD10           BIT(10)               /*!< set pin 10 very high output speed when OSPD10 is "11" */
294 #define GPIO_OSPD1_SPD11           BIT(11)               /*!< set pin 11 very high output speed when OSPD11 is "11" */
295 #define GPIO_OSPD1_SPD12           BIT(12)               /*!< set pin 12 very high output speed when OSPD12 is "11" */
296 #define GPIO_OSPD1_SPD13           BIT(13)               /*!< set pin 13 very high output speed when OSPD13 is "11" */
297 #define GPIO_OSPD1_SPD14           BIT(14)               /*!< set pin 14 very high output speed when OSPD14 is "11" */
298 #define GPIO_OSPD1_SPD15           BIT(15)               /*!< set pin 15 very high output speed when OSPD15 is "11" */
299 
300 /* constants definitions */
301 typedef FlagStatus bit_status;
302 
303 /* output mode definitions */
304 #define CTL_CLTR(regval)           (BITS(0,1) & ((uint32_t)(regval) << 0))
305 #define GPIO_MODE_INPUT            CTL_CLTR(0)           /*!< input mode */
306 #define GPIO_MODE_OUTPUT           CTL_CLTR(1)           /*!< output mode */
307 #define GPIO_MODE_AF               CTL_CLTR(2)           /*!< alternate function mode */
308 #define GPIO_MODE_ANALOG           CTL_CLTR(3)           /*!< analog mode */
309 
310 /* pull-up/pull-down definitions */
311 #define PUD_PUPD(regval)           (BITS(0,1) & ((uint32_t)(regval) << 0))
312 #define GPIO_PUPD_NONE             PUD_PUPD(0)           /*!< floating mode, no pull-up and pull-down resistors */
313 #define GPIO_PUPD_PULLUP           PUD_PUPD(1)           /*!< with pull-up resistor */
314 #define GPIO_PUPD_PULLDOWN         PUD_PUPD(2)           /*!< with pull-down resistor */
315 
316 /* GPIO pin definitions */
317 #define GPIO_PIN_0                 BIT(0)                /*!< GPIO pin 0 */
318 #define GPIO_PIN_1                 BIT(1)                /*!< GPIO pin 1 */
319 #define GPIO_PIN_2                 BIT(2)                /*!< GPIO pin 2 */
320 #define GPIO_PIN_3                 BIT(3)                /*!< GPIO pin 3 */
321 #define GPIO_PIN_4                 BIT(4)                /*!< GPIO pin 4 */
322 #define GPIO_PIN_5                 BIT(5)                /*!< GPIO pin 5 */
323 #define GPIO_PIN_6                 BIT(6)                /*!< GPIO pin 6 */
324 #define GPIO_PIN_7                 BIT(7)                /*!< GPIO pin 7 */
325 #define GPIO_PIN_8                 BIT(8)                /*!< GPIO pin 8 */
326 #define GPIO_PIN_9                 BIT(9)                /*!< GPIO pin 9 */
327 #define GPIO_PIN_10                BIT(10)               /*!< GPIO pin 10 */
328 #define GPIO_PIN_11                BIT(11)               /*!< GPIO pin 11 */
329 #define GPIO_PIN_12                BIT(12)               /*!< GPIO pin 12 */
330 #define GPIO_PIN_13                BIT(13)               /*!< GPIO pin 13 */
331 #define GPIO_PIN_14                BIT(14)               /*!< GPIO pin 14 */
332 #define GPIO_PIN_15                BIT(15)               /*!< GPIO pin 15 */
333 #define GPIO_PIN_ALL               BITS(0,15)            /*!< GPIO pin all */
334 
335 /* GPIO mode configuration values */
336 #define GPIO_MODE_SET(n, mode)     ((uint32_t)((uint32_t)(mode) << (2U * (n))))
337 #define GPIO_MODE_MASK(n)          ((uint32_t)((uint32_t)0x00000003U << (2U * (n))))
338 
339 /* GPIO pull-up/pull-down values */
340 #define GPIO_PUPD_SET(n, pupd)     ((uint32_t)((uint32_t)(pupd) << (2U * (n))))
341 #define GPIO_PUPD_MASK(n)          ((uint32_t)((uint32_t)0x00000003U << (2U * (n))))
342 
343 /* GPIO output speed values */
344 #define GPIO_OSPEED_SET(n, speed)  ((uint32_t)((uint32_t)(speed) << (2U * (n))))
345 #define GPIO_OSPEED_MASK(n)        ((uint32_t)((uint32_t)0x00000003U << (2U * (n))))
346 
347 /* GPIO output type */
348 #define GPIO_OTYPE_PP              ((uint8_t)(0x00U))    /*!< push pull mode */
349 #define GPIO_OTYPE_OD              ((uint8_t)(0x01U))    /*!< open drain mode */
350 
351 /* GPIO output max speed value */
352 #define OSPD_OSPD0(regval)         (BITS(0,1) & ((uint32_t)(regval) << 0))
353 #define GPIO_OSPEED_2MHZ           OSPD_OSPD0(0)                     /*!< output max speed 2MHz */
354 #define GPIO_OSPEED_10MHZ          OSPD_OSPD0(1)                     /*!< output max speed 10MHz */
355 #define GPIO_OSPEED_50MHZ          OSPD_OSPD0(3)                     /*!< output max speed 50MHz */
356 #define GPIO_OSPEED_MAX            ((uint32_t)0x0000FFFFU)           /*!< GPIO very high output speed, max speed more than 50MHz */
357 
358 /* GPIO alternate function values */
359 #define GPIO_AFR_SET(n, af)        ((uint32_t)((uint32_t)(af) << (4U * (n))))
360 #define GPIO_AFR_MASK(n)           ((uint32_t)((uint32_t)0x0000000FU << (4U * (n))))
361 
362 /* GPIO alternate function */
363 #define AF(regval)                 (BITS(0,3) & ((uint32_t)(regval) << 0))
364 #define GPIO_AF_0                   AF(0)                /*!< alternate function 0 selected */
365 #define GPIO_AF_1                   AF(1)                /*!< alternate function 1 selected */
366 #define GPIO_AF_2                   AF(2)                /*!< alternate function 2 selected */
367 #define GPIO_AF_3                   AF(3)                /*!< alternate function 3 selected */
368 #define GPIO_AF_4                   AF(4)                /*!< alternate function 4 selected (port A,B only) */
369 #define GPIO_AF_5                   AF(5)                /*!< alternate function 5 selected (port A,B only) */
370 #define GPIO_AF_6                   AF(6)                /*!< alternate function 6 selected (port A,B only) */
371 #define GPIO_AF_7                   AF(7)                /*!< alternate function 7 selected (port A,B only) */
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 /* GD32F3X0_GPIO_H */
410