1 /*!
2     \file    gd32e10x_exti.c
3     \brief   EXTI driver
4 
5     \version 2017-12-26, V1.0.0, firmware for GD32E10x
6     \version 2020-09-30, V1.1.0, firmware for GD32E10x
7     \version 2020-12-31, V1.2.0, firmware for GD32E10x
8     \version 2022-06-30, V1.3.0, firmware for GD32E10x
9 */
10 
11 /*
12     Copyright (c) 2022, GigaDevice Semiconductor Inc.
13 
14     Redistribution and use in source and binary forms, with or without modification,
15 are permitted provided that the following conditions are met:
16 
17     1. Redistributions of source code must retain the above copyright notice, this
18        list of conditions and the following disclaimer.
19     2. Redistributions in binary form must reproduce the above copyright notice,
20        this list of conditions and the following disclaimer in the documentation
21        and/or other materials provided with the distribution.
22     3. Neither the name of the copyright holder nor the names of its contributors
23        may be used to endorse or promote products derived from this software without
24        specific prior written permission.
25 
26     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 OF SUCH DAMAGE.
36 */
37 
38 #include "gd32e10x_exti.h"
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 = (uint32_t)0x00000000U;
50     EXTI_EVEN  = (uint32_t)0x00000000U;
51     EXTI_RTEN  = (uint32_t)0x00000000U;
52     EXTI_FTEN  = (uint32_t)0x00000000U;
53     EXTI_SWIEV = (uint32_t)0x00000000U;
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: interrupt 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 trigger
69       \arg        EXTI_TRIG_BOTH: rising and falling 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     default:
108         break;
109     }
110 }
111 
112 /*!
113     \brief      enable the interrupts from EXTI line x
114     \param[in]  linex: EXTI line number, refer to exti_line_enum
115                 only one parameter can be selected which is shown as below:
116       \arg        EXTI_x (x=0..18): EXTI line x
117     \param[out] none
118     \retval     none
119 */
exti_interrupt_enable(exti_line_enum linex)120 void exti_interrupt_enable(exti_line_enum linex)
121 {
122     EXTI_INTEN |= (uint32_t)linex;
123 }
124 
125 /*!
126     \brief      enable the events from EXTI line x
127     \param[in]  linex: EXTI line number, refer to exti_line_enum
128                 only one parameter can be selected which is shown as below:
129       \arg        EXTI_x (x=0..18): EXTI line x
130     \param[out] none
131     \retval     none
132 */
exti_event_enable(exti_line_enum linex)133 void exti_event_enable(exti_line_enum linex)
134 {
135     EXTI_EVEN |= (uint32_t)linex;
136 }
137 
138 /*!
139     \brief      disable the interrupt from EXTI line x
140     \param[in]  linex: EXTI line number, refer to exti_line_enum
141                 only one parameter can be selected which is shown as below:
142       \arg        EXTI_x (x=0..18): EXTI line x
143     \param[out] none
144     \retval     none
145 */
exti_interrupt_disable(exti_line_enum linex)146 void exti_interrupt_disable(exti_line_enum linex)
147 {
148     EXTI_INTEN &= ~(uint32_t)linex;
149 }
150 
151 /*!
152     \brief      disable the events from EXTI line x
153     \param[in]  linex: EXTI line number, refer to exti_line_enum
154                 only one parameter can be selected which is shown as below:
155       \arg        EXTI_x (x=0..18): EXTI line x
156     \param[out] none
157     \retval     none
158 */
exti_event_disable(exti_line_enum linex)159 void exti_event_disable(exti_line_enum linex)
160 {
161     EXTI_EVEN &= ~(uint32_t)linex;
162 }
163 
164 /*!
165     \brief      get EXTI lines flag
166     \param[in]  linex: EXTI line number, refer to exti_line_enum
167                 only one parameter can be selected which is shown as below:
168       \arg        EXTI_x (x=0..18): EXTI line x
169     \param[out] none
170     \retval     FlagStatus: SET or RESET
171 */
exti_flag_get(exti_line_enum linex)172 FlagStatus exti_flag_get(exti_line_enum linex)
173 {
174     if(RESET != (EXTI_PD & (uint32_t)linex)){
175         return SET;
176     }else{
177         return RESET;
178     }
179 }
180 
181 /*!
182     \brief      clear EXTI lines pending flag
183     \param[in]  linex: EXTI line number, refer to exti_line_enum
184                 only one parameter can be selected which is shown as below:
185       \arg        EXTI_x (x=0..18): EXTI line x
186     \param[out] none
187     \retval     none
188 */
exti_flag_clear(exti_line_enum linex)189 void exti_flag_clear(exti_line_enum linex)
190 {
191     EXTI_PD = (uint32_t)linex;
192 }
193 
194 /*!
195     \brief      get EXTI lines interrupt flag
196     \param[in]  linex: EXTI line number, refer to exti_line_enum
197                 only one parameter can be selected which is shown as below:
198       \arg        EXTI_x (x=0..18): EXTI line x
199     \param[out] none
200     \retval     FlagStatus: SET or RESET
201 */
exti_interrupt_flag_get(exti_line_enum linex)202 FlagStatus exti_interrupt_flag_get(exti_line_enum linex)
203 {
204     uint32_t flag_left, flag_right;
205 
206     flag_left = EXTI_PD & (uint32_t)linex;
207     flag_right = EXTI_INTEN & (uint32_t)linex;
208 
209     if((RESET != flag_left) && (RESET != flag_right)){
210         return SET;
211     }else{
212         return RESET;
213     }
214 }
215 
216 /*!
217     \brief      clear EXTI lines pending flag
218     \param[in]  linex: EXTI line number, refer to exti_line_enum
219                 only one parameter can be selected which is shown as below:
220       \arg        EXTI_x (x=0..18): EXTI line x
221     \param[out] none
222     \retval     none
223 */
exti_interrupt_flag_clear(exti_line_enum linex)224 void exti_interrupt_flag_clear(exti_line_enum linex)
225 {
226     EXTI_PD = (uint32_t)linex;
227 }
228 
229 /*!
230     \brief      enable EXTI software interrupt event
231     \param[in]  linex: EXTI line number, refer to exti_line_enum
232                 only one parameter can be selected which is shown as below:
233       \arg        EXTI_x (x=0..18): EXTI line x
234     \param[out] none
235     \retval     none
236 */
exti_software_interrupt_enable(exti_line_enum linex)237 void exti_software_interrupt_enable(exti_line_enum linex)
238 {
239     EXTI_SWIEV |= (uint32_t)linex;
240 }
241 
242 /*!
243     \brief      disable EXTI software interrupt event
244     \param[in]  linex: EXTI line number, refer to exti_line_enum
245                 only one parameter can be selected which is shown as below:
246       \arg        EXTI_x (x=0..18): EXTI line x
247     \param[out] none
248     \retval     none
249 */
exti_software_interrupt_disable(exti_line_enum linex)250 void exti_software_interrupt_disable(exti_line_enum linex)
251 {
252     EXTI_SWIEV &= ~(uint32_t)linex;
253 }
254