STM32 RTOS 0.2.3
Bare-metal preemptive RTOS for STM32F446RE
Loading...
Searching...
No Matches
iwdg.c
Go to the documentation of this file.
1
5#include "iwdg.h"
6
11
15#define IWDG_UPDATE_TIMEOUT_ITERATIONS (100000U)
16
24{
26 uint32_t timeout = IWDG_UPDATE_TIMEOUT_ITERATIONS;
27
28 while (((iwdg->SR & (IWDG_SR_PVU | IWDG_SR_RVU)) != 0U) && (timeout > 0U))
29 {
30 timeout--;
31 }
32
33 /* Re-read SR: an update that ends on the last poll still counts. */
34 if ((iwdg->SR & (IWDG_SR_PVU | IWDG_SR_RVU)) != 0U)
35 {
37 }
38
39 return status;
40}
41
42DriverStatus_e iwdgStart(IwdgRegisters_t *iwdg, uint32_t prescaler, uint32_t reload)
43{
45
46 if ((prescaler > IWDG_PR_DIV256) || (reload > (IWDG_RLR_RL_Msk >> IWDG_RLR_RL_Pos)))
47 {
49 }
50
51 if (status == DRIVER_STATUS_OK)
52 {
53 iwdg->KR = IWDG_KEY_START;
55
56 /* A write to PR or RLR is ignored while its previous update runs. */
57 status = iwdgWaitUpdates(iwdg);
58 }
59
60 if (status == DRIVER_STATUS_OK)
61 {
62 iwdg->PR = prescaler << IWDG_PR_PR_Pos;
63 iwdg->RLR = reload << IWDG_RLR_RL_Pos;
64
65 /* Refresh only once RLR has reached the counter's domain, or the
66 * refresh would load the old value. */
67 status = iwdgWaitUpdates(iwdg);
68 }
69
70 if (status == DRIVER_STATUS_OK)
71 {
72 iwdgRefresh(iwdg);
73 }
74
75 return status;
76}
77
79{
80 iwdg->KR = IWDG_KEY_RELOAD;
81}
82
87
#define DBGMCU_APB1_FZ_DBG_IWDG_STOP
<span class="regaccess">rw</span> Stop the IWDG counter while the core is halted.
Definition dbgmcu_reg.h:40
DriverStatus_e
Outcome of a driver call: a category for control flow, not a diagnosis.
@ DRIVER_STATUS_OK
Succeeded.
@ DRIVER_STATUS_ERR_INVALID_PARAM
A parameter is outside its valid domain.
@ DRIVER_STATUS_ERR_TIMEOUT
A bounded wait for a hardware flag ran out.
#define IWDG_UPDATE_TIMEOUT_ITERATIONS
Polls allowed for SR.PVU and SR.RVU to clear: an update takes up to 5 LSI cycles, under 300 us even a...
Definition iwdg.c:15
DRIVER_MUST_CHECK DriverStatus_e iwdgStart(IwdgRegisters_t *iwdg, uint32_t prescaler, uint32_t reload)
Start the watchdog, then set its prescaler and reload value.
Definition iwdg.c:42
void iwdgFreezeInDebug(DbgmcuRegisters_t *dbgmcu)
Stop the IWDG counter whenever the core is halted by a debugger.
Definition iwdg.c:83
void iwdgRefresh(IwdgRegisters_t *iwdg)
Reload the counter from RLR (KR = 0xAAAA).
Definition iwdg.c:78
static DriverStatus_e iwdgWaitUpdates(const IwdgRegisters_t *iwdg)
Poll SR until no prescaler or reload update is in progress.
Definition iwdg.c:23
#define IWDG_PR_PR_Pos
Bits 2:0.
Definition iwdg_reg.h:44
#define IWDG_SR_PVU
<span class="regaccess">ro</span> A PR update is in progress.
Definition iwdg_reg.h:72
#define IWDG_KEY_RELOAD
Reload the counter from RLR (refresh).
Definition iwdg_reg.h:37
#define IWDG_PR_DIV256
LSI / 256.
Definition iwdg_reg.h:58
#define IWDG_RLR_RL_Pos
Bits 11:0.
Definition iwdg_reg.h:63
#define IWDG_KEY_ENABLE_ACCESS
Unlock PR and RLR for writing.
Definition iwdg_reg.h:38
#define IWDG_RLR_RL_Msk
<span class="regaccess">rw</span> Counter reload value.
Definition iwdg_reg.h:65
#define IWDG_KEY_START
Start the watchdog; it cannot be stopped.
Definition iwdg_reg.h:39
#define IWDG_SR_RVU
<span class="regaccess">ro</span> An RLR update is in progress.
Definition iwdg_reg.h:75
IWDG driver: start, refresh and debug-freeze the independent watchdog.
DBGMCU register block.
Definition dbgmcu_reg.h:24
volatile uint32_t APB1_FZ
0x08: APB1 peripheral freeze while the core is halted
Definition dbgmcu_reg.h:27
IWDG register block.
Definition iwdg_reg.h:26
volatile const uint32_t SR
0x0C: status, <span class="regaccess">ro</span>
Definition iwdg_reg.h:30
volatile uint32_t KR
0x00: key, <span class="regaccess">wo</span>
Definition iwdg_reg.h:27
volatile uint32_t PR
0x04: prescaler
Definition iwdg_reg.h:28
volatile uint32_t RLR
0x08: reload value
Definition iwdg_reg.h:29