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 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
34/// Debug Writer
35pub mod io;
36
37// Number of concurrent processes this platform supports.
38const NUM_PROCS: usize = 0;
39
40/// Static variables used by io.rs.
41static 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
46kernel::stack_size! {0x2000}
47
48//------------------------------------------------------------------------------
49// SYSCALL DRIVER TYPE DEFINITIONS
50//------------------------------------------------------------------------------
51
52/// Supported drivers by the platform
53pub struct Platform {
54    scheduler: &'static RoundRobinSched<'static>,
55    systick: cortexm4::systick::SysTick,
56}
57
58impl SyscallDriverLookup for Platform {
59    fn with_driver<F, R>(&self, _driver_num: usize, f: F) -> R
60    where
61        F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
62    {
63        f(None)
64    }
65}
66
67//------------------------------------------------------------------------------
68// TEST LAUNCHER FOR RUNNING TESTS
69//------------------------------------------------------------------------------
70
71struct TestLauncher {
72    test_index: Cell<usize>,
73    peripherals: &'static Nrf52DefaultPeripherals<'static>,
74}
75impl TestLauncher {
76    fn new(peripherals: &'static Nrf52DefaultPeripherals<'static>) -> Self {
77        Self {
78            test_index: Cell::new(0),
79            peripherals,
80        }
81    }
82
83    fn next(&'static self) {
84        let index = self.test_index.get();
85        self.test_index.increment();
86        match index {
87            0 => unsafe { test::sha256_test::run_sha256(self) },
88            1 => unsafe { test::hmac_sha256_test::run_hmacsha256(self) },
89            2 => unsafe { test::siphash24_test::run_siphash24(self) },
90            3 => unsafe { test::aes_test::run_aes128_ctr(&self.peripherals.ecb, self) },
91            4 => unsafe { test::aes_test::run_aes128_cbc(&self.peripherals.ecb, self) },
92            5 => unsafe { test::aes_test::run_aes128_ecb(&self.peripherals.ecb, self) },
93            6 => unsafe { test::ecdsa_p256_test::run_ecdsa_p256(self) },
94            _ => kernel::debug!("All tests finished."),
95        }
96    }
97}
98impl CapsuleTestClient for TestLauncher {
99    fn done(&'static self, _result: Result<(), CapsuleTestError>) {
100        self.next();
101    }
102}
103
104/// This is in a separate, inline(never) function so that its stack frame is
105/// removed when this function returns. Otherwise, the stack space used for
106/// these static_inits is wasted.
107#[inline(never)]
108unsafe fn create_peripherals() -> &'static mut Nrf52840DefaultPeripherals<'static> {
109    let ieee802154_ack_buf = static_init!(
110        [u8; nrf52840::ieee802154_radio::ACK_BUF_SIZE],
111        [0; nrf52840::ieee802154_radio::ACK_BUF_SIZE]
112    );
113    // Initialize chip peripheral drivers
114    let nrf52840_peripherals = static_init!(
115        Nrf52840DefaultPeripherals,
116        Nrf52840DefaultPeripherals::new(ieee802154_ack_buf)
117    );
118
119    nrf52840_peripherals
120}
121
122impl KernelResources<nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>>
123    for Platform
124{
125    type SyscallDriverLookup = Self;
126    type SyscallFilter = ();
127    type ProcessFault = ();
128    type Scheduler = RoundRobinSched<'static>;
129    type SchedulerTimer = cortexm4::systick::SysTick;
130    type WatchDog = ();
131    type ContextSwitchCallback = ();
132
133    fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
134        self
135    }
136    fn syscall_filter(&self) -> &Self::SyscallFilter {
137        &()
138    }
139    fn process_fault(&self) -> &Self::ProcessFault {
140        &()
141    }
142    fn scheduler(&self) -> &Self::Scheduler {
143        self.scheduler
144    }
145    fn scheduler_timer(&self) -> &Self::SchedulerTimer {
146        &self.systick
147    }
148    fn watchdog(&self) -> &Self::WatchDog {
149        &()
150    }
151    fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
152        &()
153    }
154}
155
156/// Main function called after RAM initialized.
157#[no_mangle]
158pub unsafe fn main() {
159    //--------------------------------------------------------------------------
160    // INITIAL SETUP
161    //--------------------------------------------------------------------------
162
163    // Apply errata fixes and enable interrupts.
164    nrf52840::init();
165
166    // Set up peripheral drivers. Called in separate function to reduce stack
167    // usage.
168    let nrf52840_peripherals = create_peripherals();
169
170    // Set up circular peripheral dependencies.
171    nrf52840_peripherals.init();
172    let base_peripherals = &nrf52840_peripherals.nrf52;
173
174    // Create an array to hold process references.
175    let processes = components::process_array::ProcessArrayComponent::new()
176        .finalize(components::process_array_component_static!(NUM_PROCS));
177    PROCESSES = Some(processes);
178
179    // Setup space to store the core kernel data structure.
180    let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
181
182    // Create (and save for panic debugging) a chip object to setup low-level
183    // resources (e.g. MPU, systick).
184    let chip = static_init!(
185        nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
186        nrf52840::chip::NRF52::new(nrf52840_peripherals)
187    );
188    CHIP = Some(chip);
189
190    // Do nRF configuration and setup. This is shared code with other nRF-based
191    // platforms.
192    nrf52_components::startup::NrfStartupComponent::new(
193        false,
194        BUTTON_RST_PIN,
195        nrf52840::uicr::Regulator0Output::DEFAULT,
196        &base_peripherals.nvmc,
197    )
198    .finalize(());
199
200    //--------------------------------------------------------------------------
201    // CAPABILITIES
202    //--------------------------------------------------------------------------
203
204    // Create capabilities that the board needs to call certain protected kernel
205    // functions.
206    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
207
208    //--------------------------------------------------------------------------
209    // TIMER
210    //--------------------------------------------------------------------------
211
212    let rtc = &base_peripherals.rtc;
213    let _ = rtc.start();
214    let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
215        .finalize(components::alarm_mux_component_static!(nrf52840::rtc::Rtc));
216
217    //--------------------------------------------------------------------------
218    // UART & CONSOLE & DEBUG
219    //--------------------------------------------------------------------------
220
221    let uart_channel = nrf52_components::UartChannelComponent::new(
222        UartChannel::Pins(UartPins::new(UART_RTS, UART_TXD, UART_CTS, UART_RXD)),
223        mux_alarm,
224        &base_peripherals.uarte0,
225    )
226    .finalize(nrf52_components::uart_channel_component_static!(
227        nrf52840::rtc::Rtc
228    ));
229
230    // Virtualize the UART channel for the console and for kernel debug.
231    let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
232        .finalize(components::uart_mux_component_static!());
233
234    // Create the debugger object that handles calls to `debug!()`.
235    components::debug_writer::DebugWriterComponent::new(
236        uart_mux,
237        create_capability!(capabilities::SetDebugWriterCapability),
238    )
239    .finalize(components::debug_writer_component_static!());
240
241    //--------------------------------------------------------------------------
242    // NRF CLOCK SETUP
243    //--------------------------------------------------------------------------
244
245    nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
246
247    //--------------------------------------------------------------------------
248    // PLATFORM AND SCHEDULER
249    //--------------------------------------------------------------------------
250
251    let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
252        .finalize(components::round_robin_component_static!(NUM_PROCS));
253
254    let platform = Platform {
255        scheduler,
256        systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
257    };
258
259    let test_launcher = static_init!(TestLauncher, TestLauncher::new(base_peripherals));
260
261    //--------------------------------------------------------------------------
262    // TESTS
263    //--------------------------------------------------------------------------
264
265    test_launcher.next();
266
267    //--------------------------------------------------------------------------
268    // KERNEL LOOP
269    //--------------------------------------------------------------------------
270
271    board_kernel.kernel_loop(
272        &platform,
273        chip,
274        None::<&kernel::ipc::IPC<0>>,
275        &main_loop_capability,
276    );
277}