2 ******************************************************************************
3 * @file stm32f30x_wwdg.c
4 * @author MCD Application Team
6 * @date 27-February-2014
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(c) 2014 STMicroelectronics</center></h2>
71 * Redistribution and use in source and binary forms, with or without modification,
72 * are permitted provided that the following conditions are met:
73 * 1. Redistributions of source code must retain the above copyright notice,
74 * this list of conditions and the following disclaimer.
75 * 2. Redistributions in binary form must reproduce the above copyright notice,
76 * this list of conditions and the following disclaimer in the documentation
77 * and/or other materials provided with the distribution.
78 * 3. Neither the name of STMicroelectronics nor the names of its contributors
79 * may be used to endorse or promote products derived from this software
80 * without specific prior written permission.
82 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
83 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
85 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
86 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
87 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
88 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
89 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
90 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
91 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
93 ******************************************************************************
96 /* Includes ------------------------------------------------------------------*/
97 #include "stm32f30x_wwdg.h"
98 #include "stm32f30x_rcc.h"
100 /** @addtogroup STM32F30x_StdPeriph_Driver
105 * @brief WWDG driver modules
109 /* Private typedef -----------------------------------------------------------*/
110 /* Private define ------------------------------------------------------------*/
111 /* --------------------- WWDG registers bit mask ---------------------------- */
112 /* CFR register bit mask */
113 #define CFR_WDGTB_MASK ((uint32_t)0xFFFFFE7F)
114 #define CFR_W_MASK ((uint32_t)0xFFFFFF80)
115 #define BIT_MASK ((uint8_t)0x7F)
117 /* Private macro -------------------------------------------------------------*/
118 /* Private variables ---------------------------------------------------------*/
119 /* Private function prototypes -----------------------------------------------*/
120 /* Private functions ---------------------------------------------------------*/
122 /** @defgroup WWDG_Private_Functions
126 /** @defgroup WWDG_Group1 Prescaler, Refresh window and Counter configuration functions
127 * @brief Prescaler, Refresh window and Counter configuration functions
130 ==============================================================================
131 ##### Prescaler, Refresh window and Counter configuration functions #####
132 ==============================================================================
139 * @brief Deinitializes the WWDG peripheral registers to their default reset values.
143 void WWDG_DeInit(void)
145 RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG, ENABLE);
146 RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG, DISABLE);
150 * @brief Sets the WWDG Prescaler.
151 * @param WWDG_Prescaler: specifies the WWDG Prescaler.
152 * This parameter can be one of the following values:
153 * @arg WWDG_Prescaler_1: WWDG counter clock = (PCLK1/4096)/1
154 * @arg WWDG_Prescaler_2: WWDG counter clock = (PCLK1/4096)/2
155 * @arg WWDG_Prescaler_4: WWDG counter clock = (PCLK1/4096)/4
156 * @arg WWDG_Prescaler_8: WWDG counter clock = (PCLK1/4096)/8
159 void WWDG_SetPrescaler(uint32_t WWDG_Prescaler)
162 /* Check the parameters */
163 assert_param(IS_WWDG_PRESCALER(WWDG_Prescaler));
164 /* Clear WDGTB[1:0] bits */
165 tmpreg = WWDG->CFR & CFR_WDGTB_MASK;
166 /* Set WDGTB[1:0] bits according to WWDG_Prescaler value */
167 tmpreg |= WWDG_Prescaler;
168 /* Store the new value */
173 * @brief Sets the WWDG window value.
174 * @param WindowValue: specifies the window value to be compared to the downcounter.
175 * This parameter value must be lower than 0x80.
178 void WWDG_SetWindowValue(uint8_t WindowValue)
180 __IO uint32_t tmpreg = 0;
182 /* Check the parameters */
183 assert_param(IS_WWDG_WINDOW_VALUE(WindowValue));
184 /* Clear W[6:0] bits */
186 tmpreg = WWDG->CFR & CFR_W_MASK;
188 /* Set W[6:0] bits according to WindowValue value */
189 tmpreg |= WindowValue & (uint32_t) BIT_MASK;
191 /* Store the new value */
196 * @brief Enables the WWDG Early Wakeup interrupt(EWI).
197 * @note Once enabled this interrupt cannot be disabled except by a system reset.
201 void WWDG_EnableIT(void)
203 WWDG->CFR |= WWDG_CFR_EWI;
207 * @brief Sets the WWDG counter value.
208 * @param Counter: specifies the watchdog counter value.
209 * This parameter must be a number between 0x40 and 0x7F (to prevent generating
210 * an immediate reset).
213 void WWDG_SetCounter(uint8_t Counter)
215 /* Check the parameters */
216 assert_param(IS_WWDG_COUNTER(Counter));
217 /* Write to T[6:0] bits to configure the counter value, no need to do
218 a read-modify-write; writing a 0 to WDGA bit does nothing */
219 WWDG->CR = Counter & BIT_MASK;
226 /** @defgroup WWDG_Group2 WWDG activation functions
227 * @brief WWDG activation functions
230 ==============================================================================
231 ##### WWDG activation function #####
232 ==============================================================================
239 * @brief Enables WWDG and load the counter value.
240 * @param Counter: specifies the watchdog counter value.
241 * This parameter must be a number between 0x40 and 0x7F (to prevent generating
242 * an immediate reset).
245 void WWDG_Enable(uint8_t Counter)
247 /* Check the parameters */
248 assert_param(IS_WWDG_COUNTER(Counter));
249 WWDG->CR = WWDG_CR_WDGA | Counter;
256 /** @defgroup WWDG_Group3 Interrupts and flags management functions
257 * @brief Interrupts and flags management functions
260 ==============================================================================
261 ##### Interrupts and flags management functions #####
262 ==============================================================================
269 * @brief Checks whether the Early Wakeup interrupt flag is set or not.
271 * @retval The new state of the Early Wakeup interrupt flag (SET or RESET).
273 FlagStatus WWDG_GetFlagStatus(void)
275 FlagStatus bitstatus = RESET;
277 if ((WWDG->SR) != (uint32_t)RESET)
289 * @brief Clears Early Wakeup interrupt flag.
293 void WWDG_ClearFlag(void)
295 WWDG->SR = (uint32_t)RESET;
314 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/