nrf52840dk_test_kernel/
main.rs#![no_std]
#![cfg_attr(not(doc), no_main)]
#![deny(missing_docs)]
use capsules_core::test::capsule_test::{CapsuleTestClient, CapsuleTestError};
use core::cell::Cell;
use core::ptr::addr_of;
use kernel::component::Component;
use kernel::hil::time::Counter;
use kernel::platform::{KernelResources, SyscallDriverLookup};
use kernel::scheduler::round_robin::RoundRobinSched;
use kernel::utilities::cells::NumericCellExt;
use kernel::{capabilities, create_capability, static_init};
use nrf52840::chip::Nrf52DefaultPeripherals;
use nrf52840::gpio::Pin;
use nrf52840::interrupt_service::Nrf52840DefaultPeripherals;
use nrf52_components::{UartChannel, UartPins};
mod test;
const BUTTON_RST_PIN: Pin = Pin::P0_18;
const UART_RTS: Option<Pin> = Some(Pin::P0_05);
const UART_TXD: Pin = Pin::P0_06;
const UART_CTS: Option<Pin> = Some(Pin::P0_07);
const UART_RXD: Pin = Pin::P0_08;
pub mod io;
const NUM_PROCS: usize = 0;
static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
[None; NUM_PROCS];
static mut CHIP: Option<&'static nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>> = None;
static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
None;
#[no_mangle]
#[link_section = ".stack_buffer"]
pub static mut STACK_MEMORY: [u8; 0x2000] = [0; 0x2000];
pub struct Platform {
scheduler: &'static RoundRobinSched<'static>,
systick: cortexm4::systick::SysTick,
}
impl SyscallDriverLookup for Platform {
fn with_driver<F, R>(&self, _driver_num: usize, f: F) -> R
where
F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
{
f(None)
}
}
struct TestLauncher {
test_index: Cell<usize>,
peripherals: &'static Nrf52DefaultPeripherals<'static>,
}
impl TestLauncher {
fn new(peripherals: &'static Nrf52DefaultPeripherals<'static>) -> Self {
Self {
test_index: Cell::new(0),
peripherals,
}
}
fn next(&'static self) {
let index = self.test_index.get();
self.test_index.increment();
match index {
0 => unsafe { test::sha256_test::run_sha256(self) },
1 => unsafe { test::hmac_sha256_test::run_hmacsha256(self) },
2 => unsafe { test::siphash24_test::run_siphash24(self) },
3 => unsafe { test::aes_test::run_aes128_ctr(&self.peripherals.ecb, self) },
4 => unsafe { test::aes_test::run_aes128_cbc(&self.peripherals.ecb, self) },
5 => unsafe { test::aes_test::run_aes128_ecb(&self.peripherals.ecb, self) },
_ => kernel::debug!("All tests finished."),
}
}
}
impl CapsuleTestClient for TestLauncher {
fn done(&'static self, _result: Result<(), CapsuleTestError>) {
self.next();
}
}
#[inline(never)]
unsafe fn create_peripherals() -> &'static mut Nrf52840DefaultPeripherals<'static> {
let ieee802154_ack_buf = static_init!(
[u8; nrf52840::ieee802154_radio::ACK_BUF_SIZE],
[0; nrf52840::ieee802154_radio::ACK_BUF_SIZE]
);
let nrf52840_peripherals = static_init!(
Nrf52840DefaultPeripherals,
Nrf52840DefaultPeripherals::new(ieee802154_ack_buf)
);
nrf52840_peripherals
}
impl KernelResources<nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>>
for Platform
{
type SyscallDriverLookup = Self;
type SyscallFilter = ();
type ProcessFault = ();
type Scheduler = RoundRobinSched<'static>;
type SchedulerTimer = cortexm4::systick::SysTick;
type WatchDog = ();
type ContextSwitchCallback = ();
fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
self
}
fn syscall_filter(&self) -> &Self::SyscallFilter {
&()
}
fn process_fault(&self) -> &Self::ProcessFault {
&()
}
fn scheduler(&self) -> &Self::Scheduler {
self.scheduler
}
fn scheduler_timer(&self) -> &Self::SchedulerTimer {
&self.systick
}
fn watchdog(&self) -> &Self::WatchDog {
&()
}
fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
&()
}
}
#[no_mangle]
pub unsafe fn main() {
nrf52840::init();
let nrf52840_peripherals = create_peripherals();
nrf52840_peripherals.init();
let base_peripherals = &nrf52840_peripherals.nrf52;
let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&*addr_of!(PROCESSES)));
let chip = static_init!(
nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
nrf52840::chip::NRF52::new(nrf52840_peripherals)
);
CHIP = Some(chip);
nrf52_components::startup::NrfStartupComponent::new(
false,
BUTTON_RST_PIN,
nrf52840::uicr::Regulator0Output::DEFAULT,
&base_peripherals.nvmc,
)
.finalize(());
let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
let rtc = &base_peripherals.rtc;
let _ = rtc.start();
let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
.finalize(components::alarm_mux_component_static!(nrf52840::rtc::Rtc));
let uart_channel = nrf52_components::UartChannelComponent::new(
UartChannel::Pins(UartPins::new(UART_RTS, UART_TXD, UART_CTS, UART_RXD)),
mux_alarm,
&base_peripherals.uarte0,
)
.finalize(nrf52_components::uart_channel_component_static!(
nrf52840::rtc::Rtc
));
let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
.finalize(components::uart_mux_component_static!());
components::debug_writer::DebugWriterComponent::new(uart_mux)
.finalize(components::debug_writer_component_static!());
nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
let scheduler = components::sched::round_robin::RoundRobinComponent::new(&*addr_of!(PROCESSES))
.finalize(components::round_robin_component_static!(NUM_PROCS));
let platform = Platform {
scheduler,
systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
};
let test_launcher = static_init!(TestLauncher, TestLauncher::new(base_peripherals));
test_launcher.next();
board_kernel.kernel_loop(
&platform,
chip,
None::<&kernel::ipc::IPC<0>>,
&main_loop_capability,
);
}