1 ////////////////////////////////////////////////////////////////////////////////
2 /// @file     hal_exti.c
3 /// @author   AE TEAM
4 /// @brief    THIS FILE PROVIDES ALL THE EXTI FIRMWARE FUNCTIONS.
5 ////////////////////////////////////////////////////////////////////////////////
6 /// @attention
7 ///
8 /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
9 /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
10 /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
11 /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
12 /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
13 /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
14 ///
15 /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
16 ////////////////////////////////////////////////////////////////////////////////
17 
18 // Define to prevent recursive inclusion
19 #define _HAL_EXTI_C_
20 
21 // Files includes
22 #include "hal_exti.h"
23 
24 ////////////////////////////////////////////////////////////////////////////////
25 /// @addtogroup MM32_Hardware_Abstract_Layer
26 /// @{
27 
28 ////////////////////////////////////////////////////////////////////////////////
29 /// @addtogroup EXTI_HAL
30 /// @{
31 
32 ////////////////////////////////////////////////////////////////////////////////
33 /// @addtogroup EXTI_Exported_Functions
34 /// @{
35 
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// @brief  Deinitializes the EXTI peripheral registers to their default reset
39 ///         values.
40 /// @param  None.
41 /// @retval None.
42 ////////////////////////////////////////////////////////////////////////////////
43 
44 ////////////////////////////////////////////////////////////////////////////////
45 /// @brief  Deinitializes the EXTI registers to their default reset values.
46 /// @param  None.
47 /// @retval None.
48 /// @note   MEM_MODE bits are not affected by APB reset.
49 /// @note   MEM_MODE bits took the value from the user option bytes.
50 /// @note   CFGR2 register is not affected by APB reset.
51 /// @note   CLABBB configuration bits are locked when set.
52 /// @note   To unlock the configuration, perform a system reset.
53 ////////////////////////////////////////////////////////////////////////////////
EXTI_DeInit(void)54 void EXTI_DeInit(void)
55 {
56     u16 i;
57     // Clear all
58     exEXTI_LineDisable(~0x00000000);
59 
60     // rc_w1
61     EXTI->PR = EXTI->PR;
62 
63     // Set EXTI_CFGR1 register to reset value without affecting MEM_MODE bits
64     EXTI->CFGR &= EXTI_CFGR_MEMMODE;
65 
66     // Set EXTICRx registers to reset value
67     for (i = 0; i < 4; i++) {
68         EXTI->CR[i] = 0;
69     }
70 }
71 
72 
73 
74 ////////////////////////////////////////////////////////////////////////////////
75 /// @brief  Selects the GPIO pin used as EXTI Line.
76 /// @param  port_source_gpio: selects the GPIO port to be used as source for EXTI lines .
77 /// @param  pin_source: specifies the EXTI line to be configured.
78 /// @note   This parameter can be pin_source where x can be:
79 ///         For MCU: (0..15) for GPIOA, GPIOB, (13..15) for GPIOC and (0..1, 6..7) for GPIOD.
80 /// @retval None.
81 ////////////////////////////////////////////////////////////////////////////////
EXTI_LineConfig(u8 port_source_gpio,u8 pin_source)82 void EXTI_LineConfig(u8 port_source_gpio, u8 pin_source)
83 {
84     EXTI->CR[pin_source >> 0x02] &= ~(0x0F << (0x04 * (pin_source & 0x03)));
85     EXTI->CR[pin_source >> 0x02] |= ((port_source_gpio) << (0x04 * (pin_source & 0x03)));
86 }
87 
88 ////////////////////////////////////////////////////////////////////////////////
89 /// @brief  Initializes the EXTI peripheral according to the specified
90 ///         parameters in the init_struct.
91 /// @param  init_struct: pointer to a EXTI_InitTypeDef structure that
92 ///         contains the configuration information for the EXTI peripheral.
93 /// @retval None.
94 ////////////////////////////////////////////////////////////////////////////////
EXTI_Init(EXTI_InitTypeDef * init_struct)95 void EXTI_Init(EXTI_InitTypeDef* init_struct)
96 {
97     if (init_struct->EXTI_LineCmd != DISABLE) {
98         EXTI->IMR  &= ~init_struct->EXTI_Line;
99         EXTI->EMR  &= ~init_struct->EXTI_Line;
100         if (init_struct->EXTI_Mode == EXTI_Mode_Interrupt) {
101             EXTI->IMR |= init_struct->EXTI_Line;
102         }
103         else {
104             EXTI->EMR |= init_struct->EXTI_Line;
105         }
106         EXTI->RTSR &= ~init_struct->EXTI_Line;
107         EXTI->FTSR &= ~init_struct->EXTI_Line;
108         if (init_struct->EXTI_Trigger == EXTI_Trigger_Rising_Falling) {
109             EXTI->RTSR |= init_struct->EXTI_Line;
110             EXTI->FTSR |= init_struct->EXTI_Line;                               // Rising and Faling    afio
111         }
112         else if (init_struct->EXTI_Trigger == EXTI_Trigger_Rising) {
113             EXTI->RTSR |= init_struct->EXTI_Line;
114         }
115         else {
116             EXTI->FTSR |= init_struct->EXTI_Line;
117         }
118     }
119     else {
120         if (init_struct->EXTI_Mode == EXTI_Mode_Interrupt) {
121             EXTI->IMR &= ~init_struct->EXTI_Line;
122         }
123         else {
124             EXTI->EMR &= ~init_struct->EXTI_Line;
125         }
126     }
127 }
128 
129 ////////////////////////////////////////////////////////////////////////////////
130 /// @brief  Fills each init_struct member with its reset value.
131 /// @param  init_struct: pointer to a EXTI_InitTypeDef structure which will
132 ///         be initialized.
133 /// @retval None.
134 ////////////////////////////////////////////////////////////////////////////////
EXTI_StructInit(EXTI_InitTypeDef * init_struct)135 void EXTI_StructInit(EXTI_InitTypeDef* init_struct)
136 {
137     init_struct->EXTI_Line    = EXTI_LineNone;
138     init_struct->EXTI_Mode    = EXTI_Mode_Interrupt;
139     init_struct->EXTI_Trigger = EXTI_Trigger_Falling;
140     init_struct->EXTI_LineCmd = DISABLE;
141 }
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 /// @brief  Generates a Software interrupt on selected EXTI line.
145 /// @param  line: specifies the EXTI line on which the software interrupt
146 ///         will be generated.
147 /// @retval None.
148 ////////////////////////////////////////////////////////////////////////////////
EXTI_GenerateSWInterrupt(u32 line)149 void EXTI_GenerateSWInterrupt(u32 line)
150 {
151     EXTI->SWIER |= line;
152 }
153 
154 ////////////////////////////////////////////////////////////////////////////////
155 /// @brief  Checks whether the specified EXTI line flag is set or not.
156 /// @param  line: specifies the EXTI line flag to check.
157 /// @retval The new state of line (SET or RESET).
158 ////////////////////////////////////////////////////////////////////////////////
EXTI_GetFlagStatus(u32 line)159 FlagStatus EXTI_GetFlagStatus(u32 line)
160 {
161     return (EXTI->PR & line) ? SET : RESET;
162 }
163 
164 ////////////////////////////////////////////////////////////////////////////////
165 /// @brief  Clears the EXTI's line pending flags.
166 /// @param  line: specifies the EXTI lines flags to clear.
167 /// @retval None.
168 ////////////////////////////////////////////////////////////////////////////////
EXTI_ClearFlag(u32 line)169 void EXTI_ClearFlag(u32 line)
170 {
171     EXTI->PR = line;
172 }
173 
174 ////////////////////////////////////////////////////////////////////////////////
175 /// @brief  Checks whether the specified EXTI line is asserted or not.
176 /// @param  line: specifies the EXTI line to check.
177 /// @retval The new state of line (SET or RESET).
178 ////////////////////////////////////////////////////////////////////////////////
EXTI_GetITStatus(u32 line)179 ITStatus EXTI_GetITStatus(u32 line)
180 {
181     return ((EXTI->PR & line) && (EXTI->IMR & line)) ? SET : RESET;
182 }
183 
184 ////////////////////////////////////////////////////////////////////////////////
185 /// @brief  Clears the EXTI's line pending bits.
186 /// @param  line: specifies the EXTI lines to clear.
187 /// @retval None.
188 ////////////////////////////////////////////////////////////////////////////////
EXTI_ClearITPendingBit(u32 line)189 void EXTI_ClearITPendingBit(u32 line)
190 {
191     EXTI->PR = line;
192 }
193 
194 ////////////////////////////////////////////////////////////////////////////////
195 /// @brief  EXTI Line Disable
196 /// @param  line: specifies the EXTI lines to clear.
197 /// @retval None.
198 ////////////////////////////////////////////////////////////////////////////////
exEXTI_LineDisable(u32 line)199 void exEXTI_LineDisable(u32 line)
200 {
201     EXTI->IMR  &= ~line;
202     EXTI->EMR  &= ~line;
203     EXTI->RTSR &= ~line;
204     EXTI->FTSR &= ~line;
205 }
206 
207 ////////////////////////////////////////////////////////////////////////////////
208 /// @brief  Clears the EXTI's line all pending bits.
209 /// @param  None.
210 /// @retval None.
211 ////////////////////////////////////////////////////////////////////////////////
exEXTI_GetAllFlagStatus(void)212 u32 exEXTI_GetAllFlagStatus(void)
213 {
214     return EXTI->PR;
215 }
216 
217 /// @}
218 
219 /// @}
220 
221 /// @}
222 
223