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"]
50static 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 6 => unsafe { test::ecdsa_p256_test::run_ecdsa_p256(self) },
98 _ => kernel::debug!("All tests finished."),
99 }
100 }
101}
102impl CapsuleTestClient for TestLauncher {
103 fn done(&'static self, _result: Result<(), CapsuleTestError>) {
104 self.next();
105 }
106}
107
108#[inline(never)]
112unsafe fn create_peripherals() -> &'static mut Nrf52840DefaultPeripherals<'static> {
113 let ieee802154_ack_buf = static_init!(
114 [u8; nrf52840::ieee802154_radio::ACK_BUF_SIZE],
115 [0; nrf52840::ieee802154_radio::ACK_BUF_SIZE]
116 );
117 let nrf52840_peripherals = static_init!(
119 Nrf52840DefaultPeripherals,
120 Nrf52840DefaultPeripherals::new(ieee802154_ack_buf)
121 );
122
123 nrf52840_peripherals
124}
125
126impl KernelResources<nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>>
127 for Platform
128{
129 type SyscallDriverLookup = Self;
130 type SyscallFilter = ();
131 type ProcessFault = ();
132 type Scheduler = RoundRobinSched<'static>;
133 type SchedulerTimer = cortexm4::systick::SysTick;
134 type WatchDog = ();
135 type ContextSwitchCallback = ();
136
137 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
138 self
139 }
140 fn syscall_filter(&self) -> &Self::SyscallFilter {
141 &()
142 }
143 fn process_fault(&self) -> &Self::ProcessFault {
144 &()
145 }
146 fn scheduler(&self) -> &Self::Scheduler {
147 self.scheduler
148 }
149 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
150 &self.systick
151 }
152 fn watchdog(&self) -> &Self::WatchDog {
153 &()
154 }
155 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
156 &()
157 }
158}
159
160#[no_mangle]
162pub unsafe fn main() {
163 nrf52840::init();
169
170 let nrf52840_peripherals = create_peripherals();
173
174 nrf52840_peripherals.init();
176 let base_peripherals = &nrf52840_peripherals.nrf52;
177
178 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&*addr_of!(PROCESSES)));
180
181 let chip = static_init!(
184 nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
185 nrf52840::chip::NRF52::new(nrf52840_peripherals)
186 );
187 CHIP = Some(chip);
188
189 nrf52_components::startup::NrfStartupComponent::new(
192 false,
193 BUTTON_RST_PIN,
194 nrf52840::uicr::Regulator0Output::DEFAULT,
195 &base_peripherals.nvmc,
196 )
197 .finalize(());
198
199 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
206
207 let rtc = &base_peripherals.rtc;
212 let _ = rtc.start();
213 let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
214 .finalize(components::alarm_mux_component_static!(nrf52840::rtc::Rtc));
215
216 let uart_channel = nrf52_components::UartChannelComponent::new(
221 UartChannel::Pins(UartPins::new(UART_RTS, UART_TXD, UART_CTS, UART_RXD)),
222 mux_alarm,
223 &base_peripherals.uarte0,
224 )
225 .finalize(nrf52_components::uart_channel_component_static!(
226 nrf52840::rtc::Rtc
227 ));
228
229 let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
231 .finalize(components::uart_mux_component_static!());
232
233 components::debug_writer::DebugWriterComponent::new(
235 uart_mux,
236 create_capability!(capabilities::SetDebugWriterCapability),
237 )
238 .finalize(components::debug_writer_component_static!());
239
240 nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
245
246 let scheduler = components::sched::round_robin::RoundRobinComponent::new(&*addr_of!(PROCESSES))
251 .finalize(components::round_robin_component_static!(NUM_PROCS));
252
253 let platform = Platform {
254 scheduler,
255 systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
256 };
257
258 let test_launcher = static_init!(TestLauncher, TestLauncher::new(base_peripherals));
259
260 test_launcher.next();
265
266 board_kernel.kernel_loop(
271 &platform,
272 chip,
273 None::<&kernel::ipc::IPC<0>>,
274 &main_loop_capability,
275 );
276}