2 ******************************************************************************
\r
3 * @file stm32l1xx_iwdg.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 Independent watchdog (IWDG) peripheral:
\r
9 * + Prescaler and Counter configuration
\r
15 ==============================================================================
\r
16 ##### IWDG features #####
\r
17 ==============================================================================
\r
18 [..] The IWDG can be started by either software or hardware (configurable
\r
19 through option byte).
\r
21 [..] The IWDG is clocked by its own dedicated low-speed clock (LSI) and
\r
22 thus stays active even if the main clock fails.
\r
23 Once the IWDG is started, the LSI is forced ON and cannot be disabled
\r
24 (LSI cannot be disabled too), and the counter starts counting down from
\r
25 the reset value of 0xFFF. When it reaches the end of count value (0x000)
\r
26 a system reset is generated.
\r
27 The IWDG counter should be reloaded at regular intervals to prevent
\r
30 [..] The IWDG is implemented in the VDD voltage domain that is still functional
\r
31 in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
\r
33 [..] IWDGRST flag in RCC_CSR register can be used to inform when a IWDG
\r
36 [..] Min-max timeout value @37KHz (LSI): ~108us / ~28.3s
\r
37 The IWDG timeout may vary due to LSI frequency dispersion. STM32L1xx
\r
38 devices provide the capability to measure the LSI frequency (LSI clock
\r
39 connected internally to TIM10 CH1 input capture). The measured value
\r
40 can be used to have an IWDG timeout with an acceptable accuracy.
\r
41 For more information, please refer to the STM32L1xx Reference manual.
\r
43 ##### How to use this driver #####
\r
44 ==============================================================================
\r
46 (#) Enable write access to IWDG_PR and IWDG_RLR registers using
\r
47 IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable) function.
\r
48 (#) Configure the IWDG prescaler using IWDG_SetPrescaler() function.
\r
50 (#) Configure the IWDG counter value using IWDG_SetReload() function.
\r
51 This value will be loaded in the IWDG counter each time the counter
\r
52 is reloaded, then the IWDG will start counting down from this value.
\r
54 (#) Start the IWDG using IWDG_Enable() function, when the IWDG is used
\r
55 in software mode (no need to enable the LSI, it will be enabled
\r
58 (#) Then the application program must reload the IWDG counter at regular
\r
59 intervals during normal operation to prevent an MCU reset, using
\r
60 IWDG_ReloadCounter() function.
\r
64 ******************************************************************************
\r
67 * <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
\r
69 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
\r
70 * You may not use this file except in compliance with the License.
\r
71 * You may obtain a copy of the License at:
\r
73 * http://www.st.com/software_license_agreement_liberty_v2
\r
75 * Unless required by applicable law or agreed to in writing, software
\r
76 * distributed under the License is distributed on an "AS IS" BASIS,
\r
77 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
78 * See the License for the specific language governing permissions and
\r
79 * limitations under the License.
\r
81 ******************************************************************************
\r
84 /* Includes ------------------------------------------------------------------*/
\r
85 #include "stm32l1xx_iwdg.h"
\r
87 /** @addtogroup STM32L1xx_StdPeriph_Driver
\r
92 * @brief IWDG driver modules
\r
96 /* Private typedef -----------------------------------------------------------*/
\r
97 /* Private define ------------------------------------------------------------*/
\r
98 /* ---------------------- IWDG registers bit mask ----------------------------*/
\r
99 /* KR register bit mask */
\r
100 #define KR_KEY_RELOAD ((uint16_t)0xAAAA)
\r
101 #define KR_KEY_ENABLE ((uint16_t)0xCCCC)
\r
103 /* Private macro -------------------------------------------------------------*/
\r
104 /* Private variables ---------------------------------------------------------*/
\r
105 /* Private function prototypes -----------------------------------------------*/
\r
106 /* Private functions ---------------------------------------------------------*/
\r
108 /** @defgroup IWDG_Private_Functions
\r
112 /** @defgroup IWDG_Group1 Prescaler and Counter configuration functions
\r
113 * @brief Prescaler and Counter configuration functions
\r
116 ==============================================================================
\r
117 ##### Prescaler and Counter configuration functions #####
\r
118 ==============================================================================
\r
125 * @brief Enables or disables write access to IWDG_PR and IWDG_RLR registers.
\r
126 * @param IWDG_WriteAccess: new state of write access to IWDG_PR and IWDG_RLR registers.
\r
127 * This parameter can be one of the following values:
\r
128 * @arg IWDG_WriteAccess_Enable: Enable write access to IWDG_PR and IWDG_RLR registers
\r
129 * @arg IWDG_WriteAccess_Disable: Disable write access to IWDG_PR and IWDG_RLR registers
\r
132 void IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess)
\r
134 /* Check the parameters */
\r
135 assert_param(IS_IWDG_WRITE_ACCESS(IWDG_WriteAccess));
\r
136 IWDG->KR = IWDG_WriteAccess;
\r
140 * @brief Sets IWDG Prescaler value.
\r
141 * @param IWDG_Prescaler: specifies the IWDG Prescaler value.
\r
142 * This parameter can be one of the following values:
\r
143 * @arg IWDG_Prescaler_4: IWDG prescaler set to 4
\r
144 * @arg IWDG_Prescaler_8: IWDG prescaler set to 8
\r
145 * @arg IWDG_Prescaler_16: IWDG prescaler set to 16
\r
146 * @arg IWDG_Prescaler_32: IWDG prescaler set to 32
\r
147 * @arg IWDG_Prescaler_64: IWDG prescaler set to 64
\r
148 * @arg IWDG_Prescaler_128: IWDG prescaler set to 128
\r
149 * @arg IWDG_Prescaler_256: IWDG prescaler set to 256
\r
152 void IWDG_SetPrescaler(uint8_t IWDG_Prescaler)
\r
154 /* Check the parameters */
\r
155 assert_param(IS_IWDG_PRESCALER(IWDG_Prescaler));
\r
156 IWDG->PR = IWDG_Prescaler;
\r
160 * @brief Sets IWDG Reload value.
\r
161 * @param Reload: specifies the IWDG Reload value.
\r
162 * This parameter must be a number between 0 and 0x0FFF.
\r
165 void IWDG_SetReload(uint16_t Reload)
\r
167 /* Check the parameters */
\r
168 assert_param(IS_IWDG_RELOAD(Reload));
\r
169 IWDG->RLR = Reload;
\r
173 * @brief Reloads IWDG counter with value defined in the reload register
\r
174 * (write access to IWDG_PR and IWDG_RLR registers disabled).
\r
178 void IWDG_ReloadCounter(void)
\r
180 IWDG->KR = KR_KEY_RELOAD;
\r
187 /** @defgroup IWDG_Group2 IWDG activation function
\r
188 * @brief IWDG activation function
\r
191 ==============================================================================
\r
192 ##### IWDG activation function #####
\r
193 ==============================================================================
\r
200 * @brief Enables IWDG (write access to IWDG_PR and IWDG_RLR registers disabled).
\r
204 void IWDG_Enable(void)
\r
206 IWDG->KR = KR_KEY_ENABLE;
\r
213 /** @defgroup IWDG_Group3 Flag management function
\r
214 * @brief Flag management function
\r
217 ===============================================================================
\r
218 ##### Flag management function #####
\r
219 ===============================================================================
\r
226 * @brief Checks whether the specified IWDG flag is set or not.
\r
227 * @param IWDG_FLAG: specifies the flag to check.
\r
228 * This parameter can be one of the following values:
\r
229 * @arg IWDG_FLAG_PVU: Prescaler Value Update on going
\r
230 * @arg IWDG_FLAG_RVU: Reload Value Update on going
\r
231 * @retval The new state of IWDG_FLAG (SET or RESET).
\r
233 FlagStatus IWDG_GetFlagStatus(uint16_t IWDG_FLAG)
\r
235 FlagStatus bitstatus = RESET;
\r
236 /* Check the parameters */
\r
237 assert_param(IS_IWDG_FLAG(IWDG_FLAG));
\r
238 if ((IWDG->SR & IWDG_FLAG) != (uint32_t)RESET)
\r
246 /* Return the flag status */
\r
266 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r