1 /*!
2 \file gd32vf103_exti.c
3 \brief EXTI driver
4
5 \version 2019-06-05, V1.0.0, firmware for GD32VF103
6 \version 2020-08-04, V1.1.0, firmware for GD32VF103
7 */
8
9 /*
10 Copyright (c) 2020, GigaDevice Semiconductor Inc.
11
12 Redistribution and use in source and binary forms, with or without modification,
13 are permitted provided that the following conditions are met:
14
15 1. Redistributions of source code must retain the above copyright notice, this
16 list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright notice,
18 this list of conditions and the following disclaimer in the documentation
19 and/or other materials provided with the distribution.
20 3. Neither the name of the copyright holder nor the names of its contributors
21 may be used to endorse or promote products derived from this software without
22 specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGE.
34 */
35
36 #include "gd32vf103_exti.h"
37
38 #define EXTI_REG_RESET_VALUE ((uint32_t)0x00000000U)
39
40 /*!
41 \brief deinitialize the EXTI
42 \param[in] none
43 \param[out] none
44 \retval none
45 */
exti_deinit(void)46 void exti_deinit(void)
47 {
48 /* reset the value of all the EXTI registers */
49 EXTI_INTEN = EXTI_REG_RESET_VALUE;
50 EXTI_EVEN = EXTI_REG_RESET_VALUE;
51 EXTI_RTEN = EXTI_REG_RESET_VALUE;
52 EXTI_FTEN = EXTI_REG_RESET_VALUE;
53 EXTI_SWIEV = EXTI_REG_RESET_VALUE;
54 }
55
56 /*!
57 \brief initialize the EXTI
58 \param[in] linex: EXTI line number, refer to exti_line_enum
59 only one parameter can be selected which is shown as below:
60 \arg EXTI_x (x=0..18): EXTI line x
61 \param[in] mode: interrupt or event mode, refer to exti_mode_enum
62 only one parameter can be selected which is shown as below:
63 \arg EXTI_INTERRUPT: interrupt mode
64 \arg EXTI_EVENT: event mode
65 \param[in] trig_type: trigger type, refer to exti_trig_type_enum
66 only one parameter can be selected which is shown as below:
67 \arg EXTI_TRIG_RISING: rising edge trigger
68 \arg EXTI_TRIG_FALLING: falling edge trigger
69 \arg EXTI_TRIG_BOTH: rising edge and falling edge trigger
70 \arg EXTI_TRIG_NONE: without rising edge or falling edge trigger
71 \param[out] none
72 \retval none
73 */
exti_init(exti_line_enum linex,exti_mode_enum mode,exti_trig_type_enum trig_type)74 void exti_init(exti_line_enum linex, exti_mode_enum mode, exti_trig_type_enum trig_type)
75 {
76 /* reset the EXTI line x */
77 EXTI_INTEN &= ~(uint32_t) linex;
78 EXTI_EVEN &= ~(uint32_t) linex;
79 EXTI_RTEN &= ~(uint32_t) linex;
80 EXTI_FTEN &= ~(uint32_t) linex;
81
82 /* set the EXTI mode and enable the interrupts or events from EXTI line x */
83 switch (mode) {
84 case EXTI_INTERRUPT:
85 EXTI_INTEN |= (uint32_t) linex;
86 break;
87 case EXTI_EVENT:
88 EXTI_EVEN |= (uint32_t) linex;
89 break;
90 default:
91 break;
92 }
93
94 /* set the EXTI trigger type */
95 switch (trig_type) {
96 case EXTI_TRIG_RISING:
97 EXTI_RTEN |= (uint32_t) linex;
98 EXTI_FTEN &= ~(uint32_t) linex;
99 break;
100 case EXTI_TRIG_FALLING:
101 EXTI_RTEN &= ~(uint32_t) linex;
102 EXTI_FTEN |= (uint32_t) linex;
103 break;
104 case EXTI_TRIG_BOTH:
105 EXTI_RTEN |= (uint32_t) linex;
106 EXTI_FTEN |= (uint32_t) linex;
107 break;
108 case EXTI_TRIG_NONE:
109 default:
110 break;
111 }
112 }
113
114 /*!
115 \brief enable the interrupts from EXTI line x
116 \param[in] linex: EXTI line number, refer to exti_line_enum
117 only one parameter can be selected which is shown as below:
118 \arg EXTI_x (x=0..18): EXTI line x
119 \param[out] none
120 \retval none
121 */
exti_interrupt_enable(exti_line_enum linex)122 void exti_interrupt_enable(exti_line_enum linex)
123 {
124 EXTI_INTEN |= (uint32_t) linex;
125 }
126
127 /*!
128 \brief enable the events from EXTI line x
129 \param[in] linex: EXTI line number, refer to exti_line_enum
130 only one parameter can be selected which is shown as below:
131 \arg EXTI_x (x=0..18): EXTI line x
132 \param[out] none
133 \retval none
134 */
exti_event_enable(exti_line_enum linex)135 void exti_event_enable(exti_line_enum linex)
136 {
137 EXTI_EVEN |= (uint32_t) linex;
138 }
139
140 /*!
141 \brief disable the interrupt from EXTI line x
142 \param[in] linex: EXTI line number, refer to exti_line_enum
143 only one parameter can be selected which is shown as below:
144 \arg EXTI_x (x=0..18): EXTI line x
145 \param[out] none
146 \retval none
147 */
exti_interrupt_disable(exti_line_enum linex)148 void exti_interrupt_disable(exti_line_enum linex)
149 {
150 EXTI_INTEN &= ~(uint32_t) linex;
151 }
152
153 /*!
154 \brief disable the events from EXTI line x
155 \param[in] linex: EXTI line number, refer to exti_line_enum
156 only one parameter can be selected which is shown as below:
157 \arg EXTI_x (x=0..18): EXTI line x
158 \param[out] none
159 \retval none
160 */
exti_event_disable(exti_line_enum linex)161 void exti_event_disable(exti_line_enum linex)
162 {
163 EXTI_EVEN &= ~(uint32_t) linex;
164 }
165
166 /*!
167 \brief get EXTI lines flag
168 \param[in] linex: EXTI line number, refer to exti_line_enum
169 only one parameter can be selected which is shown as below:
170 \arg EXTI_x (x=0..18): EXTI line x
171 \param[out] none
172 \retval FlagStatus: status of flag (RESET or SET)
173 */
exti_flag_get(exti_line_enum linex)174 FlagStatus exti_flag_get(exti_line_enum linex)
175 {
176 if (RESET != (EXTI_PD & (uint32_t) linex)) {
177 return SET;
178 } else {
179 return RESET;
180 }
181 }
182
183 /*!
184 \brief clear EXTI lines pending flag
185 \param[in] linex: EXTI line number, refer to exti_line_enum
186 only one parameter can be selected which is shown as below:
187 \arg EXTI_x (x=0..18): EXTI line x
188 \param[out] none
189 \retval none
190 */
exti_flag_clear(exti_line_enum linex)191 void exti_flag_clear(exti_line_enum linex)
192 {
193 EXTI_PD = (uint32_t) linex;
194 }
195
196 /*!
197 \brief get EXTI lines flag when the interrupt flag is set
198 \param[in] linex: EXTI line number, refer to exti_line_enum
199 only one parameter can be selected which is shown as below:
200 \arg EXTI_x (x=0..18): EXTI line x
201 \param[out] none
202 \retval FlagStatus: status of flag (RESET or SET)
203 */
exti_interrupt_flag_get(exti_line_enum linex)204 FlagStatus exti_interrupt_flag_get(exti_line_enum linex)
205 {
206 uint32_t flag_left, flag_right;
207
208 flag_left = EXTI_PD & (uint32_t) linex;
209 flag_right = EXTI_INTEN & (uint32_t) linex;
210
211 if ((RESET != flag_left) && (RESET != flag_right)) {
212 return SET;
213 } else {
214 return RESET;
215 }
216 }
217
218 /*!
219 \brief clear EXTI lines pending flag
220 \param[in] linex: EXTI line number, refer to exti_line_enum
221 only one parameter can be selected which is shown as below:
222 \arg EXTI_x (x=0..18): EXTI line x
223 \param[out] none
224 \retval none
225 */
exti_interrupt_flag_clear(exti_line_enum linex)226 void exti_interrupt_flag_clear(exti_line_enum linex)
227 {
228 EXTI_PD = (uint32_t) linex;
229 }
230
231 /*!
232 \brief enable EXTI software interrupt event
233 \param[in] linex: EXTI line number, refer to exti_line_enum
234 only one parameter can be selected which is shown as below:
235 \arg EXTI_x (x=0..18): EXTI line x
236 \param[out] none
237 \retval none
238 */
exti_software_interrupt_enable(exti_line_enum linex)239 void exti_software_interrupt_enable(exti_line_enum linex)
240 {
241 EXTI_SWIEV |= (uint32_t) linex;
242 }
243
244 /*!
245 \brief disable EXTI software interrupt event
246 \param[in] linex: EXTI line number, refer to exti_line_enum
247 only one parameter can be selected which is shown as below:
248 \arg EXTI_x (x=0..18): EXTI line x
249 \param[out] none
250 \retval none
251 */
exti_software_interrupt_disable(exti_line_enum linex)252 void exti_software_interrupt_disable(exti_line_enum linex)
253 {
254 EXTI_SWIEV &= ~(uint32_t) linex;
255 }
256