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 auto_feed_interval_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()

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

Definition at line 7 of file stm32_watchdog.cpp.

9 : hiwdg_(hiwdg), clock_(clock)
10{
11 ASSERT(hiwdg);
12 SetConfig({timeout_ms, feed_ms});
13 Start();
14}
uint32_t clock_
LSI clock in Hz.
ErrorCode Start() override
启动看门狗 / Start the watchdog
IWDG_HandleTypeDef * hiwdg_
STM32 HAL IWDG handle.
ErrorCode SetConfig(const Configuration &config) override
初始化硬件并设置超时时间 Initialize hardware and set overflow time

Member Function Documentation

◆ Feed()

ErrorCode STM32Watchdog::Feed ( )
overridevirtual

立即手动喂狗 Feed the watchdog immediately

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

Implements LibXR::Watchdog.

Definition at line 74 of file stm32_watchdog.cpp.

75{
76 return HAL_IWDG_Refresh(hiwdg_) == HAL_OK ? ErrorCode::OK : ErrorCode::FAILED;
77}

◆ SetConfig()

ErrorCode STM32Watchdog::SetConfig ( const Configuration & config)
overridevirtual

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

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

Implements LibXR::Watchdog.

Definition at line 16 of file stm32_watchdog.cpp.

17{
18 if (config.feed_ms == 0 || config.timeout_ms == 0 || config.feed_ms > config.timeout_ms)
19 {
20 ASSERT(false);
21 return ErrorCode::ARG_ERR;
22 }
23
24 /* 分频与重载自动计算 / Prescaler & reload auto calculation */
25 static const struct
26 {
27 uint8_t pr;
28 uint16_t div;
29 } TABLE[] = {{0, 4}, {1, 8}, {2, 16}, {3, 32}, {4, 64}, {5, 128}, {6, 256}};
30 uint32_t lsi = clock_;
31 uint8_t best_pr = 6;
32 uint16_t best_rlr = 0xFFF;
33 bool found = false;
34
35 for (const auto& item : TABLE)
36 {
37 uint32_t prescaler = item.div;
38 uint32_t reload = (config.timeout_ms * lsi) / (1000 * prescaler);
39 if (reload == 0)
40 {
41 reload = 1;
42 }
43 if (reload > 1)
44 {
45 reload--;
46 }
47 if (reload <= 0xFFF)
48 {
49 best_pr = item.pr;
50 best_rlr = static_cast<uint16_t>(reload);
51 found = true;
52 break;
53 }
54 }
55 if (!found)
56 {
57 return ErrorCode::NOT_SUPPORT;
58 }
59
60 timeout_ms_ = config.timeout_ms;
61 auto_feed_interval_ms = config.feed_ms;
62
63 hiwdg_->Init.Prescaler = best_pr;
64 hiwdg_->Init.Reload = best_rlr;
65#if defined(IWDG)
66 hiwdg_->Instance = IWDG; // NOLINT
67#elif defined(IWDG1)
68 hiwdg_->Instance = IWDG1; // NOLINT
69#endif
70
71 return ErrorCode::OK;
72}
uint32_t auto_feed_interval_ms
自动喂狗间隔
Definition watchdog.hpp:110
uint32_t timeout_ms_
溢出时间
Definition watchdog.hpp:109

◆ Start()

ErrorCode STM32Watchdog::Start ( )
overridevirtual

启动看门狗 / Start the watchdog

Implements LibXR::Watchdog.

Definition at line 79 of file stm32_watchdog.cpp.

80{
81 auto_feed_ = true;
82 if (HAL_IWDG_Init(hiwdg_) != HAL_OK)
83 {
84 return ErrorCode::FAILED;
85 }
86 return ErrorCode::OK;
87}
bool auto_feed_
是否自动喂狗
Definition watchdog.hpp:111

◆ Stop()

ErrorCode STM32Watchdog::Stop ( )
overridevirtual

停止看门狗 / Stop the watchdog

Implements LibXR::Watchdog.

Definition at line 89 of file stm32_watchdog.cpp.

90{
91 /* STM32 IWDG 不支持关闭 */
92 auto_feed_ = false;
93 return ErrorCode::NOT_SUPPORT;
94}

Field Documentation

◆ clock_

uint32_t LibXR::STM32Watchdog::clock_

LSI clock in Hz.

Definition at line 31 of file stm32_watchdog.hpp.

◆ hiwdg_

IWDG_HandleTypeDef* LibXR::STM32Watchdog::hiwdg_

STM32 HAL IWDG handle.

Definition at line 30 of file stm32_watchdog.hpp.


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