1 /*!
2     \file    gd32a50x_exti.c
3     \brief   EXTI driver
4 
5     \version 2022-01-30, V1.0.0, firmware for GD32A50x
6 */
7 
8 /*
9     Copyright (c) 2022, 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 "gd32a50x_exti.h"
36 
37 /*!
38     \brief      deinitialize the EXTI
39     \param[in]  none
40     \param[out] none
41     \retval     none
42 */
exti_deinit(void)43 void exti_deinit(void)
44 {
45     /* reset the value of all the EXTI registers */
46     EXTI_INTEN = (uint32_t)0x00000000U;
47     EXTI_EVEN  = (uint32_t)0x00000000U;
48     EXTI_RTEN  = (uint32_t)0x00000000U;
49     EXTI_FTEN  = (uint32_t)0x00000000U;
50     EXTI_SWIEV = (uint32_t)0x00000000U;
51 }
52 
53 /*!
54     \brief      initialize the EXTI
55     \param[in]  linex: EXTI line number, refer to exti_line_enum
56                 only one parameter can be selected which is shown as below:
57       \arg        EXTI_x (x=0..24): EXTI line x
58     \param[in]  mode: interrupt or event mode, refer to exti_mode_enum
59                 only one parameter can be selected which is shown as below:
60       \arg        EXTI_INTERRUPT: interrupt mode
61       \arg        EXTI_EVENT: event mode
62     \param[in]  trig_type: interrupt trigger type, refer to exti_trig_type_enum
63                 only one parameter can be selected which is shown as below:
64       \arg        EXTI_TRIG_RISING: rising edge trigger
65       \arg        EXTI_TRIG_FALLING: falling trigger
66       \arg        EXTI_TRIG_BOTH: rising and falling trigger
67       \arg        EXTI_TRIG_NONE: without rising edge or falling edge trigger
68     \param[out] none
69     \retval     none
70 */
exti_init(exti_line_enum linex,exti_mode_enum mode,exti_trig_type_enum trig_type)71 void exti_init(exti_line_enum linex, exti_mode_enum mode, exti_trig_type_enum trig_type)
72 {
73     /* reset the EXTI line x */
74     EXTI_INTEN &= ~(uint32_t)linex;
75     EXTI_EVEN &= ~(uint32_t)linex;
76     EXTI_RTEN &= ~(uint32_t)linex;
77     EXTI_FTEN &= ~(uint32_t)linex;
78 
79     /* set the EXTI mode and enable the interrupts or events from EXTI line x */
80     switch(mode){
81     case EXTI_INTERRUPT:
82         EXTI_INTEN |= (uint32_t)linex;
83         break;
84     case EXTI_EVENT:
85         EXTI_EVEN |= (uint32_t)linex;
86         break;
87     default:
88         break;
89     }
90 
91     /* set the EXTI trigger type */
92     switch(trig_type){
93     case EXTI_TRIG_RISING:
94         EXTI_RTEN |= (uint32_t)linex;
95         EXTI_FTEN &= ~(uint32_t)linex;
96         break;
97     case EXTI_TRIG_FALLING:
98         EXTI_RTEN &= ~(uint32_t)linex;
99         EXTI_FTEN |= (uint32_t)linex;
100         break;
101     case EXTI_TRIG_BOTH:
102         EXTI_RTEN |= (uint32_t)linex;
103         EXTI_FTEN |= (uint32_t)linex;
104         break;
105     case EXTI_TRIG_NONE:
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..24): 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      disable the interrupt 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..24): EXTI line x
129     \param[out] none
130     \retval     none
131 */
exti_interrupt_disable(exti_line_enum linex)132 void exti_interrupt_disable(exti_line_enum linex)
133 {
134     EXTI_INTEN &= ~(uint32_t)linex;
135 }
136 
137 /*!
138     \brief      enable the events 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..24): EXTI line x
142     \param[out] none
143     \retval     none
144 */
exti_event_enable(exti_line_enum linex)145 void exti_event_enable(exti_line_enum linex)
146 {
147     EXTI_EVEN |= (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..24): 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      enable EXTI software interrupt event
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..24): EXTI line x
168     \param[out] none
169     \retval     none
170 */
exti_software_interrupt_enable(exti_line_enum linex)171 void exti_software_interrupt_enable(exti_line_enum linex)
172 {
173     EXTI_SWIEV |= (uint32_t)linex;
174 }
175 
176 /*!
177     \brief      disable EXTI software interrupt event
178     \param[in]  linex: EXTI line number, refer to exti_line_enum
179                 only one parameter can be selected which is shown as below:
180       \arg        EXTI_x (x=0..24): EXTI line x
181     \param[out] none
182     \retval     none
183 */
exti_software_interrupt_disable(exti_line_enum linex)184 void exti_software_interrupt_disable(exti_line_enum linex)
185 {
186     EXTI_SWIEV &= ~(uint32_t)linex;
187 }
188 
189 /*!
190     \brief      get EXTI line x pending flag
191     \param[in]  linex: EXTI line number, refer to exti_line_enum
192                 only one parameter can be selected which is shown as below:
193       \arg        EXTI_x (x=0..24): EXTI line x
194     \param[out] none
195     \retval     FlagStatus: status of flag (RESET or SET)
196 */
exti_flag_get(exti_line_enum linex)197 FlagStatus exti_flag_get(exti_line_enum linex)
198 {
199     if(0U != (EXTI_PD & (uint32_t)linex)){
200         return SET;
201     }else{
202         return RESET;
203     }
204 }
205 
206 /*!
207     \brief      clear EXTI line x pending flag
208     \param[in]  linex: EXTI line number, refer to exti_line_enum
209                 only one parameter can be selected which is shown as below:
210       \arg        EXTI_x (x=0..24): EXTI line x
211     \param[out] none
212     \retval     none
213 */
exti_flag_clear(exti_line_enum linex)214 void exti_flag_clear(exti_line_enum linex)
215 {
216     EXTI_PD = (uint32_t)linex;
217 }
218 
219 /*!
220     \brief      get EXTI line x flag when the interrupt flag is set
221     \param[in]  linex: EXTI line number, refer to exti_line_enum
222                 only one parameter can be selected which is shown as below:
223       \arg        EXTI_x (x=0..24): EXTI line x
224     \param[out] none
225     \retval     FlagStatus: status of flag (RESET or SET)
226 */
exti_interrupt_flag_get(exti_line_enum linex)227 FlagStatus exti_interrupt_flag_get(exti_line_enum linex)
228 {
229     uint32_t flag_left, flag_right;
230 
231     flag_left = EXTI_PD & (uint32_t)linex;
232     flag_right = EXTI_INTEN & (uint32_t)linex;
233 
234     if((0U != flag_left) && (0U != flag_right)){
235         return SET;
236     }else{
237         return RESET;
238     }
239 }
240 
241 /*!
242     \brief      clear EXTI line x pending flag
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..24): EXTI line x
246     \param[out] none
247     \retval     none
248 */
exti_interrupt_flag_clear(exti_line_enum linex)249 void exti_interrupt_flag_clear(exti_line_enum linex)
250 {
251     EXTI_PD = (uint32_t)linex;
252 }
253