1 /**
2   ******************************************************************************
3   * @file               ft32f0xx_wwdg.c
4   * @author             FMD AE
5   * @brief              This file provides firmware functions to manage the following
6   *                     functionalities of the Window watchdog (WWDG) peripheral:
7   *                 + Prescaler, Refresh window and Counter configuration
8   *                 + WWDG activation
9   *                 + Interrupts and flags management
10   * @version            V1.0.0
11   * @data                   2021-07-01
12     ******************************************************************************
13   */
14 
15 /* Includes ------------------------------------------------------------------*/
16 #include "ft32f0xx_wwdg.h"
17 #include "ft32f0xx_rcc.h"
18 /* --------------------- WWDG registers bit mask ---------------------------- */
19 /* CFR register bit mask */
20 #define CFR_WDGTB_MASK    ((uint32_t)0xFFFFFE7F)
21 #define CFR_W_MASK        ((uint32_t)0xFFFFFF80)
22 #define BIT_MASK          ((uint8_t)0x7F)
23 
24 
25 /**
26   * @brief  Deinitializes the WWDG peripheral registers to their default reset values.
27   * @param  None
28   * @retval None
29   */
WWDG_DeInit(void)30 void WWDG_DeInit(void)
31 {
32   RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG, ENABLE);
33   RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG, DISABLE);
34 }
35 
36 /**
37   * @brief  Sets the WWDG Prescaler.
38   * @param  WWDG_Prescaler: specifies the WWDG Prescaler.
39   *          This parameter can be one of the following values:
40   *            @arg WWDG_Prescaler_1: WWDG counter clock = (PCLK1/4096)/1
41   *            @arg WWDG_Prescaler_2: WWDG counter clock = (PCLK1/4096)/2
42   *            @arg WWDG_Prescaler_4: WWDG counter clock = (PCLK1/4096)/4
43   *            @arg WWDG_Prescaler_8: WWDG counter clock = (PCLK1/4096)/8
44   * @retval None
45   */
WWDG_SetPrescaler(uint32_t WWDG_Prescaler)46 void WWDG_SetPrescaler(uint32_t WWDG_Prescaler)
47 {
48   uint32_t tmpreg = 0;
49   /* Check the parameters */
50   assert_param(IS_WWDG_PRESCALER(WWDG_Prescaler));
51   /* Clear WDGTB[1:0] bits */
52   tmpreg = WWDG->CFR & CFR_WDGTB_MASK;
53   /* Set WDGTB[1:0] bits according to WWDG_Prescaler value */
54   tmpreg |= WWDG_Prescaler;
55   /* Store the new value */
56   WWDG->CFR = tmpreg;
57 }
58 
59 /**
60   * @brief  Sets the WWDG window value.
61   * @param  WindowValue: specifies the window value to be compared to the downcounter.
62   *          This parameter value must be lower than 0x80.
63   * @retval None
64   */
WWDG_SetWindowValue(uint8_t WindowValue)65 void WWDG_SetWindowValue(uint8_t WindowValue)
66 {
67   __IO uint32_t tmpreg = 0;
68 
69   /* Check the parameters */
70   assert_param(IS_WWDG_WINDOW_VALUE(WindowValue));
71   /* Clear W[6:0] bits */
72 
73   tmpreg = WWDG->CFR & CFR_W_MASK;
74 
75   /* Set W[6:0] bits according to WindowValue value */
76   tmpreg |= WindowValue & (uint32_t) BIT_MASK;
77 
78   /* Store the new value */
79   WWDG->CFR = tmpreg;
80 }
81 
82 /**
83   * @brief  Enables the WWDG Early Wakeup interrupt(EWI).
84   * @note   Once enabled this interrupt cannot be disabled except by a system reset.
85   * @param  None
86   * @retval None
87   */
WWDG_EnableIT(void)88 void WWDG_EnableIT(void)
89 {
90   WWDG->CFR |= WWDG_CFR_EWI;
91 }
92 
93 /**
94   * @brief  Sets the WWDG counter value.
95   * @param  Counter: specifies the watchdog counter value.
96   *          This parameter must be a number between 0x40 and 0x7F (to prevent
97   *          generating an immediate reset).
98   * @retval None
99   */
WWDG_SetCounter(uint8_t Counter)100 void WWDG_SetCounter(uint8_t Counter)
101 {
102   /* Check the parameters */
103   assert_param(IS_WWDG_COUNTER(Counter));
104   /* Write to T[6:0] bits to configure the counter value, no need to do
105      a read-modify-write; writing a 0 to WDGA bit does nothing */
106   WWDG->CR = Counter & BIT_MASK;
107 }
108 
109 /**
110   * @}
111   */
112 
113 /**
114   * @brief  Enables WWDG and load the counter value.
115   * @param  Counter: specifies the watchdog counter value.
116   *          This parameter must be a number between 0x40 and 0x7F (to prevent
117   *          generating an immediate reset).
118   * @retval None
119   */
WWDG_Enable(uint8_t Counter)120 void WWDG_Enable(uint8_t Counter)
121 {
122   /* Check the parameters */
123   assert_param(IS_WWDG_COUNTER(Counter));
124   WWDG->CR = WWDG_CR_WDGA | Counter;
125 }
126 
127 /**
128   * @}
129   */
130 
131 /**
132   * @brief  Checks whether the Early Wakeup interrupt flag is set or not.
133   * @param  None
134   * @retval The new state of the Early Wakeup interrupt flag (SET or RESET).
135   */
WWDG_GetFlagStatus(void)136 FlagStatus WWDG_GetFlagStatus(void)
137 {
138   FlagStatus bitstatus = RESET;
139 
140   if ((WWDG->SR) != (uint32_t)RESET)
141   {
142     bitstatus = SET;
143   }
144   else
145   {
146     bitstatus = RESET;
147   }
148   return bitstatus;
149 }
150 
151 /**
152   * @brief  Clears Early Wakeup interrupt flag.
153   * @param  None
154   * @retval None
155   */
WWDG_ClearFlag(void)156 void WWDG_ClearFlag(void)
157 {
158   WWDG->SR = (uint32_t)RESET;
159 }
160 
161 /**
162   * @}
163   */
164 
165 /**
166   * @}
167   */
168 
169 /**
170   * @}
171   */
172 
173 /**
174   * @}
175   */
176 
177 /************************ (C) COPYRIGHT FMD *****END OF FILE****/
178