-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[stm32] add basic IWDG watchdog implementation
Co-authored-by: Victor Costa <[email protected]>
- Loading branch information
Showing
4 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |