Watchdog
For each independent watchdog (IWDG) enabled in CubeMX, the generator emits an STM32Watchdog object and the code that feeds it automatically. Feeding is done either by a software timer task or by a thread of its own.
Watchdog Code Examples
The following code comes from an STM32F407 project. The // Watchdog section, after the terminal and the database, creates the watchdog object:
static STM32Watchdog iwdg(&hiwdg, 1000, 250);
The constructor takes the HAL handle, the timeout and the feed interval (ms). It computes the prescaler and reload values from the LSI clock, turns the window off on series whose IWDG has one, and then calls HAL_IWDG_Init() to write the configuration and start the watchdog.
Then come the first feed and the timer task, which feeds the watchdog every 250 ms by default:
iwdg.Feed();
static auto iwdg_task =
Timer::CreateTask(iwdg.TaskFun, reinterpret_cast<LibXR::Watchdog*>(&iwdg), 250);
Timer::Add(iwdg_task);
Timer::Start(iwdg_task);
With Watchdog.run_as_thread set to true, a thread of its own feeds the watchdog instead:
iwdg.Feed();
static Thread iwdg_thread;
iwdg_thread.Create(reinterpret_cast<LibXR::Watchdog*>(&iwdg), iwdg.ThreadFun,
"iwdg_wdg", 1024, Thread::Priority::HIGH);
Configuration File
The following excerpt comes from User/libxr_config.yaml of the same project:
IWDG:
iwdg:
timeout_ms: 1000
feed_interval_ms: 250
Watchdog:
run_as_thread: false
feed_interval_ms: 250
timeout_ms and feed_interval_ms of each instance under IWDG are the timeout and feed interval passed to the constructor, 1000 ms and 250 ms by default; the feed interval must not exceed the timeout.
The settings under Watchdog apply to all IWDG instances:
run_as_thread: withtrue, a thread feeds the watchdog in a loop at the instance'sfeed_interval_ms, andthread_stack_depth(default 1024) andthread_priority(default 3, that isHIGH; see Software Timer) are added;feed_interval_ms: period of the timer task, 250 ms by default.
After editing the file, regenerate the code with the commands in Regenerating the Code.
Notes
The MX_IWDG_Init() generated by STM32CubeMX stays generated and called. In main() it calls HAL_IWDG_Init() with the prescaler and reload values set in CubeMX, and the IWDG starts counting. Once started, the IWDG can only be stopped by a reset; STM32Watchdog::Stop() only stops the automatic feeding and returns ErrorCode::NOT_SUPPORT.
The STM32Watchdog constructor calls HAL_IWDG_Init() again, replaces the CubeMX settings with the prescaler and reload values for timeout_ms, and reloads the counter. On series whose IWDG has a window, such as F0, F3, F7, G0, G4, H7 and L0 (the HAL defines IWDG_WINDOW_DISABLE), the constructor also turns the window off, so a feed is valid at any time; the IWDG of F1 and F4 has no window.
Nothing feeds the watchdog before the constructor runs. The timeout set in CubeMX has to be longer than the startup between MX_IWDG_Init() and the constructor, including the time the database takes to erase Flash sectors on its first initialization.