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