1 /*!
2 \file gd32f403_exti.c
3 \brief EXTI driver
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 #include "gd32f403_exti.h"
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 = (uint32_t)0x00000000U;
49 EXTI_EVEN = (uint32_t)0x00000000U;
50 EXTI_RTEN = (uint32_t)0x00000000U;
51 EXTI_FTEN = (uint32_t)0x00000000U;
52 EXTI_SWIEV = (uint32_t)0x00000000U;
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: interrupt 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 trigger
68 \arg EXTI_TRIG_BOTH: rising and falling trigger
69 \param[out] none
70 \retval none
71 */
exti_init(exti_line_enum linex,exti_mode_enum mode,exti_trig_type_enum trig_type)72 void exti_init(exti_line_enum linex, exti_mode_enum mode, exti_trig_type_enum trig_type)
73 {
74 /* reset the EXTI line x */
75 EXTI_INTEN &= ~(uint32_t)linex;
76 EXTI_EVEN &= ~(uint32_t)linex;
77 EXTI_RTEN &= ~(uint32_t)linex;
78 EXTI_FTEN &= ~(uint32_t)linex;
79
80 /* set the EXTI mode and enable the interrupts or events from EXTI line x */
81 switch(mode){
82 case EXTI_INTERRUPT:
83 EXTI_INTEN |= (uint32_t)linex;
84 break;
85 case EXTI_EVENT:
86 EXTI_EVEN |= (uint32_t)linex;
87 break;
88 default:
89 break;
90 }
91
92 /* set the EXTI trigger type */
93 switch(trig_type){
94 case EXTI_TRIG_RISING:
95 EXTI_RTEN |= (uint32_t)linex;
96 EXTI_FTEN &= ~(uint32_t)linex;
97 break;
98 case EXTI_TRIG_FALLING:
99 EXTI_RTEN &= ~(uint32_t)linex;
100 EXTI_FTEN |= (uint32_t)linex;
101 break;
102 case EXTI_TRIG_BOTH:
103 EXTI_RTEN |= (uint32_t)linex;
104 EXTI_FTEN |= (uint32_t)linex;
105 break;
106 default:
107 break;
108 }
109 }
110
111 /*!
112 \brief enable the interrupts from EXTI line x
113 \param[in] linex: EXTI line number, refer to exti_line_enum
114 only one parameter can be selected which is shown as below:
115 \arg EXTI_x (x=0..18): EXTI line x
116 \param[out] none
117 \retval none
118 */
exti_interrupt_enable(exti_line_enum linex)119 void exti_interrupt_enable(exti_line_enum linex)
120 {
121 EXTI_INTEN |= (uint32_t)linex;
122 }
123
124 /*!
125 \brief enable the events from EXTI line x
126 \param[in] linex: EXTI line number, refer to exti_line_enum
127 only one parameter can be selected which is shown as below:
128 \arg EXTI_x (x=0..18): EXTI line x
129 \param[out] none
130 \retval none
131 */
exti_event_enable(exti_line_enum linex)132 void exti_event_enable(exti_line_enum linex)
133 {
134 EXTI_EVEN |= (uint32_t)linex;
135 }
136
137 /*!
138 \brief disable the interrupt from EXTI line x
139 \param[in] linex: EXTI line number, refer to exti_line_enum
140 only one parameter can be selected which is shown as below:
141 \arg EXTI_x (x=0..18): EXTI line x
142 \param[out] none
143 \retval none
144 */
exti_interrupt_disable(exti_line_enum linex)145 void exti_interrupt_disable(exti_line_enum linex)
146 {
147 EXTI_INTEN &= ~(uint32_t)linex;
148 }
149
150 /*!
151 \brief disable the events from EXTI line x
152 \param[in] linex: EXTI line number, refer to exti_line_enum
153 only one parameter can be selected which is shown as below:
154 \arg EXTI_x (x=0..18): EXTI line x
155 \param[out] none
156 \retval none
157 */
exti_event_disable(exti_line_enum linex)158 void exti_event_disable(exti_line_enum linex)
159 {
160 EXTI_EVEN &= ~(uint32_t)linex;
161 }
162
163 /*!
164 \brief get EXTI lines flag
165 \param[in] linex: EXTI line number, refer to exti_line_enum
166 only one parameter can be selected which is shown as below:
167 \arg EXTI_x (x=0..18): EXTI line x
168 \param[out] none
169 \retval FlagStatus: status of flag (RESET or SET)
170 */
exti_flag_get(exti_line_enum linex)171 FlagStatus exti_flag_get(exti_line_enum linex)
172 {
173 if(RESET != (EXTI_PD & (uint32_t)linex)){
174 return SET;
175 }else{
176 return RESET;
177 }
178 }
179
180 /*!
181 \brief clear EXTI lines pending flag
182 \param[in] linex: EXTI line number, refer to exti_line_enum
183 only one parameter can be selected which is shown as below:
184 \arg EXTI_x (x=0..18): EXTI line x
185 \param[out] none
186 \retval none
187 */
exti_flag_clear(exti_line_enum linex)188 void exti_flag_clear(exti_line_enum linex)
189 {
190 EXTI_PD = (uint32_t)linex;
191 }
192
193 /*!
194 \brief get EXTI lines flag when the interrupt flag is set
195 \param[in] linex: EXTI line number, refer to exti_line_enum
196 only one parameter can be selected which is shown as below:
197 \arg EXTI_x (x=0..18): EXTI line x
198 \param[out] none
199 \retval FlagStatus: status of flag (RESET or SET)
200 */
exti_interrupt_flag_get(exti_line_enum linex)201 FlagStatus exti_interrupt_flag_get(exti_line_enum linex)
202 {
203 uint32_t flag_left, flag_right;
204
205 flag_left = EXTI_PD & (uint32_t)linex;
206 flag_right = EXTI_INTEN & (uint32_t)linex;
207
208 if((RESET != flag_left) && (RESET != flag_right)){
209 return SET;
210 }else{
211 return RESET;
212 }
213 }
214
215 /*!
216 \brief clear EXTI lines pending flag
217 \param[in] linex: EXTI line number, refer to exti_line_enum
218 only one parameter can be selected which is shown as below:
219 \arg EXTI_x (x=0..18): EXTI line x
220 \param[out] none
221 \retval none
222 */
exti_interrupt_flag_clear(exti_line_enum linex)223 void exti_interrupt_flag_clear(exti_line_enum linex)
224 {
225 EXTI_PD = (uint32_t)linex;
226 }
227
228 /*!
229 \brief enable EXTI software interrupt event
230 \param[in] linex: EXTI line number, refer to exti_line_enum
231 only one parameter can be selected which is shown as below:
232 \arg EXTI_x (x=0..18): EXTI line x
233 \param[out] none
234 \retval none
235 */
exti_software_interrupt_enable(exti_line_enum linex)236 void exti_software_interrupt_enable(exti_line_enum linex)
237 {
238 EXTI_SWIEV |= (uint32_t)linex;
239 }
240
241 /*!
242 \brief disable EXTI software interrupt event
243 \param[in] linex: EXTI line number, refer to exti_line_enum
244 only one parameter can be selected which is shown as below:
245 \arg EXTI_x (x=0..18): EXTI line x
246 \param[out] none
247 \retval none
248 */
exti_software_interrupt_disable(exti_line_enum linex)249 void exti_software_interrupt_disable(exti_line_enum linex)
250 {
251 EXTI_SWIEV &= ~(uint32_t)linex;
252 }
253