nrf52840dk_test_appid_sha256/
main.rs1#![no_std]
8#![no_main]
9#![deny(missing_docs)]
10
11use kernel::component::Component;
12use kernel::hil::led::LedLow;
13use kernel::hil::time::Counter;
14use kernel::platform::{KernelResources, SyscallDriverLookup};
15use kernel::process::ProcessArray;
16use kernel::scheduler::round_robin::RoundRobinSched;
17use kernel::{capabilities, create_capability, static_init};
18use nrf52840::gpio::Pin;
19use nrf52840::interrupt_service::Nrf52840DefaultPeripherals;
20use nrf52_components::{UartChannel, UartPins};
21
22const LED1_PIN: Pin = Pin::P0_13;
24const LED2_PIN: Pin = Pin::P0_14;
25const LED3_PIN: Pin = Pin::P0_15;
26const LED4_PIN: Pin = Pin::P0_16;
27
28const BUTTON_RST_PIN: Pin = Pin::P0_18;
29
30const UART_RTS: Option<Pin> = Some(Pin::P0_05);
31const UART_TXD: Pin = Pin::P0_06;
32const UART_CTS: Option<Pin> = Some(Pin::P0_07);
33const UART_RXD: Pin = Pin::P0_08;
34
35pub mod io;
37
38const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
41    capsules_system::process_policies::PanicFaultPolicy {};
42
43const NUM_PROCS: usize = 8;
45
46static mut PROCESSES: Option<&'static ProcessArray<NUM_PROCS>> = None;
48static mut CHIP: Option<&'static nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>> = None;
49
50kernel::stack_size! {0x2000}
51
52type AlarmDriver = components::alarm::AlarmDriverComponentType<nrf52840::rtc::Rtc<'static>>;
57
58pub struct Platform {
60    console: &'static capsules_core::console::Console<'static>,
61    led: &'static capsules_core::led::LedDriver<
62        'static,
63        kernel::hil::led::LedLow<'static, nrf52840::gpio::GPIOPin<'static>>,
64        4,
65    >,
66    alarm: &'static AlarmDriver,
67    scheduler: &'static RoundRobinSched<'static>,
68    systick: cortexm4::systick::SysTick,
69}
70
71impl SyscallDriverLookup for Platform {
72    fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
73    where
74        F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
75    {
76        match driver_num {
77            capsules_core::console::DRIVER_NUM => f(Some(self.console)),
78            capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
79            capsules_core::led::DRIVER_NUM => f(Some(self.led)),
80            _ => f(None),
81        }
82    }
83}
84
85#[inline(never)]
89unsafe fn create_peripherals() -> &'static mut Nrf52840DefaultPeripherals<'static> {
90    let ieee802154_ack_buf = static_init!(
91        [u8; nrf52840::ieee802154_radio::ACK_BUF_SIZE],
92        [0; nrf52840::ieee802154_radio::ACK_BUF_SIZE]
93    );
94    let nrf52840_peripherals = static_init!(
96        Nrf52840DefaultPeripherals,
97        Nrf52840DefaultPeripherals::new(ieee802154_ack_buf)
98    );
99
100    nrf52840_peripherals
101}
102
103impl KernelResources<nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>>
104    for Platform
105{
106    type SyscallDriverLookup = Self;
107    type SyscallFilter = ();
108    type ProcessFault = ();
109    type Scheduler = RoundRobinSched<'static>;
110    type SchedulerTimer = cortexm4::systick::SysTick;
111    type WatchDog = ();
112    type ContextSwitchCallback = ();
113
114    fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
115        self
116    }
117    fn syscall_filter(&self) -> &Self::SyscallFilter {
118        &()
119    }
120    fn process_fault(&self) -> &Self::ProcessFault {
121        &()
122    }
123    fn scheduler(&self) -> &Self::Scheduler {
124        self.scheduler
125    }
126    fn scheduler_timer(&self) -> &Self::SchedulerTimer {
127        &self.systick
128    }
129    fn watchdog(&self) -> &Self::WatchDog {
130        &()
131    }
132    fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
133        &()
134    }
135}
136
137#[no_mangle]
139pub unsafe fn main() {
140    nrf52840::init();
146
147    let nrf52840_peripherals = create_peripherals();
150
151    nrf52840_peripherals.init();
153    let base_peripherals = &nrf52840_peripherals.nrf52;
154
155    let uart_channel = UartChannel::Pins(UartPins::new(UART_RTS, UART_TXD, UART_CTS, UART_RXD));
159
160    let processes = components::process_array::ProcessArrayComponent::new()
162        .finalize(components::process_array_component_static!(NUM_PROCS));
163    PROCESSES = Some(processes);
164
165    let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
167
168    let chip = static_init!(
171        nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
172        nrf52840::chip::NRF52::new(nrf52840_peripherals)
173    );
174    CHIP = Some(chip);
175
176    nrf52_components::startup::NrfStartupComponent::new(
179        false,
180        BUTTON_RST_PIN,
181        nrf52840::uicr::Regulator0Output::DEFAULT,
182        &base_peripherals.nvmc,
183    )
184    .finalize(());
185
186    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
193
194    let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
199        LedLow<'static, nrf52840::gpio::GPIOPin>,
200        LedLow::new(&nrf52840_peripherals.gpio_port[LED1_PIN]),
201        LedLow::new(&nrf52840_peripherals.gpio_port[LED2_PIN]),
202        LedLow::new(&nrf52840_peripherals.gpio_port[LED3_PIN]),
203        LedLow::new(&nrf52840_peripherals.gpio_port[LED4_PIN]),
204    ));
205
206    let rtc = &base_peripherals.rtc;
211    let _ = rtc.start();
212    let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
213        .finalize(components::alarm_mux_component_static!(nrf52840::rtc::Rtc));
214    let alarm = components::alarm::AlarmDriverComponent::new(
215        board_kernel,
216        capsules_core::alarm::DRIVER_NUM,
217        mux_alarm,
218    )
219    .finalize(components::alarm_component_static!(nrf52840::rtc::Rtc));
220
221    let uart_channel = nrf52_components::UartChannelComponent::new(
226        uart_channel,
227        mux_alarm,
228        &base_peripherals.uarte0,
229    )
230    .finalize(nrf52_components::uart_channel_component_static!(
231        nrf52840::rtc::Rtc
232    ));
233
234    let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
236        .finalize(components::uart_mux_component_static!());
237
238    let console = components::console::ConsoleComponent::new(
240        board_kernel,
241        capsules_core::console::DRIVER_NUM,
242        uart_mux,
243    )
244    .finalize(components::console_component_static!());
245
246    nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
251
252    let sha = components::sha::ShaSoftware256Component::new()
258        .finalize(components::sha_software_256_component_static!());
259
260    let checking_policy = components::appid::checker_sha::AppCheckerSha256Component::new(sha)
262        .finalize(components::app_checker_sha256_component_static!());
263
264    let assigner = components::appid::assigner_name::AppIdAssignerNamesComponent::new()
266        .finalize(components::appid_assigner_names_component_static!());
267
268    let checker = components::appid::checker::ProcessCheckerMachineComponent::new(checking_policy)
270        .finalize(components::process_checker_machine_component_static!());
271
272    let storage_permissions_policy =
277        components::storage_permissions::null::StoragePermissionsNullComponent::new().finalize(
278            components::storage_permissions_null_component_static!(
279                nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
280                kernel::process::ProcessStandardDebugFull,
281            ),
282        );
283
284    extern "C" {
290        static _sapps: u8;
292        static _eapps: u8;
294        static mut _sappmem: u8;
296        static _eappmem: u8;
298    }
299
300    let app_flash = core::slice::from_raw_parts(
301        core::ptr::addr_of!(_sapps),
302        core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
303    );
304    let app_memory = core::slice::from_raw_parts_mut(
305        core::ptr::addr_of_mut!(_sappmem),
306        core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
307    );
308
309    let _loader = components::loader::sequential::ProcessLoaderSequentialComponent::new(
311        checker,
312        board_kernel,
313        chip,
314        &FAULT_RESPONSE,
315        assigner,
316        storage_permissions_policy,
317        app_flash,
318        app_memory,
319    )
320    .finalize(components::process_loader_sequential_component_static!(
321        nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
322        kernel::process::ProcessStandardDebugFull,
323        NUM_PROCS
324    ));
325
326    let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
331        .finalize(components::round_robin_component_static!(NUM_PROCS));
332
333    let platform = Platform {
334        console,
335        led,
336        alarm,
337        scheduler,
338        systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
339    };
340
341    board_kernel.kernel_loop(
342        &platform,
343        chip,
344        None::<&kernel::ipc::IPC<0>>,
345        &main_loop_capability,
346    );
347}