Subversion Repositories FuelGauge

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/**
2
  ******************************************************************************
3
  * @file    stm32f0xx_hal_iwdg.c
4
  * @author  MCD Application Team
5
  * @brief   IWDG HAL module driver.
6
  *          This file provides firmware functions to manage the following
7
  *          functionalities of the Independent Watchdog (IWDG) peripheral:
8
  *           + Initialization and Start functions
9
  *           + IO operation functions
10
  *
11
  @verbatim
12
  ==============================================================================
13
                    ##### IWDG Generic features #####
14
  ==============================================================================
15
  [..]
16
    (+) The IWDG can be started by either software or hardware (configurable
17
        through option byte).
18
 
19
    (+) The IWDG is clocked by Low-Speed clock (LSI) and thus stays active even
20
        if the main clock fails.
21
 
22
    (+) Once the IWDG is started, the LSI is forced ON and both can not be
23
        disabled. The counter starts counting down from the reset value (0xFFF).
24
        When it reaches the end of count value (0x000) a reset signal is
25
        generated (IWDG reset).
26
 
27
    (+) Whenever the key value 0x0000 AAAA is written in the IWDG_KR register,
28
        the IWDG_RLR value is reloaded in the counter and the watchdog reset is
29
        prevented.
30
 
31
    (+) The IWDG is implemented in the VDD voltage domain that is still functional
32
        in STOP and STANDBY mode (IWDG reset can wake-up from STANDBY).
33
        IWDGRST flag in RCC_CSR register can be used to inform when an IWDG
34
        reset occurs.
35
 
36
    (+) Debug mode : When the microcontroller enters debug mode (core halted),
37
        the IWDG counter either continues to work normally or stops, depending
38
        on DBG_IWDG_STOP configuration bit in DBG module, accessible through
39
        __HAL_DBGMCU_FREEZE_IWDG() and __HAL_DBGMCU_UNFREEZE_IWDG() macros.
40
 
41
    [..] Min-max timeout value @32KHz (LSI): ~125us / ~32.7s
42
         The IWDG timeout may vary due to LSI frequency dispersion. STM32F0xx
43
         devices provide the capability to measure the LSI frequency (LSI clock
44
         connected internally to TIM16 CH1 input capture). The measured value
45
         can be used to have an IWDG timeout with an acceptable accuracy.
46
 
47
                     ##### How to use this driver #####
48
  ==============================================================================
49
  [..]
50
    (#) Use IWDG using HAL_IWDG_Init() function to :
51
      (++) Enable instance by writing Start keyword in IWDG_KEY register. LSI
52
           clock is forced ON and IWDG counter starts counting down.
53
      (++) Enable write access to configuration registers:
54
          IWDG_PR, IWDG_RLR and IWDG_WINR.
55
      (++) Configure the IWDG prescaler and counter reload value. This reload
56
           value will be loaded in the IWDG counter each time the watchdog is
57
           reloaded, then the IWDG will start counting down from this value.
58
      (++) Wait for status flags to be reset.
59
      (++) Depending on window parameter:
60
        (+++) If Window Init parameter is same as Window register value,
61
             nothing more is done but reload counter value in order to exit
62
             function with exact time base.
63
        (+++) Else modify Window register. This will automatically reload
64
             watchdog counter.
65
 
66
    (#) Then the application program must refresh the IWDG counter at regular
67
        intervals during normal operation to prevent an MCU reset, using
68
        HAL_IWDG_Refresh() function.
69
 
70
     *** IWDG HAL driver macros list ***
71
     ====================================
72
     [..]
73
       Below the list of most used macros in IWDG HAL driver:
74
      (+) __HAL_IWDG_START: Enable the IWDG peripheral
75
      (+) __HAL_IWDG_RELOAD_COUNTER: Reloads IWDG counter with value defined in
76
          the reload register
77
 
78
  @endverbatim
79
  ******************************************************************************
80
  * @attention
81
  *
82
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
83
  * All rights reserved.</center></h2>
84
  *
85
  * This software component is licensed by ST under BSD 3-Clause license,
86
  * the "License"; You may not use this file except in compliance with the
87
  * License. You may obtain a copy of the License at:
88
  *                        opensource.org/licenses/BSD-3-Clause
89
  *
90
  ******************************************************************************
91
  */
92
 
93
/* Includes ------------------------------------------------------------------*/
94
#include "stm32f0xx_hal.h"
95
 
96
/** @addtogroup STM32F0xx_HAL_Driver
97
  * @{
98
  */
99
 
100
#ifdef HAL_IWDG_MODULE_ENABLED
101
/** @addtogroup IWDG
102
  * @brief IWDG HAL module driver.
103
  * @{
104
  */
105
 
106
/* Private typedef -----------------------------------------------------------*/
107
/* Private define ------------------------------------------------------------*/
108
/** @defgroup IWDG_Private_Defines IWDG Private Defines
109
  * @{
110
  */
111
/* Status register need 5 RC LSI divided by prescaler clock to be updated. With
112
   higher prescaler (256), and according to LSI variation, we need to wait at
113
   least 6 cycles so 48 ms. */
114
#define HAL_IWDG_DEFAULT_TIMEOUT            48u
115
/**
116
  * @}
117
  */
118
 
