Skip to content

Commit 6f8b36f

Browse files
committed
Adapted watchdog for this crate
1 parent 2b540b4 commit 6f8b36f

File tree

3 files changed

+33
-65
lines changed

3 files changed

+33
-65
lines changed

examples/independent_watchdog.rs

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,33 @@
1+
// Originaly from stm32h7xx-hal, adapted for stm32g4xx-hal
2+
13
#![no_main]
24
#![no_std]
35

4-
#[macro_use]
5-
mod utilities;
6-
use stm32h7xx_hal::{
7-
independent_watchdog::IndependentWatchdog, pac, prelude::*,
6+
//#[macro_use]
7+
//mod utils;
8+
use stm32g4xx_hal::{
9+
independent_watchdog::IndependentWatchdog, prelude::*,
10+
stm32::Peripherals
811
};
912

1013
use cortex_m_rt::entry;
1114

12-
use log::info;
15+
// TODO: Use utils instead
16+
use defmt_rtt as _; // global logger
17+
use panic_probe as _;
18+
pub use defmt::Logger;
19+
20+
use defmt::info; // TODO: Use utils::logger instead
1321

1422
#[entry]
1523
fn main() -> ! {
16-
utilities::logger::init();
17-
let dp = pac::Peripherals::take().unwrap();
18-
19-
// Constrain and Freeze power
20-
info!("Setup PWR... ");
21-
let pwr = dp.PWR.constrain();
22-
let pwrcfg = example_power!(pwr).freeze();
24+
//utils::logger::init();
25+
let dp = Peripherals::take().unwrap();
2326

24-
// Constrain and Freeze clock
25-
info!("Setup RCC... ");
26-
let rcc = dp.RCC.constrain();
27-
let _ccdr = rcc.sys_ck(96.MHz()).freeze(pwrcfg, &dp.SYSCFG);
28-
29-
#[cfg(any(feature = "rm0433", feature = "rm0455"))]
3027
let mut watchdog = IndependentWatchdog::new(dp.IWDG);
3128

32-
// Dual core parts
33-
#[cfg(all(feature = "rm0399", feature = "cm7"))]
34-
let mut watchdog = IndependentWatchdog::new(dp.IWDG1);
35-
#[cfg(all(feature = "rm0399", feature = "cm4"))]
36-
let mut watchdog = IndependentWatchdog::new(dp.IWDG2);
37-
38-
// RM0468
39-
#[cfg(all(feature = "rm0468"))]
40-
let mut watchdog = IndependentWatchdog::new(dp.IWDG1);
41-
4229
info!("");
43-
info!("stm32h7xx-hal example - Watchdog");
30+
info!("stm32g4xx-hal example - Watchdog");
4431
info!("");
4532

4633
// If the watchdog is working correctly this print should
@@ -49,7 +36,7 @@ fn main() -> ! {
4936

5037
// Enable the watchdog with a limit of 32.76 seconds (which is the maximum this watchdog can do) and wait forever
5138
// -> restart the chip
52-
watchdog.start(32_760.millis());
39+
watchdog.start(32_760.ms());
5340

5441
// Alternatively, there's also a windowed option where if the watchdog is fed before the window time, it will reset the chip as well
5542
// watchdog.start_windowed(100.millis(), 200.millis());

src/independent_watchdog.rs

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,15 @@
66
//!
77
//! The Independent Watchdog peripheral triggers a system reset when its internal counter expires.
88
//!
9-
//! # Peripheral Naming
10-
//!
11-
//! The naming of the Independent Watchdog peripheral varies between parts
12-
//!
13-
//! | Parts | IWDG Peripheral | Second IWDG Peripheral |
14-
//! | --- | --- | --- |
15-
//! | stm32H742/743/750/753/7a3/7b0/7b3 | IWDG | - |
16-
//! | stm32h745/747/755/757 | IWDG1 | IWDG2 |
17-
//! | stm32h723/725/730/733/735 | IWDG1 | - |
18-
//!
199
//! # Examples
2010
//!
21-
//! - [IWDG Example](https://github.com/stm32-rs/stm32h7xx-hal/blob/master/examples/independent_watchdog.rs)
22-
use crate::{prelude::*, time::MilliSeconds};
23-
24-
#[cfg(any(feature = "rm0433", feature = "rm0455"))]
25-
use crate::stm32::iwdg::pr::PR_A;
26-
#[cfg(any(feature = "rm0433", feature = "rm0455"))]
27-
use crate::stm32::IWDG;
28-
29-
#[cfg(any(all(feature = "rm0399", feature = "cm7"), feature = "rm0468"))]
30-
use crate::stm32::iwdg1::pr::PR_A;
31-
#[cfg(any(all(feature = "rm0399", feature = "cm7"), feature = "rm0468"))]
32-
use crate::stm32::IWDG1 as IWDG;
33-
34-
#[cfg(all(feature = "rm0399", feature = "cm4"))]
35-
use crate::stm32::iwdg2::pr::PR_A;
36-
#[cfg(all(feature = "rm0399", feature = "cm4"))]
37-
use crate::stm32::IWDG2 as IWDG;
11+
//! - [IWDG Example](todo-insert-link-here)
12+
//!
13+
//! Originally from stm32h7-hal, adapted for stm32g4xx-hal
14+
use crate::{time::{MicroSecond, U32Ext}, stm32::{
15+
IWDG,
16+
iwdg::pr::PR_A,
17+
}};
3818

3919
/// The implementation of the hardware IWDG
4020
pub struct IndependentWatchdog {
@@ -91,13 +71,13 @@ impl IndependentWatchdog {
9171

9272
/// Start the watchdog where it must be fed before the max time is over and
9373
/// not before the min time has passed
94-
pub fn start_windowed<T: Into<MilliSeconds>>(
74+
pub fn start_windowed<T: Into<MicroSecond>>(
9575
&mut self,
9676
min_window_time: T,
9777
max_window_time: T,
9878
) {
99-
let min_window_time: MilliSeconds = min_window_time.into();
100-
let max_window_time: MilliSeconds = max_window_time.into();
79+
let min_window_time: MicroSecond = min_window_time.into();
80+
let max_window_time: MicroSecond = max_window_time.into();
10181

10282
// Start the watchdog
10383
self.iwdg.kr.write(|w| w.key().start());
@@ -107,7 +87,7 @@ impl IndependentWatchdog {
10787
// Set the prescaler
10888
let (prescaler, _) = Self::MAX_MILLIS_FOR_PRESCALER
10989
.iter()
110-
.find(|(_, max_millis)| *max_millis >= max_window_time.to_millis())
90+
.find(|(_, max_millis)| *max_millis >= max_window_time.0 / 1000)
11191
.expect("IWDG max time is greater than is possible");
11292
while self.iwdg.sr.read().pvu().bit_is_set() {
11393
cortex_m::asm::nop();
@@ -123,10 +103,10 @@ impl IndependentWatchdog {
123103
.write(|w| w.win().bits(Self::MAX_COUNTER_VALUE as u16));
124104

125105
// Calculate the counter values
126-
let reload_value = max_window_time.to_millis()
106+
let reload_value = (max_window_time.0 / 1000)
127107
* (Self::CLOCK_SPEED / 1000)
128108
/ Self::get_prescaler_divider(prescaler);
129-
let window_value = min_window_time.to_millis()
109+
let window_value = (min_window_time.0 / 1000)
130110
* (Self::CLOCK_SPEED / 1000)
131111
/ Self::get_prescaler_divider(prescaler);
132112

@@ -157,8 +137,8 @@ impl IndependentWatchdog {
157137
}
158138

159139
/// Start the watchdog with the given max time and no minimal time
160-
pub fn start<T: Into<MilliSeconds>>(&mut self, max_time: T) {
161-
self.start_windowed(0_u32.millis(), max_time.into());
140+
pub fn start<T: Into<MicroSecond>>(&mut self, max_time: T) {
141+
self.start_windowed(0_u32.ms(), max_time.into());
162142
}
163143

164144
fn get_prescaler_divider(prescaler: &PR_A) -> u32 {

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,4 @@ pub mod syscfg;
8585
pub mod time;
8686
pub mod timer;
8787
// pub mod watchdog;
88+
pub mod independent_watchdog;

0 commit comments

Comments
 (0)