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