nrf52840dk_test_appid_tbf/
main.rs#![no_std]
#![cfg_attr(not(doc), no_main)]
#![deny(missing_docs)]
use core::ptr::{addr_of, addr_of_mut};
use kernel::component::Component;
use kernel::hil::led::LedLow;
use kernel::hil::time::Counter;
use kernel::platform::{KernelResources, SyscallDriverLookup};
use kernel::process::ProcessLoadingAsync;
use kernel::scheduler::round_robin::RoundRobinSched;
use kernel::{capabilities, create_capability, static_init};
use nrf52840::gpio::Pin;
use nrf52840::interrupt_service::Nrf52840DefaultPeripherals;
use nrf52_components::{UartChannel, UartPins};
const LED1_PIN: Pin = Pin::P0_13;
const LED2_PIN: Pin = Pin::P0_14;
const LED3_PIN: Pin = Pin::P0_15;
const LED4_PIN: Pin = Pin::P0_16;
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 FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
capsules_system::process_policies::PanicFaultPolicy {};
const NUM_PROCS: usize = 8;
static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
[None; NUM_PROCS];
static mut CHIP: Option<&'static nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>> = None;
#[no_mangle]
#[link_section = ".stack_buffer"]
pub static mut STACK_MEMORY: [u8; 0x2000] = [0; 0x2000];
type AlarmDriver = components::alarm::AlarmDriverComponentType<nrf52840::rtc::Rtc<'static>>;
pub struct Platform {
console: &'static capsules_core::console::Console<'static>,
led: &'static capsules_core::led::LedDriver<
'static,
kernel::hil::led::LedLow<'static, nrf52840::gpio::GPIOPin<'static>>,
4,
>,
alarm: &'static AlarmDriver,
scheduler: &'static RoundRobinSched<'static>,
systick: cortexm4::systick::SysTick,
processes: &'static [Option<&'static dyn kernel::process::Process>],
}
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,
{
match driver_num {
capsules_core::console::DRIVER_NUM => f(Some(self.console)),
capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
capsules_core::led::DRIVER_NUM => f(Some(self.led)),
_ => f(None),
}
}
}
#[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 {
&()
}
}
impl kernel::process::ProcessLoadingAsyncClient for Platform {
fn process_loaded(&self, _result: Result<(), kernel::process::ProcessLoadError>) {}
fn process_loading_finished(&self) {
kernel::debug!("Processes Loaded:");
for (i, proc) in self.processes.iter().enumerate() {
proc.map(|p| {
kernel::debug!("[{}] {}", i, p.get_process_name());
kernel::debug!(" ShortId: {}", p.short_app_id());
});
}
}
}
#[no_mangle]
pub unsafe fn main() {
nrf52840::init();
let nrf52840_peripherals = create_peripherals();
nrf52840_peripherals.init();
let base_peripherals = &nrf52840_peripherals.nrf52;
let processes = &*addr_of!(PROCESSES);
let uart_channel = UartChannel::Pins(UartPins::new(UART_RTS, UART_TXD, UART_CTS, UART_RXD));
let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(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 led = components::led::LedsComponent::new().finalize(components::led_component_static!(
LedLow<'static, nrf52840::gpio::GPIOPin>,
LedLow::new(&nrf52840_peripherals.gpio_port[LED1_PIN]),
LedLow::new(&nrf52840_peripherals.gpio_port[LED2_PIN]),
LedLow::new(&nrf52840_peripherals.gpio_port[LED3_PIN]),
LedLow::new(&nrf52840_peripherals.gpio_port[LED4_PIN]),
));
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 alarm = components::alarm::AlarmDriverComponent::new(
board_kernel,
capsules_core::alarm::DRIVER_NUM,
mux_alarm,
)
.finalize(components::alarm_component_static!(nrf52840::rtc::Rtc));
let uart_channel = nrf52_components::UartChannelComponent::new(
uart_channel,
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!());
let console = components::console::ConsoleComponent::new(
board_kernel,
capsules_core::console::DRIVER_NUM,
uart_mux,
)
.finalize(components::console_component_static!());
let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
.finalize(components::process_printer_text_component_static!());
let pconsole = components::process_console::ProcessConsoleComponent::new(
board_kernel,
uart_mux,
mux_alarm,
process_printer,
Some(cortexm4::support::reset),
)
.finalize(components::process_console_component_static!(
nrf52840::rtc::Rtc<'static>
));
components::debug_writer::DebugWriterComponent::new(uart_mux)
.finalize(components::debug_writer_component_static!());
nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
let checking_policy = components::appid::checker_null::AppCheckerNullComponent::new()
.finalize(components::app_checker_null_component_static!());
let assigner = components::appid::assigner_tbf::AppIdAssignerTbfHeaderComponent::new()
.finalize(components::appid_assigner_tbf_header_component_static!());
let checker = components::appid::checker::ProcessCheckerMachineComponent::new(checking_policy)
.finalize(components::process_checker_machine_component_static!());
let storage_permissions_policy =
components::storage_permissions::null::StoragePermissionsNullComponent::new().finalize(
components::storage_permissions_null_component_static!(
nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
kernel::process::ProcessStandardDebugFull,
),
);
let loader = components::loader::sequential::ProcessLoaderSequentialComponent::new(
checker,
&mut *addr_of_mut!(PROCESSES),
board_kernel,
chip,
&FAULT_RESPONSE,
assigner,
storage_permissions_policy,
)
.finalize(components::process_loader_sequential_component_static!(
nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
kernel::process::ProcessStandardDebugFull,
NUM_PROCS
));
let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
.finalize(components::round_robin_component_static!(NUM_PROCS));
let platform = static_init!(
Platform,
Platform {
console,
led,
alarm,
scheduler,
systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
processes,
}
);
loader.set_client(platform);
let _ = pconsole.start();
board_kernel.kernel_loop(
platform,
chip,
None::<&kernel::ipc::IPC<0>>,
&main_loop_capability,
);
}