1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use r3::kernel::{InterruptNum, InterruptPriority};
#[macro_export]
macro_rules! use_sp804 {
(unsafe impl PortTimer for $ty:ty) => {
const _: () = {
use $crate::r3::{
kernel::{cfg::CfgBuilder, PortTimer, UTicks},
utils::Init,
};
use $crate::r3_portkit::tickless;
use $crate::{sp804, Sp804Options, Timer};
impl PortTimer for $ty {
const MAX_TICK_COUNT: UTicks = u32::MAX;
const MAX_TIMEOUT: UTicks = u32::MAX;
unsafe fn tick_count() -> UTicks {
unsafe { sp804::imp::tick_count::<Self>() }
}
unsafe fn pend_tick() {
unsafe { sp804::imp::pend_tick::<Self>() }
}
unsafe fn pend_tick_after(tick_count_delta: UTicks) {
unsafe { sp804::imp::pend_tick_after::<Self>(tick_count_delta) }
}
}
impl Timer for $ty {
unsafe fn init() {
unsafe { sp804::imp::init::<Self>() }
}
}
const TICKLESS_CFG: tickless::TicklessCfg =
match tickless::TicklessCfg::new(tickless::TicklessOptions {
hw_freq_num: <$ty as Sp804Options>::FREQUENCY,
hw_freq_denom: <$ty as Sp804Options>::FREQUENCY_DENOMINATOR,
hw_headroom_ticks: <$ty as Sp804Options>::HEADROOM,
force_full_hw_period: false,
resettable: false,
}) {
Ok(x) => x,
Err(e) => e.panic(),
};
static mut TIMER_STATE: tickless::TicklessState<TICKLESS_CFG> = Init::INIT;
unsafe impl sp804::imp::Sp804Instance for $ty {
const TICKLESS_CFG: tickless::TicklessCfg = TICKLESS_CFG;
type TicklessState = tickless::TicklessState<TICKLESS_CFG>;
fn tickless_state() -> *mut Self::TicklessState {
unsafe { &mut TIMER_STATE }
}
}
impl $ty {
pub const fn configure_sp804(b: &mut CfgBuilder<Self>) {
sp804::imp::configure(b);
}
}
};
};
}
pub trait Sp804Options {
const SP804_BASE: usize;
const FREQUENCY: u64;
const FREQUENCY_DENOMINATOR: u64 = 1;
const HEADROOM: u32 = min128(
Self::FREQUENCY as u128 * 60 / Self::FREQUENCY_DENOMINATOR as u128,
0x40000000,
) as u32;
const INTERRUPT_PRIORITY: InterruptPriority = 0xc0;
const INTERRUPT_NUM: InterruptNum;
}
const fn min128(x: u128, y: u128) -> u128 {
if x < y {
x
} else {
y
}
}