STM32 RTOS
0.2.3
Bare-metal preemptive RTOS for STM32F446RE
Toggle main menu visibility
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
23
static
DriverStatus_e
iwdgWaitUpdates
(
const
IwdgRegisters_t
*iwdg)
24
{
25
DriverStatus_e
status =
DRIVER_STATUS_OK
;
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
{
36
status =
DRIVER_STATUS_ERR_TIMEOUT
;
37
}
38
39
return
status;
40
}
41
42
DriverStatus_e
iwdgStart
(
IwdgRegisters_t
*iwdg, uint32_t prescaler, uint32_t reload)
43
{
44
DriverStatus_e
status =
DRIVER_STATUS_OK
;
45
46
if
((prescaler >
IWDG_PR_DIV256
) || (reload > (
IWDG_RLR_RL_Msk
>>
IWDG_RLR_RL_Pos
)))
47
{
48
status =
DRIVER_STATUS_ERR_INVALID_PARAM
;
49
}
50
51
if
(status ==
DRIVER_STATUS_OK
)
52
{
53
iwdg->
KR
=
IWDG_KEY_START
;
54
iwdg->
KR
=
IWDG_KEY_ENABLE_ACCESS
;
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
78
void
iwdgRefresh
(
IwdgRegisters_t
*iwdg)
79
{
80
iwdg->
KR
=
IWDG_KEY_RELOAD
;
81
}
82
83
void
iwdgFreezeInDebug
(
DbgmcuRegisters_t
*dbgmcu)
84
{
85
dbgmcu->
APB1_FZ
|=
DBGMCU_APB1_FZ_DBG_IWDG_STOP
;
86
}
87
DBGMCU_APB1_FZ_DBG_IWDG_STOP
#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
DriverStatus_e
Outcome of a driver call: a category for control flow, not a diagnosis.
Definition
driver_status.h:32
DRIVER_STATUS_OK
@ DRIVER_STATUS_OK
Succeeded.
Definition
driver_status.h:36
DRIVER_STATUS_ERR_INVALID_PARAM
@ DRIVER_STATUS_ERR_INVALID_PARAM
A parameter is outside its valid domain.
Definition
driver_status.h:38
DRIVER_STATUS_ERR_TIMEOUT
@ DRIVER_STATUS_ERR_TIMEOUT
A bounded wait for a hardware flag ran out.
Definition
driver_status.h:42
IWDG_UPDATE_TIMEOUT_ITERATIONS
#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
iwdgStart
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
iwdgFreezeInDebug
void iwdgFreezeInDebug(DbgmcuRegisters_t *dbgmcu)
Stop the IWDG counter whenever the core is halted by a debugger.
Definition
iwdg.c:83
iwdgRefresh
void iwdgRefresh(IwdgRegisters_t *iwdg)
Reload the counter from RLR (KR = 0xAAAA).
Definition
iwdg.c:78
iwdgWaitUpdates
static DriverStatus_e iwdgWaitUpdates(const IwdgRegisters_t *iwdg)
Poll SR until no prescaler or reload update is in progress.
Definition
iwdg.c:23
IWDG_PR_PR_Pos
#define IWDG_PR_PR_Pos
Bits 2:0.
Definition
iwdg_reg.h:44
IWDG_SR_PVU
#define IWDG_SR_PVU
<span class="regaccess">ro</span> A PR update is in progress.
Definition
iwdg_reg.h:72
IWDG_KEY_RELOAD
#define IWDG_KEY_RELOAD
Reload the counter from RLR (refresh).
Definition
iwdg_reg.h:37
IWDG_PR_DIV256
#define IWDG_PR_DIV256
LSI / 256.
Definition
iwdg_reg.h:58
IWDG_RLR_RL_Pos
#define IWDG_RLR_RL_Pos
Bits 11:0.
Definition
iwdg_reg.h:63
IWDG_KEY_ENABLE_ACCESS
#define IWDG_KEY_ENABLE_ACCESS
Unlock PR and RLR for writing.
Definition
iwdg_reg.h:38
IWDG_RLR_RL_Msk
#define IWDG_RLR_RL_Msk
<span class="regaccess">rw</span> Counter reload value.
Definition
iwdg_reg.h:65
IWDG_KEY_START
#define IWDG_KEY_START
Start the watchdog; it cannot be stopped.
Definition
iwdg_reg.h:39
IWDG_SR_RVU
#define IWDG_SR_RVU
<span class="regaccess">ro</span> An RLR update is in progress.
Definition
iwdg_reg.h:75
iwdg.h
IWDG driver: start, refresh and debug-freeze the independent watchdog.
DbgmcuRegisters_t
DBGMCU register block.
Definition
dbgmcu_reg.h:24
DbgmcuRegisters_t::APB1_FZ
volatile uint32_t APB1_FZ
0x08: APB1 peripheral freeze while the core is halted
Definition
dbgmcu_reg.h:27
IwdgRegisters_t
IWDG register block.
Definition
iwdg_reg.h:26
IwdgRegisters_t::SR
volatile const uint32_t SR
0x0C: status, <span class="regaccess">ro</span>
Definition
iwdg_reg.h:30
IwdgRegisters_t::KR
volatile uint32_t KR
0x00: key, <span class="regaccess">wo</span>
Definition
iwdg_reg.h:27
IwdgRegisters_t::PR
volatile uint32_t PR
0x04: prescaler
Definition
iwdg_reg.h:28
IwdgRegisters_t::RLR
volatile uint32_t RLR
0x08: reload value
Definition
iwdg_reg.h:29
drivers
src
iwdg.c
Generated by
1.18.0