Subversion Repositories DashDisplay

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/**
2
  ******************************************************************************
3
  * @file    stm32f1xx_hal_wwdg.c
4
  * @author  MCD Application Team
5 mjames 5
  * @version V1.0.4
6
  * @date    29-April-2016
2 mjames 7
  * @brief   WWDG HAL module driver.
8
  *          This file provides firmware functions to manage the following
9
  *          functionalities of the Window Watchdog (WWDG) peripheral:
10
  *           + Initialization and de-initialization functions
11
  *           + IO operation functions
12
  *           + Peripheral State functions      
13
  *        
14
  @verbatim
15
  ==============================================================================
16
                      ##### WWDG specific features #####
17
  ==============================================================================
18
  [..]
19
    Once enabled the WWDG generates a system reset on expiry of a programmed
20
    time period, unless the program refreshes the Counter (T[6;0] downcounter)
21
    before reaching 0x3F value (i.e. a reset is generated when the counter
22
    value rolls over from 0x40 to 0x3F).
23
 
24
    (+) An MCU reset is also generated if the counter value is refreshed
25
        before the counter has reached the refresh window value. This
26
        implies that the counter must be refreshed in a limited window.
27
    (+) Once enabled the WWDG cannot be disabled except by a system reset.
28
    (+) WWDGRST flag in RCC_CSR register can be used to inform when a WWDG
29
        reset occurs.              
30
    (+) The WWDG counter input clock is derived from the APB clock divided
31
        by a programmable prescaler.
32
    (+) WWDG clock (Hz) = PCLK / (4096 * Prescaler)
33
    (+) WWDG timeout (mS) = 1000 * (T[5;0] + 1) / WWDG clock
34
        where T[5;0] are the lowest 6 bits of Counter.
35
    (+) WWDG Counter refresh is allowed between the following limits :
36
        (++) min time (mS) = 1000 * (Counter - Window) / WWDG clock
37
        (++) max time (mS) = 1000 * (Counter - 0x40) / WWDG clock
38
 
39
    (+) Min-max timeout value @48 MHz(PCLK): ~85,3us / ~5,46 ms
40
 
41
                     ##### How to use this driver #####
42
  ==============================================================================
43
  [..]
44
    (+) Enable WWDG APB1 clock using __HAL_RCC_WWDG_CLK_ENABLE().
45
    (+) Set the WWDG prescaler, refresh window and counter value
46
        using HAL_WWDG_Init() function.
47
    (+) Start the WWDG using HAL_WWDG_Start() function.
48
        When the WWDG is enabled the counter value should be configured to
49
        a value greater than 0x40 to prevent generating an immediate reset.
50
    (+) Optionally you can enable the Early Wakeup Interrupt (EWI) which is
51
        generated when the counter reaches 0x40, and then start the WWDG using
52
        HAL_WWDG_Start_IT(). At EWI HAL_WWDG_WakeupCallback is executed and user can
53
        add his own code by customization of function pointer HAL_WWDG_WakeupCallback
54
        Once enabled, EWI interrupt cannot be disabled except by a system reset.          
55
    (+) Then the application program must refresh the WWDG counter at regular
56
        intervals during normal operation to prevent an MCU reset, using
57
        HAL_WWDG_Refresh() function. This operation must occur only when
58
        the counter is lower than the refresh window value already programmed.
59
 
60
     *** WWDG HAL driver macros list ***
61
     ==================================
62
     [..]
63
       Below the list of most used macros in WWDG HAL driver.
64
 
65
      (+) __HAL_WWDG_ENABLE: Enable the WWDG peripheral
66
      (+) __HAL_WWDG_GET_FLAG: Get the selected WWDG's flag status
67
      (+) __HAL_WWDG_CLEAR_FLAG: Clear the WWDG's pending flags
68
      (+) __HAL_WWDG_ENABLE_IT:  Enables the WWDG early wakeup interrupt
69
 
70
  @endverbatim
71
  ******************************************************************************
72
  * @attention
73
  *
5 mjames 74
  * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
2 mjames 75
  *
76
  * Redistribution and use in source and binary forms, with or without modification,
77
  * are permitted provided that the following conditions are met:
78
  *   1. Redistributions of source code must retain the above copyright notice,
79
  *      this list of conditions and the following disclaimer.
80
  *   2. Redistributions in binary form must reproduce the above copyright notice,
81
  *      this list of conditions and the following disclaimer in the documentation
82
  *      and/or other materials provided with the distribution.
83
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
84
  *      may be used to endorse or promote products derived from this software
85
  *      without specific prior written permission.
86
  *
87
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
88
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
89
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
90
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
91
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
92
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
93
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
94
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
95
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
96
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
97
  *
98
  ******************************************************************************
99
  */
