Skip to content

Commit f35eb51

Browse files
Johannes Cornelis Draaijerdatdenkikniet
authored andcommitted
We can't support ethernet timestamp interrupts on stm32f1
1 parent 41be631 commit f35eb51

File tree

4 files changed

+37
-47
lines changed

4 files changed

+37
-47
lines changed

examples/rtic-timestamp.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ mod app {
4545
use stm32_eth::{
4646
dma::{EthernetDMA, PacketId, RxRingEntry, TxRingEntry},
4747
mac::Speed,
48-
ptp::{EthernetPTP, Subseconds, Timestamp},
48+
ptp::{EthernetPTP, Timestamp},
4949
Parts,
5050
};
5151

@@ -145,14 +145,19 @@ mod app {
145145
// incorrect, but works well enough in low-activity systems (such as this example).
146146
let now = (cx.shared.ptp, cx.shared.scheduled_time).lock(|ptp, sched_time| {
147147
let now = ptp.get_time();
148-
let in_half_sec = now
149-
+ Timestamp::new(
150-
false,
151-
0,
152-
Subseconds::new_from_nanos(500_000_000).unwrap().raw(),
153-
)
154-
.unwrap();
155-
ptp.configure_target_time_interrupt(in_half_sec);
148+
#[cfg(not(feature = "stm32f107"))]
149+
{
150+
let in_half_sec = now
151+
+ Timestamp::new(
152+
false,
153+
0,
154+
stm32_eth::ptp::Subseconds::new_from_nanos(500_000_000)
155+
.unwrap()
156+
.raw(),
157+
)
158+
.unwrap();
159+
ptp.configure_target_time_interrupt(in_half_sec);
160+
}
156161
*sched_time = Some(now);
157162

158163
now
@@ -188,16 +193,19 @@ mod app {
188193
cx.shared.ptp,
189194
cx.shared.scheduled_time,
190195
)
191-
.lock(|dma, tx_id, ptp, sched_time| {
196+
.lock(|dma, tx_id, ptp, _sched_time| {
192197
dma.interrupt_handler();
193198

194-
if ptp.interrupt_handler() {
195-
if let Some(sched_time) = sched_time.take() {
196-
let now = ptp.get_time();
197-
defmt::info!(
198-
"Got a timestamp interrupt {} seconds after scheduling",
199-
now - sched_time
200-
);
199+
#[cfg(not(feature = "stm32f107"))]
200+
{
201+
if ptp.interrupt_handler() {
202+
if let Some(_sched_time) = sched_time.take() {
203+
let now = ptp.get_time();
204+
defmt::info!(
205+
"Got a timestamp interrupt {} seconds after scheduling",
206+
now - _sched_time
207+
);
208+
}
201209
}
202210
}
203211

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ where
106106

107107
// Configure the ethernet PTP
108108
#[cfg(feature = "ptp")]
109-
let ptp = EthernetPTP::new(&eth_mac, parts.ptp.into(), clocks, &dma);
109+
let ptp = EthernetPTP::new(parts.ptp.into(), clocks, &dma);
110110

111111
// Configure the ethernet MAC
112112
let mac = EthernetMAC::new(eth_mac, parts.mmc, clocks, Speed::FullDuplexBase100Tx, &dma)?;
@@ -175,7 +175,7 @@ where
175175

176176
// Configure the ethernet PTP
177177
#[cfg(feature = "ptp")]
178-
let ptp = EthernetPTP::new(&eth_mac, parts.ptp.into(), clocks, &dma);
178+
let ptp = EthernetPTP::new(parts.ptp.into(), clocks, &dma);
179179

180180
// Configure the ethernet MAC
181181
let mac = EthernetMAC::new(eth_mac, parts.mmc, clocks, Speed::FullDuplexBase100Tx, &dma)?

src/mac/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ impl EthernetMAC {
216216
mac.macimr.write(|w| w.tstim().set_bit());
217217
}
218218

219+
// NOTE(allow): only used on F4 and F7
220+
#[allow(dead_code)]
219221
pub(crate) fn unmask_timestamp_trigger_interrupt() {
220222
// SAFETY: MACIMR only receives atomic writes.
221223
let macimr = &unsafe { &*ETHERNET_MAC::ptr() }.macimr;

src/ptp/mod.rs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
//!
33
//! See [`EthernetPTP`] for a more details.
44
5-
use crate::{
6-
dma::EthernetDMA,
7-
hal::rcc::Clocks,
8-
mac::EthernetMAC,
9-
peripherals::{ETHERNET_MAC, ETHERNET_PTP},
10-
};
5+
use crate::{dma::EthernetDMA, hal::rcc::Clocks, mac::EthernetMAC, peripherals::ETHERNET_PTP};
116

127
mod timestamp;
138
pub use timestamp::Timestamp;
@@ -68,7 +63,6 @@ impl EthernetPTP {
6863
}
6964

7065
pub(crate) fn new(
71-
eth_mac: &ETHERNET_MAC,
7266
eth_ptp: ETHERNET_PTP,
7367
clocks: Clocks,
7468
// Note(_dma): this field exists to ensure that the PTP is not
@@ -77,7 +71,7 @@ impl EthernetPTP {
7771
_dma: &EthernetDMA,
7872
) -> Self {
7973
// Mask timestamp interrupt register
80-
eth_mac.macimr.modify(|_, w| w.tstim().set_bit());
74+
EthernetMAC::mask_timestamp_trigger_interrupt();
8175

8276
let hclk = clocks.hclk().to_Hz();
8377

@@ -197,7 +191,13 @@ impl EthernetPTP {
197191
}
198192
}
199193
}
194+
}
200195

196+
/// Setting and configuring target time interrupts on the STM32F107 does not
197+
/// make any sense: we can generate the interrupt, but it is impossible to
198+
/// clear the flag as the register required to do so does not exist.
199+
#[cfg(not(feature = "stm32f1xx-hal"))]
200+
impl EthernetPTP {
201201
/// Configure the target time.
202202
fn set_target_time(&mut self, timestamp: Timestamp) {
203203
let (high, low) = (timestamp.seconds(), timestamp.subseconds_signed());
@@ -223,27 +223,7 @@ impl EthernetPTP {
223223
/// was caused by a Timestamp trigger and clears the interrupt
224224
/// flag.
225225
pub fn interrupt_handler(&mut self) -> bool {
226-
#[cfg(not(feature = "stm32f1xx-hal"))]
227226
let is_tsint = self.eth_ptp.ptptssr.read().tsttr().bit_is_set();
228-
229-
#[cfg(feature = "stm32f1xx-hal")]
230-
// This is quite unsafe: the stm32f1xx user manual only mentions the
231-
// ETH_PTPTSSR register when describing the Time Stamp Trigger status
232-
// of the ETH_MACSR register. Given that this register exists at the
233-
// offset described below on F4 and F7, and testing has shown that this
234-
// register does indeed exist, we assume that the missing register is
235-
// simply inadequately documented.
236-
let is_tsint = {
237-
const PTPSSR_OFFSET: usize = 10;
238-
const TSTTR_MASK: u32 = 0x0000_0002;
239-
240-
let register_value = unsafe {
241-
core::ptr::read_volatile((ETHERNET_PTP::ptr() as *const u32).add(PTPSSR_OFFSET))
242-
};
243-
244-
register_value & TSTTR_MASK == TSTTR_MASK
245-
};
246-
247227
if is_tsint {
248228
EthernetMAC::mask_timestamp_trigger_interrupt();
249229
}

0 commit comments

Comments
 (0)