Skip to content

Commit

Permalink
[iwdg] Add initialize() with prescaler calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Jan 28, 2024
1 parent 43fed11 commit 2dc6769
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <modm/platform.hpp>

using namespace Board;
using namespace std::chrono_literals;

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

// Use the logging streams to print some messages.
// Change MODM_LOG_LEVEL above to enable or disable these messages
Expand Down
File renamed without changes.
10 changes: 9 additions & 1 deletion src/modm/architecture/interface/peripheral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,21 @@ class PeripheralDriver
* This method checks if the user requested baudrate is within error
* tolerance of the system achievable baudrate.
*/
template< baudrate_t available, baudrate_t requested, percent_t tolerance >
template< uint64_t available, uint64_t requested, percent_t tolerance >
static void
assertBaudrateInTolerance()
{
static_assert(modm::isValueInTolerance(requested, available, tolerance),
"The closest available baudrate exceeds the tolerance of the requested baudrate!");
}

template< double available, double requested, percent_t tolerance >
static void
assertDurationInTolerance()
{
static_assert(modm::isValueInTolerance(requested, available, tolerance),
"The closest available duration exceeds the tolerance of the requested duration!");
}
};

} // namespace modm
Expand Down
36 changes: 26 additions & 10 deletions src/modm/platform/iwdg/stm32/iwdg.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2023, Zühlke Engineering (Austria) GmbH
* Copyright (c) 2024, Niklas Hauser
*
* This file is part of the modm project.
*
Expand All @@ -10,14 +11,16 @@
// ----------------------------------------------------------------------------

#pragma once
#include <modm/architecture/interface/peripheral.hpp>
#include <modm/math/algorithm/prescaler_counter.hpp>
#include "../device.hpp"


namespace modm::platform
{

/// @ingroup modm_platform_iwdg
class Iwdg
class Iwdg : public ::modm::PeripheralDriver
{
public:
enum class
Expand All @@ -43,15 +46,19 @@ class Iwdg
};

public:
static inline void
initialize(Prescaler prescaler, uint32_t reload)
template< class SystemClock, milliseconds_t timeout, percent_t tolerance=pct(1) >
static void
initialize()
{
writeKey(writeCommand);
IWDG->PR = uint32_t(prescaler);
IWDG->RLR = reload;
writeKey(0); // disable access to PR and RLR registers
constexpr double frequency = 1000.0 / timeout.count();
constexpr auto result = modm::GenericPrescalerCounter<double>::from_power(
SystemClock::Iwdg, frequency, 1ul << 12, 256, 4);
assertDurationInTolerance< 1.0 / result.frequency, 1.0 / frequency, tolerance >();

configure(Prescaler(result.index), result.counter - 1);
}


static inline void
enable()
{
Expand All @@ -71,15 +78,24 @@ class Iwdg
}

private:
static constexpr uint16_t reloadCommand = 0xAAAA;
static constexpr uint16_t writeCommand = 0x5555;
static constexpr uint16_t enableCommand = 0xCCCC;
static inline void
configure(Prescaler prescaler, uint16_t reload)
{
writeKey(writeCommand);
IWDG->PR = uint32_t(prescaler);
IWDG->RLR = reload;
writeKey(0); // disable access to PR and RLR registers
}

static inline void
writeKey(uint16_t key)
{
IWDG->KR = key;
}

static constexpr uint16_t reloadCommand = 0xAAAA;
static constexpr uint16_t writeCommand = 0x5555;
static constexpr uint16_t enableCommand = 0xCCCC;
};

}
2 changes: 1 addition & 1 deletion src/modm/platform/iwdg/stm32/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def prepare(module, options):
if device.identifier.family in ["h7"]:
return False

module.depends(":cmsis:device")
module.depends(":cmsis:device", ":math:algorithm")
return device.has_driver("iwdg:stm32")


Expand Down

0 comments on commit 2dc6769

Please sign in to comment.