1 /**
2   ******************************************************************************
3   * @file    hk32f0xx_exti.c
4   * @author  MCD Application Team
5   * @version V1.0.1
6   * @date    2019-08-15
7   * @brief   This file provides firmware functions to manage the following
8   *          functionalities of the EXTI peripheral:
9   *           + Initialization and Configuration
10   *           + Interrupts and flags management
11   *
12   *  @verbatim
13   ==============================================================================
14                             ##### EXTI features #####
15   ==============================================================================
16     [..] External interrupt/event lines are mapped as following:
17          (#) All available GPIO pins are connected to the 16 external
18              interrupt/event lines from EXTI0 to EXTI15.
19                        ##### How to use this driver #####
20   ==============================================================================
21     [..] In order to use an I/O pin as an external interrupt source, follow
22          steps below:
23     (#) Configure the I/O in input mode using GPIO_Init()
24     (#) Select the input source pin for the EXTI line using
25         SYSCFG_EXTILineConfig().
26     (#) Select the mode(interrupt, event) and configure the trigger selection
27        (Rising, falling or both) using EXTI_Init(). For the internal interrupt,
28        the trigger selection is not needed( the active edge is always the rising one).
29     (#) Configure NVIC IRQ channel mapped to the EXTI line using NVIC_Init().
30     (#) Optionally, you can generate a software interrupt using the function EXTI_GenerateSWInterrupt().
31     [..]
32     (@) SYSCFG APB clock must be enabled to get write access to SYSCFG_EXTICRx
33       registers using RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
34     @endverbatim
35   *
36   ******************************************************************************
37   */
38 
39 /* Includes ------------------------------------------------------------------*/
40 #include "hk32f0xx_exti.h"
41 
42 /** @addtogroup HK32F0xx_StdPeriph_Driver
43   * @{
44   */
45 
46 /** @defgroup EXTI
47   * @brief EXTI driver modules
48   * @{
49   */
50 
51 /* Private typedef -----------------------------------------------------------*/
52 /* Private define ------------------------------------------------------------*/
53 #define EXTI_LINENONE     ((uint32_t)0x00000)        /* No interrupt selected */
54 
55 /* Private macro -------------------------------------------------------------*/
56 /* Private variables ---------------------------------------------------------*/
57 /* Private function prototypes -----------------------------------------------*/
58 /* Private functions ---------------------------------------------------------*/
59 
60 /** @defgroup EXTI_Private_Functions
61   * @{
62   */
63 
64 /** @defgroup EXTI_Group1 Initialization and Configuration functions
65  *  @brief   Initialization and Configuration functions
66  *
67 @verbatim
68   ==============================================================================
69             ##### Initialization and Configuration functions #####
70   ==============================================================================
71 
72 @endverbatim
73   * @{
74   */
75 
76 /**
77   * @brief  Deinitializes the EXTI peripheral registers to their default reset
78   *         values.
79   * @param  None
80   * @retval None
81   */
EXTI_DeInit(void)82 void EXTI_DeInit(void)
83 {
84     EXTI->IMR = 0x0F940000;
85     EXTI->EMR = 0x00000000;
86     EXTI->RTSR = 0x00000000;
87     EXTI->FTSR = 0x00000000;
88     EXTI->PR = 0x006BFFFF;
89 }
90 
91 /**
92   * @brief  Initializes the EXTI peripheral according to the specified
93   *         parameters in the EXTI_InitStruct.
94   * @param  EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure that
95   *         contains the configuration information for the EXTI peripheral.
96   * @retval None
97   */
EXTI_Init(EXTI_InitTypeDef * EXTI_InitStruct)98 void EXTI_Init(EXTI_InitTypeDef *EXTI_InitStruct)
99 {
100     uint32_t tmp = 0;
101 
102     /* Check the parameters */
103     assert_param(IS_EXTI_MODE(EXTI_InitStruct->EXTI_Mode));
104     assert_param(IS_EXTI_TRIGGER(EXTI_InitStruct->EXTI_Trigger));
105     assert_param(IS_EXTI_LINE(EXTI_InitStruct->EXTI_Line));
106     assert_param(IS_FUNCTIONAL_STATE(EXTI_InitStruct->EXTI_LineCmd));
107 
108     tmp = (uint32_t)EXTI_BASE;
109 
110     if (EXTI_InitStruct->EXTI_LineCmd != DISABLE)
111     {
112         /* Clear EXTI line configuration */
113         EXTI->IMR &= ~EXTI_InitStruct->EXTI_Line;
114         EXTI->EMR &= ~EXTI_InitStruct->EXTI_Line;
115 
116         tmp += EXTI_InitStruct->EXTI_Mode;
117 
118         *(__IO uint32_t *) tmp |= EXTI_InitStruct->EXTI_Line;
119 
120         /* Clear Rising Falling edge configuration */
121         EXTI->RTSR &= ~EXTI_InitStruct->EXTI_Line;
122         EXTI->FTSR &= ~EXTI_InitStruct->EXTI_Line;
123 
124         /* Select the trigger for the selected interrupts */
125         if (EXTI_InitStruct->EXTI_Trigger == EXTI_Trigger_Rising_Falling)
126         {
127             /* Rising Falling edge */
128             EXTI->RTSR |= EXTI_InitStruct->EXTI_Line;
129             EXTI->FTSR |= EXTI_InitStruct->EXTI_Line;
130         }
131         else
132         {
133             tmp = (uint32_t)EXTI_BASE;
134             tmp += EXTI_InitStruct->EXTI_Trigger;
135 
136             *(__IO uint32_t *) tmp |= EXTI_InitStruct->EXTI_Line;
137         }
138     }
139     else
140     {
141         tmp += EXTI_InitStruct->EXTI_Mode;
142 
143         /* Disable the selected external lines */
144         *(__IO uint32_t *) tmp &= ~EXTI_InitStruct->EXTI_Line;
145     }
146 }
147 
148 /**
149   * @brief  Fills each EXTI_InitStruct member with its reset value.
150   * @param  EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure which will
151   *         be initialized.
152   * @retval None
153   */
EXTI_StructInit(EXTI_InitTypeDef * EXTI_InitStruct)154 void EXTI_StructInit(EXTI_InitTypeDef *EXTI_InitStruct)
155 {
156     EXTI_InitStruct->EXTI_Line = EXTI_LINENONE;
157     EXTI_InitStruct->EXTI_Mode = EXTI_Mode_Interrupt;
158     EXTI_InitStruct->EXTI_Trigger = EXTI_Trigger_Falling;
159     EXTI_InitStruct->EXTI_LineCmd = DISABLE;
160 }
161 
162 /**
163   * @brief  Generates a Software interrupt on selected EXTI line.
164   * @param  EXTI_Line: specifies the EXTI line on which the software interrupt
165   *         will be generated.
166   *          This parameter can be any combination of EXTI_Linex where x can be (0..27).
167   * @retval None
168   */
EXTI_GenerateSWInterrupt(uint32_t EXTI_Line)169 void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line)
170 {
171     /* Check the parameters */
172     assert_param(IS_EXTI_LINE(EXTI_Line));
173 
174     EXTI->SWIER |= EXTI_Line;
175 }
176 
177 /**
178   * @}
179   */
180 
181 /** @defgroup EXTI_Group2 Interrupts and flags management functions
182  *  @brief    Interrupts and flags management functions
183  *
184 @verbatim
185   ==============================================================================
186              ##### Interrupts and flags management functions #####
187   ==============================================================================
188 
189 @endverbatim
190   * @{
191   */
192 
193 /**
194   * @brief  Checks whether the specified EXTI line flag is set or not.
195   * @param  EXTI_Line: specifies the EXTI line flag to check.
196   *          This parameter can be EXTI_Linex where x can be (0..27).
197   * @retval The new state of EXTI_Line (SET or RESET).
198   */
EXTI_GetFlagStatus(uint32_t EXTI_Line)199 FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line)
200 {
201     FlagStatus bitstatus = RESET;
202     /* Check the parameters */
203     assert_param(IS_GET_EXTI_LINE(EXTI_Line));
204 
205     if ((EXTI->PR & EXTI_Line) != (uint32_t)RESET)
206     {
207         bitstatus = SET;
208     }
209     else
210     {
211         bitstatus = RESET;
212     }
213     return bitstatus;
214 }
215 
216 /**
217   * @brief  Clears the EXTI's line pending flags.
218   * @param  EXTI_Line: specifies the EXTI lines flags to clear.
219   *          This parameter can be any combination of EXTI_Linex where x can be (0..27).
220   * @retval None
221   */
EXTI_ClearFlag(uint32_t EXTI_Line)222 void EXTI_ClearFlag(uint32_t EXTI_Line)
223 {
224     /* Check the parameters */
225     assert_param(IS_EXTI_LINE(EXTI_Line));
226 
227     EXTI->PR = EXTI_Line;
228 }
229 
230 /**
231   * @brief  Checks whether the specified EXTI line is asserted or not.
232   * @param  EXTI_Line: specifies the EXTI line to check.
233   *          This parameter can be EXTI_Linex where x can be (0..27).
234   * @retval The new state of EXTI_Line (SET or RESET).
235   */
EXTI_GetITStatus(uint32_t EXTI_Line)236 ITStatus EXTI_GetITStatus(uint32_t EXTI_Line)
237 {
238     ITStatus bitstatus = RESET;
239 
240     /* Check the parameters */
241     assert_param(IS_GET_EXTI_LINE(EXTI_Line));
242 
243     if ((EXTI->PR & EXTI_Line) != (uint32_t)RESET)
244     {
245         bitstatus = SET;
246     }
247     else
248     {
249         bitstatus = RESET;
250     }
251     return bitstatus;
252 }
253 
254 /**
255   * @brief  Clears the EXTI's line pending bits.
256   * @param  EXTI_Line: specifies the EXTI lines to clear.
257   *          This parameter can be any combination of EXTI_Linex where x can be (0..27).
258   * @retval None
259   */
EXTI_ClearITPendingBit(uint32_t EXTI_Line)260 void EXTI_ClearITPendingBit(uint32_t EXTI_Line)
261 {
262     /* Check the parameters */
263     assert_param(IS_EXTI_LINE(EXTI_Line));
264 
265     EXTI->PR = EXTI_Line;
266 }
267 
268 /**
269   * @}
270   */
271 
272 /**
273   * @}
274   */
275 
276 /**
277   * @}
278   */
279 
280 /**
281   * @}
282   */
283