1 /********************************** (C) COPYRIGHT *******************************
2 * File Name : ch32f20x_wwdg.c
3 * Author : WCH
4 * Version : V1.0.0
5 * Date : 2021/08/08
6 * Description : This file provides all the WWDG firmware functions.
7 **********************************************************************************/
8 #include "ch32f20x_wwdg.h"
9 #include "ch32f20x_rcc.h"
10
11 /* CTLR register bit mask */
12 #define CTLR_WDGA_Set ((uint32_t)0x00000080)
13
14 /* CFGR register bit mask */
15 #define CFGR_WDGTB_Mask ((uint32_t)0xFFFFFE7F)
16 #define CFGR_W_Mask ((uint32_t)0xFFFFFF80)
17 #define BIT_Mask ((uint8_t)0x7F)
18
19 /********************************************************************************
20 * Function Name : WWDG_DeInit
21 * Description : Deinitializes the WWDG peripheral registers to their default reset values
22 * Input : None
23 * Return : None
24 *********************************************************************************/
WWDG_DeInit(void)25 void WWDG_DeInit(void)
26 {
27 RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG, ENABLE);
28 RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG, DISABLE);
29 }
30
31 /********************************************************************************
32 * Function Name : WWDG_SetPrescaler
33 * Description : Sets the WWDG Prescaler
34 * Input : WWDG_Prescaler: specifies the WWDG Prescaler
35 * WWDG_Prescaler_1: WWDG counter clock = (PCLK1/4096)/1
36 * WWDG_Prescaler_2: WWDG counter clock = (PCLK1/4096)/2
37 * WWDG_Prescaler_4: WWDG counter clock = (PCLK1/4096)/4
38 * WWDG_Prescaler_8: WWDG counter clock = (PCLK1/4096)/8
39 * Return : None
40 *********************************************************************************/
WWDG_SetPrescaler(uint32_t WWDG_Prescaler)41 void WWDG_SetPrescaler(uint32_t WWDG_Prescaler)
42 {
43 uint32_t tmpreg = 0;
44 tmpreg = WWDG->CFGR & CFGR_WDGTB_Mask;
45 tmpreg |= WWDG_Prescaler;
46 WWDG->CFGR = tmpreg;
47 }
48
49 /********************************************************************************
50 * Function Name : WWDG_SetWindowValue
51 * Description : Sets the WWDG window value
52 * Input : WindowValue: specifies the window value to be compared to the
53 * downcounter,which must be lower than 0x80
54 * Return : None
55 *********************************************************************************/
WWDG_SetWindowValue(uint8_t WindowValue)56 void WWDG_SetWindowValue(uint8_t WindowValue)
57 {
58 __IO uint32_t tmpreg = 0;
59
60 tmpreg = WWDG->CFGR & CFGR_W_Mask;
61
62 tmpreg |= WindowValue & (uint32_t) BIT_Mask;
63
64 WWDG->CFGR = tmpreg;
65 }
66
67 /********************************************************************************
68 * Function Name : WWDG_EnableIT
69 * Description : Enables the WWDG Early Wakeup interrupt(EWI)
70 * Input : None
71 * Return : None
72 *********************************************************************************/
WWDG_EnableIT(void)73 void WWDG_EnableIT(void)
74 {
75 WWDG->CFGR |= (1<<9);
76 }
77
78 /********************************************************************************
79 * Function Name : WWDG_SetCounter
80 * Description : Sets the WWDG counter value
81 * Input : Counter: specifies the watchdog counter value,which must be a
82 * number between 0x40 and 0x7F
83 * Return : None
84 *********************************************************************************/
WWDG_SetCounter(uint8_t Counter)85 void WWDG_SetCounter(uint8_t Counter)
86 {
87 WWDG->CTLR = Counter & BIT_Mask;
88 }
89
90
91 /********************************************************************************
92 * Function Name : WWDG_Enable
93 * Description : Enables WWDG and load the counter value
94 * Input : Counter: specifies the watchdog counter value,which must be a
95 * number between 0x40 and 0x7F
96 * Return : None
97 *********************************************************************************/
WWDG_Enable(uint8_t Counter)98 void WWDG_Enable(uint8_t Counter)
99 {
100 WWDG->CTLR = CTLR_WDGA_Set | Counter;
101 }
102
103 /********************************************************************************
104 * Function Name : WWDG_GetFlagStatus
105 * Description : Checks whether the Early Wakeup interrupt flag is set or not
106 * Input : None
107 * Return : The new state of the Early Wakeup interrupt flag (SET or RESET)
108 *********************************************************************************/
WWDG_GetFlagStatus(void)109 FlagStatus WWDG_GetFlagStatus(void)
110 {
111 return (FlagStatus)(WWDG->STATR);
112 }
113
114 /********************************************************************************
115 * Function Name : WWDG_ClearFlag
116 * Description : Clears Early Wakeup interrupt flag
117 * Input : None
118 * Return : None
119 *********************************************************************************/
WWDG_ClearFlag(void)120 void WWDG_ClearFlag(void)
121 {
122 WWDG->STATR = (uint32_t)RESET;
123 }
124