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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use core::ops::Range;
use r3::kernel::{InterruptNum, InterruptPriority};
pub const INTERRUPT_PRIORITY_RANGE: Range<InterruptPriority> = 0..256;
pub const INTERRUPT_SYSTICK: InterruptNum = 15;
pub const INTERRUPT_EXTERNAL0: InterruptNum = 16;
pub const INTERRUPT_NUM_RANGE: Range<InterruptNum> = 0..256;
pub trait ThreadingOptions {
const CPU_LOCK_PRIORITY_MASK: u8 = 0;
const USE_WFI: bool = true;
unsafe fn interrupt_stack_top() -> usize {
#[cfg(target_os = "none")]
{
let peripherals = unsafe { cortex_m::Peripherals::steal() };
unsafe { (peripherals.SCB.vtor.read() as *const usize).read_volatile() }
}
#[cfg(not(target_os = "none"))]
panic!("unsupported target")
}
}
#[macro_export]
macro_rules! use_port {
(unsafe $vis:vis struct $sys:ident) => {
$vis struct $sys;
mod port_arm_m_impl {
use super::$sys;
use $crate::r3::kernel::{
ClearInterruptLineError, EnableInterruptLineError, InterruptNum, InterruptPriority,
PendInterruptLineError, Port, QueryInterruptLineError, SetInterruptLinePriorityError,
TaskCb, PortToKernel, PortInterrupts, PortThreading, UTicks, PortTimer,
};
use $crate::core::ops::Range;
use $crate::threading::{
imp::{State, TaskState, PortInstance},
cfg::ThreadingOptions,
};
pub(super) static PORT_STATE: State = State::new();
unsafe impl PortInstance for $sys {
#[inline(always)]
fn port_state() -> &'static State {
&PORT_STATE
}
}
unsafe impl PortThreading for $sys {
type PortTaskState = TaskState;
#[allow(clippy::declare_interior_mutable_const)]
const PORT_TASK_STATE_INIT: Self::PortTaskState =
$crate::r3::utils::Init::INIT;
const STACK_DEFAULT_SIZE: usize = 2048;
unsafe fn dispatch_first_task() -> ! {
PORT_STATE.dispatch_first_task::<Self>()
}
unsafe fn yield_cpu() {
PORT_STATE.yield_cpu::<Self>()
}
unsafe fn exit_and_dispatch(task: &'static TaskCb<Self>) -> ! {
PORT_STATE.exit_and_dispatch::<Self>(task);
}
#[inline(always)]
unsafe fn enter_cpu_lock() {
PORT_STATE.enter_cpu_lock::<Self>()
}
#[inline(always)]
unsafe fn leave_cpu_lock() {
PORT_STATE.leave_cpu_lock::<Self>()
}
unsafe fn initialize_task_state(task: &'static TaskCb<Self>) {
PORT_STATE.initialize_task_state::<Self>(task)
}
fn is_cpu_lock_active() -> bool {
PORT_STATE.is_cpu_lock_active::<Self>()
}
fn is_task_context() -> bool {
PORT_STATE.is_task_context::<Self>()
}
}
unsafe impl PortInterrupts for $sys {
const MANAGED_INTERRUPT_PRIORITY_RANGE: Range<InterruptPriority> =
(<$sys as ThreadingOptions>::CPU_LOCK_PRIORITY_MASK as _)..256;
unsafe fn set_interrupt_line_priority(
line: InterruptNum,
priority: InterruptPriority,
) -> Result<(), SetInterruptLinePriorityError> {
PORT_STATE.set_interrupt_line_priority::<Self>(line, priority)
}
unsafe fn enable_interrupt_line(line: InterruptNum) -> Result<(), EnableInterruptLineError> {
PORT_STATE.enable_interrupt_line::<Self>(line)
}
unsafe fn disable_interrupt_line(line: InterruptNum) -> Result<(), EnableInterruptLineError> {
PORT_STATE.disable_interrupt_line::<Self>(line)
}
unsafe fn pend_interrupt_line(line: InterruptNum) -> Result<(), PendInterruptLineError> {
PORT_STATE.pend_interrupt_line::<Self>(line)
}
unsafe fn clear_interrupt_line(line: InterruptNum) -> Result<(), ClearInterruptLineError> {
PORT_STATE.clear_interrupt_line::<Self>(line)
}
unsafe fn is_interrupt_line_pending(
line: InterruptNum,
) -> Result<bool, QueryInterruptLineError> {
PORT_STATE.is_interrupt_line_pending::<Self>(line)
}
}
}
const _: () = $crate::threading::imp::validate::<$sys>();
#[link_section = ".vector_table.interrupts"]
#[no_mangle]
static __INTERRUPTS: $crate::threading::imp::InterruptHandlerTable =
$crate::threading::imp::make_interrupt_handler_table::<$sys>();
#[$crate::cortex_m_rt::entry]
fn main() -> ! {
unsafe { port_arm_m_impl::PORT_STATE.port_boot::<$sys>() };
}
#[$crate::cortex_m_rt::exception]
fn PendSV() {
unsafe { port_arm_m_impl::PORT_STATE.handle_pend_sv::<$sys>() };
}
#[$crate::cortex_m_rt::exception]
fn SysTick() {
unsafe { port_arm_m_impl::PORT_STATE.handle_sys_tick::<$sys>() };
}
};
}