nrf52840dk_thread_tutorial/
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::debug;
use kernel::platform::{KernelResources, SyscallDriverLookup};
use kernel::{capabilities, create_capability};
use nrf52840::gpio::Pin;
use nrf52840dk_lib::{self, PROCESSES};
type ScreenDriver = components::screen::ScreenComponentType;
const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
capsules_system::process_policies::PanicFaultPolicy {};
type Ieee802154RawDriver =
components::ieee802154::Ieee802154RawComponentType<nrf52840::ieee802154_radio::Radio<'static>>;
struct Platform {
base: nrf52840dk_lib::Platform,
ieee802154: &'static Ieee802154RawDriver,
eui64: &'static nrf52840dk_lib::Eui64Driver,
screen: &'static ScreenDriver,
nonvolatile_storage:
&'static capsules_extra::nonvolatile_storage_driver::NonvolatileStorage<'static>,
}
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_extra::eui64::DRIVER_NUM => f(Some(self.eui64)),
capsules_extra::ieee802154::DRIVER_NUM => f(Some(self.ieee802154)),
capsules_extra::screen::DRIVER_NUM => f(Some(self.screen)),
capsules_extra::nonvolatile_storage_driver::DRIVER_NUM => {
f(Some(self.nonvolatile_storage))
}
_ => self.base.with_driver(driver_num, f),
}
}
}
type Chip = nrf52840dk_lib::Chip;
impl KernelResources<Chip> for Platform {
type SyscallDriverLookup = Self;
type SyscallFilter = <nrf52840dk_lib::Platform as KernelResources<Chip>>::SyscallFilter;
type ProcessFault = <nrf52840dk_lib::Platform as KernelResources<Chip>>::ProcessFault;
type Scheduler = <nrf52840dk_lib::Platform as KernelResources<Chip>>::Scheduler;
type SchedulerTimer = <nrf52840dk_lib::Platform as KernelResources<Chip>>::SchedulerTimer;
type WatchDog = <nrf52840dk_lib::Platform as KernelResources<Chip>>::WatchDog;
type ContextSwitchCallback =
<nrf52840dk_lib::Platform as KernelResources<Chip>>::ContextSwitchCallback;
fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
self
}
fn syscall_filter(&self) -> &Self::SyscallFilter {
self.base.syscall_filter()
}
fn process_fault(&self) -> &Self::ProcessFault {
self.base.process_fault()
}
fn scheduler(&self) -> &Self::Scheduler {
self.base.scheduler()
}
fn scheduler_timer(&self) -> &Self::SchedulerTimer {
self.base.scheduler_timer()
}
fn watchdog(&self) -> &Self::WatchDog {
self.base.watchdog()
}
fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
self.base.context_switch_callback()
}
}
#[no_mangle]
pub unsafe fn main() {
let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
let (board_kernel, base_platform, chip, nrf52840_peripherals, _mux_alarm) =
nrf52840dk_lib::start();
let device_id = (*addr_of!(nrf52840::ficr::FICR_INSTANCE)).id();
let eui64 = components::eui64::Eui64Component::new(u64::from_le_bytes(device_id))
.finalize(components::eui64_component_static!());
let ieee802154 = components::ieee802154::Ieee802154RawComponent::new(
board_kernel,
capsules_extra::ieee802154::DRIVER_NUM,
&nrf52840_peripherals.ieee802154_radio,
)
.finalize(components::ieee802154_raw_component_static!(
nrf52840::ieee802154_radio::Radio,
));
const SCREEN_I2C_SDA_PIN: Pin = Pin::P1_10;
const SCREEN_I2C_SCL_PIN: Pin = Pin::P1_11;
let i2c_bus = components::i2c::I2CMuxComponent::new(&nrf52840_peripherals.nrf52.twi1, None)
.finalize(components::i2c_mux_component_static!(nrf52840::i2c::TWI));
nrf52840_peripherals.nrf52.twi1.configure(
nrf52840::pinmux::Pinmux::new(SCREEN_I2C_SCL_PIN as u32),
nrf52840::pinmux::Pinmux::new(SCREEN_I2C_SDA_PIN as u32),
);
nrf52840_peripherals
.nrf52
.twi1
.set_speed(nrf52840::i2c::Speed::K400);
let ssd1306_sh1106_i2c = components::i2c::I2CComponent::new(i2c_bus, 0x3c)
.finalize(components::i2c_component_static!(nrf52840::i2c::TWI));
#[cfg(feature = "screen_ssd1306")]
let ssd1306_sh1106 = components::ssd1306::Ssd1306Component::new(ssd1306_sh1106_i2c, true)
.finalize(components::ssd1306_component_static!(nrf52840::i2c::TWI));
#[cfg(feature = "screen_sh1106")]
let ssd1306_sh1106 = components::sh1106::Sh1106Component::new(ssd1306_sh1106_i2c, true)
.finalize(components::sh1106_component_static!(nrf52840::i2c::TWI));
let screen = components::screen::ScreenComponent::new(
board_kernel,
capsules_extra::screen::DRIVER_NUM,
ssd1306_sh1106,
None,
)
.finalize(components::screen_component_static!(1032));
ssd1306_sh1106.init_screen();
kernel::storage_volume!(APP_STORAGE, 32);
let nonvolatile_storage = components::nonvolatile_storage::NonvolatileStorageComponent::new(
board_kernel,
capsules_extra::nonvolatile_storage_driver::DRIVER_NUM,
&nrf52840_peripherals.nrf52.nvmc,
core::ptr::addr_of!(APP_STORAGE) as usize,
APP_STORAGE.len(),
core::ptr::null::<()>() as usize,
0,
)
.finalize(components::nonvolatile_storage_component_static!(
nrf52840::nvmc::Nvmc
));
let platform = Platform {
base: base_platform,
eui64,
ieee802154,
screen,
nonvolatile_storage,
};
extern "C" {
static _sapps: u8;
static _eapps: u8;
static mut _sappmem: u8;
static _eappmem: u8;
}
let process_management_capability =
create_capability!(capabilities::ProcessManagementCapability);
kernel::process::load_processes(
board_kernel,
chip,
core::slice::from_raw_parts(
core::ptr::addr_of!(_sapps),
core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
),
core::slice::from_raw_parts_mut(
core::ptr::addr_of_mut!(_sappmem),
core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
),
&mut *addr_of_mut!(PROCESSES),
&FAULT_RESPONSE,
&process_management_capability,
)
.unwrap_or_else(|err| {
debug!("Error loading processes!");
debug!("{:?}", err);
});
board_kernel.kernel_loop(
&platform,
chip,
Some(&platform.base.ipc),
&main_loop_capability,
);
}