Skip to content

Commit c87056f

Browse files
wald-zatvictorandrehc
authored andcommitted
[stm32] add basic IWDG watchdog implementation
Co-authored-by: Victor Costa <[email protected]>
1 parent e01b5ac commit c87056f

File tree

4 files changed

+155
-0
lines changed

4 files changed

+155
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,30 @@ Please [discover modm's peripheral drivers for your specific device][discover].
396396
<td align="center">✕</td>
397397
<td align="center">✕</td>
398398
</tr><tr>
399+
<td align="left">IWDG</td>
400+
<td align="center">✅</td>
401+
<td align="center">✅</td>
402+
<td align="center">✅</td>
403+
<td align="center">✅</td>
404+
<td align="center">✅</td>
405+
<td align="center">✅</td>
406+
<td align="center">✅</td>
407+
<td align="center">✅</td>
408+
<td align="center">○</td>
409+
<td align="center">✅</td>
410+
<td align="center">✅</td>
411+
<td align="center">✅</td>
412+
<td align="center">✅</td>
413+
<td align="center">✅</td>
414+
<td align="center">✕</td>
415+
<td align="center">✕</td>
416+
<td align="center">✕</td>
417+
<td align="center">✕</td>
418+
<td align="center">✕</td>
419+
<td align="center">✕</td>
420+
<td align="center">✕</td>
421+
<td align="center">✕</td>
422+
</tr><tr>
399423
<td align="left">Random Generator</td>
400424
<td align="center">✕</td>
401425
<td align="center">✕</td>

src/modm/platform/iwdg/stm32/iwdg.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2023, Zühlke Engineering (Austria) GmbH
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include "iwdg.hpp"
13+
14+
void
15+
Iwdg::initialize(Iwdg::Prescaler prescaler, uint32_t reload)
16+
{
17+
writeKey(writeCommand);
18+
IWDG->PR = (IWDG->PR & ~static_cast<uint32_t>(Iwdg::Prescaler::All)) |
19+
static_cast<uint32_t>(prescaler);
20+
IWDG->RLR = (IWDG->RLR & ~IWDG_RLR_RL) | (reload << IWDG_RLR_RL_Pos);
21+
writeKey(0); // disable access to PR and RLR registers
22+
}
23+
24+
void
25+
Iwdg::enable()
26+
{
27+
writeKey(enableCommand);
28+
}
29+
30+
void
31+
Iwdg::trigger()
32+
{
33+
writeKey(reloadCommand);
34+
}
35+
36+
void
37+
Iwdg::writeKey(uint32_t key)
38+
{
39+
IWDG->KR = (IWDG->KR & ~IWDG_KR_KEY_Msk) | ((key & IWDG_KR_KEY_Msk) << IWDG_KR_KEY_Pos);
40+
}
41+
42+
Iwdg::Status
43+
Iwdg::getStatus()
44+
{
45+
const auto status = IWDG->SR & (IWDG_SR_PVU | IWDG_SR_RVU);
46+
return static_cast<Status>(status);
47+
};

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2023, Zühlke Engineering (Austria) GmbH
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#pragma once
13+
#include "../device.hpp"
14+
15+
class Iwdg
16+
{
17+
public:
18+
enum class
19+
Prescaler : uint32_t
20+
{
21+
Div4 = 0,
22+
Div8 = IWDG_PR_PR_0,
23+
Div16 = IWDG_PR_PR_1,
24+
Div32 = IWDG_PR_PR_1 | IWDG_PR_PR_0,
25+
Div64 = IWDG_PR_PR_2,
26+
Div128 = IWDG_PR_PR_2 | IWDG_PR_PR_0,
27+
Div256 = IWDG_PR_PR_2 | IWDG_PR_PR_1,
28+
All = IWDG_PR_PR_2 | IWDG_PR_PR_1 | IWDG_PR_PR_0,
29+
};
30+
31+
enum class
32+
Status : uint32_t
33+
{
34+
None = 0,
35+
Prescaler = IWDG_SR_PVU,
36+
Reload = IWDG_SR_RVU,
37+
All = IWDG_SR_PVU | IWDG_SR_RVU,
38+
};
39+
40+
static void
41+
initialize(Prescaler prescaler, uint32_t reload);
42+
static void
43+
enable();
44+
static void
45+
trigger();
46+
static Status
47+
getStatus();
48+
49+
private:
50+
static constexpr uint32_t reloadCommand = 0xAAAA;
51+
static constexpr uint32_t writeCommand = 0x5555;
52+
static constexpr uint32_t enableCommand = 0xCCCC;
53+
54+
static void
55+
writeKey(uint32_t key);
56+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2023, Zühlke Engineering (Austria) GmbH
5+
#
6+
# This file is part of the modm project.
7+
#
8+
# This Source Code Form is subject to the terms of the Mozilla Public
9+
# License, v. 2.0. If a copy of the MPL was not distributed with this
10+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
# -----------------------------------------------------------------------------
12+
13+
def init(module):
14+
module.name = ":platform:iwdg"
15+
module.description = "Independent watchdog"
16+
17+
def prepare(module, options):
18+
device = options[":target"]
19+
target = device.identifier
20+
if target["family"] in ["h7"]:
21+
# STM32H7 is not yet supported with any IWDG implementation im modm
22+
return False
23+
return device.has_driver("iwdg:stm32")
24+
25+
def build(env):
26+
env.outbasepath = "modm/src/modm/platform/iwdg"
27+
env.copy("iwdg.hpp")
28+
env.copy("iwdg.cpp")

0 commit comments

Comments
 (0)