Skip to content

Commit 3520d66

Browse files
committed
Update EthernetPTP interrupt handling and async/await for a little better performance
1 parent b8c3dc7 commit 3520d66

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/ptp/mod.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,9 @@ impl EthernetPTP {
252252
pub async fn wait_until(&mut self, timestamp: Timestamp) {
253253
self.configure_target_time_interrupt(timestamp);
254254
core::future::poll_fn(|ctx| {
255-
// This is the happy path, the status bits have
256-
// usually been reset at this point.
257-
if EthernetPTP::get_time().raw() >= timestamp.raw() {
255+
if EthernetPTP::read_and_clear_interrupt_flag() {
258256
Poll::Ready(())
259-
} else if EthernetPTP::interrupt_handler() {
257+
} else if EthernetPTP::get_time().raw() >= timestamp.raw() {
260258
Poll::Ready(())
261259
} else {
262260
EthernetPTP::waker().register(ctx.waker());
@@ -266,20 +264,33 @@ impl EthernetPTP {
266264
.await;
267265
}
268266

267+
#[inline(always)]
268+
fn read_and_clear_interrupt_flag() -> bool {
269+
let eth_ptp = unsafe { &*ETHERNET_PTP::ptr() };
270+
eth_ptp.ptptssr.read().tsttr().bit_is_set()
271+
}
272+
269273
/// Returns a boolean indicating whether or not the interrupt
270274
/// was caused by a Timestamp trigger and clears the interrupt
271275
/// flag.
272276
pub fn interrupt_handler() -> bool {
273277
// SAFETY: we only perform one atomic read.
274-
let eth_ptp = unsafe { &*ETHERNET_PTP::ptr() };
278+
let eth_mac = unsafe { &*crate::peripherals::ETHERNET_MAC::ptr() };
275279

276-
let is_tsint = eth_ptp.ptptssr.read().tsttr().bit_is_set();
280+
let is_tsint = eth_mac.macsr.read().tsts().bit_is_set();
277281
if is_tsint {
278282
EthernetMAC::mask_timestamp_trigger_interrupt();
279283
}
280284

281285
#[cfg(feature = "async-await")]
282-
EthernetPTP::waker().wake();
286+
if let Some(waker) = EthernetPTP::waker().take() {
287+
waker.wake();
288+
} else {
289+
EthernetPTP::read_and_clear_interrupt_flag();
290+
}
291+
292+
#[cfg(not(feature = "async-await"))]
293+
EthernetPTP::read_and_clear_interrupt_flag();
283294

284295
is_tsint
285296
}

0 commit comments

Comments
 (0)