1 /*
2   ******************************************************************************
3   * @file    HAL_EXTI.c
4   * @version V1.0.0
5   * @date    2020
6   * @brief   EXTI HAL module driver.
7   *          This file provides firmware functions to manage the following
8   *          functionalities of the General Purpose Input/Output (EXTI) peripheral:
9   *           + Initialization functions
10   *           + IO operation functions
11   ******************************************************************************
12 */
13 #include "ACM32Fxx_HAL.h"
14 
15 /*********************************************************************************
16 * Function    : HAL_EXTI_IRQHandler
17 * Description : Handle EXTI interrupt request.
18 * Input       : huart: EXTI handle.
19 * Output      :
20 * Author      : Chris_Kyle                         Data : 2020年
21 **********************************************************************************/
HAL_EXTI_IRQHandler(EXTI_HandleTypeDef * hexti)22 void HAL_EXTI_IRQHandler(EXTI_HandleTypeDef *hexti)
23 {
24     if (EXTI->PDR & hexti->u32_Line)
25     {
26         EXTI->PDR = hexti->u32_Line;
27     }
28 }
29 
30 /*********************************************************************************
31 * Function    : HAL_EXTI_SetConfigLine
32 * Description :
33 * Input       :
34 * Outpu       :
35 * Author      : Chris_Kyle                         Data : 2020年
36 **********************************************************************************/
HAL_EXTI_SetConfigLine(EXTI_HandleTypeDef * hexti)37 HAL_StatusTypeDef HAL_EXTI_SetConfigLine(EXTI_HandleTypeDef *hexti)
38 {
39     uint32_t lu32_IndexLine;
40 
41 #if (USE_FULL_ASSERT == 1)
42     if (!IS_EXTI_ALL_LINE(hexti->u32_Line))      return HAL_ERROR;
43     if (!IS_EXTI_MODE(hexti->u32_Mode))          return HAL_ERROR;
44     if (!IS_EXTI_TRIGGER(hexti->u32_Trigger))    return HAL_ERROR;
45 
46     /* Line0 ~ 15 trigger from GPIO */
47     if (!(hexti->u32_Line >> 16))
48     {
49         if (!IS_EXTI_GPIOSEL(hexti->u32_GPIOSel))    return HAL_ERROR;
50     }
51 #endif
52 
53     lu32_IndexLine = hexti->u32_Line;
54 
55     /* Interrupt Mode */
56     if (hexti->u32_Mode == EXTI_MODE_INTERRUPT)
57     {
58         EXTI->IENR |=  lu32_IndexLine;
59         EXTI->EENR &= ~lu32_IndexLine;
60 
61         NVIC_ClearPendingIRQ(EXTI_IRQn);
62         NVIC_EnableIRQ(EXTI_IRQn);
63     }
64     /* Event Mode */
65     else if (hexti->u32_Mode == EXTI_MODE_EVENT)
66     {
67         EXTI->EENR |=  lu32_IndexLine;
68         EXTI->IENR &= ~lu32_IndexLine;
69     }
70 
71 
72     if (hexti->u32_Trigger == EXTI_TRIGGER_RISING)
73     {
74         EXTI->RTENR |=  lu32_IndexLine;
75         EXTI->FTENR &= ~lu32_IndexLine;
76     }
77     else if (hexti->u32_Trigger == EXTI_TRIGGER_FALLING)
78     {
79         EXTI->FTENR |=  lu32_IndexLine;
80         EXTI->RTENR &= ~lu32_IndexLine;
81     }
82     else
83     {
84         EXTI->FTENR |= lu32_IndexLine;
85         EXTI->RTENR |= lu32_IndexLine;
86     }
87 
88     /* Line0 ~ 15 trigger from GPIO */
89     if (!(hexti->u32_Line >> 16))
90     {
91         lu32_IndexLine = 0;
92 
93         while(hexti->u32_Line >> lu32_IndexLine != 0x01)
94         {
95             lu32_IndexLine++;
96         }
97 
98         /* Line0 ~ 7 */
99         if (lu32_IndexLine < 8)
100         {
101             EXTI->EXTICR1 = (EXTI->EXTICR1 & ~(0x0F << (lu32_IndexLine * 4))) | hexti->u32_GPIOSel << (lu32_IndexLine * 4);
102         }
103         /* Line8 ~ 15 */
104         else
105         {
106             lu32_IndexLine -= 8;
107 
108             EXTI->EXTICR2 = (EXTI->EXTICR2 & ~(0x0F << (lu32_IndexLine * 4))) | hexti->u32_GPIOSel << (lu32_IndexLine * 4);
109         }
110     }
111 
112     return HAL_OK;
113 }
114 
115 /*********************************************************************************
116 * Function    : HAL_EXTI_SoftTrigger
117 * Description : Software trigger EXTI
118 * Input       :
119 * Outpu       :
120 * Author      : Chris_Kyle                         Data : 2020年
121 **********************************************************************************/
HAL_EXTI_SoftTrigger(EXTI_HandleTypeDef * hexti)122 void HAL_EXTI_SoftTrigger(EXTI_HandleTypeDef *hexti)
123 {
124 #if (USE_FULL_ASSERT == 1)
125     if (!IS_EXTI_ALL_LINE(hexti->u32_Line))    return;
126 #endif
127 
128     /* Set pending BIT */
129     EXTI->SWIER |= hexti->u32_Line;
130 }
131 
132 /*********************************************************************************
133 * Function    : HAL_EXTI_GetPending
134 * Description : Get interrupt pending bit of a dedicated line.
135 * Input       :
136 * Outpu       :
137 * Author      : Chris_Kyle                         Data : 2020年
138 **********************************************************************************/
HAL_EXTI_GetPending(EXTI_HandleTypeDef * hexti)139 bool HAL_EXTI_GetPending(EXTI_HandleTypeDef *hexti)
140 {
141 #if (USE_FULL_ASSERT == 1)
142     if (!IS_EXTI_ALL_LINE(hexti->u32_Line))    return HAL_ERROR;
143 #endif
144 
145     if (hexti->u32_Line & EXTI->PDR)
146     {
147         return true;
148     }
149     else
150     {
151         return false;
152     }
153 }
154 
155 /*********************************************************************************
156 * Function    : HAL_EXTI_ClearPending
157 * Description : Clear interrupt pending bit of a dedicated line.
158 * Input       :
159 * Outpu       :
160 * Author      : Chris_Kyle                         Data : 2020年
161 **********************************************************************************/
HAL_EXTI_ClearPending(EXTI_HandleTypeDef * hexti)162 void HAL_EXTI_ClearPending(EXTI_HandleTypeDef *hexti)
163 {
164 #if (USE_FULL_ASSERT == 1)
165     if (!IS_EXTI_ALL_LINE(hexti->u32_Line))    return;
166 #endif
167 
168     /* Clear pending status */
169     EXTI->PDR |= hexti->u32_Line;
170 }
171 
172 /*********************************************************************************
173 * Function    : HAL_EXTI_ClearAllPending
174 * Description : Clear all interrupt pending bit.
175 * Input       :
176 * Outpu       :
177 * Author      : xwl                         Data : 2021年
178 **********************************************************************************/
HAL_EXTI_ClearAllPending(void)179 void HAL_EXTI_ClearAllPending(void)
180 {
181     /* Clear pending status */
182     EXTI->PDR |= EXTI_LINE_MASK;
183 }
184 
185