100
 
101
/* Includes ------------------------------------------------------------------*/
102
#include "stm32f1xx_hal.h"
103
 
104
/** @addtogroup STM32F1xx_HAL_Driver
105
  * @{
106
  */
107
 
108
/** @defgroup WWDG WWDG
109
  * @brief WWDG HAL module driver.
110
  * @{
111
  */
112
 
113
#ifdef HAL_WWDG_MODULE_ENABLED
114
 
115
/* Private typedef -----------------------------------------------------------*/
116
/* Private define ------------------------------------------------------------*/
117
/* Private macro -------------------------------------------------------------*/
118
/* Private variables ---------------------------------------------------------*/
119
/* Private function prototypes -----------------------------------------------*/
120
/* Exported functions --------------------------------------------------------*/
121
 
122
/** @defgroup WWDG_Exported_Functions WWDG Exported Functions
123
  * @{
124
  */
125
 
126
/** @defgroup WWDG_Exported_Functions_Group1 Initialization and de-initialization functions
127
 *  @brief    Initialization and Configuration functions.
128
 *
129
@verbatim      
130
  ==============================================================================
131
          ##### Initialization and de-initialization functions #####
132
  ==============================================================================
133
  [..]  
134
    This section provides functions allowing to:
135
    (+) Initialize the WWDG according to the specified parameters
136
        in the WWDG_InitTypeDef and create the associated handle
137
    (+) DeInitialize the WWDG peripheral
138
    (+) Initialize the WWDG MSP
139
    (+) DeInitialize the WWDG MSP
140
 
141
@endverbatim
142
  * @{
143
  */
144
 
145
/**
146
  * @brief  Initializes the WWDG according to the specified
147
  *         parameters in the WWDG_InitTypeDef and creates the associated handle.
148
  * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
149
  *              the configuration information for the specified WWDG module.
150
  * @retval HAL status
151
  */
152
HAL_StatusTypeDef HAL_WWDG_Init(WWDG_HandleTypeDef *hwwdg)
153
{
154
  /* Check the WWDG handle allocation */
155
  if(hwwdg == NULL)
156
  {
157
    return HAL_ERROR;
158
  }
159
 
160
  /* Check the parameters */
161
  assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
162
  assert_param(IS_WWDG_PRESCALER(hwwdg->Init.Prescaler));
163
  assert_param(IS_WWDG_WINDOW(hwwdg->Init.Window));
164
  assert_param(IS_WWDG_COUNTER(hwwdg->Init.Counter));
165
 
166
  if(hwwdg->State == HAL_WWDG_STATE_RESET)
167
  {
168
    /* Allocate lock resource and initialize it */
169
    hwwdg->Lock = HAL_UNLOCKED;
170
 
171
    /* Init the low level hardware */
172
    HAL_WWDG_MspInit(hwwdg);
173
  }
174
 
175
  /* Change WWDG peripheral state */
176
  hwwdg->State = HAL_WWDG_STATE_BUSY;
177
 
178
  /* Set WWDG Prescaler and Window */
179
  MODIFY_REG(hwwdg->Instance->CFR, (WWDG_CFR_WDGTB | WWDG_CFR_W), (hwwdg->Init.Prescaler | hwwdg->Init.Window));
180
 
181
  /* Set WWDG Counter */
182
  MODIFY_REG(hwwdg->Instance->CR, WWDG_CR_T, hwwdg->Init.Counter);
183
 
184
  /* Change WWDG peripheral state */
185
  hwwdg->State = HAL_WWDG_STATE_READY;
186
 
187
  /* Return function status */
188
  return HAL_OK;
189
}
190
 
191
/**
192
  * @brief  DeInitializes the WWDG peripheral.
193
  * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
194
  *              the configuration information for the specified WWDG module.
195
  * @retval HAL status
196
  */
