libxr
1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
stm32_watchdog.hpp
1
#pragma once
2
3
#include "main.h"
4
#include "watchdog.hpp"
5
6
#if defined(HAL_IWDG_MODULE_ENABLED)
7
8
namespace
LibXR
9
{
10
16
class
STM32Watchdog
:
public
Watchdog
17
{
18
public
:
19
explicit
STM32Watchdog
(IWDG_HandleTypeDef* hiwdg, uint32_t timeout_ms = 1000,
20
uint32_t feed_ms = 250, uint32_t clock = LSI_VALUE)
21
:
hiwdg_
(hiwdg),
clock_
(clock)
22
{
23
ASSERT(hiwdg);
24
SetConfig
({timeout_ms, feed_ms});
25
}
26
27
ErrorCode
SetConfig
(
const
Configuration
& config)
override
28
{
29
if
(config.
feed_ms
== 0 || config.
timeout_ms
== 0 ||
30
config.
feed_ms
> config.
timeout_ms
)
31
{
32
ASSERT(
false
);
33
return
ErrorCode::ARG_ERR;
34
}
35
36
/* 分频与重载自动计算 / Prescaler & reload auto calculation */
37
static
const
struct
38
{
39
uint8_t pr;
40
uint16_t div;
41
} TABLE[] = {{0, 4}, {1, 8}, {2, 16}, {3, 32}, {4, 64}, {5, 128}, {6, 256}};
42
uint32_t lsi =
clock_
;
43
uint8_t best_pr = 6;
44
uint16_t best_rlr = 0xFFF;
45
bool
found =
false
;
46
47
for
(
const
auto
& item : TABLE)
48
{
49
uint32_t prescaler = item.div;
50
uint32_t reload = (config.
timeout_ms
* lsi) / (1000 * prescaler);
51
if
(reload == 0)
52
{
53
reload = 1;
54
}
55
if
(reload > 1)
56
{
57
reload--;
58
}
59
if
(reload <= 0xFFF)
60
{
61
best_pr = item.pr;
62
best_rlr =
static_cast<
uint16_t
>
(reload);
63
found =
true
;
64
break
;
65
}
66
}
67
if
(!found)
68
{
69
return
ErrorCode::NOT_SUPPORT;
70
}
71
72
timeout_ms_
= config.
timeout_ms
;
73
feed_ms_
= config.
feed_ms
;
74
75
hiwdg_
->Init.Prescaler = best_pr;
76
hiwdg_
->Init.Reload = best_rlr;
77
#if defined(IWDG)
78
hiwdg_
->Instance = IWDG;
// NOLINT
79
#elif defined(IWDG1)
80
hiwdg_
->Instance = IWDG1;
// NOLINT
81
#endif
82
83
return
ErrorCode::OK;
84
}
85
86
ErrorCode
Feed
()
override
87
{
88
return
HAL_IWDG_Refresh(
hiwdg_
) == HAL_OK ? ErrorCode::OK : ErrorCode::FAILED;
89
}
90
91
ErrorCode
Start
()
override
92
{
93
auto_feed_
=
true
;
94
if
(HAL_IWDG_Init(
hiwdg_
) != HAL_OK)
95
{
96
return
ErrorCode::FAILED;
97
}
98
return
ErrorCode::OK;
99
}
100
101
ErrorCode
Stop
()
override
102
{
103
/* STM32 IWDG 不支持关闭 */
104
auto_feed_
=
false
;
105
return
ErrorCode::NOT_SUPPORT;
106
}
107
108
IWDG_HandleTypeDef*
hiwdg_
;
109
uint32_t
clock_
;
110
};
111
112
}
// namespace LibXR
113
114
#endif
LibXR::STM32Watchdog
STM32 IWDG 独立看门狗 / Independent Watchdog.
Definition
stm32_watchdog.hpp:17
LibXR::STM32Watchdog::clock_
uint32_t clock_
LSI clock in Hz.
Definition
stm32_watchdog.hpp:109
LibXR::STM32Watchdog::Feed
ErrorCode Feed() override
立即手动喂狗 Feed the watchdog immediately
Definition
stm32_watchdog.hpp:86
LibXR::STM32Watchdog::Start
ErrorCode Start() override
启动看门狗 / Start the watchdog
Definition
stm32_watchdog.hpp:91
LibXR::STM32Watchdog::Stop
ErrorCode Stop() override
停止看门狗 / Stop the watchdog
Definition
stm32_watchdog.hpp:101
LibXR::STM32Watchdog::hiwdg_
IWDG_HandleTypeDef * hiwdg_
STM32 HAL IWDG handle.
Definition
stm32_watchdog.hpp:108
LibXR::STM32Watchdog::SetConfig
ErrorCode SetConfig(const Configuration &config) override
初始化硬件并设置超时时间 Initialize hardware and set overflow time
Definition
stm32_watchdog.hpp:27
LibXR::Watchdog
通用看门狗(Watchdog)抽象接口 General Watchdog interface for both thread and task style usage
Definition
watchdog.hpp:14
LibXR::Watchdog::feed_ms_
uint32_t feed_ms_
自动喂狗间隔
Definition
watchdog.hpp:110
LibXR::Watchdog::auto_feed_
bool auto_feed_
是否自动喂狗
Definition
watchdog.hpp:111
LibXR::Watchdog::timeout_ms_
uint32_t timeout_ms_
溢出时间
Definition
watchdog.hpp:109
LibXR
LibXR 命名空间
Definition
ch32_gpio.hpp:9
LibXR::Watchdog::Configuration
看门狗配置结构体 Configuration for the watchdog
Definition
watchdog.hpp:27
LibXR::Watchdog::Configuration::feed_ms
uint32_t feed_ms
自动喂狗周期 Auto feed interval (ms, < timeout_ms)
Definition
watchdog.hpp:29
LibXR::Watchdog::Configuration::timeout_ms
uint32_t timeout_ms
看门狗溢出时间 Watchdog overflow time (ms)
Definition
watchdog.hpp:28
driver
st
stm32_watchdog.hpp
Generated by
1.12.0