2 ******************************************************************************
3 * @file stm32f4xx_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
14 ===============================================================================
15 ##### WWDG features #####
16 ===============================================================================
18 Once enabled the WWDG generates a system reset on expiry of a programmed
19 time period, unless the program refreshes the counter (downcounter)
20 before to reach 0x3F value (i.e. a reset is generated when the counter
21 value rolls over from 0x40 to 0x3F).
22 An MCU reset is also generated if the counter value is refreshed
23 before the counter has reached the refresh window value. This
24 implies that the counter must be refreshed in a limited window.
26 Once enabled the WWDG cannot be disabled except by a system reset.
28 WWDGRST flag in RCC_CSR register can be used to inform when a WWDG
31 The WWDG counter input clock is derived from the APB clock divided
32 by a programmable prescaler.
34 WWDG counter clock = PCLK1 / Prescaler
35 WWDG timeout = (WWDG counter clock) * (counter value)
37 Min-max timeout value @42 MHz(PCLK1): ~97.5 us / ~49.9 ms
39 ##### How to use this driver #####
40 ===============================================================================
42 (#) Enable WWDG clock using RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE) function
44 (#) Configure the WWDG prescaler using WWDG_SetPrescaler() function
46 (#) Configure the WWDG refresh window using WWDG_SetWindowValue() function
48 (#) Set the WWDG counter value and start it using WWDG_Enable() function.
49 When the WWDG is enabled the counter value should be configured to
50 a value greater than 0x40 to prevent generating an immediate reset.
52 (#) Optionally you can enable the Early wakeup interrupt which is
53 generated when the counter reach 0x40.
54 Once enabled this interrupt cannot be disabled except by a system reset.
56 (#) Then the application program must refresh the WWDG counter at regular
57 intervals during normal operation to prevent an MCU reset, using
58 WWDG_SetCounter() function. This operation must occur only when
59 the counter value is lower than the refresh window value,
60 programmed using WWDG_SetWindowValue().
63 ******************************************************************************
66 * <h2><center>© COPYRIGHT 2016 STMicroelectronics</center></h2>
68 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
69 * You may not use this file except in compliance with the License.
70 * You may obtain a copy of the License at:
72 * http://www.st.com/software_license_agreement_liberty_v2
74 * Unless required by applicable law or agreed to in writing, software
75 * distributed under the License is distributed on an "AS IS" BASIS,
76 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
77 * See the License for the specific language governing permissions and
78 * limitations under the License.
80 ******************************************************************************
83 /* Includes ------------------------------------------------------------------*/
84 #include "stm32f4xx_wwdg.h"
85 #include "stm32f4xx_rcc.h"
87 /** @addtogroup STM32F4xx_StdPeriph_Driver
92 * @brief WWDG driver modules
96 /* Private typedef -----------------------------------------------------------*/
97 /* Private define ------------------------------------------------------------*/
99 /* ----------- WWDG registers bit address in the alias region ----------- */
100 #define WWDG_OFFSET (WWDG_BASE - PERIPH_BASE)
101 /* Alias word address of EWI bit */
102 #define CFR_OFFSET (WWDG_OFFSET + 0x04)
103 #define EWI_BitNumber 0x09
104 #define CFR_EWI_BB (PERIPH_BB_BASE + (CFR_OFFSET * 32) + (EWI_BitNumber * 4))
106 /* --------------------- WWDG registers bit mask ------------------------ */
107 /* CFR register bit mask */
108 #define CFR_WDGTB_MASK ((uint32_t)0xFFFFFE7F)
109 #define CFR_W_MASK ((uint32_t)0xFFFFFF80)
110 #define BIT_MASK ((uint8_t)0x7F)
112 /* Private macro -------------------------------------------------------------*/
113 /* Private variables ---------------------------------------------------------*/
114 /* Private function prototypes -----------------------------------------------*/
115 /* Private functions ---------------------------------------------------------*/
117 /** @defgroup WWDG_Private_Functions
121 /** @defgroup WWDG_Group1 Prescaler, Refresh window and Counter configuration functions
122 * @brief Prescaler, Refresh window and Counter configuration functions
125 ===============================================================================
126 ##### Prescaler, Refresh window and Counter configuration functions #####
127 ===============================================================================
134 * @brief Deinitializes the WWDG peripheral registers to their default reset values.
138 void WWDG_DeInit(void)
140 RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG
, ENABLE
);
141 RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG
, DISABLE
);
145 * @brief Sets the WWDG Prescaler.
146 * @param WWDG_Prescaler: specifies the WWDG Prescaler.
147 * This parameter can be one of the following values:
148 * @arg WWDG_Prescaler_1: WWDG counter clock = (PCLK1/4096)/1
149 * @arg WWDG_Prescaler_2: WWDG counter clock = (PCLK1/4096)/2
150 * @arg WWDG_Prescaler_4: WWDG counter clock = (PCLK1/4096)/4
151 * @arg WWDG_Prescaler_8: WWDG counter clock = (PCLK1/4096)/8
154 void WWDG_SetPrescaler(uint32_t WWDG_Prescaler
)
157 /* Check the parameters */
158 assert_param(IS_WWDG_PRESCALER(WWDG_Prescaler
));
159 /* Clear WDGTB[1:0] bits */
160 tmpreg
= WWDG
->CFR
& CFR_WDGTB_MASK
;
161 /* Set WDGTB[1:0] bits according to WWDG_Prescaler value */
162 tmpreg
|= WWDG_Prescaler
;
163 /* Store the new value */
168 * @brief Sets the WWDG window value.
169 * @param WindowValue: specifies the window value to be compared to the downcounter.
170 * This parameter value must be lower than 0x80.
173 void WWDG_SetWindowValue(uint8_t WindowValue
)
175 __IO
uint32_t tmpreg
= 0;
177 /* Check the parameters */
178 assert_param(IS_WWDG_WINDOW_VALUE(WindowValue
));
179 /* Clear W[6:0] bits */
181 tmpreg
= WWDG
->CFR
& CFR_W_MASK
;
183 /* Set W[6:0] bits according to WindowValue value */
184 tmpreg
|= WindowValue
& (uint32_t) BIT_MASK
;
186 /* Store the new value */
191 * @brief Enables the WWDG Early Wakeup interrupt(EWI).
192 * @note Once enabled this interrupt cannot be disabled except by a system reset.
196 void WWDG_EnableIT(void)
198 *(__IO
uint32_t *) CFR_EWI_BB
= (uint32_t)ENABLE
;
202 * @brief Sets the WWDG counter value.
203 * @param Counter: specifies the watchdog counter value.
204 * This parameter must be a number between 0x40 and 0x7F (to prevent generating
205 * an immediate reset)
208 void WWDG_SetCounter(uint8_t Counter
)
210 /* Check the parameters */
211 assert_param(IS_WWDG_COUNTER(Counter
));
212 /* Write to T[6:0] bits to configure the counter value, no need to do
213 a read-modify-write; writing a 0 to WDGA bit does nothing */
214 WWDG
->CR
= Counter
& BIT_MASK
;
220 /** @defgroup WWDG_Group2 WWDG activation functions
221 * @brief WWDG activation functions
224 ===============================================================================
225 ##### WWDG activation function #####
226 ===============================================================================
233 * @brief Enables WWDG and load the counter value.
234 * @param Counter: specifies the watchdog counter value.
235 * This parameter must be a number between 0x40 and 0x7F (to prevent generating
236 * an immediate reset)
239 void WWDG_Enable(uint8_t Counter
)
241 /* Check the parameters */
242 assert_param(IS_WWDG_COUNTER(Counter
));
243 WWDG
->CR
= WWDG_CR_WDGA
| Counter
;
249 /** @defgroup WWDG_Group3 Interrupts and flags management functions
250 * @brief Interrupts and flags management functions
253 ===============================================================================
254 ##### Interrupts and flags management functions #####
255 ===============================================================================
262 * @brief Checks whether the Early Wakeup interrupt flag is set or not.
264 * @retval The new state of the Early Wakeup interrupt flag (SET or RESET)
266 FlagStatus
WWDG_GetFlagStatus(void)
268 FlagStatus bitstatus
= RESET
;
270 if ((WWDG
->SR
) != (uint32_t)RESET
)
282 * @brief Clears Early Wakeup interrupt flag.
286 void WWDG_ClearFlag(void)
288 WWDG
->SR
= (uint32_t)RESET
;
307 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/