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"]
50pub static 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            _ => 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/// This is in a separate, inline(never) function so that its stack frame is
108/// removed when this function returns. Otherwise, the stack space used for
109/// these static_inits is wasted.
110#[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    // Initialize chip peripheral drivers
117    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/// Main function called after RAM initialized.
160#[no_mangle]
161pub unsafe fn main() {
162    //--------------------------------------------------------------------------
163    // INITIAL SETUP
164    //--------------------------------------------------------------------------
165
166    // Apply errata fixes and enable interrupts.
167    nrf52840::init();
168
169    // Set up peripheral drivers. Called in separate function to reduce stack
170    // usage.
171    let nrf52840_peripherals = create_peripherals();
172
173    // Set up circular peripheral dependencies.
174    nrf52840_peripherals.init();
175    let base_peripherals = &nrf52840_peripherals.nrf52;
176
177    // Setup space to store the core kernel data structure.
178    let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&*addr_of!(PROCESSES)));
179
180    // Create (and save for panic debugging) a chip object to setup low-level
181    // resources (e.g. MPU, systick).
182    let chip = static_init!(
183        nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
184        nrf52840::chip::NRF52::new(nrf52840_peripherals)
185    );
186    CHIP = Some(chip);
187
188    // Do nRF configuration and setup. This is shared code with other nRF-based
189    // platforms.
190    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    //--------------------------------------------------------------------------
199    // CAPABILITIES
200    //--------------------------------------------------------------------------
201
202    // Create capabilities that the board needs to call certain protected kernel
203    // functions.
204    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
205
206    //--------------------------------------------------------------------------
207    // TIMER
208    //--------------------------------------------------------------------------
209
210    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    //--------------------------------------------------------------------------
216    // UART & CONSOLE & DEBUG
217    //--------------------------------------------------------------------------
218
219    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    // Virtualize the UART channel for the console and for kernel debug.
229    let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
230        .finalize(components::uart_mux_component_static!());
231
232    // Create the debugger object that handles calls to `debug!()`.
233    components::debug_writer::DebugWriterComponent::new(
234        uart_mux,
235        create_capability!(capabilities::SetDebugWriterCapability),
236    )
237    .finalize(components::debug_writer_component_static!());
238
239    //--------------------------------------------------------------------------
240    // NRF CLOCK SETUP
241    //--------------------------------------------------------------------------
242
243    nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
244
245    //--------------------------------------------------------------------------
246    // PLATFORM AND SCHEDULER
247    //--------------------------------------------------------------------------
248
249    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    //--------------------------------------------------------------------------
260    // TESTS
261    //--------------------------------------------------------------------------
262
263    test_launcher.next();
264
265    //--------------------------------------------------------------------------
266    // KERNEL LOOP
267    //--------------------------------------------------------------------------
268
269    board_kernel.kernel_loop(
270        &platform,
271        chip,
272        None::<&kernel::ipc::IPC<0>>,
273        &main_loop_capability,
274    );
275}