libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::STM32Watchdog Class Reference

STM32 IWDG 独立看门狗 / Independent Watchdog. More...

#include <stm32_watchdog.hpp>

Inheritance diagram for LibXR::STM32Watchdog:
[legend]
Collaboration diagram for LibXR::STM32Watchdog:
[legend]

Public Member Functions

 STM32Watchdog (IWDG_HandleTypeDef *hiwdg, uint32_t timeout_ms=1000, uint32_t feed_ms=250, uint32_t clock=LSI_VALUE)
 
ErrorCode SetConfig (const Configuration &config) override
 初始化硬件并设置超时时间 Initialize hardware and set overflow time
 
ErrorCode Feed () override
 立即手动喂狗 Feed the watchdog immediately
 
ErrorCode Start () override
 启动看门狗 / Start the watchdog
 
ErrorCode Stop () override
 停止看门狗 / Stop the watchdog
 
- Public Member Functions inherited from LibXR::Watchdog
 Watchdog ()
 构造函数 Constructor
 
virtual ~Watchdog ()
 虚析构函数 Virtual destructor
 

Data Fields

IWDG_HandleTypeDef * hiwdg_
 STM32 HAL IWDG handle.
 
uint32_t clock_
 LSI clock in Hz.
 
- Data Fields inherited from LibXR::Watchdog
uint32_t timeout_ms_ = 3000
 溢出时间
 
uint32_t feed_ms_ = 1000
 自动喂狗间隔
 
bool auto_feed_ = false
 是否自动喂狗
 

Additional Inherited Members

- Static Public Member Functions inherited from LibXR::Watchdog
static void ThreadFun (Watchdog *wdg)
 Watchdog 自动喂狗线程函数 Watchdog thread function for auto-feeding.
 
static void TaskFun (Watchdog *wdg)
 Watchdog 自动喂狗定时器任务函数 Watchdog timer task function for auto-feeding.
 

Detailed Description

STM32 IWDG 独立看门狗 / Independent Watchdog.

Definition at line 16 of file stm32_watchdog.hpp.

Constructor & Destructor Documentation

◆ STM32Watchdog()

LibXR::STM32Watchdog::STM32Watchdog ( IWDG_HandleTypeDef * hiwdg,
uint32_t timeout_ms = 1000,
uint32_t feed_ms = 250,
uint32_t clock = LSI_VALUE )
inlineexplicit

Definition at line 19 of file stm32_watchdog.hpp.

21 : hiwdg_(hiwdg), clock_(clock)
22 {
23 ASSERT(hiwdg);
24 SetConfig({timeout_ms, feed_ms});
25 }
uint32_t clock_
LSI clock in Hz.
IWDG_HandleTypeDef * hiwdg_
STM32 HAL IWDG handle.
ErrorCode SetConfig(const Configuration &config) override
初始化硬件并设置超时时间 Initialize hardware and set overflow time

Member Function Documentation

◆ Feed()

ErrorCode LibXR::STM32Watchdog::Feed ( )
inlineoverridevirtual

立即手动喂狗 Feed the watchdog immediately

Returns
操作结果的错误码 Error code of the operation

Implements LibXR::Watchdog.

Definition at line 86 of file stm32_watchdog.hpp.

87 {
88 return HAL_IWDG_Refresh(hiwdg_) == HAL_OK ? ErrorCode::OK : ErrorCode::FAILED;
89 }

◆ SetConfig()

ErrorCode LibXR::STM32Watchdog::SetConfig ( const Configuration & config)
inlineoverridevirtual

初始化硬件并设置超时时间 Initialize hardware and set overflow time

Parameters
config配置参数 Configuration
Returns
操作结果的错误码 Error code of the operation

Implements LibXR::Watchdog.

Definition at line 27 of file stm32_watchdog.hpp.

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 }
uint32_t feed_ms_
自动喂狗间隔
Definition watchdog.hpp:110
uint32_t timeout_ms_
溢出时间
Definition watchdog.hpp:109

◆ Start()

ErrorCode LibXR::STM32Watchdog::Start ( )
inlineoverridevirtual

启动看门狗 / Start the watchdog

Implements LibXR::Watchdog.

Definition at line 91 of file stm32_watchdog.hpp.

92 {
93 auto_feed_ = true;
94 if (HAL_IWDG_Init(hiwdg_) != HAL_OK)
95 {
96 return ErrorCode::FAILED;
97 }
98 return ErrorCode::OK;
99 }
bool auto_feed_
是否自动喂狗
Definition watchdog.hpp:111

◆ Stop()

ErrorCode LibXR::STM32Watchdog::Stop ( )
inlineoverridevirtual

停止看门狗 / Stop the watchdog

Implements LibXR::Watchdog.

Definition at line 101 of file stm32_watchdog.hpp.

102 {
103 /* STM32 IWDG 不支持关闭 */
104 auto_feed_ = false;
105 return ErrorCode::NOT_SUPPORT;
106 }

Field Documentation

◆ clock_

uint32_t LibXR::STM32Watchdog::clock_

LSI clock in Hz.

Definition at line 109 of file stm32_watchdog.hpp.

◆ hiwdg_

IWDG_HandleTypeDef* LibXR::STM32Watchdog::hiwdg_

STM32 HAL IWDG handle.

Definition at line 108 of file stm32_watchdog.hpp.


The documentation for this class was generated from the following file: