]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0_STM32F0518_IAR/Libraries/STM32F0xx_StdPeriph_Driver/src/stm32f0xx_wwdg.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / CORTEX_M0_STM32F0518_IAR / Libraries / STM32F0xx_StdPeriph_Driver / src / stm32f0xx_wwdg.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f0xx_wwdg.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0RC1\r
6   * @date    27-January-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
10   *           + WWDG activation\r
11   *           + Interrupts and flags management\r
12   *             \r
13   *  @verbatim\r
14   *    \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
25 \r
26     [..] Once enabled the WWDG cannot be disabled except by a system reset.\r
27 \r
28     [..] WWDGRST flag in RCC_CSR register can be used to inform when a WWDG\r
29          reset occurs.\r
30 \r
31     [..] The WWDG counter input clock is derived from the APB clock divided \r
32          by a programmable prescaler.\r
33 \r
34     [..] WWDG counter clock = PCLK1 / Prescaler.\r
35     [..] WWDG timeout = (WWDG counter clock) * (counter value).\r
36 \r
37     [..] Min-max timeout value @32MHz (PCLK1): ~85us / ~43ms.\r
38 \r
39                        ##### How to use this driver ##### \r
40   ==============================================================================\r
41     [..]\r
42         (#) Enable WWDG clock using RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE) \r
43             function.\r
44               \r
45         (#) Configure the WWDG prescaler using WWDG_SetPrescaler() function.\r
46                              \r
47         (#) Configure the WWDG refresh window using WWDG_SetWindowValue() function.\r
48               \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
52               \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
56                    \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
62   \r
63   *  @endverbatim\r
64   *\r
65   ******************************************************************************\r
66   * @attention\r
67   *\r
68   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
69   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
70   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
71   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
72   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
73   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
74   *\r
75   * FOR MORE INFORMATION PLEASE READ CAREFULLY THE LICENSE AGREEMENT FILE\r
76   * LOCATED IN THE ROOT DIRECTORY OF THIS FIRMWARE PACKAGE.\r
77   *\r
78   * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>\r
79   ******************************************************************************\r
80   */\r
81 \r
82 /* Includes ------------------------------------------------------------------*/\r
83 #include "stm32f0xx_wwdg.h"\r
84 #include "stm32f0xx_rcc.h"\r
85 \r
86 /** @addtogroup STM32F0xx_StdPeriph_Driver\r
87   * @{\r
88   */\r
89 \r
90 /** @defgroup WWDG \r
91   * @brief WWDG driver modules\r
92   * @{\r
93   */\r
94 \r
95 /* Private typedef -----------------------------------------------------------*/\r
96 /* Private define ------------------------------------------------------------*/\r
97 /* --------------------- WWDG registers bit mask ---------------------------- */\r
98 /* CFR register bit mask */\r
99 #define CFR_WDGTB_MASK    ((uint32_t)0xFFFFFE7F)\r
100 #define CFR_W_MASK        ((uint32_t)0xFFFFFF80)\r
101 #define BIT_MASK          ((uint8_t)0x7F)\r
102 \r
103 /* Private macro -------------------------------------------------------------*/\r
104 /* Private variables ---------------------------------------------------------*/\r
105 /* Private function prototypes -----------------------------------------------*/\r
106 /* Private functions ---------------------------------------------------------*/\r
107 \r
108 /** @defgroup WWDG_Private_Functions\r
109   * @{\r
110   */\r
111 \r
112 /** @defgroup WWDG_Group1 Prescaler, Refresh window and Counter configuration functions\r
113  *  @brief   Prescaler, Refresh window and Counter configuration functions \r
114  *\r
115 @verbatim   \r
116   ==============================================================================\r
117     ##### Prescaler, Refresh window and Counter configuration functions #####\r
118   ==============================================================================  \r
119 \r
120 @endverbatim\r
121   * @{\r
122   */\r
123 \r
124 /**\r
125   * @brief  Deinitializes the WWDG peripheral registers to their default reset values.\r
126   * @param  None\r
127   * @retval None\r
128   */\r
129 void WWDG_DeInit(void)\r
130 {\r
131   RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG, ENABLE);\r
132   RCC_APB1PeriphResetCmd(RCC_APB1Periph_WWDG, DISABLE);\r
133 }\r
134 \r
135 /**\r
136   * @brief  Sets the WWDG Prescaler.\r
137   * @param  WWDG_Prescaler: specifies the WWDG Prescaler.\r
138   *   This parameter can be one of the following values:\r
139   *     @arg WWDG_Prescaler_1: WWDG counter clock = (PCLK1/4096)/1\r
140   *     @arg WWDG_Prescaler_2: WWDG counter clock = (PCLK1/4096)/2\r
141   *     @arg WWDG_Prescaler_4: WWDG counter clock = (PCLK1/4096)/4\r
142   *     @arg WWDG_Prescaler_8: WWDG counter clock = (PCLK1/4096)/8\r
143   * @retval None\r
144   */\r
145 void WWDG_SetPrescaler(uint32_t WWDG_Prescaler)\r
146 {\r
147   uint32_t tmpreg = 0;\r
148   /* Check the parameters */\r
149   assert_param(IS_WWDG_PRESCALER(WWDG_Prescaler));\r
150   /* Clear WDGTB[1:0] bits */\r
151   tmpreg = WWDG->CFR & CFR_WDGTB_MASK;\r
152   /* Set WDGTB[1:0] bits according to WWDG_Prescaler value */\r
153   tmpreg |= WWDG_Prescaler;\r
154   /* Store the new value */\r
155   WWDG->CFR = tmpreg;\r
156 }\r
157 \r
158 /**\r
159   * @brief  Sets the WWDG window value.\r
160   * @param  WindowValue: specifies the window value to be compared to the downcounter.\r
161   *   This parameter value must be lower than 0x80.\r
162   * @retval None\r
163   */\r
164 void WWDG_SetWindowValue(uint8_t WindowValue)\r
165 {\r
166   __IO uint32_t tmpreg = 0;\r
167 \r
168   /* Check the parameters */\r
169   assert_param(IS_WWDG_WINDOW_VALUE(WindowValue));\r
170   /* Clear W[6:0] bits */\r
171 \r
172   tmpreg = WWDG->CFR & CFR_W_MASK;\r
173 \r
174   /* Set W[6:0] bits according to WindowValue value */\r
175   tmpreg |= WindowValue & (uint32_t) BIT_MASK;\r
176 \r
177   /* Store the new value */\r
178   WWDG->CFR = tmpreg;\r
179 }\r
180 \r
181 /**\r
182   * @brief  Enables the WWDG Early Wakeup interrupt(EWI).\r
183   * @note   Once enabled this interrupt cannot be disabled except by a system reset. \r
184   * @param  None\r
185   * @retval None\r
186   */\r
187 void WWDG_EnableIT(void)\r
188 {\r
189   WWDG->CFR |= WWDG_CFR_EWI;\r
190 }\r
191 \r
192 /**\r
193   * @brief  Sets the WWDG counter value.\r
194   * @param  Counter: specifies the watchdog counter value.\r
195   *   This parameter must be a number between 0x40 and 0x7F (to prevent generating\r
196   *   an immediate reset).\r
197   * @retval None\r
198   */\r
199 void WWDG_SetCounter(uint8_t Counter)\r
200 {\r
201   /* Check the parameters */\r
202   assert_param(IS_WWDG_COUNTER(Counter));\r
203   /* Write to T[6:0] bits to configure the counter value, no need to do\r
204      a read-modify-write; writing a 0 to WDGA bit does nothing */\r
205   WWDG->CR = Counter & BIT_MASK;\r
206 }\r
207 \r
208 /**\r
209   * @}\r
210   */\r
211 \r
212 /** @defgroup WWDG_Group2 WWDG activation functions\r
213  *  @brief   WWDG activation functions \r
214  *\r
215 @verbatim   \r
216   ==============================================================================\r
217                      ##### WWDG activation function #####\r
218   ==============================================================================  \r
219 \r
220 @endverbatim\r
221   * @{\r
222   */\r
223 \r
224 /**\r
225   * @brief  Enables WWDG and load the counter value.                  \r
226   * @param  Counter: specifies the watchdog counter value.\r
227   *   This parameter must be a number between 0x40 and 0x7F (to prevent generating\r
228   *   an immediate reset).\r
229   * @retval None\r
230   */\r
231 void WWDG_Enable(uint8_t Counter)\r
232 {\r
233   /* Check the parameters */\r
234   assert_param(IS_WWDG_COUNTER(Counter));\r
235   WWDG->CR = WWDG_CR_WDGA | Counter;\r
236 }\r
237 \r
238 /**\r
239   * @}\r
240   */\r
241 \r
242 /** @defgroup WWDG_Group3 Interrupts and flags management functions\r
243  *  @brief   Interrupts and flags management functions \r
244  *\r
245 @verbatim   \r
246   ==============================================================================\r
247                 ##### Interrupts and flags management functions #####\r
248   ==============================================================================  \r
249 \r
250 @endverbatim\r
251   * @{\r
252   */\r
253 \r
254 /**\r
255   * @brief  Checks whether the Early Wakeup interrupt flag is set or not.\r
256   * @param  None\r
257   * @retval The new state of the Early Wakeup interrupt flag (SET or RESET).\r
258   */\r
259 FlagStatus WWDG_GetFlagStatus(void)\r
260 {\r
261   FlagStatus bitstatus = RESET;\r
262     \r
263   if ((WWDG->SR) != (uint32_t)RESET)\r
264   {\r
265     bitstatus = SET;\r
266   }\r
267   else\r
268   {\r
269     bitstatus = RESET;\r
270   }\r
271   return bitstatus;\r
272 }\r
273 \r
274 /**\r
275   * @brief  Clears Early Wakeup interrupt flag.\r
276   * @param  None\r
277   * @retval None\r
278   */\r
279 void WWDG_ClearFlag(void)\r
280 {\r
281   WWDG->SR = (uint32_t)RESET;\r
282 }\r
283 \r
284 /**\r
285   * @}\r
286   */\r
287 \r
288 /**\r
289   * @}\r
290   */\r
291 \r
292 /**\r
293   * @}\r
294   */\r
295 \r
296 /**\r
297   * @}\r
298   */\r
299 \r
300 /******************* (C) COPYRIGHT 2012 STMicroelectronics *****END OF FILE****/\r