• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Talkweb Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #if defined(USE_FULL_LL_DRIVER)
17 
18 #include "hal_exti.h"
19 #include "hal_gpio.h"
20 #include "stm32f4xx_ll_system.h"
21 
22 #if defined (EXTI)
23 
24 Pin_ST g_pinsGroup[STM32_GPIO_GROUP_MAX*STM32_GPIO_PIN_MAX] = {0};
25 static uint16_t g_exitSetupCounts = 0;
26 
27 static const uint32_t g_sysCfgExitLineMap[] = {
28     LL_SYSCFG_EXTI_LINE0,
29     LL_SYSCFG_EXTI_LINE1,
30     LL_SYSCFG_EXTI_LINE2,
31     LL_SYSCFG_EXTI_LINE3,
32     LL_SYSCFG_EXTI_LINE4,
33     LL_SYSCFG_EXTI_LINE5,
34     LL_SYSCFG_EXTI_LINE6,
35     LL_SYSCFG_EXTI_LINE7,
36     LL_SYSCFG_EXTI_LINE8,
37     LL_SYSCFG_EXTI_LINE9,
38     LL_SYSCFG_EXTI_LINE10,
39     LL_SYSCFG_EXTI_LINE11,
40     LL_SYSCFG_EXTI_LINE12,
41     LL_SYSCFG_EXTI_LINE13,
42     LL_SYSCFG_EXTI_LINE14,
43     LL_SYSCFG_EXTI_LINE15
44 };
45 
46 #define PIN_EXIT_ZERO 0
47 #define PIN_EXIT_FIVE 5
48 #define PIN_EXIT_TEN  10
49 #define PIN_EXIT_SIXTEEN  16
50 
LL_Gpio_Exti_Handler(void)51 static void LL_Gpio_Exti_Handler(void)
52 {
53     uint32_t pinIndex;
54     ITStatus status = RESET;
55     for (uint16_t index = 0; index < g_exitSetupCounts; index++) {
56         if (g_pinsGroup[index].setup) {
57             if (g_pinsGroup[index].trigger == LL_EXTI_TRIGGER_FALLING) {
58                 status = RESET;
59             } else {
60                 status = SET;
61             }
62             if (LL_EXTI_IsActiveFlag_0_31(g_pinsGroup[index].exitLine) != status) {
63                 LL_EXTI_ClearFlag_0_31(g_pinsGroup[index].exitLine);
64                 if (LL_GPIO_IsInputPinSet(g_pinsGroup[index].gpiox, g_pinsGroup[index].pinReg) == status) {
65                     g_pinsGroup[index].handler(g_pinsGroup[index].localPin);
66                     break;
67                 }
68             }
69         }
70     }
71 }
72 
LL_SETUP_EXTI(LL_EXTI_InitConfig * cfg,uint16_t pin,uint16_t local,uint8_t group)73 uint32_t LL_SETUP_EXTI(LL_EXTI_InitConfig* cfg, uint16_t pin, uint16_t local, uint8_t group)
74 {
75     ErrorStatus status = SUCCESS;
76     uint32_t ret = SUCCESS;
77     if (cfg == NULL) {
78         status = ERROR;
79         return status;
80     }
81     LL_SYSCFG_SetEXTISource(group, g_sysCfgExitLineMap[pin]);
82 
83     status = LL_EXTI_Init(&cfg->initType);
84     if (status != SUCCESS) {
85         return status;
86     }
87 
88     if (cfg->initType.LineCommand) {
89         g_pinsGroup[g_exitSetupCounts].setup = SET;
90     } else {
91         g_pinsGroup[g_exitSetupCounts].setup= RESET;
92     }
93     g_pinsGroup[g_exitSetupCounts].pin = pin;
94     g_pinsGroup[g_exitSetupCounts].localPin = local;
95     g_pinsGroup[g_exitSetupCounts].exitLine = cfg->initType.Line_0_31;
96     g_pinsGroup[g_exitSetupCounts].group = group;
97     g_pinsGroup[g_exitSetupCounts].handler = cfg->Exithandler;
98     g_pinsGroup[g_exitSetupCounts].trigger = cfg->initType.Trigger;
99     g_pinsGroup[g_exitSetupCounts].pinReg = cfg->PinReg;
100     g_pinsGroup[g_exitSetupCounts].gpiox = cfg->Gpiox;
101     if (pin < PIN_EXIT_FIVE) {
102         NVIC_SetVector(EXTI0_IRQn + pin, (uint32_t)LL_Gpio_Exti_Handler);
103         NVIC_SetPriority(EXTI0_IRQn + pin, 0);
104         NVIC_EnableIRQ(EXTI0_IRQn + pin);
105     } else if (pin >= PIN_EXIT_FIVE && pin < PIN_EXIT_TEN) {
106         NVIC_SetVector(EXTI9_5_IRQn, (uint32_t)LL_Gpio_Exti_Handler);
107         NVIC_SetPriority(EXTI9_5_IRQn, 0);
108         NVIC_EnableIRQ(EXTI9_5_IRQn);
109     } else if (pin >= PIN_EXIT_TEN && pin < PIN_EXIT_SIXTEEN) {
110         NVIC_SetVector(EXTI15_10_IRQn, (uint32_t)LL_Gpio_Exti_Handler);
111         NVIC_SetPriority(EXTI15_10_IRQn, 0);
112         NVIC_EnableIRQ(EXTI15_10_IRQn);
113     }
114     g_exitSetupCounts++;
115 
116     if (ret != 0) {
117         status = ret;
118     }
119 
120     return status;
121 }
122 
123 #endif /* defined (EXTI) */
124 
125 #endif /* USE_FULL_LL_DRIVER */
126