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
46/// Dummy buffer that causes the linker to reserve enough space for the stack.
47#[no_mangle]
48#[link_section = ".stack_buffer"]
49static mut STACK_MEMORY: [u8; 0x2000] = [0; 0x2000];
50
51//------------------------------------------------------------------------------
52// SYSCALL DRIVER TYPE DEFINITIONS
53//------------------------------------------------------------------------------
54
55/// Supported drivers by the platform
56pub struct Platform {
57    scheduler: &'static RoundRobinSched<'static>,
58    systick: cortexm4::systick::SysTick,
59}
60
61impl SyscallDriverLookup for Platform {
62    fn with_driver<F, R>(&self, _driver_num: usize, f: F) -> R
63    where
64        F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
65    {
66        f(None)
67    }
68}
69
70//------------------------------------------------------------------------------
71// TEST LAUNCHER FOR RUNNING TESTS
72//------------------------------------------------------------------------------
73
74struct TestLauncher {
75    test_index: Cell<usize>,
76    peripherals: &'static Nrf52DefaultPeripherals<'static>,
77}
78impl TestLauncher {
79    fn new(peripherals: &'static Nrf52DefaultPeripherals<'static>) -> Self {
80        Self {
81            test_index: Cell::new(0),
82            peripherals,
83        }
84    }
85
86    fn next(&'static self) {
87        let index = self.test_index.get();
88        self.test_index.increment();
89        match index {
90            0 => unsafe { test::sha256_test::run_sha256(self) },
91            1 => unsafe { test::hmac_sha256_test::run_hmacsha256(self) },
92            2 => unsafe { test::siphash24_test::run_siphash24(self) },
93            3 => unsafe { test::aes_test::run_aes128_ctr(&self.peripherals.ecb, self) },
94            4 => unsafe { test::aes_test::run_aes128_cbc(&self.peripherals.ecb, self) },
95            5 => unsafe { test::aes_test::run_aes128_ecb(&self.peripherals.ecb, self) },
96            6 => unsafe { test::ecdsa_p256_test::run_ecdsa_p256(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    // Create an array to hold process references.
178    let processes = components::process_array::ProcessArrayComponent::new()
179        .finalize(components::process_array_component_static!(NUM_PROCS));
180    PROCESSES = Some(processes);
181
182    // Setup space to store the core kernel data structure.
183    let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
184
185    // Create (and save for panic debugging) a chip object to setup low-level
186    // resources (e.g. MPU, systick).
187    let chip = static_init!(
188        nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
189        nrf52840::chip::NRF52::new(nrf52840_peripherals)
190    );
191    CHIP = Some(chip);
192
193    // Do nRF configuration and setup. This is shared code with other nRF-based
194    // platforms.
195    nrf52_components::startup::NrfStartupComponent::new(
196        false,
197        BUTTON_RST_PIN,
198        nrf52840::uicr::Regulator0Output::DEFAULT,
199        &base_peripherals.nvmc,
200    )
201    .finalize(());
202
203    //--------------------------------------------------------------------------
204    // CAPABILITIES
205    //--------------------------------------------------------------------------
206
207    // Create capabilities that the board needs to call certain protected kernel
208    // functions.
209    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
210
211    //--------------------------------------------------------------------------
212    // TIMER
213    //--------------------------------------------------------------------------
214
215    let rtc = &base_peripherals.rtc;
216    let _ = rtc.start();
217    let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
218        .finalize(components::alarm_mux_component_static!(nrf52840::rtc::Rtc));
219
220    //--------------------------------------------------------------------------
221    // UART & CONSOLE & DEBUG
222    //--------------------------------------------------------------------------
223
224    let uart_channel = nrf52_components::UartChannelComponent::new(
225        UartChannel::Pins(UartPins::new(UART_RTS, UART_TXD, UART_CTS, UART_RXD)),
226        mux_alarm,
227        &base_peripherals.uarte0,
228    )
229    .finalize(nrf52_components::uart_channel_component_static!(
230        nrf52840::rtc::Rtc
231    ));
232
233    // Virtualize the UART channel for the console and for kernel debug.
234    let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
235        .finalize(components::uart_mux_component_static!());
236
237    // Create the debugger object that handles calls to `debug!()`.
238    components::debug_writer::DebugWriterComponent::new(
239        uart_mux,
240        create_capability!(capabilities::SetDebugWriterCapability),
241    )
242    .finalize(components::debug_writer_component_static!());
243
244    //--------------------------------------------------------------------------
245    // NRF CLOCK SETUP
246    //--------------------------------------------------------------------------
247
248    nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
249
250    //--------------------------------------------------------------------------
251    // PLATFORM AND SCHEDULER
252    //--------------------------------------------------------------------------
253
254    let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
255        .finalize(components::round_robin_component_static!(NUM_PROCS));
256
257    let platform = Platform {
258        scheduler,
259        systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
260    };
261
262    let test_launcher = static_init!(TestLauncher, TestLauncher::new(base_peripherals));
263
264    //--------------------------------------------------------------------------
265    // TESTS
266    //--------------------------------------------------------------------------
267
268    test_launcher.next();
269
270    //--------------------------------------------------------------------------
271    // KERNEL LOOP
272    //--------------------------------------------------------------------------
273
274    board_kernel.kernel_loop(
275        &platform,
276        chip,
277        None::<&kernel::ipc::IPC<0>>,
278        &main_loop_capability,
279    );
280}