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