Skip to content

Commit faf6a35

Browse files
committed
examples/tcp_stm32f407.rs: Replace CYCCNT software timer with SysTick
1 parent b27cc1f commit faf6a35

File tree

1 file changed

+51
-14
lines changed

1 file changed

+51
-14
lines changed

examples/tcp_stm32f407.rs

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,47 @@ use smoltcp::socket::{SocketSet, TcpSocket, TcpSocketBuffer};
3131
use core::str;
3232
use core::fmt::Write;
3333

34-
use rtic::cyccnt::{Duration, Instant};
34+
/// Timer
35+
use core::cell::RefCell;
36+
use cortex_m::interrupt::Mutex;
37+
use cortex_m_rt::exception;
38+
use stm32f4xx_hal::{
39+
rcc::Clocks,
40+
time::MilliSeconds,
41+
timer::{Timer, Event as TimerEvent},
42+
stm32::SYST,
43+
};
44+
use smoltcp::time::{Instant, Duration};
45+
/// Rate in Hz
46+
const TIMER_RATE: u32 = 20;
47+
/// Interval duration in milliseconds
48+
const TIMER_DELTA: u32 = 1000 / TIMER_RATE;
49+
/// Elapsed time in milliseconds
50+
static TIMER_MS: Mutex<RefCell<u32>> = Mutex::new(RefCell::new(0));
51+
52+
/// Setup SysTick exception
53+
fn timer_setup(syst: SYST, clocks: Clocks) {
54+
let mut timer = Timer::syst(syst, TIMER_RATE.hz(), clocks);
55+
timer.listen(TimerEvent::TimeOut);
56+
}
57+
58+
/// SysTick exception (Timer)
59+
#[exception]
60+
fn SysTick() {
61+
cortex_m::interrupt::free(|cs| {
62+
*TIMER_MS.borrow(cs)
63+
.borrow_mut() += TIMER_DELTA;
64+
});
65+
}
66+
67+
/// Obtain current time in milliseconds
68+
pub fn timer_now() -> MilliSeconds {
69+
let ms = cortex_m::interrupt::free(|cs| {
70+
*TIMER_MS.borrow(cs)
71+
.borrow()
72+
});
73+
ms.ms()
74+
}
3575

3676
///
3777
use stm32f4xx_hal::{
@@ -89,6 +129,11 @@ const APP: () = {
89129
// Init ITM
90130
let mut itm = c.core.ITM;
91131
let stim0 = &mut itm.stim[0];
132+
133+
// Setup SysTick
134+
// Reference to stm32-eth:examples/ip.rs
135+
timer_setup(c.core.SYST, clocks);
136+
iprintln!(stim0, "Timer initialised.");
92137

93138
// NIC100 / ENC424J600 Set-up
94139
let spi1 = c.device.SPI1;
@@ -149,6 +194,9 @@ const APP: () = {
149194
.finalize()
150195
};
151196

197+
iprintln!(stim0,
198+
"Ethernet initialization complete");
199+
152200
eth_iface
153201
};
154202

@@ -163,9 +211,6 @@ const APP: () = {
163211
let stim0 = &mut c.resources.itm.stim[0];
164212
let mut iface = c.resources.eth_iface;
165213

166-
iprintln!(stim0,
167-
"Ethernet initialization complete");
168-
169214
// Copied / modified from smoltcp:
170215
// examples/loopback.rs
171216
let echo_socket = {
@@ -191,18 +236,10 @@ const APP: () = {
191236
// smoltcp:examples/loopback.rs, examples/server.rs;
192237
// stm32-eth:examples/ip.rs,
193238
// git.m-labs.hk/M-Labs/tnetplug
194-
let mut time = 0u32;
195-
let mut next_ms = Instant::now();
196-
use rtic::cyccnt::U32Ext;
197-
next_ms += 168_000_u32.cycles();
198239
loop {
199240
// Poll
200-
let tick = Instant::now() > next_ms;
201-
if tick {
202-
next_ms += 168_000_u32.cycles();
203-
time += 1;
204-
}
205-
let instant = smoltcp::time::Instant::from_millis(time as i64);
241+
let now = timer_now().0;
242+
let instant = Instant::from_millis(now as i64);
206243
match iface.poll(&mut socket_set, instant) {
207244
Ok(_) => {
208245
},

0 commit comments

Comments
 (0)