1 /*!
2     \file    gd32f403_exti.h
3     \brief   definitions for the EXTI
4 
5     \version 2017-02-10, V1.0.0, firmware for GD32F403
6     \version 2018-12-25, V2.0.0, firmware for GD32F403
7     \version 2020-09-30, V2.1.0, firmware for GD32F403
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 GD32F403_EXTI_H
38 #define GD32F403_EXTI_H
39 
40 #include "gd32f403.h"
41 
42 /* EXTI definitions */
43 #define EXTI                         EXTI_BASE
44 
45 /* registers definitions */
46 #define EXTI_INTEN                   REG32(EXTI + 0x00U)      /*!< interrupt enable register */
47 #define EXTI_EVEN                    REG32(EXTI + 0x04U)      /*!< event enable register */
48 #define EXTI_RTEN                    REG32(EXTI + 0x08U)      /*!< rising edge trigger enable register */
49 #define EXTI_FTEN                    REG32(EXTI + 0x0CU)      /*!< falling trigger enable register */
50 #define EXTI_SWIEV                   REG32(EXTI + 0x10U)      /*!< software interrupt event register */
51 #define EXTI_PD                      REG32(EXTI + 0x14U)      /*!< pending register */
52 
53 /* bits definitions */
54 /* EXTI_INTEN */
55 #define EXTI_INTEN_INTEN0            BIT(0)                   /*!< interrupt from line 0 */
56 #define EXTI_INTEN_INTEN1            BIT(1)                   /*!< interrupt from line 1 */
57 #define EXTI_INTEN_INTEN2            BIT(2)                   /*!< interrupt from line 2 */
58 #define EXTI_INTEN_INTEN3            BIT(3)                   /*!< interrupt from line 3 */
59 #define EXTI_INTEN_INTEN4            BIT(4)                   /*!< interrupt from line 4 */
60 #define EXTI_INTEN_INTEN5            BIT(5)                   /*!< interrupt from line 5 */
61 #define EXTI_INTEN_INTEN6            BIT(6)                   /*!< interrupt from line 6 */
62 #define EXTI_INTEN_INTEN7            BIT(7)                   /*!< interrupt from line 7 */
63 #define EXTI_INTEN_INTEN8            BIT(8)                   /*!< interrupt from line 8 */
64 #define EXTI_INTEN_INTEN9            BIT(9)                   /*!< interrupt from line 9 */
65 #define EXTI_INTEN_INTEN10           BIT(10)                  /*!< interrupt from line 10 */
66 #define EXTI_INTEN_INTEN11           BIT(11)                  /*!< interrupt from line 11 */
67 #define EXTI_INTEN_INTEN12           BIT(12)                  /*!< interrupt from line 12 */
68 #define EXTI_INTEN_INTEN13           BIT(13)                  /*!< interrupt from line 13 */
69 #define EXTI_INTEN_INTEN14           BIT(14)                  /*!< interrupt from line 14 */
70 #define EXTI_INTEN_INTEN15           BIT(15)                  /*!< interrupt from line 15 */
71 #define EXTI_INTEN_INTEN16           BIT(16)                  /*!< interrupt from line 16 */
72 #define EXTI_INTEN_INTEN17           BIT(17)                  /*!< interrupt from line 17 */
73 #define EXTI_INTEN_INTEN18           BIT(18)                  /*!< interrupt from line 18 */
74 
75 /* EXTI_EVEN */
76 #define EXTI_EVEN_EVEN0              BIT(0)                   /*!< event from line 0 */
77 #define EXTI_EVEN_EVEN1              BIT(1)                   /*!< event from line 1 */
78 #define EXTI_EVEN_EVEN2              BIT(2)                   /*!< event from line 2 */
79 #define EXTI_EVEN_EVEN3              BIT(3)                   /*!< event from line 3 */
80 #define EXTI_EVEN_EVEN4              BIT(4)                   /*!< event from line 4 */
81 #define EXTI_EVEN_EVEN5              BIT(5)                   /*!< event from line 5 */
82 #define EXTI_EVEN_EVEN6              BIT(6)                   /*!< event from line 6 */
83 #define EXTI_EVEN_EVEN7              BIT(7)                   /*!< event from line 7 */
84 #define EXTI_EVEN_EVEN8              BIT(8)                   /*!< event from line 8 */
85 #define EXTI_EVEN_EVEN9              BIT(9)                   /*!< event from line 9 */
86 #define EXTI_EVEN_EVEN10             BIT(10)                  /*!< event from line 10 */
87 #define EXTI_EVEN_EVEN11             BIT(11)                  /*!< event from line 11 */
88 #define EXTI_EVEN_EVEN12             BIT(12)                  /*!< event from line 12 */
89 #define EXTI_EVEN_EVEN13             BIT(13)                  /*!< event from line 13 */
90 #define EXTI_EVEN_EVEN14             BIT(14)                  /*!< event from line 14 */
91 #define EXTI_EVEN_EVEN15             BIT(15)                  /*!< event from line 15 */
92 #define EXTI_EVEN_EVEN16             BIT(16)                  /*!< event from line 16 */
93 #define EXTI_EVEN_EVEN17             BIT(17)                  /*!< event from line 17 */
94 #define EXTI_EVEN_EVEN18             BIT(18)                  /*!< event from line 18 */
95 
96 /* EXTI_RTEN */
97 #define EXTI_RTEN_RTEN0              BIT(0)                   /*!< rising edge from line 0 */
98 #define EXTI_RTEN_RTEN1              BIT(1)                   /*!< rising edge from line 1 */
99 #define EXTI_RTEN_RTEN2              BIT(2)                   /*!< rising edge from line 2 */
100 #define EXTI_RTEN_RTEN3              BIT(3)                   /*!< rising edge from line 3 */
101 #define EXTI_RTEN_RTEN4              BIT(4)                   /*!< rising edge from line 4 */
102 #define EXTI_RTEN_RTEN5              BIT(5)                   /*!< rising edge from line 5 */
103 #define EXTI_RTEN_RTEN6              BIT(6)                   /*!< rising edge from line 6 */
104 #define EXTI_RTEN_RTEN7              BIT(7)                   /*!< rising edge from line 7 */
105 #define EXTI_RTEN_RTEN8              BIT(8)                   /*!< rising edge from line 8 */
106 #define EXTI_RTEN_RTEN9              BIT(9)                   /*!< rising edge from line 9 */
107 #define EXTI_RTEN_RTEN10             BIT(10)                  /*!< rising edge from line 10 */
108 #define EXTI_RTEN_RTEN11             BIT(11)                  /*!< rising edge from line 11 */
109 #define EXTI_RTEN_RTEN12             BIT(12)                  /*!< rising edge from line 12 */
110 #define EXTI_RTEN_RTEN13             BIT(13)                  /*!< rising edge from line 13 */
111 #define EXTI_RTEN_RTEN14             BIT(14)                  /*!< rising edge from line 14 */
112 #define EXTI_RTEN_RTEN15             BIT(15)                  /*!< rising edge from line 15 */
113 #define EXTI_RTEN_RTEN16             BIT(16)                  /*!< rising edge from line 16 */
114 #define EXTI_RTEN_RTEN17             BIT(17)                  /*!< rising edge from line 17 */
115 #define EXTI_RTEN_RTEN18             BIT(18)                  /*!< rising edge from line 18 */
116 
117 /* EXTI_FTEN */
118 #define EXTI_FTEN_FTEN0              BIT(0)                   /*!< falling edge from line 0 */
119 #define EXTI_FTEN_FTEN1              BIT(1)                   /*!< falling edge from line 1 */
120 #define EXTI_FTEN_FTEN2              BIT(2)                   /*!< falling edge from line 2 */
121 #define EXTI_FTEN_FTEN3              BIT(3)                   /*!< falling edge from line 3 */
122 #define EXTI_FTEN_FTEN4              BIT(4)                   /*!< falling edge from line 4 */
123 #define EXTI_FTEN_FTEN5              BIT(5)                   /*!< falling edge from line 5 */
124 #define EXTI_FTEN_FTEN6              BIT(6)                   /*!< falling edge from line 6 */
125 #define EXTI_FTEN_FTEN7              BIT(7)                   /*!< falling edge from line 7 */
126 #define EXTI_FTEN_FTEN8              BIT(8)                   /*!< falling edge from line 8 */
127 #define EXTI_FTEN_FTEN9              BIT(9)                   /*!< falling edge from line 9 */
128 #define EXTI_FTEN_FTEN10             BIT(10)                  /*!< falling edge from line 10 */
129 #define EXTI_FTEN_FTEN11             BIT(11)                  /*!< falling edge from line 11 */
130 #define EXTI_FTEN_FTEN12             BIT(12)                  /*!< falling edge from line 12 */
131 #define EXTI_FTEN_FTEN13             BIT(13)                  /*!< falling edge from line 13 */
132 #define EXTI_FTEN_FTEN14             BIT(14)                  /*!< falling edge from line 14 */
133 #define EXTI_FTEN_FTEN15             BIT(15)                  /*!< falling edge from line 15 */
134 #define EXTI_FTEN_FTEN16             BIT(16)                  /*!< falling edge from line 16 */
135 #define EXTI_FTEN_FTEN17             BIT(17)                  /*!< falling edge from line 17 */
136 #define EXTI_FTEN_FTEN18             BIT(18)                  /*!< falling edge from line 18 */
137 
138 /* EXTI_SWIEV */
139 #define EXTI_SWIEV_SWIEV0            BIT(0)                   /*!< software interrupt/event request from line 0 */
140 #define EXTI_SWIEV_SWIEV1            BIT(1)                   /*!< software interrupt/event request from line 1 */
141 #define EXTI_SWIEV_SWIEV2            BIT(2)                   /*!< software interrupt/event request from line 2 */
142 #define EXTI_SWIEV_SWIEV3            BIT(3)                   /*!< software interrupt/event request from line 3 */
143 #define EXTI_SWIEV_SWIEV4            BIT(4)                   /*!< software interrupt/event request from line 4 */
144 #define EXTI_SWIEV_SWIEV5            BIT(5)                   /*!< software interrupt/event request from line 5 */
145 #define EXTI_SWIEV_SWIEV6            BIT(6)                   /*!< software interrupt/event request from line 6 */
146 #define EXTI_SWIEV_SWIEV7            BIT(7)                   /*!< software interrupt/event request from line 7 */
147 #define EXTI_SWIEV_SWIEV8            BIT(8)                   /*!< software interrupt/event request from line 8 */
148 #define EXTI_SWIEV_SWIEV9            BIT(9)                   /*!< software interrupt/event request from line 9 */
149 #define EXTI_SWIEV_SWIEV10           BIT(10)                  /*!< software interrupt/event request from line 10 */
150 #define EXTI_SWIEV_SWIEV11           BIT(11)                  /*!< software interrupt/event request from line 11 */
151 #define EXTI_SWIEV_SWIEV12           BIT(12)                  /*!< software interrupt/event request from line 12 */
152 #define EXTI_SWIEV_SWIEV13           BIT(13)                  /*!< software interrupt/event request from line 13 */
153 #define EXTI_SWIEV_SWIEV14           BIT(14)                  /*!< software interrupt/event request from line 14 */
154 #define EXTI_SWIEV_SWIEV15           BIT(15)                  /*!< software interrupt/event request from line 15 */
155 #define EXTI_SWIEV_SWIEV16           BIT(16)                  /*!< software interrupt/event request from line 16 */
156 #define EXTI_SWIEV_SWIEV17           BIT(17)                  /*!< software interrupt/event request from line 17 */
157 #define EXTI_SWIEV_SWIEV18           BIT(18)                  /*!< software interrupt/event request from line 18 */
158 
159 /* EXTI_PD */
160 #define EXTI_PD_PD0                  BIT(0)                   /*!< interrupt/event pending status from line 0 */
161 #define EXTI_PD_PD1                  BIT(1)                   /*!< interrupt/event pending status from line 1 */
162 #define EXTI_PD_PD2                  BIT(2)                   /*!< interrupt/event pending status from line 2 */
163 #define EXTI_PD_PD3                  BIT(3)                   /*!< interrupt/event pending status from line 3 */
164 #define EXTI_PD_PD4                  BIT(4)                   /*!< interrupt/event pending status from line 4 */
165 #define EXTI_PD_PD5                  BIT(5)                   /*!< interrupt/event pending status from line 5 */
166 #define EXTI_PD_PD6                  BIT(6)                   /*!< interrupt/event pending status from line 6 */
167 #define EXTI_PD_PD7                  BIT(7)                   /*!< interrupt/event pending status from line 7 */
168 #define EXTI_PD_PD8                  BIT(8)                   /*!< interrupt/event pending status from line 8 */
169 #define EXTI_PD_PD9                  BIT(9)                   /*!< interrupt/event pending status from line 9 */
170 #define EXTI_PD_PD10                 BIT(10)                  /*!< interrupt/event pending status from line 10 */
171 #define EXTI_PD_PD11                 BIT(11)                  /*!< interrupt/event pending status from line 11 */
172 #define EXTI_PD_PD12                 BIT(12)                  /*!< interrupt/event pending status from line 12 */
173 #define EXTI_PD_PD13                 BIT(13)                  /*!< interrupt/event pending status from line 13 */
174 #define EXTI_PD_PD14                 BIT(14)                  /*!< interrupt/event pending status from line 14 */
175 #define EXTI_PD_PD15                 BIT(15)                  /*!< interrupt/event pending status from line 15 */
176 #define EXTI_PD_PD16                 BIT(16)                  /*!< interrupt/event pending status from line 16 */
177 #define EXTI_PD_PD17                 BIT(17)                  /*!< interrupt/event pending status from line 17 */
178 #define EXTI_PD_PD18                 BIT(18)                  /*!< interrupt/event pending status from line 18 */
179 
180 /* constants definitions */
181 /* EXTI line number */
182 typedef enum
183 {
184     EXTI_0      = BIT(0),                                     /*!< EXTI line 0 */
185     EXTI_1      = BIT(1),                                     /*!< EXTI line 1 */
186     EXTI_2      = BIT(2),                                     /*!< EXTI line 2 */
187     EXTI_3      = BIT(3),                                     /*!< EXTI line 3 */
188     EXTI_4      = BIT(4),                                     /*!< EXTI line 4 */
189     EXTI_5      = BIT(5),                                     /*!< EXTI line 5 */
190     EXTI_6      = BIT(6),                                     /*!< EXTI line 6 */
191     EXTI_7      = BIT(7),                                     /*!< EXTI line 7 */
192     EXTI_8      = BIT(8),                                     /*!< EXTI line 8 */
193     EXTI_9      = BIT(9),                                     /*!< EXTI line 9 */
194     EXTI_10     = BIT(10),                                    /*!< EXTI line 10 */
195     EXTI_11     = BIT(11),                                    /*!< EXTI line 11 */
196     EXTI_12     = BIT(12),                                    /*!< EXTI line 12 */
197     EXTI_13     = BIT(13),                                    /*!< EXTI line 13 */
198     EXTI_14     = BIT(14),                                    /*!< EXTI line 14 */
199     EXTI_15     = BIT(15),                                    /*!< EXTI line 15 */
200     EXTI_16     = BIT(16),                                    /*!< EXTI line 16 */
201     EXTI_17     = BIT(17),                                    /*!< EXTI line 17 */
202     EXTI_18     = BIT(18),                                    /*!< EXTI line 18 */
203 }exti_line_enum;
204 
205 /* external interrupt and event  */
206 typedef enum
207 {
208     EXTI_INTERRUPT   = 0,                                     /*!< EXTI interrupt mode */
209     EXTI_EVENT                                                /*!< EXTI event mode */
210 }exti_mode_enum;
211 
212 /* interrupt trigger mode */
213 typedef enum
214 {
215     EXTI_TRIG_RISING = 0,                                     /*!< EXTI rising edge trigger */
216     EXTI_TRIG_FALLING,                                        /*!< EXTI falling edge trigger */
217     EXTI_TRIG_BOTH                                            /*!< EXTI rising and falling edge trigger */
218 }exti_trig_type_enum;
219 
220 /* function declarations */
221 /* deinitialize the EXTI */
222 void exti_deinit(void);
223 /* enable the configuration of EXTI initialize */
224 void exti_init(exti_line_enum linex, exti_mode_enum mode, exti_trig_type_enum trig_type);
225 /* enable the interrupts from EXTI line x */
226 void exti_interrupt_enable(exti_line_enum linex);
227 /* enable the events from EXTI line x */
228 void exti_event_enable(exti_line_enum linex);
229 /* disable the interrupts from EXTI line x */
230 void exti_interrupt_disable(exti_line_enum linex);
231 /* disable the events from EXTI line x */
232 void exti_event_disable(exti_line_enum linex);
233 
234 /* get EXTI lines pending flag */
235 FlagStatus exti_flag_get(exti_line_enum linex);
236 /* clear EXTI lines pending flag */
237 void exti_flag_clear(exti_line_enum linex);
238 /* get EXTI lines flag when the interrupt flag is set */
239 FlagStatus exti_interrupt_flag_get(exti_line_enum linex);
240 /* clear EXTI lines pending flag */
241 void exti_interrupt_flag_clear(exti_line_enum linex);
242 /* enable the EXTI software interrupt event  */
243 void exti_software_interrupt_enable(exti_line_enum linex);
244 /* disable the EXTI software interrupt event  */
245 void exti_software_interrupt_disable(exti_line_enum linex);
246 
247 #endif /* GD32F403_EXTI_H */
248