Skip to content

Commit 6c16e7d

Browse files
committed
Update examples
1 parent 7a7dbad commit 6c16e7d

File tree

5 files changed

+157
-174
lines changed

5 files changed

+157
-174
lines changed

examples/arp.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,5 @@ fn ETH() {
147147
*eth_pending = true;
148148
});
149149

150-
// Clear interrupt flags
151-
let p = unsafe { Peripherals::steal() };
152-
stm32_eth::eth_interrupt_handler(&p.ETHERNET_DMA);
150+
stm32_eth::eth_interrupt_handler();
153151
}

examples/pktgen.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,5 @@ fn ETH() {
182182
*eth_pending = true;
183183
});
184184

185-
// Clear interrupt flags
186-
let p = unsafe { Peripherals::steal() };
187-
stm32_eth::eth_interrupt_handler(&p.ETHERNET_DMA);
185+
stm32_eth::eth_interrupt_handler();
188186
}

examples/rtic-timestamp.rs

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ mod common;
3535
#[rtic::app(device = stm32_eth::stm32, dispatchers = [SPI1])]
3636
mod app {
3737

38+
use core::task::Poll;
39+
3840
use crate::common::EthernetPhy;
3941

4042
use fugit::ExtU64;
@@ -146,7 +148,7 @@ mod app {
146148
// Obtain the current time to use as the "TX time" of our frame. It is clearly
147149
// incorrect, but works well enough in low-activity systems (such as this example).
148150
let now = (cx.shared.ptp, cx.shared.scheduled_time).lock(|ptp, sched_time| {
149-
let now = ptp.get_time();
151+
let now = EthernetPTP::get_time();
150152
#[cfg(not(feature = "stm32f107"))]
151153
{
152154
let in_half_sec = now
@@ -184,22 +186,24 @@ mod app {
184186
});
185187
}
186188

187-
#[task(binds = ETH, shared = [dma, tx_id, ptp, scheduled_time], priority = 2)]
189+
#[task(binds = ETH, shared = [dma, tx_id, ptp, scheduled_time], local = [pkt_id_ctr: u32 = 0], priority = 2)]
188190
fn eth_interrupt(cx: eth_interrupt::Context) {
191+
let packet_id_ctr = cx.local.pkt_id_ctr;
192+
189193
(
190194
cx.shared.dma,
191195
cx.shared.tx_id,
192196
cx.shared.ptp,
193197
cx.shared.scheduled_time,
194198
)
195199
.lock(|dma, tx_id, ptp, _sched_time| {
196-
dma.interrupt_handler();
200+
let int_reason = stm32_eth::eth_interrupt_handler();
197201

198202
#[cfg(not(feature = "stm32f107"))]
199203
{
200-
if ptp.interrupt_handler() {
204+
if int_reason.time_passed {
201205
if let Some(sched_time) = _sched_time.take() {
202-
let now = ptp.get_time();
206+
let now = EthernetPTP::get_time();
203207
defmt::info!(
204208
"Got a timestamp interrupt {} seconds after scheduling",
205209
now - sched_time
@@ -208,64 +212,67 @@ mod app {
208212
}
209213
}
210214

211-
let mut packet_id = 0;
215+
loop {
216+
let packet_id = PacketId(*packet_id_ctr);
217+
let rx_timestamp = if let Ok(packet) = dma.recv_next(Some(packet_id.clone())) {
218+
let mut dst_mac = [0u8; 6];
219+
dst_mac.copy_from_slice(&packet[..6]);
212220

213-
while let Ok(packet) = dma.recv_next(Some(packet_id.into())) {
214-
let mut dst_mac = [0u8; 6];
215-
dst_mac.copy_from_slice(&packet[..6]);
221+
let ts = if let Some(timestamp) = packet.timestamp() {
222+
timestamp
223+
} else {
224+
continue;
225+
};
216226

217-
// Note that, instead of grabbing the timestamp from the `RxPacket` directly, it
218-
// is also possible to retrieve a cached version of the timestamp using
219-
// `EthernetDMA::get_timestamp_for_id` (in the same way as for TX timestamps).
220-
let ts = if let Some(timestamp) = packet.timestamp() {
221-
timestamp
222-
} else {
223-
continue;
224-
};
227+
defmt::debug!("RX timestamp: {}", ts);
225228

226-
defmt::debug!("RX timestamp: {}", ts);
229+
if dst_mac == [0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56] {
230+
let mut timestamp_data = [0u8; 8];
231+
timestamp_data.copy_from_slice(&packet[14..22]);
232+
let raw = i64::from_be_bytes(timestamp_data);
227233

228-
let timestamp = if dst_mac == [0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56] {
229-
let mut timestamp_data = [0u8; 8];
230-
timestamp_data.copy_from_slice(&packet[14..22]);
231-
let raw = i64::from_be_bytes(timestamp_data);
234+
let timestamp = Timestamp::new_raw(raw);
232235

233-
let timestamp = Timestamp::new_raw(raw);
234-
timestamp
235-
} else {
236-
continue;
237-
};
236+
defmt::debug!("Contained TX timestamp: {}", ts);
238237

239-
defmt::debug!("Contained TX timestamp: {}", ts);
238+
let diff = timestamp - ts;
240239

241-
let diff = timestamp - ts;
240+
defmt::info!("Difference between TX and RX time: {}", diff);
242241

243-
defmt::info!("Difference between TX and RX time: {}", diff);
242+
let addend = ptp.addend();
243+
let nanos = diff.nanos() as u64;
244244

245-
let addend = ptp.addend();
246-
let nanos = diff.nanos() as u64;
245+
if nanos <= 20_000 {
246+
let p1 = ((nanos * addend as u64) / 1_000_000_000) as u32;
247247

248-
if nanos <= 20_000 {
249-
let p1 = ((nanos * addend as u64) / 1_000_000_000) as u32;
248+
defmt::debug!("Addend correction value: {}", p1);
250249

251-
defmt::debug!("Addend correction value: {}", p1);
250+
if diff.is_negative() {
251+
ptp.set_addend(addend - p1 / 2);
252+
} else {
253+
ptp.set_addend(addend + p1 / 2);
254+
};
255+
} else {
256+
defmt::warn!("Updated time.");
257+
ptp.update_time(diff);
258+
}
259+
}
252260

253-
if diff.is_negative() {
254-
ptp.set_addend(addend - p1 / 2);
255-
} else {
256-
ptp.set_addend(addend + p1 / 2);
257-
};
261+
ts
258262
} else {
259-
defmt::warn!("Updated time.");
260-
ptp.update_time(diff);
261-
}
263+
break;
264+
};
265+
266+
let polled_ts = dma.poll_timestamp(&packet_id);
267+
268+
assert_eq!(polled_ts, Poll::Ready(Ok(Some(rx_timestamp))));
262269

263-
packet_id += 1;
264-
packet_id &= !0x8000_0000;
270+
*packet_id_ctr += 1;
271+
*packet_id_ctr &= !0x8000_0000;
265272
}
266273

267274
if let Some((tx_id, sent_time)) = tx_id.take() {
268-
if let Ok(ts) = dma.get_timestamp_for_id(PacketId(tx_id)) {
275+
if let Poll::Ready(Ok(Some(ts))) = dma.poll_timestamp(&PacketId(tx_id)) {
269276
defmt::info!("TX timestamp: {}", ts);
270277
defmt::debug!(
271278
"Diff between TX timestamp and the time that was put into the packet: {}",

examples/timesync/client.rs

Lines changed: 70 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub fn calculate_offset(
8080
mod app {
8181

8282
use crate::common::EthernetPhy;
83+
use core::task::Poll;
8384

8485
use ieee802_3_miim::{phy::PhySpeed, Phy};
8586
use systick_monotonic::Systick;
@@ -100,14 +101,13 @@ mod app {
100101
#[local]
101102
struct Local {
102103
start_addend: u32,
103-
}
104-
105-
#[shared]
106-
struct Shared {
107104
dma: EthernetDMA<'static, 'static>,
108105
ptp: EthernetPTP,
109106
}
110107

108+
#[shared]
109+
struct Shared {}
110+
111111
#[monotonic(binds = SysTick, default = true)]
112112
type Monotonic = Systick<1000>;
113113

@@ -179,21 +179,25 @@ mod app {
179179
runner::spawn().ok();
180180

181181
(
182-
Shared { dma, ptp },
183-
Local { start_addend },
182+
Shared {},
183+
Local {
184+
start_addend,
185+
ptp,
186+
dma,
187+
},
184188
init::Monotonics(mono),
185189
)
186190
}
187191

188-
#[task(shared = [dma, ptp], local = [addend_integrator: f32 = 0.0, start_addend, packet_id: u32 = 0])]
192+
#[task(local = [addend_integrator: f32 = 0.0, start_addend, packet_id: u32 = 0, dma, ptp])]
189193
fn runner(cx: runner::Context) {
190194
use fugit::ExtU64;
191195

192196
runner::spawn_after(100.millis()).ok();
193197

194-
let start = monotonics::now();
198+
let (dma, ptp) = (cx.local.dma, cx.local.ptp);
195199

196-
let (mut dma, mut ptp) = (cx.shared.dma, cx.shared.ptp);
200+
let start = monotonics::now();
197201

198202
let (addend_integrator, start_addend, packet_id) = (
199203
cx.local.addend_integrator,
@@ -210,26 +214,24 @@ mod app {
210214
return;
211215
}
212216

213-
let res = dma.lock(|dma| {
214-
if let Ok(rx_packet) = dma.recv_next(None) {
215-
if rx_packet.len() > 14
216-
&& &rx_packet[6..12] == &SERVER_ADDR
217-
&& &rx_packet[12..14] == &ETHER_TYPE
218-
{
219-
if let Some(timestamp) = rx_packet.timestamp() {
220-
let data_len = rx_packet.len() - 14;
221-
buf[..data_len].copy_from_slice(&rx_packet[14..14 + data_len]);
222-
Ok((&buf[..data_len], timestamp))
223-
} else {
224-
Err(true)
225-
}
217+
let res = if let Ok(rx_packet) = dma.recv_next(None) {
218+
if rx_packet.len() > 14
219+
&& &rx_packet[6..12] == &SERVER_ADDR
220+
&& &rx_packet[12..14] == &ETHER_TYPE
221+
{
222+
if let Some(timestamp) = rx_packet.timestamp() {
223+
let data_len = rx_packet.len() - 14;
224+
buf[..data_len].copy_from_slice(&rx_packet[14..14 + data_len]);
225+
Ok((&buf[..data_len], timestamp))
226226
} else {
227-
Err(false)
227+
Err(true)
228228
}
229229
} else {
230230
Err(false)
231231
}
232-
});
232+
} else {
233+
Err(false)
234+
};
233235

234236
if let Ok(res) = res {
235237
break res;
@@ -245,23 +247,22 @@ mod app {
245247
let current_id = PacketId(*packet_id);
246248
*packet_id += 1;
247249
let current_clone = current_id.clone();
248-
dma.lock(|dma| {
249-
dma.send(14 + $data.len(), Some(current_clone), |buf| {
250-
buf[0..6].copy_from_slice(&BROADCAST);
251-
buf[6..12].copy_from_slice(&CLIENT_ADDR);
252-
buf[12..14].copy_from_slice(&ETHER_TYPE);
253-
buf[14..14 + $data.len()].copy_from_slice($data);
254-
})
255-
.unwrap();
256-
});
250+
251+
dma.send(14 + $data.len(), Some(current_clone), |buf| {
252+
buf[0..6].copy_from_slice(&BROADCAST);
253+
buf[6..12].copy_from_slice(&CLIENT_ADDR);
254+
buf[12..14].copy_from_slice(&ETHER_TYPE);
255+
buf[14..14 + $data.len()].copy_from_slice($data);
256+
})
257+
.unwrap();
257258

258259
loop {
259260
if monotonics::now() - 500u64.millis() > start {
260261
return;
261262
}
262263

263264
let current_id = current_id.clone();
264-
if let Ok(timestamp) = dma.lock(|dma| dma.get_timestamp_for_id(current_id)) {
265+
if let Poll::Ready(Ok(Some(timestamp))) = dma.poll_timestamp(&current_id) {
265266
break timestamp;
266267
}
267268
}
@@ -310,57 +311,48 @@ mod app {
310311

311312
let offset = crate::calculate_offset(t1, t1_prim, t2, t2_prim);
312313

313-
ptp.lock(|ptp| {
314-
let now = ptp.get_time();
315-
if offset.seconds() > 0 || offset.nanos() > 200_000 {
316-
*addend_integrator = 0.0;
317-
defmt::info!("Updating time. Offset {} ", offset);
318-
let updated_time = now + offset;
319-
ptp.set_time(updated_time);
320-
} else {
321-
let mut offset_nanos = offset.nanos() as i64;
322-
if offset.is_negative() {
323-
offset_nanos *= -1;
324-
}
325-
326-
let error = (offset_nanos * start_addend as i64) / 1_000_000_000;
327-
*addend_integrator += error as f32 / 500.;
328-
329-
defmt::info!(
330-
"Error: {}. Integrator: {}, Offset: {} ns",
331-
error,
332-
addend_integrator,
333-
offset_nanos
334-
);
335-
336-
defmt::debug!(
337-
"DATA: {}, {}, {}, {}",
338-
error,
339-
addend_integrator,
340-
offset_nanos,
341-
now
342-
);
343-
344-
let new_addend =
345-
(start_addend as i64 + error / 4 + (*addend_integrator as i64)) as u32;
346-
ptp.set_addend(new_addend);
314+
let now = EthernetPTP::get_time();
315+
if offset.seconds() > 0 || offset.nanos() > 200_000 {
316+
*addend_integrator = 0.0;
317+
defmt::info!("Updating time. Offset {} ", offset);
318+
let updated_time = now + offset;
319+
ptp.set_time(updated_time);
320+
} else {
321+
let mut offset_nanos = offset.nanos() as i64;
322+
if offset.is_negative() {
323+
offset_nanos *= -1;
347324
}
348-
});
325+
326+
let error = (offset_nanos * start_addend as i64) / 1_000_000_000;
327+
*addend_integrator += error as f32 / 500.;
328+
329+
defmt::info!(
330+
"Error: {}. Integrator: {}, Offset: {} ns",
331+
error,
332+
addend_integrator,
333+
offset_nanos
334+
);
335+
336+
defmt::debug!(
337+
"DATA: {}, {}, {}, {}",
338+
error,
339+
addend_integrator,
340+
offset_nanos,
341+
now
342+
);
343+
344+
let new_addend = (start_addend as i64 + error / 4 + (*addend_integrator as i64)) as u32;
345+
ptp.set_addend(new_addend);
346+
}
349347

350348
defmt::debug!(
351349
"The exchange took {} ms",
352350
(monotonics::now() - start).to_millis()
353351
);
354352
}
355353

356-
#[task(binds = ETH, shared = [dma, ptp], priority = 2)]
357-
fn eth_interrupt(cx: eth_interrupt::Context) {
358-
let (dma, ptp) = (cx.shared.dma, cx.shared.ptp);
359-
(dma, ptp).lock(|dma, _ptp| {
360-
dma.interrupt_handler();
361-
362-
#[cfg(not(feature = "stm32f107"))]
363-
_ptp.interrupt_handler();
364-
})
354+
#[task(binds = ETH, priority = 2)]
355+
fn eth_interrupt(_: eth_interrupt::Context) {
356+
stm32_eth::eth_interrupt_handler();
365357
}
366358
}

0 commit comments

Comments
 (0)