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