Skip to content

Commit

Permalink
[stm32] add basic IWDG watchdog implementation
Browse files Browse the repository at this point in the history
Co-authored-by: Victor Costa <victor.houriecosta@zuehlke.com>
  • Loading branch information
2 people authored and rleh committed May 5, 2023
1 parent e01b5ac commit c87056f
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
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
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
}

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")

0 comments on commit c87056f

Please sign in to comment.