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