nrf52840dk_test_kernel/
main.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Tock kernel for the Nordic Semiconductor nRF52840 development kit (DK).
6
7#![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
34/// Debug Writer
35pub mod io;
36
37// Number of concurrent processes this platform supports.
38const 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/// Dummy buffer that causes the linker to reserve enough space for the stack.
48#[no_mangle]
49#[link_section = ".stack_buffer"]
50static mut STACK_MEMORY: [u8; 0x2000] = [0; 0x2000];
51
52//------------------------------------------------------------------------------
53// SYSCALL DRIVER TYPE DEFINITIONS
54//------------------------------------------------------------------------------
55
56/// Supported drivers by the platform
57pub 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
71//------------------------------------------------------------------------------
72// TEST LAUNCHER FOR RUNNING TESTS
73//------------------------------------------------------------------------------
74
75struct 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/// This is in a separate, inline(never) function so that its stack frame is
109/// removed when this function returns. Otherwise, the stack space used for
110/// these static_inits is wasted.
111#[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    // Initialize chip peripheral drivers
118    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/// Main function called after RAM initialized.
161#[no_mangle]
162pub unsafe fn main() {
163    //--------------------------------------------------------------------------
164    // INITIAL SETUP
165    //--------------------------------------------------------------------------
166
167    // Apply errata fixes and enable interrupts.
168    nrf52840::init();
169
170    // Set up peripheral drivers. Called in separate function to reduce stack
171    // usage.
172    let nrf52840_peripherals = create_peripherals();
173
174    // Set up circular peripheral dependencies.
175    nrf52840_peripherals.init();
176    let base_peripherals = &nrf52840_peripherals.nrf52;
177
178    // Setup space to store the core kernel data structure.
179    let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&*addr_of!(PROCESSES)));
180
181    // Create (and save for panic debugging) a chip object to setup low-level
182    // resources (e.g. MPU, systick).
183    let chip = static_init!(
184        nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
185        nrf52840::chip::NRF52::new(nrf52840_peripherals)
186    );
187    CHIP = Some(chip);
188
189    // Do nRF configuration and setup. This is shared code with other nRF-based
190    // platforms.
191    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    //--------------------------------------------------------------------------
200    // CAPABILITIES
201    //--------------------------------------------------------------------------
202
203    // Create capabilities that the board needs to call certain protected kernel
204    // functions.
205    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
206
207    //--------------------------------------------------------------------------
208    // TIMER
209    //--------------------------------------------------------------------------
210
211    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    //--------------------------------------------------------------------------
217    // UART & CONSOLE & DEBUG
218    //--------------------------------------------------------------------------
219
220    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    // Virtualize the UART channel for the console and for kernel debug.
230    let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
231        .finalize(components::uart_mux_component_static!());
232
233    // Create the debugger object that handles calls to `debug!()`.
234    components::debug_writer::DebugWriterComponent::new(
235        uart_mux,
236        create_capability!(capabilities::SetDebugWriterCapability),
237    )
238    .finalize(components::debug_writer_component_static!());
239
240    //--------------------------------------------------------------------------
241    // NRF CLOCK SETUP
242    //--------------------------------------------------------------------------
243
244    nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
245
246    //--------------------------------------------------------------------------
247    // PLATFORM AND SCHEDULER
248    //--------------------------------------------------------------------------
249
250    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    //--------------------------------------------------------------------------
261    // TESTS
262    //--------------------------------------------------------------------------
263
264    test_launcher.next();
265
266    //--------------------------------------------------------------------------
267    // KERNEL LOOP
268    //--------------------------------------------------------------------------
269
270    board_kernel.kernel_loop(
271        &platform,
272        chip,
273        None::<&kernel::ipc::IPC<0>>,
274        &main_loop_capability,
275    );
276}