1 /*****************************************************************************
2  * Copyright (c) 2022, Nations Technologies Inc.
3  *
4  * All rights reserved.
5  * ****************************************************************************
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * - Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the disclaimer below.
12  *
13  * Nations' name may not be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
19  * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  * ****************************************************************************/
27 
28 /**
29  * @file n32l40x_exti.c
30  * @author Nations
31  * @version v1.2.0
32  *
33  * @copyright Copyright (c) 2022, Nations Technologies Inc. All rights reserved.
34  */
35 #include "n32l40x_exti.h"
36 
37 /** @addtogroup n32l40x_StdPeriph_Driver
38  * @{
39  */
40 
41 /** @addtogroup EXTI
42  * @brief EXTI driver modules
43  * @{
44  */
45 
46 /** @addtogroup EXTI_Private_TypesDefinitions
47  * @{
48  */
49 
50 /**
51  * @}
52  */
53 
54 /** @addtogroup EXTI_Private_Defines
55  * @{
56  */
57 
58 #define EXTI_LINENONE ((uint32_t)0x00000) /* No interrupt selected */
59 
60 /**
61  * @}
62  */
63 
64 /** @addtogroup EXTI_Private_Macros
65  * @{
66  */
67 
68 /**
69  * @}
70  */
71 
72 /** @addtogroup EXTI_Private_Variables
73  * @{
74  */
75 
76 /**
77  * @}
78  */
79 
80 /** @addtogroup EXTI_Private_FunctionPrototypes
81  * @{
82  */
83 
84 /**
85  * @}
86  */
87 
88 /** @addtogroup EXTI_Private_Functions
89  * @{
90  */
91 
92 /**
93  * @brief  Deinitializes the EXTI peripheral registers to their default reset values.
94  */
EXTI_DeInit(void)95 void EXTI_DeInit(void)
96 {
97     EXTI->IMASK  = 0x00000000;
98     EXTI->EMASK  = 0x00000000;
99     EXTI->RT_CFG = 0x00000000;
100     EXTI->FT_CFG = 0x00000000;
101     EXTI->PEND   = 0x0FFFFFFF;
102 }
103 
104 /**
105  * @brief  Initializes the EXTI peripheral according to the specified
106  *         parameters in the EXTI_InitStruct.
107  * @param EXTI_InitStruct pointer to a EXTI_InitType structure
108  *         that contains the configuration information for the EXTI peripheral.
109  */
EXTI_InitPeripheral(EXTI_InitType * EXTI_InitStruct)110 void EXTI_InitPeripheral(EXTI_InitType* EXTI_InitStruct)
111 {
112     uint32_t tmp = 0;
113 
114     /* Check the parameters */
115     assert_param(IS_EXTI_MODE(EXTI_InitStruct->EXTI_Mode));
116     assert_param(IS_EXTI_TRIGGER(EXTI_InitStruct->EXTI_Trigger));
117     assert_param(IS_EXTI_LINE(EXTI_InitStruct->EXTI_Line));
118     assert_param(IS_FUNCTIONAL_STATE(EXTI_InitStruct->EXTI_LineCmd));
119 
120     tmp = (uint32_t)EXTI_BASE;
121 
122     if (EXTI_InitStruct->EXTI_LineCmd != DISABLE)
123     {
124         /* Clear EXTI line configuration */
125         EXTI->IMASK &= ~EXTI_InitStruct->EXTI_Line;
126         EXTI->EMASK &= ~EXTI_InitStruct->EXTI_Line;
127 
128         tmp += EXTI_InitStruct->EXTI_Mode;
129 
130         *(__IO uint32_t*)tmp |= EXTI_InitStruct->EXTI_Line;
131 
132         /* Clear Rising Falling edge configuration */
133         EXTI->RT_CFG &= ~EXTI_InitStruct->EXTI_Line;
134         EXTI->FT_CFG &= ~EXTI_InitStruct->EXTI_Line;
135 
136         /* Select the trigger for the selected external interrupts */
137         if (EXTI_InitStruct->EXTI_Trigger == EXTI_Trigger_Rising_Falling)
138         {
139             /* Rising Falling edge */
140             EXTI->RT_CFG |= EXTI_InitStruct->EXTI_Line;
141             EXTI->FT_CFG |= EXTI_InitStruct->EXTI_Line;
142         }
143         else
144         {
145             tmp = (uint32_t)EXTI_BASE;
146             tmp += EXTI_InitStruct->EXTI_Trigger;
147 
148             *(__IO uint32_t*)tmp |= EXTI_InitStruct->EXTI_Line;
149         }
150     }
151     else
152     {
153         tmp += EXTI_InitStruct->EXTI_Mode;
154 
155         /* Disable the selected external lines */
156         *(__IO uint32_t*)tmp &= ~EXTI_InitStruct->EXTI_Line;
157     }
158 }
159 
160 /**
161  * @brief  Fills each EXTI_InitStruct member with its reset value.
162  * @param EXTI_InitStruct pointer to a EXTI_InitType structure which will
163  *         be initialized.
164  */
EXTI_InitStruct(EXTI_InitType * EXTI_InitStruct)165 void EXTI_InitStruct(EXTI_InitType* EXTI_InitStruct)
166 {
167     EXTI_InitStruct->EXTI_Line    = EXTI_LINENONE;
168     EXTI_InitStruct->EXTI_Mode    = EXTI_Mode_Interrupt;
169     EXTI_InitStruct->EXTI_Trigger = EXTI_Trigger_Falling;
170     EXTI_InitStruct->EXTI_LineCmd = DISABLE;
171 }
172 
173 /**
174  * @brief  Generates a Software interrupt.
175  * @param EXTI_Line specifies the EXTI lines to be enabled or disabled.
176  *   This parameter can be any combination of EXTI_Linex where x can be (0..27).
177  */
EXTI_TriggerSWInt(uint32_t EXTI_Line)178 void EXTI_TriggerSWInt(uint32_t EXTI_Line)
179 {
180     /* Check the parameters */
181     assert_param(IS_EXTI_LINE(EXTI_Line));
182 
183     EXTI->SWIE |= EXTI_Line;
184 }
185 
186 /**
187  * @brief  Checks whether the specified EXTI line flag is set or not.
188  * @param EXTI_Line specifies the EXTI line flag to check.
189  *   This parameter can be:
190  *     @arg EXTI_Linex External interrupt line x where x(0..27)
191  * @return The new state of EXTI_Line (SET or RESET).
192  */
EXTI_GetStatusFlag(uint32_t EXTI_Line)193 FlagStatus EXTI_GetStatusFlag(uint32_t EXTI_Line)
194 {
195     FlagStatus bitstatus = RESET;
196     /* Check the parameters */
197     assert_param(IS_GET_EXTI_LINE(EXTI_Line));
198 
199     if ((EXTI->PEND & EXTI_Line) != (uint32_t)RESET)
200     {
201         bitstatus = SET;
202     }
203     else
204     {
205         bitstatus = RESET;
206     }
207     return bitstatus;
208 }
209 
210 /**
211  * @brief  Clears the EXTI's line pending flags.
212  * @param EXTI_Line specifies the EXTI lines flags to clear.
213  *   This parameter can be any combination of EXTI_Linex where x can be (0..27).
214  */
EXTI_ClrStatusFlag(uint32_t EXTI_Line)215 void EXTI_ClrStatusFlag(uint32_t EXTI_Line)
216 {
217     /* Check the parameters */
218     assert_param(IS_EXTI_LINE(EXTI_Line));
219 
220     EXTI->PEND = EXTI_Line;
221 }
222 
223 /**
224  * @brief  Checks whether the specified EXTI line is asserted or not.
225  * @param EXTI_Line specifies the EXTI line to check.
226  *   This parameter can be:
227  *     @arg EXTI_Linex External interrupt line x where x(0..27)
228  * @return The new state of EXTI_Line (SET or RESET).
229  */
EXTI_GetITStatus(uint32_t EXTI_Line)230 INTStatus EXTI_GetITStatus(uint32_t EXTI_Line)
231 {
232     INTStatus bitstatus   = RESET;
233     uint32_t enablestatus = 0;
234     /* Check the parameters */
235     assert_param(IS_GET_EXTI_LINE(EXTI_Line));
236 
237     enablestatus = EXTI->IMASK & EXTI_Line;
238     if (((EXTI->PEND & EXTI_Line) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET))
239     {
240         bitstatus = SET;
241     }
242     else
243     {
244         bitstatus = RESET;
245     }
246     return bitstatus;
247 }
248 
249 /**
250  * @brief  Clears the EXTI's line pending bits.
251  * @param EXTI_Line specifies the EXTI lines to clear.
252  *   This parameter can be any combination of EXTI_Linex where x can be (0..27).
253  */
EXTI_ClrITPendBit(uint32_t EXTI_Line)254 void EXTI_ClrITPendBit(uint32_t EXTI_Line)
255 {
256     /* Check the parameters */
257     assert_param(IS_EXTI_LINE(EXTI_Line));
258 
259     EXTI->PEND = EXTI_Line;
260 }
261 
262 /**
263  * @brief  Select one of EXTI inputs to the RTC TimeStamp event.
264  * @param EXTI_TSSEL_Line specifies the EXTI lines to select.
265  *   This parameter can be any combination of EXTI_TSSEL_Line where x can be (0..15).
266  */
EXTI_RTCTimeStampSel(uint32_t EXTI_TSSEL_Line)267 void EXTI_RTCTimeStampSel(uint32_t EXTI_TSSEL_Line)
268 {
269     /* Check the parameters */
270     assert_param(IS_EXTI_TSSEL_LINE(EXTI_TSSEL_Line));
271 
272     EXTI->TS_SEL &= EXTI_TSSEL_LINE_MASK;
273     EXTI->TS_SEL |= EXTI_TSSEL_Line;
274 }
275 
276 /**
277  * @}
278  */
279 
280 /**
281  * @}
282  */
283 
284 /**
285  * @}
286  */
287