1 /**
2   ******************************************************************************
3   * @file    stm32c0xx_ll_exti.c
4   * @author  MCD Application Team
5   * @brief   EXTI LL module driver.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2022 STMicroelectronics.
10   * All rights reserved.
11   *
12   * This software is licensed under terms that can be found in the LICENSE file
13   * in the root directory of this software component.
14   * If no LICENSE file comes with this software, it is provided AS-IS.
15   *
16   ******************************************************************************
17   */
18 #if defined(USE_FULL_LL_DRIVER)
19 
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32c0xx_ll_exti.h"
22 #ifdef  USE_FULL_ASSERT
23 #include "stm32_assert.h"
24 #else
25 #define assert_param(expr) ((void)0U)
26 #endif /* USE_FULL_ASSERT */
27 
28 /** @addtogroup STM32C0xx_LL_Driver
29   * @{
30   */
31 
32 #if defined (EXTI)
33 
34 /** @defgroup EXTI_LL EXTI
35   * @{
36   */
37 
38 /* Private types -------------------------------------------------------------*/
39 /* Private variables ---------------------------------------------------------*/
40 /* Private constants ---------------------------------------------------------*/
41 /* Private macros ------------------------------------------------------------*/
42 /** @addtogroup EXTI_LL_Private_Macros
43   * @{
44   */
45 
46 #define IS_LL_EXTI_LINE_0_31(__VALUE__)              (((__VALUE__) & ~LL_EXTI_LINE_ALL_0_31) == 0x00000000U)
47 #if defined(EXTI_IMR2_IM36)
48 #define IS_LL_EXTI_LINE_32_63(__VALUE__)             (((__VALUE__) & ~LL_EXTI_LINE_ALL_32_63) == 0x00000000U)
49 #endif /* EXTI_IMR2_IM36 */
50 #define IS_LL_EXTI_MODE(__VALUE__)                   (((__VALUE__) == LL_EXTI_MODE_IT)            \
51                                                       || ((__VALUE__) == LL_EXTI_MODE_EVENT)         \
52                                                       || ((__VALUE__) == LL_EXTI_MODE_IT_EVENT))
53 
54 
55 #define IS_LL_EXTI_TRIGGER(__VALUE__)                (((__VALUE__) == LL_EXTI_TRIGGER_NONE)       \
56                                                       || ((__VALUE__) == LL_EXTI_TRIGGER_RISING)     \
57                                                       || ((__VALUE__) == LL_EXTI_TRIGGER_FALLING)    \
58                                                       || ((__VALUE__) == LL_EXTI_TRIGGER_RISING_FALLING))
59 
60 /**
61   * @}
62   */
63 
64 /* Private function prototypes -----------------------------------------------*/
65 
66 /* Exported functions --------------------------------------------------------*/
67 /** @addtogroup EXTI_LL_Exported_Functions
68   * @{
69   */
70 
71 /** @addtogroup EXTI_LL_EF_Init
72   * @{
73   */
74 
75 /**
76   * @brief  De-initialize the EXTI registers to their default reset values.
77   * @retval An ErrorStatus enumeration value:
78   *          - SUCCESS: EXTI registers are de-initialized
79   *          - ERROR: not applicable
80   */
LL_EXTI_DeInit(void)81 ErrorStatus LL_EXTI_DeInit(void)
82 {
83   /* Interrupt mask register set to default reset values */
84   LL_EXTI_WriteReg(IMR1,   0xFFF80000U);
85   /* Event mask register set to default reset values */
86   LL_EXTI_WriteReg(EMR1,   0x00000000U);
87   /* Rising Trigger selection register set to default reset values */
88   LL_EXTI_WriteReg(RTSR1,  0x00000000U);
89   /* Falling Trigger selection register set to default reset values */
90   LL_EXTI_WriteReg(FTSR1,  0x00000000U);
91   /* Software interrupt event register set to default reset values */
92   LL_EXTI_WriteReg(SWIER1, 0x00000000U);
93   /* Pending register set to default reset values */
94   LL_EXTI_WriteReg(RPR1,    0x0007FFFFU);
95   LL_EXTI_WriteReg(FPR1,    0x0007FFFFU);
96 #if defined(EXTI_IMR2_IM36)
97   /* Interrupt mask register set to default reset values */
98   LL_EXTI_WriteReg(IMR2,   0x00000010U);
99   /* Event mask register set to default reset values */
100   LL_EXTI_WriteReg(EMR2,   0x00000000U);
101   /* Rising Trigger selection register set to default reset values */
102   LL_EXTI_WriteReg(RTSR2,  0x00000000U);
103   /* Falling Trigger selection register set to default reset values */
104   LL_EXTI_WriteReg(FTSR2,  0x00000000U);
105   /* Software interrupt event register set to default reset values */
106   LL_EXTI_WriteReg(SWIER2, 0x00000000U);
107   /* Pending register set to default reset values */
108   LL_EXTI_WriteReg(RPR2,    0x00000004U);
109   LL_EXTI_WriteReg(FPR2,    0x00000004U);
110 #endif /* EXTI_IMR2_IM36*/
111 
112   return SUCCESS;
113 }
114 
115 /**
116   * @brief  Initialize the EXTI registers according to the specified parameters in EXTI_InitStruct.
117   * @param  EXTI_InitStruct pointer to a @ref LL_EXTI_InitTypeDef structure.
118   * @retval An ErrorStatus enumeration value:
119   *          - SUCCESS: EXTI registers are initialized
120   *          - ERROR: not applicable
121   */
LL_EXTI_Init(LL_EXTI_InitTypeDef * EXTI_InitStruct)122 ErrorStatus LL_EXTI_Init(LL_EXTI_InitTypeDef *EXTI_InitStruct)
123 {
124   ErrorStatus status = SUCCESS;
125   /* Check the parameters */
126   assert_param(IS_LL_EXTI_LINE_0_31(EXTI_InitStruct->Line_0_31));
127 #if defined(EXTI_IMR2_IM36)
128   assert_param(IS_LL_EXTI_LINE_32_63(EXTI_InitStruct->Line_32_63));
129 #endif /* EXTI_IMR2_IM36 */
130   assert_param(IS_FUNCTIONAL_STATE(EXTI_InitStruct->LineCommand));
131   assert_param(IS_LL_EXTI_MODE(EXTI_InitStruct->Mode));
132 
133   /* ENABLE LineCommand */
134   if (EXTI_InitStruct->LineCommand != DISABLE)
135   {
136     assert_param(IS_LL_EXTI_TRIGGER(EXTI_InitStruct->Trigger));
137 
138     /* Configure EXTI Lines in range from 0 to 31 */
139     if (EXTI_InitStruct->Line_0_31 != LL_EXTI_LINE_NONE)
140     {
141       switch (EXTI_InitStruct->Mode)
142       {
143         case LL_EXTI_MODE_IT:
144           /* First Disable Event on provided Lines */
145           LL_EXTI_DisableEvent_0_31(EXTI_InitStruct->Line_0_31);
146           /* Then Enable IT on provided Lines */
147           LL_EXTI_EnableIT_0_31(EXTI_InitStruct->Line_0_31);
148           break;
149         case LL_EXTI_MODE_EVENT:
150           /* First Disable IT on provided Lines */
151           LL_EXTI_DisableIT_0_31(EXTI_InitStruct->Line_0_31);
152           /* Then Enable Event on provided Lines */
153           LL_EXTI_EnableEvent_0_31(EXTI_InitStruct->Line_0_31);
154           break;
155         case LL_EXTI_MODE_IT_EVENT:
156           /* Directly Enable IT & Event on provided Lines */
157           LL_EXTI_EnableIT_0_31(EXTI_InitStruct->Line_0_31);
158           LL_EXTI_EnableEvent_0_31(EXTI_InitStruct->Line_0_31);
159           break;
160         default:
161           status = ERROR;
162           break;
163       }
164 
165       if (EXTI_InitStruct->Trigger != LL_EXTI_TRIGGER_NONE)
166       {
167         switch (EXTI_InitStruct->Trigger)
168         {
169           case LL_EXTI_TRIGGER_RISING:
170             /* First Disable Falling Trigger on provided Lines */
171             LL_EXTI_DisableFallingTrig_0_31(EXTI_InitStruct->Line_0_31);
172             /* Then Enable Rising Trigger on provided Lines */
173             LL_EXTI_EnableRisingTrig_0_31(EXTI_InitStruct->Line_0_31);
174             break;
175           case LL_EXTI_TRIGGER_FALLING:
176             /* First Disable Rising Trigger on provided Lines */
177             LL_EXTI_DisableRisingTrig_0_31(EXTI_InitStruct->Line_0_31);
178             /* Then Enable Falling Trigger on provided Lines */
179             LL_EXTI_EnableFallingTrig_0_31(EXTI_InitStruct->Line_0_31);
180             break;
181           case LL_EXTI_TRIGGER_RISING_FALLING:
182             LL_EXTI_EnableRisingTrig_0_31(EXTI_InitStruct->Line_0_31);
183             LL_EXTI_EnableFallingTrig_0_31(EXTI_InitStruct->Line_0_31);
184             break;
185           default:
186             status = ERROR;
187             break;
188         }
189       }
190     }
191 #if defined(EXTI_IMR2_IM36)
192     /* Configure EXTI Lines in range from 32 to 63 */
193     if (EXTI_InitStruct->Line_32_63 != LL_EXTI_LINE_NONE)
194     {
195       switch (EXTI_InitStruct->Mode)
196       {
197         case LL_EXTI_MODE_IT:
198           /* First Disable Event on provided Lines */
199           LL_EXTI_DisableEvent_32_63(EXTI_InitStruct->Line_32_63);
200           /* Then Enable IT on provided Lines */
201           LL_EXTI_EnableIT_32_63(EXTI_InitStruct->Line_32_63);
202           break;
203         case LL_EXTI_MODE_EVENT:
204           /* First Disable IT on provided Lines */
205           LL_EXTI_DisableIT_32_63(EXTI_InitStruct->Line_32_63);
206           /* Then Enable Event on provided Lines */
207           LL_EXTI_EnableEvent_32_63(EXTI_InitStruct->Line_32_63);
208           break;
209         case LL_EXTI_MODE_IT_EVENT:
210           /* Directly Enable IT & Event on provided Lines */
211           LL_EXTI_EnableIT_32_63(EXTI_InitStruct->Line_32_63);
212           LL_EXTI_EnableEvent_32_63(EXTI_InitStruct->Line_32_63);
213           break;
214         default:
215           status = ERROR;
216           break;
217       }
218       if (EXTI_InitStruct->Trigger != LL_EXTI_TRIGGER_NONE)
219       {
220         switch (EXTI_InitStruct->Trigger)
221         {
222           case LL_EXTI_TRIGGER_RISING:
223             /* First Disable Falling Trigger on provided Lines */
224             LL_EXTI_DisableFallingTrig_32_63(EXTI_InitStruct->Line_32_63);
225             /* Then Enable Rising Trigger on provided Lines */
226             LL_EXTI_EnableRisingTrig_32_63(EXTI_InitStruct->Line_32_63);
227             break;
228           case LL_EXTI_TRIGGER_FALLING:
229             /* First Disable Rising Trigger on provided Lines */
230             LL_EXTI_DisableRisingTrig_32_63(EXTI_InitStruct->Line_32_63);
231             /* Then Enable Falling Trigger on provided Lines */
232             LL_EXTI_EnableFallingTrig_32_63(EXTI_InitStruct->Line_32_63);
233             break;
234           case LL_EXTI_TRIGGER_RISING_FALLING:
235             LL_EXTI_EnableRisingTrig_32_63(EXTI_InitStruct->Line_32_63);
236             LL_EXTI_EnableFallingTrig_32_63(EXTI_InitStruct->Line_32_63);
237             break;
238           default:
239             status = ERROR;
240             break;
241         }
242       }
243     }
244 #endif /* EXTI_IMR2_IM36 */
245   }
246   /* DISABLE LineCommand */
247   else
248   {
249     /* De-configure EXTI Lines in range from 0 to 31 */
250     LL_EXTI_DisableIT_0_31(EXTI_InitStruct->Line_0_31);
251     LL_EXTI_DisableEvent_0_31(EXTI_InitStruct->Line_0_31);
252 #if defined(EXTI_IMR2_IM36)
253     /* De-configure EXTI Lines in range from 32 to 63 */
254     LL_EXTI_DisableIT_32_63(EXTI_InitStruct->Line_32_63);
255     LL_EXTI_DisableEvent_32_63(EXTI_InitStruct->Line_32_63);
256 #endif /* EXTI_IMR2_IM36 */
257   }
258   return status;
259 }
260 
261 /**
262   * @brief  Set each @ref LL_EXTI_InitTypeDef field to default value.
263   * @param  EXTI_InitStruct Pointer to a @ref LL_EXTI_InitTypeDef structure.
264   * @retval None
265   */
LL_EXTI_StructInit(LL_EXTI_InitTypeDef * EXTI_InitStruct)266 void LL_EXTI_StructInit(LL_EXTI_InitTypeDef *EXTI_InitStruct)
267 {
268   EXTI_InitStruct->Line_0_31      = LL_EXTI_LINE_NONE;
269 #if defined(EXTI_IMR2_IM36)
270   EXTI_InitStruct->Line_32_63     = LL_EXTI_LINE_NONE;
271 #endif /* EXTI_IMR2_IM36 */
272   EXTI_InitStruct->LineCommand    = DISABLE;
273   EXTI_InitStruct->Mode           = LL_EXTI_MODE_IT;
274   EXTI_InitStruct->Trigger        = LL_EXTI_TRIGGER_FALLING;
275 }
276 
277 /**
278   * @}
279   */
280 
281 /**
282   * @}
283   */
284 
285 /**
286   * @}
287   */
288 
289 #endif /* defined (EXTI) */
290 
291 /**
292   * @}
293   */
294 
295 #endif /* USE_FULL_LL_DRIVER */
296