1#![no_std]
11#![no_main]
12#![deny(missing_docs)]
13
14use capsules_core::i2c_master_slave_driver::I2CMasterSlaveDriver;
15use capsules_core::virtualizers::virtual_aes_ccm::MuxAES128CCM;
16use capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm;
17use kernel::component::Component;
18use kernel::debug::PanicResources;
19use kernel::deferred_call::DeferredCallClient;
20use kernel::hil::gpio::Configure;
21use kernel::hil::gpio::FloatingState;
22use kernel::hil::i2c::{I2CMaster, I2CSlave};
23use kernel::hil::led::LedLow;
24use kernel::hil::symmetric_encryption::AES128;
25use kernel::hil::time::Counter;
26use kernel::platform::{KernelResources, SyscallDriverLookup};
27use kernel::scheduler::round_robin::RoundRobinSched;
28use kernel::utilities::single_thread_value::SingleThreadValue;
29#[allow(unused_imports)]
30use kernel::{capabilities, create_capability, debug, debug_gpio, debug_verbose, static_init};
31use nrf52840::gpio::Pin;
32use nrf52840::interrupt_service::Nrf52840DefaultPeripherals;
33#[allow(unused_imports)]
34use nrf52_components::{self, UartChannel, UartPins};
35
36const LED_USR_PIN: Pin = Pin::P1_12;
38const LED2_R_PIN: Pin = Pin::P0_13;
39const LED2_G_PIN: Pin = Pin::P0_14;
40const LED2_B_PIN: Pin = Pin::P0_15;
41
42const BUTTON_PIN: Pin = Pin::P0_11;
44const BUTTON_RST_PIN: Pin = Pin::P0_18;
45
46const _UART_RTS: Option<Pin> = Some(Pin::P0_30);
48const _UART_CTS: Option<Pin> = Some(Pin::P0_31);
49const UART_TXD: Pin = Pin::P0_06;
50const UART_RXD: Pin = Pin::P0_08;
51
52const _SPI_MOSI: Pin = Pin::P1_13;
54const _SPI_MISO: Pin = Pin::P1_14;
55const _SPI_CLK: Pin = Pin::P1_15;
56
57const I2C_SDA_PIN: Pin = Pin::P0_26;
59const I2C_SCL_PIN: Pin = Pin::P0_27;
60
61const SRC_MAC: u16 = 0xf00f;
64const PAN_ID: u16 = 0xABCD;
65const DEFAULT_EXT_SRC_MAC: [u8; 8] = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77];
66
67pub mod io;
69
70const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
73 capsules_system::process_policies::PanicFaultPolicy {};
74
75const NUM_PROCS: usize = 8;
77
78type ChipHw = nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>;
79type ProcessPrinterInUse = capsules_system::process_printer::ProcessPrinterText;
80
81static PANIC_RESOURCES: SingleThreadValue<PanicResources<ChipHw, ProcessPrinterInUse>> =
83 SingleThreadValue::new(PanicResources::new());
84
85static mut NRF52_POWER: Option<&'static nrf52840::power::Power> = None;
86
87kernel::stack_size! {0x1000}
88
89type TemperatureDriver =
90 components::temperature::TemperatureComponentType<nrf52840::temperature::Temp<'static>>;
91type RngDriver = components::rng::RngComponentType<nrf52840::trng::Trng<'static>>;
92
93type Ieee802154Driver = components::ieee802154::Ieee802154ComponentType<
94 nrf52840::ieee802154_radio::Radio<'static>,
95 nrf52840::aes::AesECB<'static>,
96>;
97
98pub struct Platform {
100 ble_radio: &'static capsules_extra::ble_advertising_driver::BLE<
101 'static,
102 nrf52840::ble_radio::Radio<'static>,
103 VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>,
104 >,
105 ieee802154_radio: &'static Ieee802154Driver,
106 button: &'static capsules_core::button::Button<'static, nrf52840::gpio::GPIOPin<'static>>,
107 console: &'static capsules_core::console::Console<'static>,
108 gpio: &'static capsules_core::gpio::GPIO<'static, nrf52840::gpio::GPIOPin<'static>>,
109 led: &'static capsules_core::led::LedDriver<
110 'static,
111 LedLow<'static, nrf52840::gpio::GPIOPin<'static>>,
112 4,
113 >,
114 adc: &'static capsules_core::adc::AdcVirtualized<'static>,
115 rng: &'static RngDriver,
116 temp: &'static TemperatureDriver,
117 ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
118 i2c_master_slave: &'static capsules_core::i2c_master_slave_driver::I2CMasterSlaveDriver<
119 'static,
120 nrf52840::i2c::TWI<'static>,
121 >,
122 alarm: &'static capsules_core::alarm::AlarmDriver<
123 'static,
124 capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<
125 'static,
126 nrf52840::rtc::Rtc<'static>,
127 >,
128 >,
129 scheduler: &'static RoundRobinSched<'static>,
130 systick: cortexm4::systick::SysTick,
131}
132
133impl SyscallDriverLookup for Platform {
134 fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
135 where
136 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
137 {
138 match driver_num {
139 capsules_core::console::DRIVER_NUM => f(Some(self.console)),
140 capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
141 capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
142 capsules_core::led::DRIVER_NUM => f(Some(self.led)),
143 capsules_core::button::DRIVER_NUM => f(Some(self.button)),
144 capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
145 capsules_core::rng::DRIVER_NUM => f(Some(self.rng)),
146 capsules_extra::ble_advertising_driver::DRIVER_NUM => f(Some(self.ble_radio)),
147 capsules_extra::ieee802154::DRIVER_NUM => f(Some(self.ieee802154_radio)),
148 capsules_extra::temperature::DRIVER_NUM => f(Some(self.temp)),
149 kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
150 capsules_core::i2c_master_slave_driver::DRIVER_NUM => f(Some(self.i2c_master_slave)),
151 _ => f(None),
152 }
153 }
154}
155
156impl KernelResources<nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>>
157 for Platform
158{
159 type SyscallDriverLookup = Self;
160 type SyscallFilter = ();
161 type ProcessFault = ();
162 type Scheduler = RoundRobinSched<'static>;
163 type SchedulerTimer = cortexm4::systick::SysTick;
164 type WatchDog = ();
165 type ContextSwitchCallback = ();
166
167 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
168 self
169 }
170 fn syscall_filter(&self) -> &Self::SyscallFilter {
171 &()
172 }
173 fn process_fault(&self) -> &Self::ProcessFault {
174 &()
175 }
176 fn scheduler(&self) -> &Self::Scheduler {
177 self.scheduler
178 }
179 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
180 &self.systick
181 }
182 fn watchdog(&self) -> &Self::WatchDog {
183 &()
184 }
185 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
186 &()
187 }
188}
189
190#[inline(never)]
194unsafe fn create_peripherals() -> &'static mut Nrf52840DefaultPeripherals<'static> {
195 let ieee802154_ack_buf = static_init!(
196 [u8; nrf52840::ieee802154_radio::ACK_BUF_SIZE],
197 [0; nrf52840::ieee802154_radio::ACK_BUF_SIZE]
198 );
199 let nrf52840_peripherals = static_init!(
201 Nrf52840DefaultPeripherals,
202 Nrf52840DefaultPeripherals::new(ieee802154_ack_buf)
203 );
204
205 nrf52840_peripherals
206}
207
208#[inline(never)]
212pub unsafe fn start_particle_boron() -> (
213 &'static kernel::Kernel,
214 Platform,
215 &'static nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>,
216) {
217 nrf52840::init();
218
219 kernel::deferred_call::initialize_deferred_call_state::<
221 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
222 >();
223
224 PANIC_RESOURCES.bind_to_thread::<<ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider>();
226
227 let nrf52840_peripherals = create_peripherals();
228
229 nrf52840_peripherals.init();
231 let base_peripherals = &nrf52840_peripherals.nrf52;
232
233 NRF52_POWER = Some(&base_peripherals.pwr_clk);
236
237 let processes = components::process_array::ProcessArrayComponent::new()
239 .finalize(components::process_array_component_static!(NUM_PROCS));
240 PANIC_RESOURCES.get().map(|resources| {
241 resources.processes.put(processes.as_slice());
242 });
243
244 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
246
247 let process_management_capability =
254 create_capability!(capabilities::ProcessManagementCapability);
255 let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
256
257 let gpio_port = &nrf52840_peripherals.gpio_port;
262 let debug_gpios = static_init!(
266 [&'static dyn kernel::hil::gpio::Pin; 1],
267 [&gpio_port[LED2_R_PIN]]
268 );
269 kernel::debug::initialize_debug_gpio::<
270 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
271 >();
272 kernel::debug::assign_gpios(debug_gpios);
273
274 let uart_channel = UartChannel::Pins(UartPins::new(None, UART_TXD, None, UART_RXD));
275
276 let gpio = components::gpio::GpioComponent::new(
281 board_kernel,
282 capsules_core::gpio::DRIVER_NUM,
283 components::gpio_component_helper!(
284 nrf52840::gpio::GPIOPin,
285 6 => &nrf52840_peripherals.gpio_port[Pin::P1_15],
300 7 => &nrf52840_peripherals.gpio_port[Pin::P1_13],
302 8 => &nrf52840_peripherals.gpio_port[Pin::P1_14],
304 9 => &nrf52840_peripherals.gpio_port[Pin::P0_08],
306 10 => &nrf52840_peripherals.gpio_port[Pin::P0_06],
308 11 => &nrf52840_peripherals.gpio_port[Pin::P1_03],
311 12 => &nrf52840_peripherals.gpio_port[Pin::P1_12],
313 13 => &nrf52840_peripherals.gpio_port[Pin::P1_11],
315 14 => &nrf52840_peripherals.gpio_port[Pin::P1_10],
317 15 => &nrf52840_peripherals.gpio_port[Pin::P1_08],
319 16 => &nrf52840_peripherals.gpio_port[Pin::P1_02],
321 17 => &nrf52840_peripherals.gpio_port[Pin::P0_01],
323 18 => &nrf52840_peripherals.gpio_port[Pin::P0_27],
325 19 => &nrf52840_peripherals.gpio_port[Pin::P0_26],
327 ),
328 )
329 .finalize(components::gpio_component_static!(nrf52840::gpio::GPIOPin));
330
331 let button = components::button::ButtonComponent::new(
336 board_kernel,
337 capsules_core::button::DRIVER_NUM,
338 components::button_component_helper!(
339 nrf52840::gpio::GPIOPin,
340 (
341 &nrf52840_peripherals.gpio_port[BUTTON_PIN],
342 kernel::hil::gpio::ActivationMode::ActiveLow,
343 kernel::hil::gpio::FloatingState::PullUp
344 )
345 ),
346 )
347 .finalize(components::button_component_static!(
348 nrf52840::gpio::GPIOPin
349 ));
350
351 let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
356 LedLow<'static, nrf52840::gpio::GPIOPin>,
357 LedLow::new(&nrf52840_peripherals.gpio_port[LED_USR_PIN]),
358 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_R_PIN]),
359 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_G_PIN]),
360 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_B_PIN]),
361 ));
362
363 nrf52_components::startup::NrfStartupComponent::new(
364 false,
365 BUTTON_RST_PIN,
366 nrf52840::uicr::Regulator0Output::V3_0,
367 &base_peripherals.nvmc,
368 )
369 .finalize(());
370
371 let rtc = &base_peripherals.rtc;
376 let _ = rtc.start();
377 let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
378 .finalize(components::alarm_mux_component_static!(nrf52840::rtc::Rtc));
379 let alarm = components::alarm::AlarmDriverComponent::new(
380 board_kernel,
381 capsules_core::alarm::DRIVER_NUM,
382 mux_alarm,
383 )
384 .finalize(components::alarm_component_static!(nrf52840::rtc::Rtc));
385
386 let uart_channel = nrf52_components::UartChannelComponent::new(
391 uart_channel,
392 mux_alarm,
393 &base_peripherals.uarte0,
394 )
395 .finalize(nrf52_components::uart_channel_component_static!(
396 nrf52840::rtc::Rtc
397 ));
398
399 let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
401 .finalize(components::process_printer_text_component_static!());
402 PANIC_RESOURCES.get().map(|resources| {
403 resources.printer.put(process_printer);
404 });
405
406 let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
408 .finalize(components::uart_mux_component_static!(132));
409
410 let console = components::console::ConsoleComponent::new(
412 board_kernel,
413 capsules_core::console::DRIVER_NUM,
414 uart_mux,
415 )
416 .finalize(components::console_component_static!(132, 132));
417 components::debug_writer::DebugWriterComponent::new::<
419 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
420 >(
421 uart_mux,
422 create_capability!(capabilities::SetDebugWriterCapability),
423 )
424 .finalize(components::debug_writer_component_static!());
425
426 let ble_radio = components::ble::BLEComponent::new(
431 board_kernel,
432 capsules_extra::ble_advertising_driver::DRIVER_NUM,
433 &base_peripherals.ble_radio,
434 mux_alarm,
435 )
436 .finalize(components::ble_component_static!(
437 nrf52840::rtc::Rtc,
438 nrf52840::ble_radio::Radio
439 ));
440
441 let aes_mux = static_init!(
442 MuxAES128CCM<'static, nrf52840::aes::AesECB>,
443 MuxAES128CCM::new(&base_peripherals.ecb,)
444 );
445 base_peripherals.ecb.set_client(aes_mux);
446 aes_mux.register();
447
448 let (ieee802154_radio, _mux_mac) = components::ieee802154::Ieee802154Component::new(
449 board_kernel,
450 capsules_extra::ieee802154::DRIVER_NUM,
451 &nrf52840_peripherals.ieee802154_radio,
452 aes_mux,
453 PAN_ID,
454 SRC_MAC,
455 DEFAULT_EXT_SRC_MAC,
456 )
457 .finalize(components::ieee802154_component_static!(
458 nrf52840::ieee802154_radio::Radio,
459 nrf52840::aes::AesECB<'static>
460 ));
461
462 let temp = components::temperature::TemperatureComponent::new(
467 board_kernel,
468 capsules_extra::temperature::DRIVER_NUM,
469 &base_peripherals.temp,
470 )
471 .finalize(components::temperature_component_static!(
472 nrf52840::temperature::Temp
473 ));
474
475 let rng = components::rng::RngComponent::new(
480 board_kernel,
481 capsules_core::rng::DRIVER_NUM,
482 &base_peripherals.trng,
483 )
484 .finalize(components::rng_component_static!(nrf52840::trng::Trng));
485
486 base_peripherals.adc.calibrate();
491
492 let adc_mux = components::adc::AdcMuxComponent::new(&base_peripherals.adc)
493 .finalize(components::adc_mux_component_static!(nrf52840::adc::Adc));
494
495 let adc_syscall =
496 components::adc::AdcVirtualComponent::new(board_kernel, capsules_core::adc::DRIVER_NUM)
497 .finalize(components::adc_syscall_component_helper!(
498 components::adc::AdcComponent::new(
500 adc_mux,
501 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput1)
502 )
503 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
504 components::adc::AdcComponent::new(
506 adc_mux,
507 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput2)
508 )
509 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
510 components::adc::AdcComponent::new(
512 adc_mux,
513 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput4)
514 )
515 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
516 components::adc::AdcComponent::new(
518 adc_mux,
519 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput5)
520 )
521 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
522 components::adc::AdcComponent::new(
524 adc_mux,
525 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput6)
526 )
527 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
528 components::adc::AdcComponent::new(
530 adc_mux,
531 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput7)
532 )
533 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
534 ));
535
536 let i2c_master_buffer = static_init!([u8; 128], [0; 128]);
541 let i2c_slave_buffer1 = static_init!([u8; 128], [0; 128]);
542 let i2c_slave_buffer2 = static_init!([u8; 128], [0; 128]);
543
544 let i2c_master_slave = static_init!(
545 I2CMasterSlaveDriver<nrf52840::i2c::TWI<'static>>,
546 I2CMasterSlaveDriver::new(
547 &base_peripherals.twi1,
548 i2c_master_buffer,
549 i2c_slave_buffer1,
550 i2c_slave_buffer2,
551 board_kernel.create_grant(
552 capsules_core::i2c_master_slave_driver::DRIVER_NUM,
553 &memory_allocation_capability
554 ),
555 )
556 );
557 base_peripherals.twi1.configure(
558 nrf52840::pinmux::Pinmux::new(I2C_SCL_PIN as u32),
559 nrf52840::pinmux::Pinmux::new(I2C_SDA_PIN as u32),
560 );
561 base_peripherals.twi1.set_master_client(i2c_master_slave);
562 base_peripherals.twi1.set_slave_client(i2c_master_slave);
563 base_peripherals.twi1.set_speed(nrf52840::i2c::Speed::K400);
566
567 nrf52840_peripherals.gpio_port[I2C_SDA_PIN].set_i2c_pin_cfg();
569 nrf52840_peripherals.gpio_port[I2C_SCL_PIN].set_i2c_pin_cfg();
570 nrf52840_peripherals.gpio_port[I2C_SDA_PIN].set_floating_state(FloatingState::PullUp);
572 nrf52840_peripherals.gpio_port[I2C_SCL_PIN].set_floating_state(FloatingState::PullUp);
573
574 nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
579
580 let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
581 .finalize(components::round_robin_component_static!(NUM_PROCS));
582
583 let platform = Platform {
584 button,
585 ble_radio,
586 ieee802154_radio,
587 console,
588 led,
589 gpio,
590 adc: adc_syscall,
591 rng,
592 temp,
593 alarm,
594 ipc: kernel::ipc::IPC::new(
595 board_kernel,
596 kernel::ipc::DRIVER_NUM,
597 &memory_allocation_capability,
598 ),
599 i2c_master_slave,
600 scheduler,
601 systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
602 };
603
604 let chip = static_init!(
605 nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
606 nrf52840::chip::NRF52::new(nrf52840_peripherals)
607 );
608 PANIC_RESOURCES.get().map(|resources| {
609 resources.chip.put(chip);
610 });
611
612 debug!("Particle Boron: Initialization complete. Entering main loop\r");
613
614 extern "C" {
620 static _sapps: u8;
622 static _eapps: u8;
624 static mut _sappmem: u8;
626 static _eappmem: u8;
628 }
629
630 kernel::process::load_processes(
631 board_kernel,
632 chip,
633 core::slice::from_raw_parts(
634 core::ptr::addr_of!(_sapps),
635 core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
636 ),
637 core::slice::from_raw_parts_mut(
638 core::ptr::addr_of_mut!(_sappmem),
639 core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
640 ),
641 &FAULT_RESPONSE,
642 &process_management_capability,
643 )
644 .unwrap_or_else(|err| {
645 debug!("Error loading processes!");
646 debug!("{:?}", err);
647 });
648
649 (board_kernel, platform, chip)
650}
651
652#[no_mangle]
654pub unsafe fn main() {
655 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
656
657 let (board_kernel, platform, chip) = start_particle_boron();
658 board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
659}