@@ -4,63 +4,32 @@ use crate::aclint::mtimer::MTIME;
4
4
pub use crate :: hal:: aclint:: Delay ;
5
5
pub use crate :: hal_async:: delay:: DelayUs ;
6
6
use core:: {
7
- cmp:: Ordering ,
8
7
future:: Future ,
9
8
pin:: Pin ,
10
9
task:: { Context , Poll } ,
11
10
} ;
12
11
13
- enum DelayAsyncState {
14
- WaitOverflow ( u64 ) ,
15
- Wait ( u64 ) ,
16
- Ready ,
17
- }
18
-
19
- struct FSMDelay {
12
+ struct DelayAsync {
20
13
mtime : MTIME ,
21
- state : DelayAsyncState ,
14
+ t0 : u64 ,
15
+ n_ticks : u64 ,
22
16
}
23
17
24
- impl FSMDelay {
25
- pub fn new ( n_ticks : u64 , mtime : MTIME ) -> Self {
26
- let t_from = mtime. read ( ) ;
27
- let t_to = t_from. wrapping_add ( n_ticks) ;
28
-
29
- let state = match t_to. cmp ( & t_from) {
30
- Ordering :: Less => DelayAsyncState :: WaitOverflow ( t_to) ,
31
- Ordering :: Greater => DelayAsyncState :: Wait ( t_to) ,
32
- Ordering :: Equal => DelayAsyncState :: Ready ,
33
- } ;
34
-
35
- Self { mtime, state }
18
+ impl DelayAsync {
19
+ pub fn new ( mtime : MTIME , n_ticks : u64 ) -> Self {
20
+ let t0 = mtime. read ( ) ;
21
+ Self { mtime, t0, n_ticks }
36
22
}
37
23
}
38
24
39
- impl Future for FSMDelay {
25
+ impl Future for DelayAsync {
40
26
type Output = ( ) ;
41
27
42
- fn poll ( mut self : Pin < & mut Self > , _cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
43
- match self . state {
44
- DelayAsyncState :: WaitOverflow ( t_to) => match t_to. cmp ( & self . mtime . read ( ) ) {
45
- Ordering :: Less => Poll :: Pending ,
46
- Ordering :: Greater => {
47
- self . state = DelayAsyncState :: Wait ( t_to) ;
48
- Poll :: Pending
49
- }
50
- Ordering :: Equal => {
51
- self . state = DelayAsyncState :: Ready ;
52
- Poll :: Ready ( ( ) )
53
- }
54
- } ,
55
- DelayAsyncState :: Wait ( t_to) => {
56
- if self . mtime . read ( ) < t_to {
57
- Poll :: Pending
58
- } else {
59
- self . state = DelayAsyncState :: Ready ;
60
- Poll :: Ready ( ( ) )
61
- }
62
- }
63
- DelayAsyncState :: Ready => Poll :: Ready ( ( ) ) ,
28
+ #[ inline]
29
+ fn poll ( self : Pin < & mut Self > , _cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
30
+ match self . mtime . read ( ) . wrapping_sub ( self . t0 ) < self . n_ticks {
31
+ true => Poll :: Pending ,
32
+ false => Poll :: Ready ( ( ) ) ,
64
33
}
65
34
}
66
35
}
@@ -69,14 +38,12 @@ impl DelayUs for Delay {
69
38
#[ inline]
70
39
async fn delay_us ( & mut self , us : u32 ) {
71
40
let n_ticks = us as u64 * self . get_freq ( ) as u64 / 1_000_000 ;
72
- let state = FSMDelay :: new ( n_ticks, self . get_mtime ( ) ) ;
73
- state. await ;
41
+ DelayAsync :: new ( self . get_mtime ( ) , n_ticks) . await ;
74
42
}
75
43
76
44
#[ inline]
77
45
async fn delay_ms ( & mut self , ms : u32 ) {
78
46
let n_ticks = ms as u64 * self . get_freq ( ) as u64 / 1_000 ;
79
- let state = FSMDelay :: new ( n_ticks, self . get_mtime ( ) ) ;
80
- state. await ;
47
+ DelayAsync :: new ( self . get_mtime ( ) , n_ticks) . await ;
81
48
}
82
49
}
0 commit comments