Skip to content

Commit 1b40d99

Browse files
committed
fm175: add timeouts to loops.
1 parent 00fd739 commit 1b40d99

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

rnfc-fm175xx/src/iso14443a.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use embassy_futures::yield_now;
2-
use embassy_time::{Duration, Timer};
2+
use embassy_time::{Duration, Instant, Timer};
33
use embedded_hal::digital::{InputPin, OutputPin};
44
use embedded_hal_async::digital::Wait;
55
use rnfc_traits::iso14443a_ll as ll;
@@ -175,7 +175,14 @@ where
175175
});
176176

177177
let mut tx_done = false;
178+
let deadline = Instant::now() + Duration::from_secs(1);
178179
loop {
180+
// make sure to not loop forever if timeri never fires for whatever reason.
181+
if Instant::now() > deadline {
182+
warn!("emergency timeout");
183+
return Err(Error::Other);
184+
}
185+
179186
let mut irqs = r.regs().commirq().read();
180187

181188
if irqs.timeri() {

rnfc-fm175xx/src/lib.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod regs;
1010

1111
use core::convert::Infallible;
1212

13-
use embassy_time::{with_timeout, Duration, TimeoutError, Timer};
13+
use embassy_time::{with_timeout, Duration, Instant, TimeoutError, Timer};
1414
use embedded_hal::digital::{InputPin, OutputPin};
1515
use embedded_hal_async::digital::Wait;
1616
pub use interface::*;
@@ -133,7 +133,14 @@ where
133133

134134
debug!("softreset");
135135
self.regs().command().write(|w| w.set_command(regs::CommandVal::SOFTRESET));
136-
while self.regs().command().read().command() != regs::CommandVal::IDLE {}
136+
137+
let deadline = Instant::now() + Duration::from_secs(1);
138+
while self.regs().command().read().command() != regs::CommandVal::IDLE {
139+
if Instant::now() > deadline {
140+
warn!("timeout waiting for softreset.");
141+
break;
142+
}
143+
}
137144

138145
// again, just in case
139146
Timer::after(Duration::from_millis(1)).await;
@@ -456,7 +463,13 @@ where
456463
//cortex_m::asm::delay(640_000); // 100ms
457464

458465
//info!("calib: waiting for irq..");
459-
while !self.regs().lpcd_irq().read().calib_irq() {}
466+
let deadline = Instant::now() + Duration::from_secs(1);
467+
while !self.regs().lpcd_irq().read().calib_irq() {
468+
if Instant::now() > deadline {
469+
warn!("timeout waiting for adc calibration.");
470+
break;
471+
}
472+
}
460473

461474
// calibra_en = 0
462475
self.regs().lpcd_ctrl1().write(|w| {

0 commit comments

Comments
 (0)