Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stm32] Watchdog IWDG driver #1009

Merged
merged 2 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,30 @@ Please [discover modm's peripheral drivers for your specific device][discover].
<td align="center">✕</td>
<td align="center">✕</td>
</tr><tr>
<td align="left">IWDG</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">○</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✅</td>
<td align="center">✕</td>
<td align="center">✕</td>
<td align="center">✕</td>
<td align="center">✕</td>
<td align="center">✕</td>
<td align="center">✕</td>
<td align="center">✕</td>
<td align="center">✕</td>
</tr><tr>
<td align="left">Random Generator</td>
<td align="center">✕</td>
<td align="center">✕</td>
Expand Down
57 changes: 57 additions & 0 deletions examples/nucleo_f072rb/independend_watchdog/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2023, Zühlke Engineering (Austria) GmbH
rleh marked this conversation as resolved.
Show resolved Hide resolved
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <modm/platform.hpp>

using namespace Board;

/**
* If the button is pressed for more than 4 seconds, the MCU will be reset by the Watchdog.
* This can be observed by the faster blinking LED after startup.
*/

int
main()
{
Board::initialize();
LedD13::setOutput();
// set the watchdog timeout to 4 seconds
Iwdg::initialize(Iwdg::Prescaler::Div32, 0x0FFFu);

// Use the logging streams to print some messages.
// Change MODM_LOG_LEVEL above to enable or disable these messages
MODM_LOG_DEBUG << "debug" << modm::endl;
MODM_LOG_INFO << "info" << modm::endl;
MODM_LOG_WARNING << "warning" << modm::endl;
MODM_LOG_ERROR << "error" << modm::endl;

uint32_t counter(0);

while (counter < 10)
{
LedD13::toggle();
modm::delay(100ms);
MODM_LOG_INFO << "loop: " << counter++ << modm::endl;
}

Iwdg::enable();

while (1)
{
LedD13::toggle();
modm::delay(500ms);
if (!Button::read()) { Iwdg::trigger(); }
MODM_LOG_INFO << "loop: " << counter++ << modm::endl;
}

return 0;
}
10 changes: 10 additions & 0 deletions examples/nucleo_f072rb/independend_watchdog/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<library>
<extends>modm:nucleo-f072rb</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_f072rb/independend_watchdog</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:platform:iwdg</module>
</modules>
</library>
47 changes: 47 additions & 0 deletions src/modm/platform/iwdg/stm32/iwdg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2023, Zühlke Engineering (Austria) GmbH
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include "iwdg.hpp"

void
Iwdg::initialize(Iwdg::Prescaler prescaler, uint32_t reload)
{
writeKey(writeCommand);
IWDG->PR = (IWDG->PR & ~static_cast<uint32_t>(Iwdg::Prescaler::All)) |
static_cast<uint32_t>(prescaler);
IWDG->RLR = (IWDG->RLR & ~IWDG_RLR_RL) | (reload << IWDG_RLR_RL_Pos);
writeKey(0); // disable access to PR and RLR registers
}
victorandrehc marked this conversation as resolved.
Show resolved Hide resolved

void
Iwdg::enable()
{
writeKey(enableCommand);
}

void
Iwdg::trigger()
{
writeKey(reloadCommand);
}

void
Iwdg::writeKey(uint32_t key)
{
IWDG->KR = (IWDG->KR & ~IWDG_KR_KEY_Msk) | ((key & IWDG_KR_KEY_Msk) << IWDG_KR_KEY_Pos);
}

Iwdg::Status
Iwdg::getStatus()
{
const auto status = IWDG->SR & (IWDG_SR_PVU | IWDG_SR_RVU);
return static_cast<Status>(status);
};
56 changes: 56 additions & 0 deletions src/modm/platform/iwdg/stm32/iwdg.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2023, Zühlke Engineering (Austria) GmbH
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#pragma once
#include "../device.hpp"

class Iwdg
{
public:
enum class
Prescaler : uint32_t
{
Div4 = 0,
Div8 = IWDG_PR_PR_0,
Div16 = IWDG_PR_PR_1,
Div32 = IWDG_PR_PR_1 | IWDG_PR_PR_0,
Div64 = IWDG_PR_PR_2,
Div128 = IWDG_PR_PR_2 | IWDG_PR_PR_0,
Div256 = IWDG_PR_PR_2 | IWDG_PR_PR_1,
All = IWDG_PR_PR_2 | IWDG_PR_PR_1 | IWDG_PR_PR_0,
};

enum class
Status : uint32_t
{
None = 0,
Prescaler = IWDG_SR_PVU,
Reload = IWDG_SR_RVU,
All = IWDG_SR_PVU | IWDG_SR_RVU,
};

static void
initialize(Prescaler prescaler, uint32_t reload);
static void
enable();
static void
trigger();
static Status
getStatus();

private:
static constexpr uint32_t reloadCommand = 0xAAAA;
static constexpr uint32_t writeCommand = 0x5555;
static constexpr uint32_t enableCommand = 0xCCCC;

static void
writeKey(uint32_t key);
};
28 changes: 28 additions & 0 deletions src/modm/platform/iwdg/stm32/module.lb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2023, Zühlke Engineering (Austria) GmbH
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------

def init(module):
module.name = ":platform:iwdg"
module.description = "Independent watchdog"

def prepare(module, options):
device = options[":target"]
target = device.identifier
if target["family"] in ["h7"]:
# STM32H7 is not yet supported with any IWDG implementation im modm
return False
return device.has_driver("iwdg:stm32")

def build(env):
env.outbasepath = "modm/src/modm/platform/iwdg"
env.copy("iwdg.hpp")
env.copy("iwdg.cpp")