1 /***************COPYRIGHT(C)  2019 WCH. A11 rights reserved*********************
2 * File Name          : ch32f10x_iwdg.c
3 * Author             : WCH
4 * Version            : V1.0.0
5 * Date               : 2019/10/15
6 * Description        : This file provides all the IWDG firmware functions.
7 *******************************************************************************/
8 #include "ch32f10x_iwdg.h"
9 
10 /* CTLR register bit mask */
11 #define CTLR_KEY_Reload    ((uint16_t)0xAAAA)
12 #define CTLR_KEY_Enable    ((uint16_t)0xCCCC)
13 
14 /*******************************************************************************
15 * Function Name  : IWDG_WriteAccessCmd
16 * Description    : Enables or disables write access to IWDG_PR and IWDG_RLR registers.
17 * Input          : WDG_WriteAccess: new state of write access to IWDG_PR and
18 *      IWDG_RLR registers.
19 *                    IWDG_WriteAccess_Enable: Enable write access to IWDG_PR and
20 *      IWDG_RLR registers.
21 *                    IWDG_WriteAccess_Disable: Disable write access to IWDG_PR
22 *      and IWDG_RLR registers.
23 * Return         : None
24 *******************************************************************************/
IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess)25 void IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess)
26 {
27   IWDG->CTLR = IWDG_WriteAccess;
28 }
29 
30 /*******************************************************************************
31 * Function Name  : IWDG_SetPrescaler
32 * Description    : Sets IWDG Prescaler value.
33 * Input          : IWDG_Prescaler: specifies the IWDG Prescaler value.
34 *                    IWDG_Prescaler_4: IWDG prescaler set to 4.
35 *                    IWDG_Prescaler_8: IWDG prescaler set to 8.
36 *                    IWDG_Prescaler_16: IWDG prescaler set to 16.
37 *                    IWDG_Prescaler_32: IWDG prescaler set to 32.
38 *                    IWDG_Prescaler_64: IWDG prescaler set to 64.
39 *                    IWDG_Prescaler_128: IWDG prescaler set to 128.
40 *                    IWDG_Prescaler_256: IWDG prescaler set to 256.
41 * Return         : None
42 *******************************************************************************/
IWDG_SetPrescaler(uint8_t IWDG_Prescaler)43 void IWDG_SetPrescaler(uint8_t IWDG_Prescaler)
44 {
45   IWDG->PSCR = IWDG_Prescaler;
46 }
47 
48 /*******************************************************************************
49 * Function Name  : IWDG_SetReload
50 * Description    : Sets IWDG Reload value.
51 * Input          : Reload: specifies the IWDG Reload value.
52 *                    This parameter must be a number between 0 and 0x0FFF.
53 * Return         : None
54 *******************************************************************************/
IWDG_SetReload(uint16_t Reload)55 void IWDG_SetReload(uint16_t Reload)
56 {
57   IWDG->RLDR = Reload;
58 }
59 
60 /*******************************************************************************
61 * Function Name  : IWDG_ReloadCounter
62 * Description    : Reloads IWDG counter with value defined in the reload register.
63 * Input          : None
64 * Return         : None
65 *******************************************************************************/
IWDG_ReloadCounter(void)66 void IWDG_ReloadCounter(void)
67 {
68   IWDG->CTLR = CTLR_KEY_Reload;
69 }
70 
71 /*******************************************************************************
72 * Function Name  : IWDG_Enable
73 * Description    : Enables IWDG (write access to IWDG_PR and IWDG_RLR registers disabled).
74 * Input          : None
75 * Return         : None
76 *******************************************************************************/
IWDG_Enable(void)77 void IWDG_Enable(void)
78 {
79   IWDG->CTLR = CTLR_KEY_Enable;
80 }
81 
82 /*******************************************************************************
83 * Function Name  : IWDG_GetFlagStatus
84 * Description    : Checks whether the specified IWDG flag is set or not.
85 * Input          : IWDG_FLAG: specifies the flag to check.
86 *                    IWDG_FLAG_PVU: Prescaler Value Update on going.
87 *                    IWDG_FLAG_RVU: Reload Value Update on going.
88 * Return         : FlagStatus: SET or RESET.
89 *******************************************************************************/
IWDG_GetFlagStatus(uint16_t IWDG_FLAG)90 FlagStatus IWDG_GetFlagStatus(uint16_t IWDG_FLAG)
91 {
92   FlagStatus bitstatus = RESET;
93 
94   if ((IWDG->STATR & IWDG_FLAG) != (uint32_t)RESET)
95   {
96     bitstatus = SET;
97   }
98   else
99   {
100     bitstatus = RESET;
101   }
102 
103   return bitstatus;
104 }
105 
106 
107 
108 
109 
110