nrf52840dk_test_kernel/
main.rs
1#![no_std]
8#![no_main]
9#![deny(missing_docs)]
10
11use capsules_core::test::capsule_test::{CapsuleTestClient, CapsuleTestError};
12use core::cell::Cell;
13use kernel::component::Component;
14use kernel::hil::time::Counter;
15use kernel::platform::{KernelResources, SyscallDriverLookup};
16use kernel::process::ProcessArray;
17use kernel::scheduler::round_robin::RoundRobinSched;
18use kernel::utilities::cells::NumericCellExt;
19use kernel::{capabilities, create_capability, static_init};
20use nrf52840::chip::Nrf52DefaultPeripherals;
21use nrf52840::gpio::Pin;
22use nrf52840::interrupt_service::Nrf52840DefaultPeripherals;
23use nrf52_components::{UartChannel, UartPins};
24
25mod test;
26
27const BUTTON_RST_PIN: Pin = Pin::P0_18;
28
29const UART_RTS: Option<Pin> = Some(Pin::P0_05);
30const UART_TXD: Pin = Pin::P0_06;
31const UART_CTS: Option<Pin> = Some(Pin::P0_07);
32const UART_RXD: Pin = Pin::P0_08;
33
34pub mod io;
36
37const NUM_PROCS: usize = 0;
39
40static mut PROCESSES: Option<&'static ProcessArray<NUM_PROCS>> = None;
42static mut CHIP: Option<&'static nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>> = None;
43static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
44 None;
45
46#[no_mangle]
48#[link_section = ".stack_buffer"]
49static mut STACK_MEMORY: [u8; 0x2000] = [0; 0x2000];
50
51pub struct Platform {
57 scheduler: &'static RoundRobinSched<'static>,
58 systick: cortexm4::systick::SysTick,
59}
60
61impl SyscallDriverLookup for Platform {
62 fn with_driver<F, R>(&self, _driver_num: usize, f: F) -> R
63 where
64 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
65 {
66 f(None)
67 }
68}
69
70struct TestLauncher {
75 test_index: Cell<usize>,
76 peripherals: &'static Nrf52DefaultPeripherals<'static>,
77}
78impl TestLauncher {
79 fn new(peripherals: &'static Nrf52DefaultPeripherals<'static>) -> Self {
80 Self {
81 test_index: Cell::new(0),
82 peripherals,
83 }
84 }
85
86 fn next(&'static self) {
87 let index = self.test_index.get();
88 self.test_index.increment();
89 match index {
90 0 => unsafe { test::sha256_test::run_sha256(self) },
91 1 => unsafe { test::hmac_sha256_test::run_hmacsha256(self) },
92 2 => unsafe { test::siphash24_test::run_siphash24(self) },
93 3 => unsafe { test::aes_test::run_aes128_ctr(&self.peripherals.ecb, self) },
94 4 => unsafe { test::aes_test::run_aes128_cbc(&self.peripherals.ecb, self) },
95 5 => unsafe { test::aes_test::run_aes128_ecb(&self.peripherals.ecb, self) },
96 6 => unsafe { test::ecdsa_p256_test::run_ecdsa_p256(self) },
97 _ => kernel::debug!("All tests finished."),
98 }
99 }
100}
101impl CapsuleTestClient for TestLauncher {
102 fn done(&'static self, _result: Result<(), CapsuleTestError>) {
103 self.next();
104 }
105}
106
107#[inline(never)]
111unsafe fn create_peripherals() -> &'static mut Nrf52840DefaultPeripherals<'static> {
112 let ieee802154_ack_buf = static_init!(
113 [u8; nrf52840::ieee802154_radio::ACK_BUF_SIZE],
114 [0; nrf52840::ieee802154_radio::ACK_BUF_SIZE]
115 );
116 let nrf52840_peripherals = static_init!(
118 Nrf52840DefaultPeripherals,
119 Nrf52840DefaultPeripherals::new(ieee802154_ack_buf)
120 );
121
122 nrf52840_peripherals
123}
124
125impl KernelResources<nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>>
126 for Platform
127{
128 type SyscallDriverLookup = Self;
129 type SyscallFilter = ();
130 type ProcessFault = ();
131 type Scheduler = RoundRobinSched<'static>;
132 type SchedulerTimer = cortexm4::systick::SysTick;
133 type WatchDog = ();
134 type ContextSwitchCallback = ();
135
136 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
137 self
138 }
139 fn syscall_filter(&self) -> &Self::SyscallFilter {
140 &()
141 }
142 fn process_fault(&self) -> &Self::ProcessFault {
143 &()
144 }
145 fn scheduler(&self) -> &Self::Scheduler {
146 self.scheduler
147 }
148 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
149 &self.systick
150 }
151 fn watchdog(&self) -> &Self::WatchDog {
152 &()
153 }
154 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
155 &()
156 }
157}
158
159#[no_mangle]
161pub unsafe fn main() {
162 nrf52840::init();
168
169 let nrf52840_peripherals = create_peripherals();
172
173 nrf52840_peripherals.init();
175 let base_peripherals = &nrf52840_peripherals.nrf52;
176
177 let processes = components::process_array::ProcessArrayComponent::new()
179 .finalize(components::process_array_component_static!(NUM_PROCS));
180 PROCESSES = Some(processes);
181
182 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
184
185 let chip = static_init!(
188 nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
189 nrf52840::chip::NRF52::new(nrf52840_peripherals)
190 );
191 CHIP = Some(chip);
192
193 nrf52_components::startup::NrfStartupComponent::new(
196 false,
197 BUTTON_RST_PIN,
198 nrf52840::uicr::Regulator0Output::DEFAULT,
199 &base_peripherals.nvmc,
200 )
201 .finalize(());
202
203 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
210
211 let rtc = &base_peripherals.rtc;
216 let _ = rtc.start();
217 let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
218 .finalize(components::alarm_mux_component_static!(nrf52840::rtc::Rtc));
219
220 let uart_channel = nrf52_components::UartChannelComponent::new(
225 UartChannel::Pins(UartPins::new(UART_RTS, UART_TXD, UART_CTS, UART_RXD)),
226 mux_alarm,
227 &base_peripherals.uarte0,
228 )
229 .finalize(nrf52_components::uart_channel_component_static!(
230 nrf52840::rtc::Rtc
231 ));
232
233 let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
235 .finalize(components::uart_mux_component_static!());
236
237 components::debug_writer::DebugWriterComponent::new(
239 uart_mux,
240 create_capability!(capabilities::SetDebugWriterCapability),
241 )
242 .finalize(components::debug_writer_component_static!());
243
244 nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
249
250 let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
255 .finalize(components::round_robin_component_static!(NUM_PROCS));
256
257 let platform = Platform {
258 scheduler,
259 systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
260 };
261
262 let test_launcher = static_init!(TestLauncher, TestLauncher::new(base_peripherals));
263
264 test_launcher.next();
269
270 board_kernel.kernel_loop(
275 &platform,
276 chip,
277 None::<&kernel::ipc::IPC<0>>,
278 &main_loop_capability,
279 );
280}