Skip to content

Commit 6d72622

Browse files
committed
[iwdg] Add initialize() with prescaler calculator
1 parent f647254 commit 6d72622

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

examples/nucleo_f072rb/independend_watchdog/main.cpp renamed to examples/nucleo_f072rb/iwdg/main.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <modm/platform.hpp>
1414

1515
using namespace Board;
16+
using namespace std::chrono_literals;
1617

1718
/**
1819
* If the button is pressed for more than 4 seconds, the MCU will be reset by the Watchdog.
@@ -25,7 +26,7 @@ main()
2526
Board::initialize();
2627
LedD13::setOutput();
2728
// set the watchdog timeout to 4 seconds
28-
Iwdg::initialize(Iwdg::Prescaler::Div32, 0x0FFFu);
29+
Iwdg::initialize<SystemClock, 4s>();
2930

3031
// Use the logging streams to print some messages.
3132
// Change MODM_LOG_LEVEL above to enable or disable these messages

src/modm/architecture/interface/peripheral.hpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,21 @@ class PeripheralDriver
8484
* This method checks if the user requested baudrate is within error
8585
* tolerance of the system achievable baudrate.
8686
*/
87-
template< baudrate_t available, baudrate_t requested, percent_t tolerance >
87+
template< uint64_t available, uint64_t requested, percent_t tolerance >
8888
static void
8989
assertBaudrateInTolerance()
9090
{
9191
static_assert(modm::isValueInTolerance(requested, available, tolerance),
9292
"The closest available baudrate exceeds the tolerance of the requested baudrate!");
9393
}
94+
95+
template< double available, double requested, percent_t tolerance >
96+
static void
97+
assertDurationInTolerance()
98+
{
99+
static_assert(modm::isValueInTolerance(requested, available, tolerance),
100+
"The closest available duration exceeds the tolerance of the requested duration!");
101+
}
94102
};
95103

96104
} // namespace modm

src/modm/platform/iwdg/stm32/iwdg.hpp

+26-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2023, Zühlke Engineering (Austria) GmbH
3+
* Copyright (c) 2024, Niklas Hauser
34
*
45
* This file is part of the modm project.
56
*
@@ -10,14 +11,16 @@
1011
// ----------------------------------------------------------------------------
1112

1213
#pragma once
14+
#include <modm/architecture/interface/peripheral.hpp>
15+
#include <modm/math/algorithm/prescaler_counter.hpp>
1316
#include "../device.hpp"
1417

1518

1619
namespace modm::platform
1720
{
1821

1922
/// @ingroup modm_platform_iwdg
20-
class Iwdg
23+
class Iwdg : public ::modm::PeripheralDriver
2124
{
2225
public:
2326
enum class
@@ -43,15 +46,19 @@ class Iwdg
4346
};
4447

4548
public:
46-
static inline void
47-
initialize(Prescaler prescaler, uint32_t reload)
49+
template< class SystemClock, milliseconds_t timeout, percent_t tolerance=pct(1) >
50+
static void
51+
initialize()
4852
{
49-
writeKey(writeCommand);
50-
IWDG->PR = uint32_t(prescaler);
51-
IWDG->RLR = reload;
52-
writeKey(0); // disable access to PR and RLR registers
53+
constexpr double frequency = 1000.0 / timeout.count();
54+
constexpr auto result = modm::GenericPrescalerCounter<double>::from_power(
55+
SystemClock::Iwdg, frequency, 1ul << 12, 256, 4);
56+
assertDurationInTolerance< 1.0 / result.frequency, 1.0 / frequency, tolerance >();
57+
58+
configure(Prescaler(result.index), result.counter - 1);
5359
}
5460

61+
5562
static inline void
5663
enable()
5764
{
@@ -71,15 +78,24 @@ class Iwdg
7178
}
7279

7380
private:
74-
static constexpr uint16_t reloadCommand = 0xAAAA;
75-
static constexpr uint16_t writeCommand = 0x5555;
76-
static constexpr uint16_t enableCommand = 0xCCCC;
81+
static inline void
82+
configure(Prescaler prescaler, uint16_t reload)
83+
{
84+
writeKey(writeCommand);
85+
IWDG->PR = uint32_t(prescaler);
86+
IWDG->RLR = reload;
87+
writeKey(0); // disable access to PR and RLR registers
88+
}
7789

7890
static inline void
7991
writeKey(uint16_t key)
8092
{
8193
IWDG->KR = key;
8294
}
95+
96+
static constexpr uint16_t reloadCommand = 0xAAAA;
97+
static constexpr uint16_t writeCommand = 0x5555;
98+
static constexpr uint16_t enableCommand = 0xCCCC;
8399
};
84100

85101
}

src/modm/platform/iwdg/stm32/module.lb

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def prepare(module, options):
2121
if device.identifier.family in ["h7"]:
2222
return False
2323

24-
module.depends(":cmsis:device")
24+
module.depends(":cmsis:device", ":math:algorithm")
2525
return device.has_driver("iwdg:stm32")
2626

2727

0 commit comments

Comments
 (0)