nrf52840dk/
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 kernel::debug;
12use kernel::platform::{KernelResources, SyscallDriverLookup};
13use kernel::{capabilities, create_capability};
14
15// State for loading and holding applications.
16// How should the kernel respond when a process faults.
17const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
18    capsules_system::process_policies::PanicFaultPolicy {};
19
20struct Platform {
21    base: nrf52840dk_lib::Platform,
22    eui64_driver: &'static nrf52840dk_lib::Eui64Driver,
23    ieee802154_driver: &'static nrf52840dk_lib::Ieee802154Driver,
24    udp_driver: &'static capsules_extra::net::udp::UDPDriver<'static>,
25}
26
27impl SyscallDriverLookup for Platform {
28    fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
29    where
30        F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
31    {
32        match driver_num {
33            capsules_extra::eui64::DRIVER_NUM => f(Some(self.eui64_driver)),
34            capsules_extra::net::udp::DRIVER_NUM => f(Some(self.udp_driver)),
35            capsules_extra::ieee802154::DRIVER_NUM => f(Some(self.ieee802154_driver)),
36            _ => self.base.with_driver(driver_num, f),
37        }
38    }
39}
40
41type Chip = nrf52840dk_lib::Chip;
42
43impl KernelResources<Chip> for Platform {
44    type SyscallDriverLookup = Self;
45    type SyscallFilter = <nrf52840dk_lib::Platform as KernelResources<Chip>>::SyscallFilter;
46    type ProcessFault = <nrf52840dk_lib::Platform as KernelResources<Chip>>::ProcessFault;
47    type Scheduler = <nrf52840dk_lib::Platform as KernelResources<Chip>>::Scheduler;
48    type SchedulerTimer = <nrf52840dk_lib::Platform as KernelResources<Chip>>::SchedulerTimer;
49    type WatchDog = <nrf52840dk_lib::Platform as KernelResources<Chip>>::WatchDog;
50    type ContextSwitchCallback =
51        <nrf52840dk_lib::Platform as KernelResources<Chip>>::ContextSwitchCallback;
52
53    fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
54        self
55    }
56    fn syscall_filter(&self) -> &Self::SyscallFilter {
57        self.base.syscall_filter()
58    }
59    fn process_fault(&self) -> &Self::ProcessFault {
60        self.base.process_fault()
61    }
62    fn scheduler(&self) -> &Self::Scheduler {
63        self.base.scheduler()
64    }
65    fn scheduler_timer(&self) -> &Self::SchedulerTimer {
66        self.base.scheduler_timer()
67    }
68    fn watchdog(&self) -> &Self::WatchDog {
69        self.base.watchdog()
70    }
71    fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
72        self.base.context_switch_callback()
73    }
74}
75
76/// Main function called after RAM initialized.
77#[no_mangle]
78pub unsafe fn main() {
79    let (board_kernel, base_platform, chip, default_peripherals, mux_alarm) =
80        nrf52840dk_lib::start();
81
82    //--------------------------------------------------------------------------
83    // IEEE 802.15.4 and UDP
84    //--------------------------------------------------------------------------
85
86    let (eui64_driver, ieee802154_driver, udp_driver) =
87        nrf52840dk_lib::ieee802154_udp(board_kernel, default_peripherals, mux_alarm);
88
89    let platform = Platform {
90        base: base_platform,
91        eui64_driver,
92        ieee802154_driver,
93        udp_driver,
94    };
95
96    // These symbols are defined in the linker script.
97    extern "C" {
98        /// Beginning of the ROM region containing app images.
99        static _sapps: u8;
100        /// End of the ROM region containing app images.
101        static _eapps: u8;
102        /// Beginning of the RAM region for app memory.
103        static mut _sappmem: u8;
104        /// End of the RAM region for app memory.
105        static _eappmem: u8;
106    }
107
108    let process_management_capability =
109        create_capability!(capabilities::ProcessManagementCapability);
110    kernel::process::load_processes(
111        board_kernel,
112        chip,
113        core::slice::from_raw_parts(
114            core::ptr::addr_of!(_sapps),
115            core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
116        ),
117        core::slice::from_raw_parts_mut(
118            core::ptr::addr_of_mut!(_sappmem),
119            core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
120        ),
121        &FAULT_RESPONSE,
122        &process_management_capability,
123    )
124    .unwrap_or_else(|err| {
125        debug!("Error loading processes!");
126        debug!("{:?}", err);
127    });
128
129    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
130    board_kernel.kernel_loop(
131        &platform,
132        chip,
133        Some(&platform.base.ipc),
134        &main_loop_capability,
135    );
136}