2 ******************************************************************************
3 * @file stm32f30x_wwdg.c
4 * @author MCD Application Team
7 * @brief This file provides firmware functions to manage the following
8 * functionalities of the Window watchdog (WWDG) peripheral:
9 * + Prescaler, Refresh window and Counter configuration
11 * + Interrupts and flags management
15 ==============================================================================
16 ##### WWDG features #####
17 ==============================================================================
19 [..] Once enabled the WWDG generates a system reset on expiry of a programmed
20 time period, unless the program refreshes the counter (downcounter)
21 before to reach 0x3F value (i.e. a reset is generated when the counter
22 value rolls over from 0x40 to 0x3F).
23 [..] An MCU reset is also generated if the counter value is refreshed
24 before the counter has reached the refresh window value. This
25 implies that the counter must be refreshed in a limited window.
27 [..] Once enabled the WWDG cannot be disabled except by a system reset.
29 [..] WWDGRST flag in RCC_CSR register can be used to inform when a WWDG
32 [..] The WWDG counter input clock is derived from the APB clock divided
33 by a programmable prescaler.
35 [..] WWDG counter clock = PCLK1 / Prescaler.
36 [..] WWDG timeout = (WWDG counter clock) * (counter value).
38 [..] Min-max timeout value @36MHz (PCLK1): ~114us / ~58.3ms.
40 ##### How to use this driver #####
41 ==============================================================================
43 (#) Enable WWDG clock using RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE)
46 (#) Configure the WWDG prescaler using WWDG_SetPrescaler() function.
48 (#) Configure the WWDG refresh window using WWDG_SetWindowValue() function.
50 (#) Set the WWDG counter value and start it using WWDG_Enable() function.
51 When the WWDG is enabled the counter value should be configured to
52 a value greater than 0x40 to prevent generating an immediate reset.
54 (#) Optionally you can enable the Early wakeup interrupt which is
55 generated when the counter reach 0x40.
56 Once enabled this interrupt cannot be disabled except by a system reset.
58 (#) Then the application program must refresh the WWDG counter at regular
59 intervals during normal operation to prevent an MCU reset, using
60 WWDG_SetCounter() function. This operation must occur only when
61 the counter value is lower than the refresh window value,
62 programmed using WWDG_SetWindowValue().
66 ******************************************************************************
69 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2>
71 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
72 * You may not use this file except in compliance with the License.
73 * You may obtain a copy of the License at:
75 * http://www.st.com/software_license_agreement_liberty_v2
77 * Unless required by applicable law or agreed to in writing, software
78 * distributed under the License is distributed on an "AS IS" BASIS,
79 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
80 * See the License for the specific language governing permissions and
81 * limitations under the License.
83 ******************************************************************************
86 /* Includes ------------------------------------------------------------------*/
87 #include "stm32f30x_wwdg.h"
88 #include "stm32f30x_rcc.h"
90 /** @addtogroup STM32F30x_StdPeriph_Driver
95 * @brief WWDG driver modules
99 /* Private typedef -----------------------------------------------------------*/
100 /* Private define ------------------------------------------------------------*/
101 /* --------------------- WWDG registers bit mask ---------------------------- */
102 /* CFR register bit mask */
103 #define CFR_WDGTB_MASK ((uint32_t)0xFFFFFE7F)
104 #define CFR_W_MASK ((uint32_t)0xFFFFFF80)
105 #define BIT_MASK ((uint8_t)0x7F)
107 /* Private macro -------------------------------------------------------------*/
108 /* Private variables ---------------------------------------------------------*/
109 /* Private function prototypes -----------------------------------------------*/
110 /* Private functions ---------------------------------------------------------*/
112 /** @defgroup WWDG_Private_Functions
116 /** @defgroup WWDG_Group1 Prescaler, Refresh window and Counter configuration functions
117 * @brief Prescaler, Refresh window and Counter configuration functions
120 ==============================================================================
121 ##### Prescaler, Refresh window and Counter configuration functions #####
122 ==============================================================================
129 * @brief Deinitializes the WWDG peripheral registers to their default reset values.
133 void WWDG_DeInit(void)
135 RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG
, ENABLE
);
136 RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG
, DISABLE
);
140 * @brief Sets the WWDG Prescaler.
141 * @param WWDG_Prescaler: specifies the WWDG Prescaler.
142 * This parameter can be one of the following values:
143 * @arg WWDG_Prescaler_1: WWDG counter clock = (PCLK1/4096)/1
144 * @arg WWDG_Prescaler_2: WWDG counter clock = (PCLK1/4096)/2
145 * @arg WWDG_Prescaler_4: WWDG counter clock = (PCLK1/4096)/4
146 * @arg WWDG_Prescaler_8: WWDG counter clock = (PCLK1/4096)/8
149 void WWDG_SetPrescaler(uint32_t WWDG_Prescaler
)
152 /* Check the parameters */
153 assert_param(IS_WWDG_PRESCALER(WWDG_Prescaler
));
154 /* Clear WDGTB[1:0] bits */
155 tmpreg
= WWDG
->CFR
& CFR_WDGTB_MASK
;
156 /* Set WDGTB[1:0] bits according to WWDG_Prescaler value */
157 tmpreg
|= WWDG_Prescaler
;
158 /* Store the new value */
163 * @brief Sets the WWDG window value.
164 * @param WindowValue: specifies the window value to be compared to the downcounter.
165 * This parameter value must be lower than 0x80.
168 void WWDG_SetWindowValue(uint8_t WindowValue
)
170 __IO
uint32_t tmpreg
= 0;
172 /* Check the parameters */
173 assert_param(IS_WWDG_WINDOW_VALUE(WindowValue
));
174 /* Clear W[6:0] bits */
176 tmpreg
= WWDG
->CFR
& CFR_W_MASK
;
178 /* Set W[6:0] bits according to WindowValue value */
179 tmpreg
|= WindowValue
& (uint32_t) BIT_MASK
;
181 /* Store the new value */
186 * @brief Enables the WWDG Early Wakeup interrupt(EWI).
187 * @note Once enabled this interrupt cannot be disabled except by a system reset.
191 void WWDG_EnableIT(void)
193 WWDG
->CFR
|= WWDG_CFR_EWI
;
197 * @brief Sets the WWDG counter value.
198 * @param Counter: specifies the watchdog counter value.
199 * This parameter must be a number between 0x40 and 0x7F (to prevent generating
200 * an immediate reset).
203 void WWDG_SetCounter(uint8_t Counter
)
205 /* Check the parameters */
206 assert_param(IS_WWDG_COUNTER(Counter
));
207 /* Write to T[6:0] bits to configure the counter value, no need to do
208 a read-modify-write; writing a 0 to WDGA bit does nothing */
209 WWDG
->CR
= Counter
& BIT_MASK
;
216 /** @defgroup WWDG_Group2 WWDG activation functions
217 * @brief WWDG activation functions
220 ==============================================================================
221 ##### WWDG activation function #####
222 ==============================================================================
229 * @brief Enables WWDG and load the counter value.
230 * @param Counter: specifies the watchdog counter value.
231 * This parameter must be a number between 0x40 and 0x7F (to prevent generating
232 * an immediate reset).
235 void WWDG_Enable(uint8_t Counter
)
237 /* Check the parameters */
238 assert_param(IS_WWDG_COUNTER(Counter
));
239 WWDG
->CR
= WWDG_CR_WDGA
| Counter
;
246 /** @defgroup WWDG_Group3 Interrupts and flags management functions
247 * @brief Interrupts and flags management functions
250 ==============================================================================
251 ##### Interrupts and flags management functions #####
252 ==============================================================================
259 * @brief Checks whether the Early Wakeup interrupt flag is set or not.
261 * @retval The new state of the Early Wakeup interrupt flag (SET or RESET).
263 FlagStatus
WWDG_GetFlagStatus(void)
265 FlagStatus bitstatus
= RESET
;
267 if ((WWDG
->SR
) != (uint32_t)RESET
)
279 * @brief Clears Early Wakeup interrupt flag.
283 void WWDG_ClearFlag(void)
285 WWDG
->SR
= (uint32_t)RESET
;
304 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/