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