makepython_nrf52840/
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 MakePython nRF52840.
6//!
7//! It is based on nRF52840 SoC.
8
9#![no_std]
10#![no_main]
11#![deny(missing_docs)]
12
13use core::ptr::{addr_of, addr_of_mut};
14
15use kernel::capabilities;
16use kernel::component::Component;
17use kernel::hil::led::LedLow;
18use kernel::hil::time::Counter;
19use kernel::hil::usb::Client;
20use kernel::platform::{KernelResources, SyscallDriverLookup};
21use kernel::scheduler::round_robin::RoundRobinSched;
22#[allow(unused_imports)]
23use kernel::{create_capability, debug, debug_gpio, debug_verbose, static_init};
24
25use nrf52840::gpio::Pin;
26use nrf52840::interrupt_service::Nrf52840DefaultPeripherals;
27
28// The datasheet and website and everything say this is connected to P1.10, but
29// actually looking at the hardware files (and what actually works) is that the
30// LED is connected to P1.11 (as of a board I received in September 2023).
31//
32// https://github.com/Makerfabs/NRF52840/issues/1
33const LED_PIN: Pin = Pin::P1_11;
34
35const BUTTON_RST_PIN: Pin = Pin::P0_18;
36const BUTTON_PIN: Pin = Pin::P1_15;
37
38const GPIO_D0: Pin = Pin::P0_23;
39const GPIO_D1: Pin = Pin::P0_12;
40const GPIO_D2: Pin = Pin::P0_09;
41const GPIO_D3: Pin = Pin::P0_07;
42
43const _UART_TX_PIN: Pin = Pin::P0_06;
44const _UART_RX_PIN: Pin = Pin::P0_08;
45
46/// I2C pins for all of the sensors.
47const I2C_SDA_PIN: Pin = Pin::P0_26;
48const I2C_SCL_PIN: Pin = Pin::P0_27;
49
50// Constants related to the configuration of the 15.4 network stack
51/// Personal Area Network ID for the IEEE 802.15.4 radio
52const PAN_ID: u16 = 0xABCD;
53/// Gateway (or next hop) MAC Address
54const DST_MAC_ADDR: capsules_extra::net::ieee802154::MacAddress =
55    capsules_extra::net::ieee802154::MacAddress::Short(49138);
56const DEFAULT_CTX_PREFIX_LEN: u8 = 8; //Length of context for 6LoWPAN compression
57const DEFAULT_CTX_PREFIX: [u8; 16] = [0x0_u8; 16]; //Context for 6LoWPAN Compression
58
59/// UART Writer for panic!()s.
60pub mod io;
61
62// How should the kernel respond when a process faults. For this board we choose
63// to stop the app and print a notice, but not immediately panic. This allows
64// users to debug their apps, but avoids issues with using the USB/CDC stack
65// synchronously for panic! too early after the board boots.
66const FAULT_RESPONSE: capsules_system::process_policies::StopWithDebugFaultPolicy =
67    capsules_system::process_policies::StopWithDebugFaultPolicy {};
68
69// Number of concurrent processes this platform supports.
70const NUM_PROCS: usize = 8;
71
72// State for loading and holding applications.
73static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
74    [None; NUM_PROCS];
75
76static mut CHIP: Option<&'static nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>> = None;
77static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
78    None;
79static mut CDC_REF_FOR_PANIC: Option<
80    &'static capsules_extra::usb::cdc::CdcAcm<
81        'static,
82        nrf52::usbd::Usbd,
83        capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, nrf52::rtc::Rtc>,
84    >,
85> = None;
86static mut NRF52_POWER: Option<&'static nrf52840::power::Power> = None;
87
88/// Dummy buffer that causes the linker to reserve enough space for the stack.
89#[no_mangle]
90#[link_section = ".stack_buffer"]
91pub static mut STACK_MEMORY: [u8; 0x1000] = [0; 0x1000];
92
93// Function for the CDC/USB stack to use to enter the bootloader.
94fn baud_rate_reset_bootloader_enter() {
95    unsafe {
96        // 0x90 is the magic value the bootloader expects
97        NRF52_POWER.unwrap().set_gpregret(0x90);
98        cortexm4::scb::reset();
99    }
100}
101
102fn crc(s: &'static str) -> u32 {
103    kernel::utilities::helpers::crc32_posix(s.as_bytes())
104}
105
106//------------------------------------------------------------------------------
107// SYSCALL DRIVER TYPE DEFINITIONS
108//------------------------------------------------------------------------------
109
110type AlarmDriver = components::alarm::AlarmDriverComponentType<nrf52840::rtc::Rtc<'static>>;
111
112type Screen = components::ssd1306::Ssd1306ComponentType<nrf52840::i2c::TWI<'static>>;
113type ScreenDriver = components::screen::ScreenSharedComponentType<Screen>;
114
115type Ieee802154MacDevice = components::ieee802154::Ieee802154ComponentMacDeviceType<
116    nrf52840::ieee802154_radio::Radio<'static>,
117    nrf52840::aes::AesECB<'static>,
118>;
119type Ieee802154Driver = components::ieee802154::Ieee802154ComponentType<
120    nrf52840::ieee802154_radio::Radio<'static>,
121    nrf52840::aes::AesECB<'static>,
122>;
123type RngDriver = components::rng::RngComponentType<nrf52840::trng::Trng<'static>>;
124
125/// Supported drivers by the platform
126pub struct Platform {
127    ble_radio: &'static capsules_extra::ble_advertising_driver::BLE<
128        'static,
129        nrf52::ble_radio::Radio<'static>,
130        capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<
131            'static,
132            nrf52::rtc::Rtc<'static>,
133        >,
134    >,
135    ieee802154_radio: &'static Ieee802154Driver,
136    console: &'static capsules_core::console::Console<'static>,
137    pconsole: &'static capsules_core::process_console::ProcessConsole<
138        'static,
139        { capsules_core::process_console::DEFAULT_COMMAND_HISTORY_LEN },
140        capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<
141            'static,
142            nrf52::rtc::Rtc<'static>,
143        >,
144        components::process_console::Capability,
145    >,
146    gpio: &'static capsules_core::gpio::GPIO<'static, nrf52::gpio::GPIOPin<'static>>,
147    led: &'static capsules_core::led::LedDriver<
148        'static,
149        LedLow<'static, nrf52::gpio::GPIOPin<'static>>,
150        1,
151    >,
152    adc: &'static capsules_core::adc::AdcVirtualized<'static>,
153    rng: &'static RngDriver,
154    ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
155    alarm: &'static AlarmDriver,
156    button: &'static capsules_core::button::Button<'static, nrf52840::gpio::GPIOPin<'static>>,
157    screen: &'static ScreenDriver,
158    udp_driver: &'static capsules_extra::net::udp::UDPDriver<'static>,
159    scheduler: &'static RoundRobinSched<'static>,
160    systick: cortexm4::systick::SysTick,
161}
162
163impl SyscallDriverLookup for Platform {
164    fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
165    where
166        F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
167    {
168        match driver_num {
169            capsules_core::console::DRIVER_NUM => f(Some(self.console)),
170            capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
171            capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
172            capsules_core::led::DRIVER_NUM => f(Some(self.led)),
173            capsules_core::button::DRIVER_NUM => f(Some(self.button)),
174            capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
175            capsules_core::rng::DRIVER_NUM => f(Some(self.rng)),
176            capsules_extra::screen::DRIVER_NUM => f(Some(self.screen)),
177            capsules_extra::ble_advertising_driver::DRIVER_NUM => f(Some(self.ble_radio)),
178            capsules_extra::ieee802154::DRIVER_NUM => f(Some(self.ieee802154_radio)),
179            capsules_extra::net::udp::DRIVER_NUM => f(Some(self.udp_driver)),
180            kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
181            _ => f(None),
182        }
183    }
184}
185
186impl KernelResources<nrf52::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>>
187    for Platform
188{
189    type SyscallDriverLookup = Self;
190    type SyscallFilter = ();
191    type ProcessFault = ();
192    type Scheduler = RoundRobinSched<'static>;
193    type SchedulerTimer = cortexm4::systick::SysTick;
194    type WatchDog = ();
195    type ContextSwitchCallback = ();
196
197    fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
198        self
199    }
200    fn syscall_filter(&self) -> &Self::SyscallFilter {
201        &()
202    }
203    fn process_fault(&self) -> &Self::ProcessFault {
204        &()
205    }
206    fn scheduler(&self) -> &Self::Scheduler {
207        self.scheduler
208    }
209    fn scheduler_timer(&self) -> &Self::SchedulerTimer {
210        &self.systick
211    }
212    fn watchdog(&self) -> &Self::WatchDog {
213        &()
214    }
215    fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
216        &()
217    }
218}
219
220/// This is in a separate, inline(never) function so that its stack frame is
221/// removed when this function returns. Otherwise, the stack space used for
222/// these static_inits is wasted.
223#[inline(never)]
224pub unsafe fn start() -> (
225    &'static kernel::Kernel,
226    Platform,
227    &'static nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>,
228) {
229    nrf52840::init();
230
231    let ieee802154_ack_buf = static_init!(
232        [u8; nrf52840::ieee802154_radio::ACK_BUF_SIZE],
233        [0; nrf52840::ieee802154_radio::ACK_BUF_SIZE]
234    );
235
236    // Initialize chip peripheral drivers
237    let nrf52840_peripherals = static_init!(
238        Nrf52840DefaultPeripherals,
239        Nrf52840DefaultPeripherals::new(ieee802154_ack_buf)
240    );
241
242    // set up circular peripheral dependencies
243    nrf52840_peripherals.init();
244    let base_peripherals = &nrf52840_peripherals.nrf52;
245
246    // Save a reference to the power module for resetting the board into the
247    // bootloader.
248    NRF52_POWER = Some(&base_peripherals.pwr_clk);
249
250    let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&*addr_of!(PROCESSES)));
251
252    // Do nRF configuration and setup. This is shared code with other nRF-based
253    // platforms.
254    nrf52_components::startup::NrfStartupComponent::new(
255        false,
256        BUTTON_RST_PIN,
257        nrf52840::uicr::Regulator0Output::DEFAULT,
258        &base_peripherals.nvmc,
259    )
260    .finalize(());
261
262    let chip = static_init!(
263        nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
264        nrf52840::chip::NRF52::new(nrf52840_peripherals)
265    );
266    CHIP = Some(chip);
267
268    //--------------------------------------------------------------------------
269    // CAPABILITIES
270    //--------------------------------------------------------------------------
271
272    // Create capabilities that the board needs to call certain protected kernel
273    // functions.
274    let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
275
276    //--------------------------------------------------------------------------
277    // DEBUG GPIO
278    //--------------------------------------------------------------------------
279
280    // Configure kernel debug GPIOs as early as possible. These are used by the
281    // `debug_gpio!(0, toggle)` macro. We configure these early so that the
282    // macro is available during most of the setup code and kernel execution.
283    kernel::debug::assign_gpios(Some(&nrf52840_peripherals.gpio_port[LED_PIN]), None, None);
284
285    //--------------------------------------------------------------------------
286    // GPIO
287    //--------------------------------------------------------------------------
288
289    let gpio = components::gpio::GpioComponent::new(
290        board_kernel,
291        capsules_core::gpio::DRIVER_NUM,
292        components::gpio_component_helper!(
293            nrf52840::gpio::GPIOPin,
294            0 => &nrf52840_peripherals.gpio_port[GPIO_D0],
295            1 => &nrf52840_peripherals.gpio_port[GPIO_D1],
296            2 => &nrf52840_peripherals.gpio_port[GPIO_D2],
297            3 => &nrf52840_peripherals.gpio_port[GPIO_D3],
298        ),
299    )
300    .finalize(components::gpio_component_static!(nrf52840::gpio::GPIOPin));
301
302    //--------------------------------------------------------------------------
303    // LEDs
304    //--------------------------------------------------------------------------
305
306    let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
307        LedLow<'static, nrf52840::gpio::GPIOPin>,
308        LedLow::new(&nrf52840_peripherals.gpio_port[LED_PIN]),
309    ));
310
311    //--------------------------------------------------------------------------
312    // BUTTONS
313    //--------------------------------------------------------------------------
314
315    let button = components::button::ButtonComponent::new(
316        board_kernel,
317        capsules_core::button::DRIVER_NUM,
318        components::button_component_helper!(
319            nrf52840::gpio::GPIOPin,
320            (
321                &nrf52840_peripherals.gpio_port[BUTTON_PIN],
322                kernel::hil::gpio::ActivationMode::ActiveLow,
323                kernel::hil::gpio::FloatingState::PullUp
324            )
325        ),
326    )
327    .finalize(components::button_component_static!(
328        nrf52840::gpio::GPIOPin
329    ));
330
331    //--------------------------------------------------------------------------
332    // ALARM & TIMER
333    //--------------------------------------------------------------------------
334
335    let rtc = &base_peripherals.rtc;
336    let _ = rtc.start();
337
338    let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
339        .finalize(components::alarm_mux_component_static!(nrf52::rtc::Rtc));
340    let alarm = components::alarm::AlarmDriverComponent::new(
341        board_kernel,
342        capsules_core::alarm::DRIVER_NUM,
343        mux_alarm,
344    )
345    .finalize(components::alarm_component_static!(nrf52::rtc::Rtc));
346
347    //--------------------------------------------------------------------------
348    // UART & CONSOLE & DEBUG
349    //--------------------------------------------------------------------------
350
351    // Setup the CDC-ACM over USB driver that we will use for UART.
352    // We use the Arduino Vendor ID and Product ID since the device is the same.
353
354    // Create the strings we include in the USB descriptor. We use the hardcoded
355    // DEVICEADDR register on the nRF52 to set the serial number.
356    let serial_number_buf = static_init!([u8; 17], [0; 17]);
357    let serial_number_string: &'static str =
358        (*addr_of!(nrf52::ficr::FICR_INSTANCE)).address_str(serial_number_buf);
359    let strings = static_init!(
360        [&str; 3],
361        [
362            "MakePython",         // Manufacturer
363            "NRF52840 - TockOS",  // Product
364            serial_number_string, // Serial number
365        ]
366    );
367
368    let cdc = components::cdc::CdcAcmComponent::new(
369        &nrf52840_peripherals.usbd,
370        capsules_extra::usb::cdc::MAX_CTRL_PACKET_SIZE_NRF52840,
371        0x2341,
372        0x005a,
373        strings,
374        mux_alarm,
375        Some(&baud_rate_reset_bootloader_enter),
376    )
377    .finalize(components::cdc_acm_component_static!(
378        nrf52::usbd::Usbd,
379        nrf52::rtc::Rtc
380    ));
381    CDC_REF_FOR_PANIC = Some(cdc); //for use by panic handler
382
383    // Process Printer for displaying process information.
384    let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
385        .finalize(components::process_printer_text_component_static!());
386    PROCESS_PRINTER = Some(process_printer);
387
388    // Create a shared UART channel for the console and for kernel debug.
389    let uart_mux = components::console::UartMuxComponent::new(cdc, 115200)
390        .finalize(components::uart_mux_component_static!());
391
392    let pconsole = components::process_console::ProcessConsoleComponent::new(
393        board_kernel,
394        uart_mux,
395        mux_alarm,
396        process_printer,
397        Some(cortexm4::support::reset),
398    )
399    .finalize(components::process_console_component_static!(
400        nrf52::rtc::Rtc<'static>
401    ));
402
403    // Setup the console.
404    let console = components::console::ConsoleComponent::new(
405        board_kernel,
406        capsules_core::console::DRIVER_NUM,
407        uart_mux,
408    )
409    .finalize(components::console_component_static!());
410    // Create the debugger object that handles calls to `debug!()`.
411    components::debug_writer::DebugWriterComponent::new(
412        uart_mux,
413        create_capability!(capabilities::SetDebugWriterCapability),
414    )
415    .finalize(components::debug_writer_component_static!());
416
417    //--------------------------------------------------------------------------
418    // RANDOM NUMBERS
419    //--------------------------------------------------------------------------
420
421    let rng = components::rng::RngComponent::new(
422        board_kernel,
423        capsules_core::rng::DRIVER_NUM,
424        &base_peripherals.trng,
425    )
426    .finalize(components::rng_component_static!(nrf52840::trng::Trng));
427
428    //--------------------------------------------------------------------------
429    // ADC
430    //--------------------------------------------------------------------------
431    base_peripherals.adc.calibrate();
432
433    let adc_mux = components::adc::AdcMuxComponent::new(&base_peripherals.adc)
434        .finalize(components::adc_mux_component_static!(nrf52840::adc::Adc));
435
436    let adc_syscall =
437        components::adc::AdcVirtualComponent::new(board_kernel, capsules_core::adc::DRIVER_NUM)
438            .finalize(components::adc_syscall_component_helper!(
439                // A0
440                components::adc::AdcComponent::new(
441                    adc_mux,
442                    nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput2)
443                )
444                .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
445                // A1
446                components::adc::AdcComponent::new(
447                    adc_mux,
448                    nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput3)
449                )
450                .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
451                // A2
452                components::adc::AdcComponent::new(
453                    adc_mux,
454                    nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput6)
455                )
456                .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
457                // A3
458                components::adc::AdcComponent::new(
459                    adc_mux,
460                    nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput5)
461                )
462                .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
463                // A4
464                components::adc::AdcComponent::new(
465                    adc_mux,
466                    nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput7)
467                )
468                .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
469                // A5
470                components::adc::AdcComponent::new(
471                    adc_mux,
472                    nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput0)
473                )
474                .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
475                // A6
476                components::adc::AdcComponent::new(
477                    adc_mux,
478                    nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput4)
479                )
480                .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
481                // A7
482                components::adc::AdcComponent::new(
483                    adc_mux,
484                    nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput1)
485                )
486                .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
487            ));
488
489    //--------------------------------------------------------------------------
490    // SCREEN
491    //--------------------------------------------------------------------------
492
493    let i2c_bus = components::i2c::I2CMuxComponent::new(&base_peripherals.twi1, None)
494        .finalize(components::i2c_mux_component_static!(nrf52840::i2c::TWI));
495    base_peripherals.twi1.configure(
496        nrf52840::pinmux::Pinmux::new(I2C_SCL_PIN as u32),
497        nrf52840::pinmux::Pinmux::new(I2C_SDA_PIN as u32),
498    );
499
500    // I2C address is b011110X, and on this board D/C̅ is GND.
501    let ssd1306_i2c = components::i2c::I2CComponent::new(i2c_bus, 0x3c)
502        .finalize(components::i2c_component_static!(nrf52840::i2c::TWI));
503
504    // Create the ssd1306 object for the actual screen driver.
505    let ssd1306 = components::ssd1306::Ssd1306Component::new(ssd1306_i2c, true)
506        .finalize(components::ssd1306_component_static!(nrf52840::i2c::TWI));
507
508    // Create a Driver for userspace access to the screen.
509    // let screen = components::screen::ScreenComponent::new(
510    //     board_kernel,
511    //     capsules_extra::screen::DRIVER_NUM,
512    //     ssd1306,
513    //     Some(ssd1306),
514    // )
515    // .finalize(components::screen_component_static!(1032));
516
517    let apps_regions = static_init!(
518        [capsules_extra::screen_shared::AppScreenRegion; 3],
519        [
520            capsules_extra::screen_shared::AppScreenRegion::new(
521                kernel::process::ShortId::Fixed(core::num::NonZeroU32::new(crc("circle")).unwrap()),
522                0,     // x
523                0,     // y
524                8 * 8, // width
525                8 * 8  // height
526            ),
527            capsules_extra::screen_shared::AppScreenRegion::new(
528                kernel::process::ShortId::Fixed(core::num::NonZeroU32::new(crc("count")).unwrap()),
529                8 * 8, // x
530                0,     // y
531                8 * 8, // width
532                4 * 8  // height
533            ),
534            capsules_extra::screen_shared::AppScreenRegion::new(
535                kernel::process::ShortId::Fixed(
536                    core::num::NonZeroU32::new(crc("tock-scroll")).unwrap()
537                ),
538                8 * 8, // x
539                4 * 8, // y
540                8 * 8, // width
541                4 * 8  // height
542            )
543        ]
544    );
545
546    let screen = components::screen::ScreenSharedComponent::new(
547        board_kernel,
548        capsules_extra::screen::DRIVER_NUM,
549        ssd1306,
550        apps_regions,
551    )
552    .finalize(components::screen_shared_component_static!(1032, Screen));
553
554    //--------------------------------------------------------------------------
555    // WIRELESS
556    //--------------------------------------------------------------------------
557
558    let ble_radio = components::ble::BLEComponent::new(
559        board_kernel,
560        capsules_extra::ble_advertising_driver::DRIVER_NUM,
561        &base_peripherals.ble_radio,
562        mux_alarm,
563    )
564    .finalize(components::ble_component_static!(
565        nrf52840::rtc::Rtc,
566        nrf52840::ble_radio::Radio
567    ));
568
569    use capsules_extra::net::ieee802154::MacAddress;
570
571    let aes_mux = components::ieee802154::MuxAes128ccmComponent::new(&base_peripherals.ecb)
572        .finalize(components::mux_aes128ccm_component_static!(
573            nrf52840::aes::AesECB
574        ));
575
576    let device_id = (*addr_of!(nrf52840::ficr::FICR_INSTANCE)).id();
577    let device_id_bottom_16 = u16::from_le_bytes([device_id[0], device_id[1]]);
578    let (ieee802154_radio, mux_mac) = components::ieee802154::Ieee802154Component::new(
579        board_kernel,
580        capsules_extra::ieee802154::DRIVER_NUM,
581        &nrf52840_peripherals.ieee802154_radio,
582        aes_mux,
583        PAN_ID,
584        device_id_bottom_16,
585        device_id,
586    )
587    .finalize(components::ieee802154_component_static!(
588        nrf52840::ieee802154_radio::Radio,
589        nrf52840::aes::AesECB<'static>
590    ));
591    use capsules_extra::net::ipv6::ip_utils::IPAddr;
592
593    let local_ip_ifaces = static_init!(
594        [IPAddr; 3],
595        [
596            IPAddr([
597                0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
598                0x0e, 0x0f,
599            ]),
600            IPAddr([
601                0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
602                0x1e, 0x1f,
603            ]),
604            IPAddr::generate_from_mac(capsules_extra::net::ieee802154::MacAddress::Short(
605                device_id_bottom_16
606            )),
607        ]
608    );
609
610    let (udp_send_mux, udp_recv_mux, udp_port_table) = components::udp_mux::UDPMuxComponent::new(
611        mux_mac,
612        DEFAULT_CTX_PREFIX_LEN,
613        DEFAULT_CTX_PREFIX,
614        DST_MAC_ADDR,
615        MacAddress::Short(device_id_bottom_16),
616        local_ip_ifaces,
617        mux_alarm,
618    )
619    .finalize(components::udp_mux_component_static!(
620        nrf52840::rtc::Rtc,
621        Ieee802154MacDevice
622    ));
623
624    // UDP driver initialization happens here
625    let udp_driver = components::udp_driver::UDPDriverComponent::new(
626        board_kernel,
627        capsules_extra::net::udp::DRIVER_NUM,
628        udp_send_mux,
629        udp_recv_mux,
630        udp_port_table,
631        local_ip_ifaces,
632    )
633    .finalize(components::udp_driver_component_static!(nrf52840::rtc::Rtc));
634
635    //--------------------------------------------------------------------------
636    // APP ID CHECKING
637    //--------------------------------------------------------------------------
638
639    // Create the software-based SHA engine.
640    let sha = components::sha::ShaSoftware256Component::new()
641        .finalize(components::sha_software_256_component_static!());
642
643    // Create the credential checker.
644    let checking_policy = components::appid::checker_sha::AppCheckerSha256Component::new(sha)
645        .finalize(components::app_checker_sha256_component_static!());
646
647    // Create the AppID assigner.
648    let assigner = components::appid::assigner_name::AppIdAssignerNamesComponent::new()
649        .finalize(components::appid_assigner_names_component_static!());
650
651    // Create the process checking machine.
652    let checker = components::appid::checker::ProcessCheckerMachineComponent::new(checking_policy)
653        .finalize(components::process_checker_machine_component_static!());
654
655    //--------------------------------------------------------------------------
656    // STORAGE PERMISSIONS
657    //--------------------------------------------------------------------------
658
659    let storage_permissions_policy =
660        components::storage_permissions::individual::StoragePermissionsIndividualComponent::new()
661            .finalize(
662                components::storage_permissions_individual_component_static!(
663                    nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
664                    kernel::process::ProcessStandardDebugFull,
665                ),
666            );
667
668    //--------------------------------------------------------------------------
669    // PROCESS LOADING
670    //--------------------------------------------------------------------------
671
672    // These symbols are defined in the standard Tock linker script.
673    extern "C" {
674        /// Beginning of the ROM region containing app images.
675        static _sapps: u8;
676        /// End of the ROM region containing app images.
677        static _eapps: u8;
678        /// Beginning of the RAM region for app memory.
679        static mut _sappmem: u8;
680        /// End of the RAM region for app memory.
681        static _eappmem: u8;
682    }
683
684    let app_flash = core::slice::from_raw_parts(
685        core::ptr::addr_of!(_sapps),
686        core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
687    );
688    let app_memory = core::slice::from_raw_parts_mut(
689        core::ptr::addr_of_mut!(_sappmem),
690        core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
691    );
692
693    // Create and start the asynchronous process loader.
694    let _loader = components::loader::sequential::ProcessLoaderSequentialComponent::new(
695        checker,
696        &mut *addr_of_mut!(PROCESSES),
697        board_kernel,
698        chip,
699        &FAULT_RESPONSE,
700        assigner,
701        storage_permissions_policy,
702        app_flash,
703        app_memory,
704    )
705    .finalize(components::process_loader_sequential_component_static!(
706        nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
707        kernel::process::ProcessStandardDebugFull,
708        NUM_PROCS
709    ));
710
711    //--------------------------------------------------------------------------
712    // FINAL SETUP AND BOARD BOOT
713    //--------------------------------------------------------------------------
714
715    // Start all of the clocks. Low power operation will require a better
716    // approach than this.
717    nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
718
719    let scheduler = components::sched::round_robin::RoundRobinComponent::new(&*addr_of!(PROCESSES))
720        .finalize(components::round_robin_component_static!(NUM_PROCS));
721
722    let platform = Platform {
723        ble_radio,
724        ieee802154_radio,
725        console,
726        pconsole,
727        adc: adc_syscall,
728        led,
729        button,
730        gpio,
731        rng,
732        screen,
733        alarm,
734        udp_driver,
735        ipc: kernel::ipc::IPC::new(
736            board_kernel,
737            kernel::ipc::DRIVER_NUM,
738            &memory_allocation_capability,
739        ),
740        scheduler,
741        systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
742    };
743
744    // Configure the USB stack to enable a serial port over CDC-ACM.
745    cdc.enable();
746    cdc.attach();
747
748    //--------------------------------------------------------------------------
749    // TESTS
750    //--------------------------------------------------------------------------
751    // test::linear_log_test::run(
752    //     mux_alarm,
753    //     &nrf52840_peripherals.nrf52.nvmc,
754    // );
755    // test::log_test::run(
756    //     mux_alarm,
757    //     &nrf52840_peripherals.nrf52.nvmc,
758    // );
759
760    debug!("Initialization complete. Entering main loop.");
761    let _ = platform.pconsole.start();
762
763    ssd1306.init_screen();
764
765    //--------------------------------------------------------------------------
766    // PROCESSES AND MAIN LOOP
767    //--------------------------------------------------------------------------
768
769    (board_kernel, platform, chip)
770}
771
772/// Main function called after RAM initialized.
773#[no_mangle]
774pub unsafe fn main() {
775    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
776
777    let (board_kernel, platform, chip) = start();
778    board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
779}