Skip to content

Commit eaed96e

Browse files
committed
Use rticv2 for the async example
1 parent c013e26 commit eaed96e

File tree

2 files changed

+43
-25
lines changed

2 files changed

+43
-25
lines changed

Cargo.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,20 @@ package = "cortex-m-rtic"
8080
version = "1.0"
8181

8282
[dev-dependencies.async-rtic]
83-
package = "cortex-m-rtic"
83+
package = "rtic"
8484
git = "https://github.com/rtic-rs/cortex-m-rtic.git"
85-
rev = "613b3c59fc841caa3ca8c4d7ac440fbfa2f71fd1"
85+
rev = "364edb707ebf7baa2e7bdd1b1e6b02156d7b5569"
8686

87-
[dev-dependencies.arbiter]
87+
[dev-dependencies.rtic-arbiter]
8888
package = "rtic-arbiter"
8989
git = "https://github.com/rtic-rs/cortex-m-rtic.git"
9090
rev = "364edb707ebf7baa2e7bdd1b1e6b02156d7b5569"
9191

92+
[dev-dependencies.rtic-channel]
93+
package = "rtic-channel"
94+
git = "https://github.com/rtic-rs/cortex-m-rtic.git"
95+
rev = "364edb707ebf7baa2e7bdd1b1e6b02156d7b5569"
96+
9297
# This isn't an actual example. It just exists so we can easily
9398
# test the common items :)
9499
[[example]]

examples/async-rtic-timestamp.rs

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,13 @@ extern crate async_rtic as rtic;
4141
mod app {
4242

4343
use async_rtic as rtic;
44+
use rtic_channel::{Channel, Receiver, Sender};
4445

4546
use crate::common::EthernetPhy;
4647

47-
use fugit::ExtU64;
48-
49-
use arbiter::Arbiter;
48+
use rtic_arbiter::Arbiter;
5049

5150
use ieee802_3_miim::{phy::PhySpeed, Phy};
52-
use systick_monotonic::Systick;
5351

5452
use stm32_eth::{
5553
dma::{EthernetDMA, PacketId, RxRing, RxRingEntry, TxRing, TxRingEntry},
@@ -66,25 +64,24 @@ mod app {
6664
#[shared]
6765
struct Shared {}
6866

69-
#[monotonic(binds = SysTick, default = true)]
70-
type Monotonic = Systick<1000>;
71-
7267
#[init(local = [
7368
rx_ring: [RxRingEntry; 2] = [RxRingEntry::new(),RxRingEntry::new()],
7469
tx_ring: [TxRingEntry; 2] = [TxRingEntry::new(),TxRingEntry::new()],
7570
dma: MaybeUninit<EthernetDMA<'static, 'static>> = MaybeUninit::uninit(),
7671
arbiter: MaybeUninit<Arbiter<EthernetPTP> > = MaybeUninit::uninit(),
72+
// We use a channel to signal when 1 second has passed.
73+
// We should use `rtic_monotonic`, but its embedded-hal
74+
// version requirements conflict with that of stm32f4xx-hal.
75+
tx_channel: Channel<(), 1> = Channel::new(),
7776
])]
78-
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
77+
fn init(cx: init::Context) -> (Shared, Local) {
7978
defmt::info!("Pre-init");
80-
let core = cx.core;
8179
let p = cx.device;
8280

8381
let rx_ring = cx.local.rx_ring;
8482
let tx_ring = cx.local.tx_ring;
8583

8684
let (clocks, gpio, ethernet) = crate::common::setup_peripherals(p);
87-
let mono = Systick::new(core.SYST, clocks.hclk().raw());
8885

8986
defmt::info!("Setting up pins");
9087
let (pins, mdio, mdc, pps) = crate::common::setup_pins(gpio);
@@ -104,6 +101,7 @@ mod app {
104101
dma.enable_interrupt();
105102

106103
let (rx, tx) = dma.split();
104+
let (do_tx_send, do_tx_recv) = cx.local.tx_channel.split();
107105

108106
match EthernetPhy::from_miim(mac, 0) {
109107
Ok(mut phy) => {
@@ -137,11 +135,11 @@ mod app {
137135
}
138136
};
139137

140-
sender::spawn(tx).ok();
138+
sender::spawn(tx, do_tx_recv).ok();
141139
receiver::spawn(rx, arbiter).ok();
142-
ptp_scheduler::spawn(arbiter).ok();
140+
ptp_scheduler::spawn(arbiter, do_tx_send).ok();
143141

144-
(Shared {}, Local {}, init::Monotonics(mono))
142+
(Shared {}, Local {})
145143
}
146144

147145
#[task]
@@ -211,34 +209,51 @@ mod app {
211209
}
212210

213211
#[task]
214-
async fn ptp_scheduler(_: ptp_scheduler::Context, ptp: &'static Arbiter<EthernetPTP>) {
212+
async fn ptp_scheduler(
213+
_: ptp_scheduler::Context,
214+
ptp: &'static Arbiter<EthernetPTP>,
215+
mut do_tx_recv: Sender<'static, (), 1>,
216+
) {
217+
let mut last_tx = EthernetPTP::now();
215218
loop {
216219
let mut ptp = ptp.access().await;
217-
let start = EthernetPTP::now();
218-
let int_time = start + Timestamp::new_raw(Subseconds::MAX_VALUE as i64);
220+
221+
let int_time = last_tx + Timestamp::new_raw(Subseconds::MAX_VALUE as i64 / 2);
219222
ptp.wait_until(int_time).await;
223+
220224
let now = EthernetPTP::now();
221225

222-
defmt::info!("Got to PTP time after {}.", now - start);
226+
do_tx_recv.send(()).await.ok();
227+
defmt::info!("Got to PTP time after {}. It is now {}", now - last_tx, now);
228+
229+
last_tx = int_time;
223230
}
224231
}
225232

226233
#[task]
227-
async fn sender(_: sender::Context, tx: &'static mut TxRing<'static>) {
234+
async fn sender(
235+
_: sender::Context,
236+
tx: &'static mut TxRing<'static>,
237+
mut receiver: Receiver<'static, (), 1>,
238+
) {
228239
let mut tx_id_ctr = 0x8000_0000;
229240

230241
const SIZE: usize = 42;
231242

232243
loop {
244+
receiver.recv().await.ok();
245+
246+
defmt::info!("Starting TX. It is now {}", EthernetPTP::now());
247+
233248
// Obtain the current time to use as the "TX time" of our frame. It is clearly
234249
// incorrect, but works well enough in low-activity systems (such as this example).
235-
let now = EthernetPTP::now();
236-
let start = monotonics::now();
237250

238251
const DST_MAC: [u8; 6] = [0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56];
239252
const SRC_MAC: [u8; 6] = [0x00, 0x00, 0xDE, 0xAD, 0xBE, 0xEF];
240253
const ETH_TYPE: [u8; 2] = [0xFF, 0xFF]; // Custom/unknown ethertype
241254

255+
let now = EthernetPTP::now();
256+
242257
let tx_id_val = tx_id_ctr;
243258
let packet_id = PacketId(tx_id_val);
244259
let mut tx_buffer = tx.prepare_packet(SIZE, Some(packet_id.clone())).await;
@@ -259,8 +274,6 @@ mod app {
259274
if let Ok(Some(timestamp)) = timestamp {
260275
defmt::info!("Tx timestamp: {}", timestamp);
261276
}
262-
263-
monotonics::delay_until(start + 500.millis()).await;
264277
}
265278
}
266279

0 commit comments

Comments
 (0)