2 ******************************************************************************
\r
3 * @file stm32l1xx_wwdg.c
\r
4 * @author MCD Application Team
\r
6 * @date 05-March-2012
\r
7 * @brief This file provides firmware functions to manage the following
\r
8 * functionalities of the Window watchdog (WWDG) peripheral:
\r
9 * + Prescaler, Refresh window and Counter configuration
\r
11 * + Interrupts and flags management
\r
15 ==============================================================================
\r
16 ##### WWDG features #####
\r
17 ==============================================================================
\r
18 [..] Once enabled the WWDG generates a system reset on expiry of a programmed
\r
19 time period, unless the program refreshes the counter (downcounter)
\r
20 before to reach 0x3F value (i.e. a reset is generated when the counter
\r
21 value rolls over from 0x40 to 0x3F).
\r
22 [..] An MCU reset is also generated if the counter value is refreshed
\r
23 before the counter has reached the refresh window value. This
\r
24 implies that the counter must be refreshed in a limited window.
\r
26 [..] Once enabled the WWDG cannot be disabled except by a system reset.
\r
28 [..] WWDGRST flag in RCC_CSR register can be used to inform when a WWDG
\r
31 [..] The WWDG counter input clock is derived from the APB clock divided
\r
32 by a programmable prescaler.
\r
34 [..] WWDG counter clock = PCLK1 / Prescaler.
\r
35 [..] WWDG timeout = (WWDG counter clock) * (counter value).
\r
37 [..] Min-max timeout value @32MHz (PCLK1): ~128us / ~65.6ms.
\r
39 ##### How to use this driver #####
\r
40 ==============================================================================
\r
42 (#) Enable WWDG clock using RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE)
\r
45 (#) Configure the WWDG prescaler using WWDG_SetPrescaler() function.
\r
47 (#) Configure the WWDG refresh window using WWDG_SetWindowValue() function.
\r
49 (#) Set the WWDG counter value and start it using WWDG_Enable() function.
\r
50 When the WWDG is enabled the counter value should be configured to
\r
51 a value greater than 0x40 to prevent generating an immediate reset.
\r
53 (#) Optionally you can enable the Early wakeup interrupt which is
\r
54 generated when the counter reach 0x40.
\r
55 Once enabled this interrupt cannot be disabled except by a system reset.
\r
57 (#) Then the application program must refresh the WWDG counter at regular
\r
58 intervals during normal operation to prevent an MCU reset, using
\r
59 WWDG_SetCounter() function. This operation must occur only when
\r
60 the counter value is lower than the refresh window value,
\r
61 programmed using WWDG_SetWindowValue().
\r
65 ******************************************************************************
\r
68 * <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
\r
70 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
\r
71 * You may not use this file except in compliance with the License.
\r
72 * You may obtain a copy of the License at:
\r
74 * http://www.st.com/software_license_agreement_liberty_v2
\r
76 * Unless required by applicable law or agreed to in writing, software
\r
77 * distributed under the License is distributed on an "AS IS" BASIS,
\r
78 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
79 * See the License for the specific language governing permissions and
\r
80 * limitations under the License.
\r
82 ******************************************************************************
\r
85 /* Includes ------------------------------------------------------------------*/
\r
86 #include "stm32l1xx_wwdg.h"
\r
87 #include "stm32l1xx_rcc.h"
\r
89 /** @addtogroup STM32L1xx_StdPeriph_Driver
\r
94 * @brief WWDG driver modules
\r
98 /* Private typedef -----------------------------------------------------------*/
\r
99 /* Private define ------------------------------------------------------------*/
\r
101 /* ----------- WWDG registers bit address in the alias region ----------- */
\r
102 #define WWDG_OFFSET (WWDG_BASE - PERIPH_BASE)
\r
104 /* Alias word address of EWI bit */
\r
105 #define CFR_OFFSET (WWDG_OFFSET + 0x04)
\r
106 #define EWI_BitNumber 0x09
\r
107 #define CFR_EWI_BB (PERIPH_BB_BASE + (CFR_OFFSET * 32) + (EWI_BitNumber * 4))
\r
109 /* --------------------- WWDG registers bit mask ------------------------ */
\r
111 /* CFR register bit mask */
\r
112 #define CFR_WDGTB_MASK ((uint32_t)0xFFFFFE7F)
\r
113 #define CFR_W_MASK ((uint32_t)0xFFFFFF80)
\r
114 #define BIT_MASK ((uint8_t)0x7F)
\r
116 /* Private macro -------------------------------------------------------------*/
\r
117 /* Private variables ---------------------------------------------------------*/
\r
118 /* Private function prototypes -----------------------------------------------*/
\r
119 /* Private functions ---------------------------------------------------------*/
\r
121 /** @defgroup WWDG_Private_Functions
\r
125 /** @defgroup WWDG_Group1 Prescaler, Refresh window and Counter configuration functions
\r
126 * @brief Prescaler, Refresh window and Counter configuration functions
\r
129 ==============================================================================
\r
130 ##### Prescaler, Refresh window and Counter configuration functions #####
\r
131 ==============================================================================
\r
138 * @brief Deinitializes the WWDG peripheral registers to their default reset values.
\r
142 void WWDG_DeInit(void)
\r
144 RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG, ENABLE);
\r
145 RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG, DISABLE);
\r
149 * @brief Sets the WWDG Prescaler.
\r
150 * @param WWDG_Prescaler: specifies the WWDG Prescaler.
\r
151 * This parameter can be one of the following values:
\r
152 * @arg WWDG_Prescaler_1: WWDG counter clock = (PCLK1/4096)/1
\r
153 * @arg WWDG_Prescaler_2: WWDG counter clock = (PCLK1/4096)/2
\r
154 * @arg WWDG_Prescaler_4: WWDG counter clock = (PCLK1/4096)/4
\r
155 * @arg WWDG_Prescaler_8: WWDG counter clock = (PCLK1/4096)/8
\r
158 void WWDG_SetPrescaler(uint32_t WWDG_Prescaler)
\r
160 uint32_t tmpreg = 0;
\r
161 /* Check the parameters */
\r
162 assert_param(IS_WWDG_PRESCALER(WWDG_Prescaler));
\r
163 /* Clear WDGTB[1:0] bits */
\r
164 tmpreg = WWDG->CFR & CFR_WDGTB_MASK;
\r
165 /* Set WDGTB[1:0] bits according to WWDG_Prescaler value */
\r
166 tmpreg |= WWDG_Prescaler;
\r
167 /* Store the new value */
\r
168 WWDG->CFR = tmpreg;
\r
172 * @brief Sets the WWDG window value.
\r
173 * @param WindowValue: specifies the window value to be compared to the downcounter.
\r
174 * This parameter value must be lower than 0x80.
\r
177 void WWDG_SetWindowValue(uint8_t WindowValue)
\r
179 __IO uint32_t tmpreg = 0;
\r
181 /* Check the parameters */
\r
182 assert_param(IS_WWDG_WINDOW_VALUE(WindowValue));
\r
183 /* Clear W[6:0] bits */
\r
185 tmpreg = WWDG->CFR & CFR_W_MASK;
\r
187 /* Set W[6:0] bits according to WindowValue value */
\r
188 tmpreg |= WindowValue & (uint32_t) BIT_MASK;
\r
190 /* Store the new value */
\r
191 WWDG->CFR = tmpreg;
\r
195 * @brief Enables the WWDG Early Wakeup interrupt(EWI).
\r
196 * @note Once enabled this interrupt cannot be disabled except by a system reset.
\r
200 void WWDG_EnableIT(void)
\r
202 *(__IO uint32_t *) CFR_EWI_BB = (uint32_t)ENABLE;
\r
206 * @brief Sets the WWDG counter value.
\r
207 * @param Counter: specifies the watchdog counter value.
\r
208 * This parameter must be a number between 0x40 and 0x7F (to prevent generating
\r
209 * an immediate reset).
\r
212 void WWDG_SetCounter(uint8_t Counter)
\r
214 /* Check the parameters */
\r
215 assert_param(IS_WWDG_COUNTER(Counter));
\r
216 /* Write to T[6:0] bits to configure the counter value, no need to do
\r
217 a read-modify-write; writing a 0 to WDGA bit does nothing */
\r
218 WWDG->CR = Counter & BIT_MASK;
\r
225 /** @defgroup WWDG_Group2 WWDG activation functions
\r
226 * @brief WWDG activation functions
\r
229 ==============================================================================
\r
230 ##### WWDG activation function #####
\r
231 ==============================================================================
\r
238 * @brief Enables WWDG and load the counter value.
\r
239 * @param Counter: specifies the watchdog counter value.
\r
240 * This parameter must be a number between 0x40 and 0x7F (to prevent generating
\r
241 * an immediate reset).
\r
244 void WWDG_Enable(uint8_t Counter)
\r
246 /* Check the parameters */
\r
247 assert_param(IS_WWDG_COUNTER(Counter));
\r
248 WWDG->CR = WWDG_CR_WDGA | Counter;
\r
255 /** @defgroup WWDG_Group3 Interrupts and flags management functions
\r
256 * @brief Interrupts and flags management functions
\r
259 ==============================================================================
\r
260 ##### Interrupts and flags management functions #####
\r
261 ==============================================================================
\r
268 * @brief Checks whether the Early Wakeup interrupt flag is set or not.
\r
270 * @retval The new state of the Early Wakeup interrupt flag (SET or RESET).
\r
272 FlagStatus WWDG_GetFlagStatus(void)
\r
274 FlagStatus bitstatus = RESET;
\r
276 if ((WWDG->SR) != (uint32_t)RESET)
\r
288 * @brief Clears Early Wakeup interrupt flag.
\r
292 void WWDG_ClearFlag(void)
\r
294 WWDG->SR = (uint32_t)RESET;
\r
313 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r