xref: /btstack/port/stm32-l451-miromico-sx1280/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_ll_exti.c (revision 2fd737d36a1de5d778cacc671d4b4d8c4f3fed82)
1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_ll_exti.c
4   * @author  MCD Application Team
5   * @brief   EXTI LL module driver.
6   ******************************************************************************
7   * @attention
8   *
9   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
10   * All rights reserved.</center></h2>
11   *
12   * This software component is licensed by ST under BSD 3-Clause license,
13   * the "License"; You may not use this file except in compliance with the
14   * License. You may obtain a copy of the License at:
15   *                        opensource.org/licenses/BSD-3-Clause
16   *
17   ******************************************************************************
18   */
19 #if defined(USE_FULL_LL_DRIVER)
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32l4xx_ll_exti.h"
23 #ifdef  USE_FULL_ASSERT
24 #include "stm32_assert.h"
25 #else
26 #define assert_param(expr) ((void)0U)
27 #endif
28 
29 /** @addtogroup STM32L4xx_LL_Driver
30   * @{
31   */
32 
33 #if defined (EXTI)
34 
35 /** @defgroup EXTI_LL EXTI
36   * @{
37   */
38 
39 /* Private types -------------------------------------------------------------*/
40 /* Private variables ---------------------------------------------------------*/
41 /* Private constants ---------------------------------------------------------*/
42 /* Private macros ------------------------------------------------------------*/
43 /** @addtogroup EXTI_LL_Private_Macros
44   * @{
45   */
46 
47 #define IS_LL_EXTI_LINE_0_31(__VALUE__)              (((__VALUE__) & ~LL_EXTI_LINE_ALL_0_31) == 0x00000000U)
48 #define IS_LL_EXTI_LINE_32_63(__VALUE__)             (((__VALUE__) & ~LL_EXTI_LINE_ALL_32_63) == 0x00000000U)
49 
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   *          - 0x00: EXTI registers are de-initialized
79   */
LL_EXTI_DeInit(void)80 uint32_t LL_EXTI_DeInit(void)
81 {
82   /* Interrupt mask register set to default reset values */
83   LL_EXTI_WriteReg(IMR1,   0xFF820000U);
84   /* Event mask register set to default reset values */
85   LL_EXTI_WriteReg(EMR1,   0x00000000U);
86   /* Rising Trigger selection register set to default reset values */
87   LL_EXTI_WriteReg(RTSR1,  0x00000000U);
88   /* Falling Trigger selection register set to default reset values */
89   LL_EXTI_WriteReg(FTSR1,  0x00000000U);
90   /* Software interrupt event register set to default reset values */
91   LL_EXTI_WriteReg(SWIER1, 0x00000000U);
92   /* Pending register clear */
93   LL_EXTI_WriteReg(PR1,    0x007DFFFFU);
94 
95   /* Interrupt mask register 2 set to default reset values */
96 #if defined(LL_EXTI_LINE_40)
97   LL_EXTI_WriteReg(IMR2,        0x00000187U);
98 #else
99   LL_EXTI_WriteReg(IMR2,        0x00000087U);
100 #endif
101   /* Event mask register 2 set to default reset values */
102   LL_EXTI_WriteReg(EMR2,        0x00000000U);
103   /* Rising Trigger selection register 2 set to default reset values */
104   LL_EXTI_WriteReg(RTSR2,       0x00000000U);
105   /* Falling Trigger selection register 2 set to default reset values */
106   LL_EXTI_WriteReg(FTSR2,       0x00000000U);
107   /* Software interrupt event register 2 set to default reset values */
108   LL_EXTI_WriteReg(SWIER2,      0x00000000U);
109   /* Pending register 2 clear */
110   LL_EXTI_WriteReg(PR2,         0x00000078U);
111 
112   return 0x00u;
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   *          - 0x00: EXTI registers are initialized
120   *          - any other calue : wrong configuration
121   */
LL_EXTI_Init(LL_EXTI_InitTypeDef * EXTI_InitStruct)122 uint32_t LL_EXTI_Init(LL_EXTI_InitTypeDef *EXTI_InitStruct)
123 {
124   uint32_t status = 0x00u;
125 
126   /* Check the parameters */
127   assert_param(IS_LL_EXTI_LINE_0_31(EXTI_InitStruct->Line_0_31));
128   assert_param(IS_LL_EXTI_LINE_32_63(EXTI_InitStruct->Line_32_63));
129   assert_param(IS_FUNCTIONAL_STATE(EXTI_InitStruct->LineCommand));
130   assert_param(IS_LL_EXTI_MODE(EXTI_InitStruct->Mode));
131 
132   /* ENABLE LineCommand */
133   if (EXTI_InitStruct->LineCommand != DISABLE)
134   {
135     assert_param(IS_LL_EXTI_TRIGGER(EXTI_InitStruct->Trigger));
136 
137     /* Configure EXTI Lines in range from 0 to 31 */
138     if (EXTI_InitStruct->Line_0_31 != LL_EXTI_LINE_NONE)
139     {
140       switch (EXTI_InitStruct->Mode)
141       {
142         case LL_EXTI_MODE_IT:
143           /* First Disable Event on provided Lines */
144           LL_EXTI_DisableEvent_0_31(EXTI_InitStruct->Line_0_31);
145           /* Then Enable IT on provided Lines */
146           LL_EXTI_EnableIT_0_31(EXTI_InitStruct->Line_0_31);
147           break;
148         case LL_EXTI_MODE_EVENT:
149           /* First Disable IT on provided Lines */
150           LL_EXTI_DisableIT_0_31(EXTI_InitStruct->Line_0_31);
151           /* Then Enable Event on provided Lines */
152           LL_EXTI_EnableEvent_0_31(EXTI_InitStruct->Line_0_31);
153           break;
154         case LL_EXTI_MODE_IT_EVENT:
155           /* Directly Enable IT & Event on provided Lines */
156           LL_EXTI_EnableIT_0_31(EXTI_InitStruct->Line_0_31);
157           LL_EXTI_EnableEvent_0_31(EXTI_InitStruct->Line_0_31);
158           break;
159         default:
160           status = 0x01u;
161           break;
162       }
163       if (EXTI_InitStruct->Trigger != LL_EXTI_TRIGGER_NONE)
164       {
165         switch (EXTI_InitStruct->Trigger)
166         {
167           case LL_EXTI_TRIGGER_RISING:
168             /* First Disable Falling Trigger on provided Lines */
169             LL_EXTI_DisableFallingTrig_0_31(EXTI_InitStruct->Line_0_31);
170             /* Then Enable Rising Trigger on provided Lines */
171             LL_EXTI_EnableRisingTrig_0_31(EXTI_InitStruct->Line_0_31);
172             break;
173           case LL_EXTI_TRIGGER_FALLING:
174             /* First Disable Rising Trigger on provided Lines */
175             LL_EXTI_DisableRisingTrig_0_31(EXTI_InitStruct->Line_0_31);
176             /* Then Enable Falling Trigger on provided Lines */
177             LL_EXTI_EnableFallingTrig_0_31(EXTI_InitStruct->Line_0_31);
178             break;
179           case LL_EXTI_TRIGGER_RISING_FALLING:
180             LL_EXTI_EnableRisingTrig_0_31(EXTI_InitStruct->Line_0_31);
181             LL_EXTI_EnableFallingTrig_0_31(EXTI_InitStruct->Line_0_31);
182             break;
183           default:
184             status |= 0x02u;
185             break;
186         }
187       }
188     }
189     /* Configure EXTI Lines in range from 32 to 63 */
190     if (EXTI_InitStruct->Line_32_63 != LL_EXTI_LINE_NONE)
191     {
192       switch (EXTI_InitStruct->Mode)
193       {
194         case LL_EXTI_MODE_IT:
195           /* First Disable Event on provided Lines */
196           LL_EXTI_DisableEvent_32_63(EXTI_InitStruct->Line_32_63);
197           /* Then Enable IT on provided Lines */
198           LL_EXTI_EnableIT_32_63(EXTI_InitStruct->Line_32_63);
199           break;
200         case LL_EXTI_MODE_EVENT:
201           /* First Disable IT on provided Lines */
202           LL_EXTI_DisableIT_32_63(EXTI_InitStruct->Line_32_63);
203           /* Then Enable Event on provided Lines */
204           LL_EXTI_EnableEvent_32_63(EXTI_InitStruct->Line_32_63);
205           break;
206         case LL_EXTI_MODE_IT_EVENT:
207           /* Directly Enable IT & Event on provided Lines */
208           LL_EXTI_EnableIT_32_63(EXTI_InitStruct->Line_32_63);
209           LL_EXTI_EnableEvent_32_63(EXTI_InitStruct->Line_32_63);
210           break;
211         default:
212           status |= 0x04u;
213           break;
214       }
215       if (EXTI_InitStruct->Trigger != LL_EXTI_TRIGGER_NONE)
216       {
217         switch (EXTI_InitStruct->Trigger)
218         {
219           case LL_EXTI_TRIGGER_RISING:
220             /* First Disable Falling Trigger on provided Lines */
221             LL_EXTI_DisableFallingTrig_32_63(EXTI_InitStruct->Line_32_63);
222             /* Then Enable IT on provided Lines */
223             LL_EXTI_EnableRisingTrig_32_63(EXTI_InitStruct->Line_32_63);
224             break;
225           case LL_EXTI_TRIGGER_FALLING:
226             /* First Disable Rising Trigger on provided Lines */
227             LL_EXTI_DisableRisingTrig_32_63(EXTI_InitStruct->Line_32_63);
228             /* Then Enable Falling Trigger on provided Lines */
229             LL_EXTI_EnableFallingTrig_32_63(EXTI_InitStruct->Line_32_63);
230             break;
231           case LL_EXTI_TRIGGER_RISING_FALLING:
232             LL_EXTI_EnableRisingTrig_32_63(EXTI_InitStruct->Line_32_63);
233             LL_EXTI_EnableFallingTrig_32_63(EXTI_InitStruct->Line_32_63);
234             break;
235           default:
236             status = ERROR;
237             break;
238         }
239       }
240     }
241   }
242   /* DISABLE LineCommand */
243   else
244   {
245     /* De-configure EXTI Lines in range from 0 to 31 */
246     LL_EXTI_DisableIT_0_31(EXTI_InitStruct->Line_0_31);
247     LL_EXTI_DisableEvent_0_31(EXTI_InitStruct->Line_0_31);
248     /* De-configure EXTI Lines in range from 32 to 63 */
249     LL_EXTI_DisableIT_32_63(EXTI_InitStruct->Line_32_63);
250     LL_EXTI_DisableEvent_32_63(EXTI_InitStruct->Line_32_63);
251   }
252 
253   return status;
254 }
255 
256 /**
257   * @brief  Set each @ref LL_EXTI_InitTypeDef field to default value.
258   * @param  EXTI_InitStruct Pointer to a @ref LL_EXTI_InitTypeDef structure.
259   * @retval None
260   */
LL_EXTI_StructInit(LL_EXTI_InitTypeDef * EXTI_InitStruct)261 void LL_EXTI_StructInit(LL_EXTI_InitTypeDef *EXTI_InitStruct)
262 {
263   EXTI_InitStruct->Line_0_31      = LL_EXTI_LINE_NONE;
264   EXTI_InitStruct->Line_32_63     = LL_EXTI_LINE_NONE;
265   EXTI_InitStruct->LineCommand    = DISABLE;
266   EXTI_InitStruct->Mode           = LL_EXTI_MODE_IT;
267   EXTI_InitStruct->Trigger        = LL_EXTI_TRIGGER_FALLING;
268 }
269 
270 /**
271   * @}
272   */
273 
274 /**
275   * @}
276   */
277 
278 /**
279   * @}
280   */
281 
282 #endif /* defined (EXTI) */
283 
284 /**
285   * @}
286   */
287 
288 #endif /* USE_FULL_LL_DRIVER */
289 
290 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
291