1 /*!
2     \file    gd32e50x_exti.c
3     \brief   EXTI driver
4 
5     \version 2020-03-10, V1.0.0, firmware for GD32E50x
6     \version 2020-08-26, V1.1.0, firmware for GD32E50x
7     \version 2021-03-23, V1.2.0, firmware for GD32E50x
8 */
9 
10 /*
11     Copyright (c) 2021, 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 "gd32e50x_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..21): 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, 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..21): 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      disable the interrupt 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..21): EXTI line x
131     \param[out] none
132     \retval     none
133 */
exti_interrupt_disable(exti_line_enum linex)134 void exti_interrupt_disable(exti_line_enum linex)
135 {
136     EXTI_INTEN &= ~(uint32_t)linex;
137 }
138 
139 /*!
140     \brief      enable the events 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..21): EXTI line x
144     \param[out] none
145     \retval     none
146 */
exti_event_enable(exti_line_enum linex)147 void exti_event_enable(exti_line_enum linex)
148 {
149     EXTI_EVEN |= (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..21): 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      enable EXTI software interrupt event
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..21): EXTI line x
170     \param[out] none
171     \retval     none
172 */
exti_software_interrupt_enable(exti_line_enum linex)173 void exti_software_interrupt_enable(exti_line_enum linex)
174 {
175     EXTI_SWIEV |= (uint32_t)linex;
176 }
177 
178 /*!
179     \brief      disable EXTI software interrupt event
180     \param[in]  linex: EXTI line number, refer to exti_line_enum
181                 only one parameter can be selected which is shown as below:
182       \arg        EXTI_x (x=0..21): EXTI line x
183     \param[out] none
184     \retval     none
185 */
exti_software_interrupt_disable(exti_line_enum linex)186 void exti_software_interrupt_disable(exti_line_enum linex)
187 {
188     EXTI_SWIEV &= ~(uint32_t)linex;
189 }
190 
191 /*!
192     \brief      get EXTI line x pending flag
193     \param[in]  linex: EXTI line number, refer to exti_line_enum
194                 only one parameter can be selected which is shown as below:
195       \arg        EXTI_x (x=0..21): EXTI line x
196     \param[out] none
197     \retval     FlagStatus: status of flag (RESET or SET)
198 */
exti_flag_get(exti_line_enum linex)199 FlagStatus exti_flag_get(exti_line_enum linex)
200 {
201     if(RESET != (EXTI_PD & (uint32_t)linex)){
202         return SET;
203     }else{
204         return RESET;
205     }
206 }
207 
208 /*!
209     \brief      clear EXTI line x pending flag
210     \param[in]  linex: EXTI line number, refer to exti_line_enum
211                 only one parameter can be selected which is shown as below:
212       \arg        EXTI_x (x=0..21): EXTI line x
213     \param[out] none
214     \retval     none
215 */
exti_flag_clear(exti_line_enum linex)216 void exti_flag_clear(exti_line_enum linex)
217 {
218     EXTI_PD = (uint32_t)linex;
219 }
220 
221 /*!
222     \brief      get EXTI line x flag when the interrupt flag is set
223     \param[in]  linex: EXTI line number, refer to exti_line_enum
224                 only one parameter can be selected which is shown as below:
225       \arg        EXTI_x (x=0..21): EXTI line x
226     \param[out] none
227     \retval     FlagStatus: status of flag (RESET or SET)
228 */
exti_interrupt_flag_get(exti_line_enum linex)229 FlagStatus exti_interrupt_flag_get(exti_line_enum linex)
230 {
231     uint32_t flag_left, flag_right;
232 
233     flag_left = EXTI_PD & (uint32_t)linex;
234     flag_right = EXTI_INTEN & (uint32_t)linex;
235 
236     if((RESET != flag_left) && (RESET != flag_right)){
237         return SET;
238     }else{
239         return RESET;
240     }
241 }
242 
243 /*!
244     \brief      clear EXTI line x pending flag
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..21): EXTI line x
248     \param[out] none
249     \retval     none
250 */
exti_interrupt_flag_clear(exti_line_enum linex)251 void exti_interrupt_flag_clear(exti_line_enum linex)
252 {
253     EXTI_PD = (uint32_t)linex;
254 }
255