197
HAL_StatusTypeDef HAL_WWDG_DeInit(WWDG_HandleTypeDef *hwwdg)
198
{
199
  /* Check the WWDG handle allocation */
200
  if(hwwdg == NULL)
201
  {
202
    return HAL_ERROR;
203
  }
204
 
205
  /* Check the parameters */
206
  assert_param(IS_WWDG_ALL_INSTANCE(hwwdg->Instance));
207
 
208
  /* Change WWDG peripheral state */  
209
  hwwdg->State = HAL_WWDG_STATE_BUSY;
210
 
211
  /* DeInit the low level hardware */
212
  HAL_WWDG_MspDeInit(hwwdg);
213
 
214
  /* Reset WWDG Control register */
215
  hwwdg->Instance->CR  = (uint32_t)0x0000007F;
216
 
217
  /* Reset WWDG Configuration register */
218
  hwwdg->Instance->CFR = (uint32_t)0x0000007F;
219
 
220
  /* Reset WWDG Status register */
221
  hwwdg->Instance->SR  = 0;
222
 
223
  /* Change WWDG peripheral state */    
224
  hwwdg->State = HAL_WWDG_STATE_RESET;
225
 
226
  /* Release Lock */
227
  __HAL_UNLOCK(hwwdg);
228
 
229
  /* Return function status */
230
  return HAL_OK;
231
}
232
 
233
/**
234
  * @brief  Initializes the WWDG MSP.
235
  * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
236
  *              the configuration information for the specified WWDG module.
237
  * @retval None
238
  */
239
__weak void HAL_WWDG_MspInit(WWDG_HandleTypeDef *hwwdg)
240
{
5 mjames 241
  /* Prevent unused argument(s) compilation warning */
242
  UNUSED(hwwdg);
2 mjames 243
  /* NOTE: This function Should not be modified, when the callback is needed,
244
           the HAL_WWDG_MspInit could be implemented in the user file
245
   */
246
}
247
 
248
/**
249
  * @brief  DeInitializes the WWDG MSP.
250
  * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
251
  *              the configuration information for the specified WWDG module.
252
  * @retval None
253
  */
254
__weak void HAL_WWDG_MspDeInit(WWDG_HandleTypeDef *hwwdg)
255
{
5 mjames 256
  /* Prevent unused argument(s) compilation warning */
257
  UNUSED(hwwdg);
2 mjames 258
  /* NOTE: This function Should not be modified, when the callback is needed,
259
           the HAL_WWDG_MspDeInit could be implemented in the user file
260
   */
261
}
262
 
263
/**
264
  * @}
265
  */
266
 
267
/** @defgroup WWDG_Exported_Functions_Group2 IO operation functions  
268
 *  @brief   IO operation functions  
269
 *
270
@verbatim  
271
  ==============================================================================
272
                       ##### IO operation functions #####
273
  ==============================================================================  
274
  [..]  
275
    This section provides functions allowing to:
276
    (+) Start the WWDG.
277
    (+) Refresh the WWDG.
278
    (+) Handle WWDG interrupt request.
279
 
280
@endverbatim
281
  * @{
282
  */
283
 
284
/**
285
  * @brief  Starts the WWDG.
286
  * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
287
  *              the configuration information for the specified WWDG module.
288
  * @retval HAL status
289
  */
290
HAL_StatusTypeDef HAL_WWDG_Start(WWDG_HandleTypeDef *hwwdg)
291
{
292
  /* Process Locked */
293
  __HAL_LOCK(hwwdg);
294
 
295
  /* Change WWDG peripheral state */  
296
  hwwdg->State = HAL_WWDG_STATE_BUSY;
297
 
298
  /* Enable the peripheral */
299
  __HAL_WWDG_ENABLE(hwwdg);  
300
 
301
  /* Change WWDG peripheral state */    
302
  hwwdg->State = HAL_WWDG_STATE_READY;
303
 
304
  /* Process Unlocked */
305
  __HAL_UNLOCK(hwwdg);
306
 
307
  /* Return function status */
308
  return HAL_OK;
309
}
310
 
311
/**
312
  * @brief  Starts the WWDG with interrupt enabled.
313
  * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
314
  *              the configuration information for the specified WWDG module.
315
  * @retval HAL status
316
  */
317
HAL_StatusTypeDef HAL_WWDG_Start_IT(WWDG_HandleTypeDef *hwwdg)
318
{
319
  /* Process Locked */
320
  __HAL_LOCK(hwwdg);
321
 
322
  /* Change WWDG peripheral state */  
323
  hwwdg->State = HAL_WWDG_STATE_BUSY;
324
 
325
  /* Enable the Early Wakeup Interrupt */
326
  __HAL_WWDG_ENABLE_IT(hwwdg, WWDG_IT_EWI);
327
 
328
  /* Enable the peripheral */
329
  __HAL_WWDG_ENABLE(hwwdg);  
330
 
331
  /* Return function status */
332
  return HAL_OK;
333
}
334
 
335
/**
336
  * @brief  Refreshes the WWDG.
337
  * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
338
  *              the configuration information for the specified WWDG module.
339
  * @param  Counter: value of counter to put in WWDG counter
340
  * @retval HAL status
341
  */
