1 /**
2   ******************************************************************************
3   * @file    stm32h5xx_ll_exti.c
4   * @author  MCD Application Team
5   * @brief   EXTI LL module driver.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2023 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 "stm32h5xx_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 STM32H5xx_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   /* Interrupt mask register set to default reset values */
83   LL_EXTI_WriteReg(IMR1,   0xFFFE0000U);
84 #if (defined(STM32H573xx) || defined(STM32H563xx) || defined(STM32H562xx))
85   LL_EXTI_WriteReg(IMR2,   0x03DBBFFFU);
86 #elif defined(STM32H533xx)
87   LL_EXTI_WriteReg(IMR2,   0x07DBFFFFU);
88 #else
89   LL_EXTI_WriteReg(IMR2,   0x001BFFFFU);
90 #endif /* defined(STM32H573xx) || defined(STM32H563xx) || defined(STM32H562xx) */
91 
92   /* Event mask register set to default reset values */
93   LL_EXTI_WriteReg(EMR1,   0x00000000U);
94   LL_EXTI_WriteReg(EMR2,   0x00000000U);
95 
96   /* Rising Trigger selection register set to default reset values */
97   LL_EXTI_WriteReg(RTSR1,  0x00000000U);
98   LL_EXTI_WriteReg(RTSR2,  0x00000000U);
99 
100   /* Falling Trigger selection register set to default reset values */
101   LL_EXTI_WriteReg(FTSR1,  0x00000000U);
102   LL_EXTI_WriteReg(FTSR2,  0x00000000U);
103 
104   /* Software interrupt event register set to default reset values */
105   LL_EXTI_WriteReg(SWIER1, 0x00000000U);
106   LL_EXTI_WriteReg(SWIER2, 0x00000000U);
107 
108   /* Pending register set to default reset values */
109   LL_EXTI_WriteReg(RPR1,   0xFFFFFFFFU);
110   LL_EXTI_WriteReg(FPR1,   0xFFFFFFFFU);
111   LL_EXTI_WriteReg(RPR2,   0xFFFFFFFFU);
112   LL_EXTI_WriteReg(FPR2,   0xFFFFFFFFU);
113 
114   /* Privilege register set to default reset values */
115   LL_EXTI_WriteReg(PRIVCFGR1,  0x00000000U);
116   LL_EXTI_WriteReg(PRIVCFGR2,  0x00000000U);
117 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
118   /* Secure register set to default reset values */
119   LL_EXTI_WriteReg(SECCFGR1,  0x00000000U);
120   LL_EXTI_WriteReg(SECCFGR2,  0x00000000U);
121 #endif /* __ARM_FEATURE_CMSE */
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 
199     /* Configure EXTI Lines in range from 32 to 63 */
200     if (EXTI_InitStruct->Line_32_63 != LL_EXTI_LINE_NONE)
201     {
202       switch (EXTI_InitStruct->Mode)
203       {
204         case LL_EXTI_MODE_IT:
205           /* First Disable Event on provided Lines */
206           LL_EXTI_DisableEvent_32_63(EXTI_InitStruct->Line_32_63);
207           /* Then Enable IT on provided Lines */
208           LL_EXTI_EnableIT_32_63(EXTI_InitStruct->Line_32_63);
209           break;
210         case LL_EXTI_MODE_EVENT:
211           /* First Disable IT on provided Lines */
212           LL_EXTI_DisableIT_32_63(EXTI_InitStruct->Line_32_63);
213           /* Then Enable Event on provided Lines */
214           LL_EXTI_EnableEvent_32_63(EXTI_InitStruct->Line_32_63);
215           break;
216         case LL_EXTI_MODE_IT_EVENT:
217           /* Directly Enable IT & Event on provided Lines */
218           LL_EXTI_EnableIT_32_63(EXTI_InitStruct->Line_32_63);
219           LL_EXTI_EnableEvent_32_63(EXTI_InitStruct->Line_32_63);
220           break;
221         default:
222           status = ERROR;
223           break;
224       }
225       if (EXTI_InitStruct->Trigger != LL_EXTI_TRIGGER_NONE)
226       {
227         switch (EXTI_InitStruct->Trigger)
228         {
229           case LL_EXTI_TRIGGER_RISING:
230             /* First Disable Falling Trigger on provided Lines */
231             LL_EXTI_DisableFallingTrig_32_63(EXTI_InitStruct->Line_32_63);
232             /* Then Enable Rising Trigger on provided Lines */
233             LL_EXTI_EnableRisingTrig_32_63(EXTI_InitStruct->Line_32_63);
234             break;
235           case LL_EXTI_TRIGGER_FALLING:
236             /* First Disable Rising Trigger on provided Lines */
237             LL_EXTI_DisableRisingTrig_32_63(EXTI_InitStruct->Line_32_63);
238             /* Then Enable Falling Trigger on provided Lines */
239             LL_EXTI_EnableFallingTrig_32_63(EXTI_InitStruct->Line_32_63);
240             break;
241           case LL_EXTI_TRIGGER_RISING_FALLING:
242             LL_EXTI_EnableRisingTrig_32_63(EXTI_InitStruct->Line_32_63);
243             LL_EXTI_EnableFallingTrig_32_63(EXTI_InitStruct->Line_32_63);
244             break;
245           default:
246             status = ERROR;
247             break;
248         }
249       }
250     }
251   }
252   /* DISABLE LineCommand */
253   else
254   {
255     /* De-configure EXTI Lines in range from 0 to 31 */
256     LL_EXTI_DisableIT_0_31(EXTI_InitStruct->Line_0_31);
257     LL_EXTI_DisableEvent_0_31(EXTI_InitStruct->Line_0_31);
258 
259     /* De-configure EXTI Lines in range from 32 to 63 */
260     LL_EXTI_DisableIT_32_63(EXTI_InitStruct->Line_32_63);
261     LL_EXTI_DisableEvent_32_63(EXTI_InitStruct->Line_32_63);
262   }
263   return status;
264 }
265 
266 /**
267   * @brief  Set each @ref LL_EXTI_InitTypeDef field to default value.
268   * @param  EXTI_InitStruct Pointer to a @ref LL_EXTI_InitTypeDef structure.
269   * @retval None
270   */
LL_EXTI_StructInit(LL_EXTI_InitTypeDef * EXTI_InitStruct)271 void LL_EXTI_StructInit(LL_EXTI_InitTypeDef *EXTI_InitStruct)
272 {
273   EXTI_InitStruct->Line_0_31      = LL_EXTI_LINE_NONE;
274   EXTI_InitStruct->Line_32_63     = LL_EXTI_LINE_NONE;
275   EXTI_InitStruct->LineCommand    = DISABLE;
276   EXTI_InitStruct->Mode           = LL_EXTI_MODE_IT;
277   EXTI_InitStruct->Trigger        = LL_EXTI_TRIGGER_FALLING;
278 }
279 
280 /**
281   * @}
282   */
283 
284 /**
285   * @}
286   */
287 
288 /**
289   * @}
290   */
291 
292 #endif /* defined (EXTI) */
293 
294 /**
295   * @}
296   */
297 
298 #endif /* USE_FULL_LL_DRIVER */
299