1 /********************************** (C) COPYRIGHT  *******************************
2 * File Name          : ch32f20x_exti.c
3 * Author             : WCH
4 * Version            : V1.0.0
5 * Date               : 2021/08/08
6 * Description        : This file provides all the EXTI firmware functions.
7 ***************************************************************************************/
8 #include "ch32f20x_exti.h"
9 
10 /* No interrupt selected */
11 #define EXTI_LINENONE    ((uint32_t)0x00000)
12 
13 /********************************************************************************
14 * Function Name  : EXTI_DeInit
15 * Description    : Deinitializes the EXTI peripheral registers to their default
16 *                  reset values.
17 * Input          : None
18 * Return         : None
19 *********************************************************************************/
EXTI_DeInit(void)20 void EXTI_DeInit(void)
21 {
22   EXTI->INTENR = 0x00000000;
23   EXTI->EVENR = 0x00000000;
24   EXTI->RTENR = 0x00000000;
25   EXTI->FTENR = 0x00000000;
26   EXTI->INTFR = 0x000FFFFF;
27 }
28 
29 /********************************************************************************
30 * Function Name  : EXTI_Init
31 * Description    : Initializes the EXTI peripheral according to the specified
32 *                  parameters in the EXTI_InitStruct.
33 * Input          : EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure
34 * Return         : None
35 *********************************************************************************/
EXTI_Init(EXTI_InitTypeDef * EXTI_InitStruct)36 void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct)
37 {
38   uint32_t tmp = 0;
39 
40   tmp = (uint32_t)EXTI_BASE;
41   if (EXTI_InitStruct->EXTI_LineCmd != DISABLE)
42   {
43     EXTI->INTENR &= ~EXTI_InitStruct->EXTI_Line;
44     EXTI->EVENR &= ~EXTI_InitStruct->EXTI_Line;
45     tmp += EXTI_InitStruct->EXTI_Mode;
46     *(__IO uint32_t *) tmp |= EXTI_InitStruct->EXTI_Line;
47     EXTI->RTENR &= ~EXTI_InitStruct->EXTI_Line;
48     EXTI->FTENR &= ~EXTI_InitStruct->EXTI_Line;
49     if (EXTI_InitStruct->EXTI_Trigger == EXTI_Trigger_Rising_Falling)
50     {
51       EXTI->RTENR |= EXTI_InitStruct->EXTI_Line;
52       EXTI->FTENR |= EXTI_InitStruct->EXTI_Line;
53     }
54     else
55     {
56       tmp = (uint32_t)EXTI_BASE;
57       tmp += EXTI_InitStruct->EXTI_Trigger;
58       *(__IO uint32_t *) tmp |= EXTI_InitStruct->EXTI_Line;
59     }
60   }
61   else
62   {
63     tmp += EXTI_InitStruct->EXTI_Mode;
64     *(__IO uint32_t *) tmp &= ~EXTI_InitStruct->EXTI_Line;
65   }
66 }
67 
68 /********************************************************************************
69 * Function Name  : EXTI_StructInit
70 * Description    : Fills each EXTI_InitStruct member with its reset value.
71 * Input          : EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure
72 * Return         : None
73 *********************************************************************************/
EXTI_StructInit(EXTI_InitTypeDef * EXTI_InitStruct)74 void EXTI_StructInit(EXTI_InitTypeDef* EXTI_InitStruct)
75 {
76   EXTI_InitStruct->EXTI_Line = EXTI_LINENONE;
77   EXTI_InitStruct->EXTI_Mode = EXTI_Mode_Interrupt;
78   EXTI_InitStruct->EXTI_Trigger = EXTI_Trigger_Falling;
79   EXTI_InitStruct->EXTI_LineCmd = DISABLE;
80 }
81 
82 /********************************************************************************
83 * Function Name  : EXTI_GenerateSWInterrupt
84 * Description    : Generates a Software interrupt.
85 * Input          : EXTI_Line: specifies the EXTI lines to be enabled or disabled.
86 * Return         : None
87 *********************************************************************************/
EXTI_GenerateSWInterrupt(uint32_t EXTI_Line)88 void EXTI_GenerateSWInterrupt(uint32_t EXTI_Line)
89 {
90   EXTI->SWIEVR |= EXTI_Line;
91 }
92 
93 /********************************************************************************
94 * Function Name  : EXTI_GetFlagStatus
95 * Description    : Checks whether the specified EXTI line flag is set or not.
96 * Input          : EXTI_Line: specifies the EXTI lines to be enabled or disabled.
97 * Return         : The new state of EXTI_Line (SET or RESET).
98 *********************************************************************************/
EXTI_GetFlagStatus(uint32_t EXTI_Line)99 FlagStatus EXTI_GetFlagStatus(uint32_t EXTI_Line)
100 {
101   FlagStatus bitstatus = RESET;
102   if ((EXTI->INTFR & EXTI_Line) != (uint32_t)RESET)
103   {
104     bitstatus = SET;
105   }
106   else
107   {
108     bitstatus = RESET;
109   }
110   return bitstatus;
111 }
112 
113 /********************************************************************************
114 * Function Name  : EXTI_ClearFlag
115 * Description    : Clears the EXTI's line pending flags.
116 * Input          : EXTI_Line: specifies the EXTI lines to be enabled or disabled.
117 * Return         : None
118 *********************************************************************************/
EXTI_ClearFlag(uint32_t EXTI_Line)119 void EXTI_ClearFlag(uint32_t EXTI_Line)
120 {
121   EXTI->INTFR = EXTI_Line;
122 }
123 
124 /********************************************************************************
125 * Function Name  : EXTI_GetITStatus
126 * Description    : Checks whether the specified EXTI line is asserted or not.
127 * Input          : EXTI_Line: specifies the EXTI lines to be enabled or disabled.
128 * Return         : The new state of EXTI_Line (SET or RESET).
129 *********************************************************************************/
EXTI_GetITStatus(uint32_t EXTI_Line)130 ITStatus EXTI_GetITStatus(uint32_t EXTI_Line)
131 {
132   ITStatus bitstatus = RESET;
133   uint32_t enablestatus = 0;
134 
135   enablestatus =  EXTI->INTENR & EXTI_Line;
136   if (((EXTI->INTFR & EXTI_Line) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET))
137   {
138     bitstatus = SET;
139   }
140   else
141   {
142     bitstatus = RESET;
143   }
144   return bitstatus;
145 }
146 
147 /********************************************************************************
148 * Function Name  : EXTI_ClearITPendingBit
149 * Description    : Clears the EXTI's line pending bits.
150 * Input          : EXTI_Line: specifies the EXTI lines to be enabled or disabled.
151 * Return         : None
152 *********************************************************************************/
EXTI_ClearITPendingBit(uint32_t EXTI_Line)153 void EXTI_ClearITPendingBit(uint32_t EXTI_Line)
154 {
155   EXTI->INTFR = EXTI_Line;
156 }
157 
158