342
HAL_StatusTypeDef HAL_WWDG_Refresh(WWDG_HandleTypeDef *hwwdg, uint32_t Counter)
343
{
344
  /* Process Locked */
345
  __HAL_LOCK(hwwdg);
346
 
347
  /* Change WWDG peripheral state */  
348
  hwwdg->State = HAL_WWDG_STATE_BUSY;
349
 
350
  /* Check the parameters */
351
  assert_param(IS_WWDG_COUNTER(Counter));
352
 
353
  /* Write to WWDG CR the WWDG Counter value to refresh with */
354
  MODIFY_REG(hwwdg->Instance->CR, (uint32_t)WWDG_CR_T, Counter);
355
 
356
  /* Change WWDG peripheral state */    
357
  hwwdg->State = HAL_WWDG_STATE_READY;
358
 
359
  /* Process Unlocked */
360
  __HAL_UNLOCK(hwwdg);
361
 
362
  /* Return function status */
363
  return HAL_OK;
364
}
365
 
366
/**
367
  * @brief  Handles WWDG interrupt request.
368
  * @note   The Early Wakeup Interrupt (EWI) can be used if specific safety operations
369
  *         or data logging must be performed before the actual reset is generated.
370
  *         The EWI interrupt is enabled when calling HAL_WWDG_Start_IT function.
371
  *         When the downcounter reaches the value 0x40, and EWI interrupt is
372
  *         generated and the corresponding Interrupt Service Routine (ISR) can
373
  *         be used to trigger specific actions (such as communications or data
374
  *         logging), before resetting the device.
375
  * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
376
  *              the configuration information for the specified WWDG module.
377
  * @retval None
378
  */
379
void HAL_WWDG_IRQHandler(WWDG_HandleTypeDef *hwwdg)
380
{
381
  /* Check if Early Wakeup Interrupt is enable */
382
  if(__HAL_WWDG_GET_IT_SOURCE(hwwdg, WWDG_IT_EWI) != RESET)
383
  {
384
    /* Wheck if WWDG Early Wakeup Interrupt occurred */
385
  if(__HAL_WWDG_GET_FLAG(hwwdg, WWDG_FLAG_EWIF) != RESET)
386
  {
387
    /* Early Wakeup callback */
388
    HAL_WWDG_WakeupCallback(hwwdg);
389
 
390
    /* Change WWDG peripheral state */
391
    hwwdg->State = HAL_WWDG_STATE_READY;
392
 
393
      /* Clear the WWDG Early Wakeup flag */
394
    __HAL_WWDG_CLEAR_FLAG(hwwdg, WWDG_FLAG_EWIF);
395
 
396
    /* Process Unlocked */
397
    __HAL_UNLOCK(hwwdg);
398
  }
399
}
400
}
401
 
402
/**
403
  * @brief  Early Wakeup WWDG callback.
404
  * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
405
  *              the configuration information for the specified WWDG module.
406
  * @retval None
407
  */
408
__weak void HAL_WWDG_WakeupCallback(WWDG_HandleTypeDef* hwwdg)
409
{
5 mjames 410
  /* Prevent unused argument(s) compilation warning */
411
  UNUSED(hwwdg);
2 mjames 412
  /* NOTE: This function Should not be modified, when the callback is needed,
413
           the HAL_WWDG_WakeupCallback could be implemented in the user file
414
   */
415
}
416
 
417
/**
418
  * @}
419
  */
420
 
421
/** @defgroup WWDG_Exported_Functions_Group3 Peripheral State functions
422
 *  @brief    Peripheral State functions.
423
 *
424
@verbatim  
425
  ==============================================================================
426
                        ##### Peripheral State functions #####
427
  ==============================================================================  
428
  [..]
429
    This subsection permits to get in run-time the status of the peripheral
430
    and the data flow.
431
 
432
@endverbatim
433
  * @{
434
  */
435
 
436
/**
437
  * @brief  Returns the WWDG state.
438
  * @param  hwwdg: pointer to a WWDG_HandleTypeDef structure that contains
439
  *              the configuration information for the specified WWDG module.
440
  * @retval HAL state
441
  */
442
HAL_WWDG_StateTypeDef HAL_WWDG_GetState(WWDG_HandleTypeDef *hwwdg)
443
{
444
  return hwwdg->State;
445
}
446
 
447
/**
448
  * @}
449
  */
450
 
451
/**
452
  * @}
453
  */
454
 
455
#endif /* HAL_WWDG_MODULE_ENABLED */
456
/**
457
  * @}
458
  */
459
 
460
/**
461
  * @}
462
  */
463
 
464
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/