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 core::ptr::addr_of;
14use kernel::component::Component;
15use kernel::hil::time::Counter;
16use kernel::platform::{KernelResources, SyscallDriverLookup};
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 dyn kernel::process::Process>; NUM_PROCS] =
41 [None; NUM_PROCS];
42
43static mut CHIP: Option<&'static nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>> = None;
44static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
45 None;
46
47#[no_mangle]
49#[link_section = ".stack_buffer"]
50pub static mut STACK_MEMORY: [u8; 0x2000] = [0; 0x2000];
51
52pub struct Platform {
58 scheduler: &'static RoundRobinSched<'static>,
59 systick: cortexm4::systick::SysTick,
60}
61
62impl SyscallDriverLookup for Platform {
63 fn with_driver<F, R>(&self, _driver_num: usize, f: F) -> R
64 where
65 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
66 {
67 f(None)
68 }
69}
70
71struct TestLauncher {
76 test_index: Cell<usize>,
77 peripherals: &'static Nrf52DefaultPeripherals<'static>,
78}
79impl TestLauncher {
80 fn new(peripherals: &'static Nrf52DefaultPeripherals<'static>) -> Self {
81 Self {
82 test_index: Cell::new(0),
83 peripherals,
84 }
85 }
86
87 fn next(&'static self) {
88 let index = self.test_index.get();
89 self.test_index.increment();
90 match index {
91 0 => unsafe { test::sha256_test::run_sha256(self) },
92 1 => unsafe { test::hmac_sha256_test::run_hmacsha256(self) },
93 2 => unsafe { test::siphash24_test::run_siphash24(self) },
94 3 => unsafe { test::aes_test::run_aes128_ctr(&self.peripherals.ecb, self) },
95 4 => unsafe { test::aes_test::run_aes128_cbc(&self.peripherals.ecb, self) },
96 5 => unsafe { test::aes_test::run_aes128_ecb(&self.peripherals.ecb, 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 board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&*addr_of!(PROCESSES)));
179
180 let chip = static_init!(
183 nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
184 nrf52840::chip::NRF52::new(nrf52840_peripherals)
185 );
186 CHIP = Some(chip);
187
188 nrf52_components::startup::NrfStartupComponent::new(
191 false,
192 BUTTON_RST_PIN,
193 nrf52840::uicr::Regulator0Output::DEFAULT,
194 &base_peripherals.nvmc,
195 )
196 .finalize(());
197
198 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
205
206 let rtc = &base_peripherals.rtc;
211 let _ = rtc.start();
212 let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
213 .finalize(components::alarm_mux_component_static!(nrf52840::rtc::Rtc));
214
215 let uart_channel = nrf52_components::UartChannelComponent::new(
220 UartChannel::Pins(UartPins::new(UART_RTS, UART_TXD, UART_CTS, UART_RXD)),
221 mux_alarm,
222 &base_peripherals.uarte0,
223 )
224 .finalize(nrf52_components::uart_channel_component_static!(
225 nrf52840::rtc::Rtc
226 ));
227
228 let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
230 .finalize(components::uart_mux_component_static!());
231
232 components::debug_writer::DebugWriterComponent::new(
234 uart_mux,
235 create_capability!(capabilities::SetDebugWriterCapability),
236 )
237 .finalize(components::debug_writer_component_static!());
238
239 nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
244
245 let scheduler = components::sched::round_robin::RoundRobinComponent::new(&*addr_of!(PROCESSES))
250 .finalize(components::round_robin_component_static!(NUM_PROCS));
251
252 let platform = Platform {
253 scheduler,
254 systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
255 };
256
257 let test_launcher = static_init!(TestLauncher, TestLauncher::new(base_peripherals));
258
259 test_launcher.next();
264
265 board_kernel.kernel_loop(
270 &platform,
271 chip,
272 None::<&kernel::ipc::IPC<0>>,
273 &main_loop_capability,
274 );
275}