119
/* Private macro -------------------------------------------------------------*/
120
/* Private variables ---------------------------------------------------------*/
121
/* Private function prototypes -----------------------------------------------*/
122
/* Exported functions --------------------------------------------------------*/
123
 
124
/** @addtogroup IWDG_Exported_Functions
125
  * @{
126
  */
127
 
128
/** @addtogroup IWDG_Exported_Functions_Group1
129
  *  @brief    Initialization and Start functions.
130
  *
131
@verbatim
132
 ===============================================================================
133
          ##### Initialization and Start functions #####
134
 ===============================================================================
135
 [..]  This section provides functions allowing to:
136
      (+) Initialize the IWDG according to the specified parameters in the
137
          IWDG_InitTypeDef of associated handle.
138
      (+) Manage Window option.
139
      (+) Once initialization is performed in HAL_IWDG_Init function, Watchdog
140
          is reloaded in order to exit function with correct time base.
141
 
142
@endverbatim
143
  * @{
144
  */
145
 
146
/**
147
  * @brief  Initialize the IWDG according to the specified parameters in the
148
  *         IWDG_InitTypeDef and start watchdog. Before exiting function,
149
  *         watchdog is refreshed in order to have correct time base.
150
  * @param  hiwdg  pointer to a IWDG_HandleTypeDef structure that contains
151
  *                the configuration information for the specified IWDG module.
152
  * @retval HAL status
153
  */
154
HAL_StatusTypeDef HAL_IWDG_Init(IWDG_HandleTypeDef *hiwdg)
155
{
156
  uint32_t tickstart;
157
 
158
  /* Check the IWDG handle allocation */
159
  if (hiwdg == NULL)
160
  {
161
    return HAL_ERROR;
162
  }
163
 
164
  /* Check the parameters */
165
  assert_param(IS_IWDG_ALL_INSTANCE(hiwdg->Instance));
166
  assert_param(IS_IWDG_PRESCALER(hiwdg->Init.Prescaler));
167
  assert_param(IS_IWDG_RELOAD(hiwdg->Init.Reload));
168
  assert_param(IS_IWDG_WINDOW(hiwdg->Init.Window));
169
 
170
  /* Enable IWDG. LSI is turned on automatically */
171
  __HAL_IWDG_START(hiwdg);
172
 
173
  /* Enable write access to IWDG_PR, IWDG_RLR and IWDG_WINR registers by writing
174
  0x5555 in KR */
175
  IWDG_ENABLE_WRITE_ACCESS(hiwdg);
176
 
177
  /* Write to IWDG registers the Prescaler & Reload values to work with */
178
  hiwdg->Instance->PR = hiwdg->Init.Prescaler;
179
  hiwdg->Instance->RLR = hiwdg->Init.Reload;
180
 
181
  /* Check pending flag, if previous update not done, return timeout */
182
  tickstart = HAL_GetTick();
183
 
184
  /* Wait for register to be updated */
185
  while (hiwdg->Instance->SR != 0x00u)
186
  {
187
    if ((HAL_GetTick() - tickstart) > HAL_IWDG_DEFAULT_TIMEOUT)
188
    {
189
      return HAL_TIMEOUT;
190
    }
191
  }
192
 
193
  /* If window parameter is different than current value, modify window
194
  register */
195
  if (hiwdg->Instance->WINR != hiwdg->Init.Window)
196
  {
197
    /* Write to IWDG WINR the IWDG_Window value to compare with. In any case,
198
    even if window feature is disabled, Watchdog will be reloaded by writing
199
    windows register */
200
    hiwdg->Instance->WINR = hiwdg->Init.Window;
201
  }
202
  else
203
  {
204
    /* Reload IWDG counter with value defined in the reload register */
205
    __HAL_IWDG_RELOAD_COUNTER(hiwdg);
206
  }
207
 
208
  /* Return function status */
209
  return HAL_OK;
210
}
211
 
212
/**
213
  * @}
214
  */
215
 
216
 
217
/** @addtogroup IWDG_Exported_Functions_Group2
218
  *  @brief   IO operation functions
219
  *
220
@verbatim
221
 ===============================================================================
222
                      ##### IO operation functions #####
223
 ===============================================================================
224
 [..]  This section provides functions allowing to:
225
      (+) Refresh the IWDG.
226
 
227
@endverbatim
228
  * @{
229
  */
230
 
231
 
232
/**
233
  * @brief  Refresh the IWDG.
234
  * @param  hiwdg  pointer to a IWDG_HandleTypeDef structure that contains
235
  *                the configuration information for the specified IWDG module.
236
  * @retval HAL status
237
  */
238
HAL_StatusTypeDef HAL_IWDG_Refresh(IWDG_HandleTypeDef *hiwdg)
239
{
240
  /* Reload IWDG counter with value defined in the reload register */
241
  __HAL_IWDG_RELOAD_COUNTER(hiwdg);
242
 
243
  /* Return function status */
244
  return HAL_OK;
245
}
246
 
247
/**
248
  * @}
249
  */
250
 
251
/**
252
  * @}
253
  */
254
 
255
#endif /* HAL_IWDG_MODULE_ENABLED */
256
/**
257
  * @}
258
  */
259
 
260
/**
261
  * @}
262
  */
263
 
264
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/