Skip to content

Commit

Permalink
feat: add configuration struct to init (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksKovalenko authored Feb 14, 2024
1 parent 85dceea commit 59d9331
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
12 changes: 3 additions & 9 deletions hal_st/stm32fxxx/WatchDogStm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@

namespace hal
{
WatchDogStm::WatchDogStm(const infra::Function<void()>& onExpired)
WatchDogStm::WatchDogStm(const infra::Function<void()>& onExpired, const Config& config)
: onExpired(onExpired)
, interruptRegistration(WWDG_IRQn, [this]()
{
Interrupt();
})
{
__WWDG_CLK_ENABLE();

// WWDG clock (Hz) = PCLK1 / (4096 * Prescaler) --> 54Mhz / 32768 = 1728 Hz
// WWDG timeout (mS) = 1000 * Counter / WWDG clock --> 73 ms
// WWDG Counter refresh is allowed between the following limits :
// min time (mS) = 1000 * (Counter _ Window) / WWDG clock --> 0
// max time (mS) = 1000 * (Counter _ 0x40) / WWDG clock --> 36 ms
handle.Instance = WWDG;
handle.Init.Prescaler = WWDG_PRESCALER_8;
handle.Init.Prescaler = config.prescaler;
handle.Init.Window = WWDG_CR_T;
handle.Init.Counter = WWDG_CR_T;
#ifdef STM32F7
Expand All @@ -33,7 +27,7 @@ namespace hal
NVIC_SetPriority(WWDG_IRQn, 0);
WWDG->CFR |= WWDG_CFR_EWI;

feedingTimer.Start(std::chrono::milliseconds(25), [this]()
feedingTimer.Start(config.feedTimerInterval, [this]()
{
Feed();
});
Expand Down
17 changes: 16 additions & 1 deletion hal_st/stm32fxxx/WatchDogStm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@ namespace hal
class WatchDogStm
{
public:
WatchDogStm(const infra::Function<void()>& onExpired);

struct Config
{
constexpr Config()
{}

// WWDG clock (Hz) = PCLK1 / (4096 * Prescaler) --> 54Mhz / 32768 = 1728 Hz
// WWDG timeout (mS) = 1000 * Counter / WWDG clock --> 73 ms
// WWDG Counter refresh is allowed between the following limits :
// min time (mS) = 1000 * (Counter _ Window) / WWDG clock --> 0
// max time (mS) = 1000 * (Counter _ 0x40) / WWDG clock --> 36 ms
uint32_t prescaler { WWDG_PRESCALER_8 };
infra::Duration feedTimerInterval{ std::chrono::milliseconds(25) };
};

WatchDogStm(const infra::Function<void()>& onExpired, const Config& config = Config());

void Interrupt();

Expand Down

0 comments on commit 59d9331

Please sign in to comment.