]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32L152_Discovery_IAR/ST_Code/Libraries/STM32L1xx_StdPeriph_Driver/src/stm32l1xx_iwdg.c
Add STM32L Discovery board project as a starting point to adapt to an RTOS demo.
[freertos] / FreeRTOS / Demo / CORTEX_STM32L152_Discovery_IAR / ST_Code / Libraries / STM32L1xx_StdPeriph_Driver / src / stm32l1xx_iwdg.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32l1xx_iwdg.c\r
4   * @author  MCD Application Team\r
5   * @version V1.1.1\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
10   *           + IWDG activation\r
11   *           + Flag management\r
12   *\r
13   *  @verbatim  \r
14   *  \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
20              \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
28          an MCU reset.\r
29                              \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
32               \r
33     [..] IWDGRST flag in RCC_CSR register can be used to inform when a IWDG\r
34          reset occurs.\r
35               \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
42             \r
43                           ##### How to use this driver ##### \r
44   ============================================================================== \r
45     [..]\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
49 \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
53 \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
56         by hardware).\r
57 \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
61 \r
62     @endverbatim\r
63   *    \r
64   ******************************************************************************\r
65   * @attention\r
66   *\r
67   * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>\r
68   *\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
72   *\r
73   *        http://www.st.com/software_license_agreement_liberty_v2\r
74   *\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
80   *\r
81   ******************************************************************************\r
82   */\r
83 \r
84 /* Includes ------------------------------------------------------------------*/\r
85 #include "stm32l1xx_iwdg.h"\r
86 \r
87 /** @addtogroup STM32L1xx_StdPeriph_Driver\r
88   * @{\r
89   */\r
90 \r
91 /** @defgroup IWDG \r
92   * @brief IWDG driver modules\r
93   * @{\r
94   */ \r
95 \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
102 \r
103 /* Private macro -------------------------------------------------------------*/\r
104 /* Private variables ---------------------------------------------------------*/\r
105 /* Private function prototypes -----------------------------------------------*/\r
106 /* Private functions ---------------------------------------------------------*/\r
107 \r
108 /** @defgroup IWDG_Private_Functions\r
109   * @{\r
110   */\r
111 \r
112 /** @defgroup IWDG_Group1 Prescaler and Counter configuration functions\r
113  *  @brief   Prescaler and Counter configuration functions\r
114  *\r
115 @verbatim   \r
116   ==============================================================================\r
117             ##### Prescaler and Counter configuration functions #####\r
118   ==============================================================================  \r
119 \r
120 @endverbatim\r
121   * @{\r
122   */\r
123 \r
124 /**\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
130   * @retval None\r
131   */\r
132 void IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess)\r
133 {\r
134   /* Check the parameters */\r
135   assert_param(IS_IWDG_WRITE_ACCESS(IWDG_WriteAccess));\r
136   IWDG->KR = IWDG_WriteAccess;\r
137 }\r
138 \r
139 /**\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
150   * @retval None\r
151   */\r
152 void IWDG_SetPrescaler(uint8_t IWDG_Prescaler)\r
153 {\r
154   /* Check the parameters */\r
155   assert_param(IS_IWDG_PRESCALER(IWDG_Prescaler));\r
156   IWDG->PR = IWDG_Prescaler;\r
157 }\r
158 \r
159 /**\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
163   * @retval None\r
164   */\r
165 void IWDG_SetReload(uint16_t Reload)\r
166 {\r
167   /* Check the parameters */\r
168   assert_param(IS_IWDG_RELOAD(Reload));\r
169   IWDG->RLR = Reload;\r
170 }\r
171 \r
172 /**\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
175   * @param  None\r
176   * @retval None\r
177   */\r
178 void IWDG_ReloadCounter(void)\r
179 {\r
180   IWDG->KR = KR_KEY_RELOAD;\r
181 }\r
182 \r
183 /**\r
184   * @}\r
185   */\r
186 \r
187 /** @defgroup IWDG_Group2 IWDG activation function\r
188  *  @brief   IWDG activation function \r
189  *\r
190 @verbatim   \r
191   ==============================================================================\r
192                           ##### IWDG activation function #####\r
193   ==============================================================================  \r
194 \r
195 @endverbatim\r
196   * @{\r
197   */\r
198 \r
199 /**\r
200   * @brief  Enables IWDG (write access to IWDG_PR and IWDG_RLR registers disabled).\r
201   * @param  None.\r
202   * @retval None.\r
203   */\r
204 void IWDG_Enable(void)\r
205 {\r
206   IWDG->KR = KR_KEY_ENABLE;\r
207 }\r
208 \r
209 /**\r
210   * @}\r
211   */\r
212 \r
213 /** @defgroup IWDG_Group3 Flag management function \r
214  *  @brief  Flag management function  \r
215  *\r
216 @verbatim   \r
217  ===============================================================================\r
218                       ##### Flag management function ##### \r
219  ===============================================================================  \r
220 \r
221 @endverbatim\r
222   * @{\r
223   */\r
224 \r
225 /**\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
232   */\r
233 FlagStatus IWDG_GetFlagStatus(uint16_t IWDG_FLAG)\r
234 {\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
239   {\r
240     bitstatus = SET;\r
241   }\r
242   else\r
243   {\r
244     bitstatus = RESET;\r
245   }\r
246   /* Return the flag status */\r
247   return bitstatus;\r
248 }\r
249 \r
250 /**\r
251   * @}\r
252   */\r
253 \r
254 /**\r
255   * @}\r
256   */\r
257 \r
258 /**\r
259   * @}\r
260   */\r
261 \r
262 /**\r
263   * @}\r
264   */\r
265 \r
266 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r