Skip to content
Snippets Groups Projects
Commit 57c754fb authored by Samuel Tardieu's avatar Samuel Tardieu
Browse files

Use window watchdog

parent 4ba5a893
No related branches found
No related tags found
No related merge requests found
......@@ -3,8 +3,8 @@ use crate::{
tb6612fng::{Movement, Tb6612fng},
};
use core::sync::atomic::{AtomicU32, Ordering};
use embassy_stm32::time::hz;
use embassy_time::{Instant, Timer};
use embassy_stm32::{pac::{self, wwdg::vals::Wdgtb}, peripherals::WWDG, time::hz};
use embassy_time::{Duration, Instant, Ticker};
use heapless::Deque;
use i2c2_target::{I2C2, MESSAGE_SIZE};
......@@ -94,19 +94,44 @@ fn current_millis() -> u32 {
Instant::now().as_millis() as u32
}
fn wwdg_configure() {
// With PCLK1 at 36MHz, 63 as a timeout value with wdgtb at /8
// means 58.25ms. Comparing with a value of 31 means that at
// least 29.125ms must have passed before the watchdog can be
// petted.
pac::WWDG.cfr().write(|w| {
w.set_wdgtb(Wdgtb::DIV8);
w.set_w((1 << 6) | 31);
});
wwdg_pet();
}
fn wwdg_pet() {
// Reset watchdog and start it if not started yet
pac::WWDG.cr().modify(|w| {
w.set_t((1 << 6) | 63);
w.set_wdga(true);
});
}
#[allow(clippy::used_underscore_binding)] // Clippy bug
#[embassy_executor::task]
pub async fn start_i2c_target(
i2c2: I2C2,
mut motors: Tb6612fng<'static>,
encoders: Encoders<'static>,
_wwdg: WWDG,
) {
motors.standby_enter();
unsafe {
STATE = Some(State::new(i2c2, motors, encoders));
STATE.as_mut().unwrap().i2c.start(&i2c_callback);
}
wwdg_configure();
let mut ticker = Ticker::every(Duration::from_millis(50));
loop {
Timer::after_millis(100).await;
ticker.next().await;
wwdg_pet();
if current_millis() >= WATCHDOG_EXPIRATION.load(Ordering::Relaxed) {
State::with_critical_section(|state| {
if state.is_moving()
......
......@@ -34,7 +34,7 @@ async fn main(spawner: Spawner) {
prediv: PllPreDiv::DIV1,
mul: PllMul::MUL9,
});
// APB1 speed is max 36MHz (SysClk/2)
// APB1 speed (PCLK1) is max 36MHz (SysClk/2), use that
config.rcc.apb1_pre = APBPrescaler::DIV2;
let p = embassy_stm32::init(config);
......@@ -62,8 +62,9 @@ async fn main(spawner: Spawner) {
let encoders = Encoders::new(p.PA0, p.PA1, p.PA6, p.PA7, p.TIM2, p.TIM3);
let i2c2 = i2c2_target::I2C2::new(p.I2C2, p.PB10, p.PB11, 0x57, Priority::P3);
spawner
.spawn(logic::start_i2c_target(i2c2, motors, encoders))
.spawn(logic::start_i2c_target(i2c2, motors, encoders, p.WWDG))
.unwrap();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment