1#![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
22mod invs_permissions;
23
24const LED1_PIN: Pin = Pin::P0_13;
26const LED2_PIN: Pin = Pin::P0_14;
27const LED3_PIN: Pin = Pin::P0_15;
28const LED4_PIN: Pin = Pin::P0_16;
29
30const BUTTON_RST_PIN: Pin = Pin::P0_18;
31
32const UART_RTS: Option<Pin> = Some(Pin::P0_05);
33const UART_TXD: Pin = Pin::P0_06;
34const UART_CTS: Option<Pin> = Some(Pin::P0_07);
35const UART_RXD: Pin = Pin::P0_08;
36
37const SPI_MOSI: Pin = Pin::P0_20;
38const SPI_MISO: Pin = Pin::P0_21;
39const SPI_CLK: Pin = Pin::P0_19;
40
41const SPI_MX25R6435F_CHIP_SELECT: Pin = Pin::P0_17;
42const SPI_MX25R6435F_WRITE_PROTECT_PIN: Pin = Pin::P0_22;
43const SPI_MX25R6435F_HOLD_PIN: Pin = Pin::P0_23;
44
45pub mod io;
47
48const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
51    capsules_system::process_policies::PanicFaultPolicy {};
52
53const NUM_PROCS: usize = 8;
55
56static mut PROCESSES: Option<&'static ProcessArray<NUM_PROCS>> = None;
58static mut CHIP: Option<&'static nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>> = None;
59
60kernel::stack_size! {0x2000}
61
62const APP_STORAGE_REGION_SIZE: usize = 4096;
63
64type AlarmDriver = components::alarm::AlarmDriverComponentType<nrf52840::rtc::Rtc<'static>>;
69
70type Mx25r6435f = components::mx25r6435f::Mx25r6435fComponentType<
71    nrf52840::spi::SPIM<'static>,
72    nrf52840::gpio::GPIOPin<'static>,
73    nrf52840::rtc::Rtc<'static>,
74>;
75type InvsDriver = components::isolated_nonvolatile_storage::IsolatedNonvolatileStorageComponentType<
76    APP_STORAGE_REGION_SIZE,
77>;
78
79pub struct Platform {
81    console: &'static capsules_core::console::Console<'static>,
82    led: &'static capsules_core::led::LedDriver<
83        'static,
84        kernel::hil::led::LedLow<'static, nrf52840::gpio::GPIOPin<'static>>,
85        4,
86    >,
87    alarm: &'static AlarmDriver,
88    invs: &'static InvsDriver,
89    scheduler: &'static RoundRobinSched<'static>,
90    systick: cortexm4::systick::SysTick,
91}
92
93impl SyscallDriverLookup for Platform {
94    fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
95    where
96        F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
97    {
98        match driver_num {
99            capsules_core::console::DRIVER_NUM => f(Some(self.console)),
100            capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
101            capsules_core::led::DRIVER_NUM => f(Some(self.led)),
102            capsules_extra::isolated_nonvolatile_storage_driver::DRIVER_NUM => f(Some(self.invs)),
103            _ => f(None),
104        }
105    }
106}
107
108#[inline(never)]
112unsafe fn create_peripherals() -> &'static mut Nrf52840DefaultPeripherals<'static> {
113    let ieee802154_ack_buf = static_init!(
114        [u8; nrf52840::ieee802154_radio::ACK_BUF_SIZE],
115        [0; nrf52840::ieee802154_radio::ACK_BUF_SIZE]
116    );
117    let nrf52840_peripherals = static_init!(
119        Nrf52840DefaultPeripherals,
120        Nrf52840DefaultPeripherals::new(ieee802154_ack_buf)
121    );
122
123    nrf52840_peripherals
124}
125
126impl KernelResources<nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>>
127    for Platform
128{
129    type SyscallDriverLookup = Self;
130    type SyscallFilter = ();
131    type ProcessFault = ();
132    type Scheduler = RoundRobinSched<'static>;
133    type SchedulerTimer = cortexm4::systick::SysTick;
134    type WatchDog = ();
135    type ContextSwitchCallback = ();
136
137    fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
138        self
139    }
140    fn syscall_filter(&self) -> &Self::SyscallFilter {
141        &()
142    }
143    fn process_fault(&self) -> &Self::ProcessFault {
144        &()
145    }
146    fn scheduler(&self) -> &Self::Scheduler {
147        self.scheduler
148    }
149    fn scheduler_timer(&self) -> &Self::SchedulerTimer {
150        &self.systick
151    }
152    fn watchdog(&self) -> &Self::WatchDog {
153        &()
154    }
155    fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
156        &()
157    }
158}
159
160#[no_mangle]
162pub unsafe fn main() {
163    nrf52840::init();
169
170    let nrf52840_peripherals = create_peripherals();
173
174    nrf52840_peripherals.init();
176    let base_peripherals = &nrf52840_peripherals.nrf52;
177
178    let uart_channel = UartChannel::Pins(UartPins::new(UART_RTS, UART_TXD, UART_CTS, UART_RXD));
182
183    let processes = components::process_array::ProcessArrayComponent::new()
185        .finalize(components::process_array_component_static!(NUM_PROCS));
186    PROCESSES = Some(processes);
187
188    let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
190
191    let chip = static_init!(
194        nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
195        nrf52840::chip::NRF52::new(nrf52840_peripherals)
196    );
197    CHIP = Some(chip);
198
199    nrf52_components::startup::NrfStartupComponent::new(
202        false,
203        BUTTON_RST_PIN,
204        nrf52840::uicr::Regulator0Output::DEFAULT,
205        &base_peripherals.nvmc,
206    )
207    .finalize(());
208
209    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
216
217    let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
222        LedLow<'static, nrf52840::gpio::GPIOPin>,
223        LedLow::new(&nrf52840_peripherals.gpio_port[LED1_PIN]),
224        LedLow::new(&nrf52840_peripherals.gpio_port[LED2_PIN]),
225        LedLow::new(&nrf52840_peripherals.gpio_port[LED3_PIN]),
226        LedLow::new(&nrf52840_peripherals.gpio_port[LED4_PIN]),
227    ));
228
229    let rtc = &base_peripherals.rtc;
234    let _ = rtc.start();
235    let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
236        .finalize(components::alarm_mux_component_static!(nrf52840::rtc::Rtc));
237    let alarm = components::alarm::AlarmDriverComponent::new(
238        board_kernel,
239        capsules_core::alarm::DRIVER_NUM,
240        mux_alarm,
241    )
242    .finalize(components::alarm_component_static!(nrf52840::rtc::Rtc));
243
244    let uart_channel = nrf52_components::UartChannelComponent::new(
249        uart_channel,
250        mux_alarm,
251        &base_peripherals.uarte0,
252    )
253    .finalize(nrf52_components::uart_channel_component_static!(
254        nrf52840::rtc::Rtc
255    ));
256
257    let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
259        .finalize(components::uart_mux_component_static!());
260
261    let console = components::console::ConsoleComponent::new(
263        board_kernel,
264        capsules_core::console::DRIVER_NUM,
265        uart_mux,
266    )
267    .finalize(components::console_component_static!());
268
269    let mux_spi = components::spi::SpiMuxComponent::new(&base_peripherals.spim0)
274        .finalize(components::spi_mux_component_static!(nrf52840::spi::SPIM));
275
276    base_peripherals.spim0.configure(
277        nrf52840::pinmux::Pinmux::new(SPI_MOSI as u32),
278        nrf52840::pinmux::Pinmux::new(SPI_MISO as u32),
279        nrf52840::pinmux::Pinmux::new(SPI_CLK as u32),
280    );
281
282    let mx25r6435f = components::mx25r6435f::Mx25r6435fComponent::new(
283        Some(&nrf52840_peripherals.gpio_port[SPI_MX25R6435F_WRITE_PROTECT_PIN]),
284        Some(&nrf52840_peripherals.gpio_port[SPI_MX25R6435F_HOLD_PIN]),
285        &nrf52840_peripherals.gpio_port[SPI_MX25R6435F_CHIP_SELECT],
286        mux_alarm,
287        mux_spi,
288    )
289    .finalize(components::mx25r6435f_component_static!(
290        nrf52840::spi::SPIM,
291        nrf52840::gpio::GPIOPin,
292        nrf52840::rtc::Rtc
293    ));
294
295    let invs = components::isolated_nonvolatile_storage::IsolatedNonvolatileStorageComponent::new(
300        board_kernel,
301        capsules_extra::isolated_nonvolatile_storage_driver::DRIVER_NUM,
302        mx25r6435f,
303        0x40000,  0x100000, )
306    .finalize(components::isolated_nonvolatile_storage_component_static!(
307        Mx25r6435f,
308        APP_STORAGE_REGION_SIZE
309    ));
310
311    nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
316
317    let sha = components::sha::ShaSoftware256Component::new()
323        .finalize(components::sha_software_256_component_static!());
324
325    let checking_policy = components::appid::checker_sha::AppCheckerSha256Component::new(sha)
327        .finalize(components::app_checker_sha256_component_static!());
328
329    let assigner = components::appid::assigner_name::AppIdAssignerNamesComponent::new()
331        .finalize(components::appid_assigner_names_component_static!());
332
333    let checker = components::appid::checker::ProcessCheckerMachineComponent::new(checking_policy)
335        .finalize(components::process_checker_machine_component_static!());
336
337    #[derive(Clone)]
346    pub struct AppStoreCapability;
347    unsafe impl capabilities::ApplicationStorageCapability for AppStoreCapability {}
348
349    let storage_permissions_policy = static_init!(
350        invs_permissions::InvsStoragePermissions<
351            nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
352            kernel::process::ProcessStandardDebugFull,
353            AppStoreCapability,
354        >,
355        invs_permissions::InvsStoragePermissions::new(AppStoreCapability)
356    );
357
358    extern "C" {
364        static _sapps: u8;
366        static _eapps: u8;
368        static mut _sappmem: u8;
370        static _eappmem: u8;
372    }
373
374    let app_flash = core::slice::from_raw_parts(
375        core::ptr::addr_of!(_sapps),
376        core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
377    );
378    let app_memory = core::slice::from_raw_parts_mut(
379        core::ptr::addr_of_mut!(_sappmem),
380        core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
381    );
382
383    let _loader = components::loader::sequential::ProcessLoaderSequentialComponent::new(
385        checker,
386        board_kernel,
387        chip,
388        &FAULT_RESPONSE,
389        assigner,
390        storage_permissions_policy,
391        app_flash,
392        app_memory,
393    )
394    .finalize(components::process_loader_sequential_component_static!(
395        nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
396        kernel::process::ProcessStandardDebugFull,
397        NUM_PROCS
398    ));
399
400    let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
405        .finalize(components::round_robin_component_static!(NUM_PROCS));
406
407    let platform = Platform {
408        console,
409        led,
410        alarm,
411        invs,
412        scheduler,
413        systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
414    };
415
416    board_kernel.kernel_loop(
417        &platform,
418        chip,
419        None::<&kernel::ipc::IPC<0>>,
420        &main_loop_capability,
421    );
422}