@@ -31,7 +31,47 @@ use smoltcp::socket::{SocketSet, TcpSocket, TcpSocketBuffer};
31
31
use core:: str;
32
32
use core:: fmt:: Write ;
33
33
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
+ }
35
75
36
76
///
37
77
use stm32f4xx_hal:: {
@@ -89,6 +129,11 @@ const APP: () = {
89
129
// Init ITM
90
130
let mut itm = c. core . ITM ;
91
131
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." ) ;
92
137
93
138
// NIC100 / ENC424J600 Set-up
94
139
let spi1 = c. device . SPI1 ;
@@ -149,6 +194,9 @@ const APP: () = {
149
194
. finalize ( )
150
195
} ;
151
196
197
+ iprintln ! ( stim0,
198
+ "Ethernet initialization complete" ) ;
199
+
152
200
eth_iface
153
201
} ;
154
202
@@ -163,9 +211,6 @@ const APP: () = {
163
211
let stim0 = & mut c. resources . itm . stim [ 0 ] ;
164
212
let mut iface = c. resources . eth_iface ;
165
213
166
- iprintln ! ( stim0,
167
- "Ethernet initialization complete" ) ;
168
-
169
214
// Copied / modified from smoltcp:
170
215
// examples/loopback.rs
171
216
let echo_socket = {
@@ -191,18 +236,10 @@ const APP: () = {
191
236
// smoltcp:examples/loopback.rs, examples/server.rs;
192
237
// stm32-eth:examples/ip.rs,
193
238
// 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 ( ) ;
198
239
loop {
199
240
// 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 ) ;
206
243
match iface. poll ( & mut socket_set, instant) {
207
244
Ok ( _) => {
208
245
} ,
0 commit comments