1 /*!
2     \file    gd32l23x_exti.c
3     \brief   EXTI driver
4 
5     \version 2021-08-04, V1.0.0, firmware for GD32L23x
6 */
7 
8 /*
9     Copyright (c) 2021, 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 "gd32l23x_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)0x0F940000U;
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, enable the configuration of EXTI initialize
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..29): 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, \
72                exti_mode_enum mode, \
73                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         break;
109     default:
110         break;
111     }
112 }
113 
114 /*!
115     \brief      enable the interrupts from EXTI line x
116     \param[in]  linex: EXTI line number, refer to exti_line_enum
117                 only one parameter can be selected which is shown as below:
118       \arg        EXTI_x (x=0..29): EXTI line x
119     \param[out] none
120     \retval     none
121 */
exti_interrupt_enable(exti_line_enum linex)122 void exti_interrupt_enable(exti_line_enum linex)
123 {
124     EXTI_INTEN |= (uint32_t)linex;
125 }
126 
127 /*!
128     \brief      disable the interrupt from EXTI line x
129     \param[in]  linex: EXTI line number, refer to exti_line_enum
130                 only one parameter can be selected which is shown as below:
131       \arg        EXTI_x (x=0..29): EXTI line x
132     \param[out] none
133     \retval     none
134 */
exti_interrupt_disable(exti_line_enum linex)135 void exti_interrupt_disable(exti_line_enum linex)
136 {
137     EXTI_INTEN &= ~(uint32_t)linex;
138 }
139 
140 /*!
141     \brief      enable the events from EXTI line x
142     \param[in]  linex: EXTI line number, refer to exti_line_enum
143                 only one parameter can be selected which is shown as below:
144       \arg        EXTI_x (x=0..29): EXTI line x
145     \param[out] none
146     \retval     none
147 */
exti_event_enable(exti_line_enum linex)148 void exti_event_enable(exti_line_enum linex)
149 {
150     EXTI_EVEN |= (uint32_t)linex;
151 }
152 
153 /*!
154     \brief      disable the events from EXTI line x
155     \param[in]  linex: EXTI line number, refer to exti_line_enum
156                 only one parameter can be selected which is shown as below:
157       \arg        EXTI_x (x=0..29): EXTI line x
158     \param[out] none
159     \retval     none
160 */
exti_event_disable(exti_line_enum linex)161 void exti_event_disable(exti_line_enum linex)
162 {
163     EXTI_EVEN &= ~(uint32_t)linex;
164 }
165 
166 /*!
167     \brief      enable EXTI software interrupt event
168     \param[in]  linex: EXTI line number, refer to exti_line_enum
169                 only one parameter can be selected which is shown as below:
170       \arg        EXTI_x (x=0..29): EXTI line x
171     \param[out] none
172     \retval     none
173 */
exti_software_interrupt_enable(exti_line_enum linex)174 void exti_software_interrupt_enable(exti_line_enum linex)
175 {
176     EXTI_SWIEV |= (uint32_t)linex;
177 }
178 
179 /*!
180     \brief      disable EXTI software interrupt event
181     \param[in]  linex: EXTI line number, refer to exti_line_enum
182                 only one parameter can be selected which is shown as below:
183       \arg        EXTI_x (x=0..29): EXTI line x
184     \param[out] none
185     \retval     none
186 */
exti_software_interrupt_disable(exti_line_enum linex)187 void exti_software_interrupt_disable(exti_line_enum linex)
188 {
189     EXTI_SWIEV &= ~(uint32_t)linex;
190 }
191 
192 /*!
193     \brief      get EXTI lines flag
194     \param[in]  linex: EXTI line number, refer to exti_line_enum
195                 only one parameter can be selected which is shown as below:
196       \arg        EXTI_x (x=0..29): EXTI line x
197     \param[out] none
198     \retval     FlagStatus: status of flag (RESET or SET)
199 */
exti_flag_get(exti_line_enum linex)200 FlagStatus exti_flag_get(exti_line_enum linex)
201 {
202     if(RESET != (EXTI_PD & (uint32_t)linex)) {
203         return SET;
204     } else {
205         return RESET;
206     }
207 }
208 
209 /*!
210     \brief      clear EXTI lines pending flag
211     \param[in]  linex: EXTI line number, refer to exti_line_enum
212                 only one parameter can be selected which is shown as below:
213       \arg        EXTI_x (x=0..29): EXTI line x
214     \param[out] none
215     \retval     none
216 */
exti_flag_clear(exti_line_enum linex)217 void exti_flag_clear(exti_line_enum linex)
218 {
219     EXTI_PD = (uint32_t)linex;
220 }
221 
222 /*!
223     \brief      get EXTI lines flag when the interrupt flag is set
224     \param[in]  linex: EXTI line number, refer to exti_line_enum
225                 only one parameter can be selected which is shown as below:
226       \arg        EXTI_x (x=0..29): EXTI line x
227     \param[out] none
228     \retval     FlagStatus: status of flag (RESET or SET)
229 */
exti_interrupt_flag_get(exti_line_enum linex)230 FlagStatus exti_interrupt_flag_get(exti_line_enum linex)
231 {
232     uint32_t flag_left, flag_right;
233 
234     flag_left = EXTI_PD & (uint32_t)linex;
235     flag_right = EXTI_INTEN & (uint32_t)linex;
236 
237     if((RESET != flag_left) && (RESET != flag_right)) {
238         return SET;
239     } else {
240         return RESET;
241     }
242 }
243 
244 /*!
245     \brief      clear EXTI lines pending flag
246     \param[in]  linex: EXTI line number, refer to exti_line_enum
247                 only one parameter can be selected which is shown as below:
248       \arg        EXTI_x (x=0..29): EXTI line x
249     \param[out] none
250     \retval     none
251 */
exti_interrupt_flag_clear(exti_line_enum linex)252 void exti_interrupt_flag_clear(exti_line_enum linex)
253 {
254     EXTI_PD = (uint32_t)linex;
255 }
256