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