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::deferred_call::DeferredCallClient;
19use kernel::hil::gpio::Configure;
20use kernel::hil::gpio::FloatingState;
21use kernel::hil::i2c::{I2CMaster, I2CSlave};
22use kernel::hil::led::LedLow;
23use kernel::hil::symmetric_encryption::AES128;
24use kernel::hil::time::Counter;
25use kernel::platform::{KernelResources, SyscallDriverLookup};
26use kernel::process::ProcessArray;
27use kernel::scheduler::round_robin::RoundRobinSched;
28#[allow(unused_imports)]
29use kernel::{capabilities, create_capability, debug, debug_gpio, debug_verbose, static_init};
30use nrf52840::gpio::Pin;
31use nrf52840::interrupt_service::Nrf52840DefaultPeripherals;
32#[allow(unused_imports)]
33use nrf52_components::{self, UartChannel, UartPins};
34
35const LED_USR_PIN: Pin = Pin::P1_12;
37const LED2_R_PIN: Pin = Pin::P0_13;
38const LED2_G_PIN: Pin = Pin::P0_14;
39const LED2_B_PIN: Pin = Pin::P0_15;
40
41const BUTTON_PIN: Pin = Pin::P0_11;
43const BUTTON_RST_PIN: Pin = Pin::P0_18;
44
45const _UART_RTS: Option<Pin> = Some(Pin::P0_30);
47const _UART_CTS: Option<Pin> = Some(Pin::P0_31);
48const UART_TXD: Pin = Pin::P0_06;
49const UART_RXD: Pin = Pin::P0_08;
50
51const _SPI_MOSI: Pin = Pin::P1_13;
53const _SPI_MISO: Pin = Pin::P1_14;
54const _SPI_CLK: Pin = Pin::P1_15;
55
56const I2C_SDA_PIN: Pin = Pin::P0_26;
58const I2C_SCL_PIN: Pin = Pin::P0_27;
59
60const SRC_MAC: u16 = 0xf00f;
63const PAN_ID: u16 = 0xABCD;
64const DEFAULT_EXT_SRC_MAC: [u8; 8] = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77];
65
66pub mod io;
68
69const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
72 capsules_system::process_policies::PanicFaultPolicy {};
73
74const NUM_PROCS: usize = 8;
76
77static mut PROCESSES: Option<&'static ProcessArray<NUM_PROCS>> = None;
79
80type ChipHw = nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>;
81
82static mut CHIP: Option<&'static nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>> = None;
84static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
86 None;
87static mut NRF52_POWER: Option<&'static nrf52840::power::Power> = None;
88
89kernel::stack_size! {0x1000}
90
91type TemperatureDriver =
92 components::temperature::TemperatureComponentType<nrf52840::temperature::Temp<'static>>;
93type RngDriver = components::rng::RngComponentType<nrf52840::trng::Trng<'static>>;
94
95type Ieee802154Driver = components::ieee802154::Ieee802154ComponentType<
96 nrf52840::ieee802154_radio::Radio<'static>,
97 nrf52840::aes::AesECB<'static>,
98>;
99
100pub struct Platform {
102 ble_radio: &'static capsules_extra::ble_advertising_driver::BLE<
103 'static,
104 nrf52840::ble_radio::Radio<'static>,
105 VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>,
106 >,
107 ieee802154_radio: &'static Ieee802154Driver,
108 button: &'static capsules_core::button::Button<'static, nrf52840::gpio::GPIOPin<'static>>,
109 console: &'static capsules_core::console::Console<'static>,
110 gpio: &'static capsules_core::gpio::GPIO<'static, nrf52840::gpio::GPIOPin<'static>>,
111 led: &'static capsules_core::led::LedDriver<
112 'static,
113 LedLow<'static, nrf52840::gpio::GPIOPin<'static>>,
114 4,
115 >,
116 adc: &'static capsules_core::adc::AdcVirtualized<'static>,
117 rng: &'static RngDriver,
118 temp: &'static TemperatureDriver,
119 ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
120 i2c_master_slave: &'static capsules_core::i2c_master_slave_driver::I2CMasterSlaveDriver<
121 'static,
122 nrf52840::i2c::TWI<'static>,
123 >,
124 alarm: &'static capsules_core::alarm::AlarmDriver<
125 'static,
126 capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<
127 'static,
128 nrf52840::rtc::Rtc<'static>,
129 >,
130 >,
131 scheduler: &'static RoundRobinSched<'static>,
132 systick: cortexm4::systick::SysTick,
133}
134
135impl SyscallDriverLookup for Platform {
136 fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
137 where
138 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
139 {
140 match driver_num {
141 capsules_core::console::DRIVER_NUM => f(Some(self.console)),
142 capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
143 capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
144 capsules_core::led::DRIVER_NUM => f(Some(self.led)),
145 capsules_core::button::DRIVER_NUM => f(Some(self.button)),
146 capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
147 capsules_core::rng::DRIVER_NUM => f(Some(self.rng)),
148 capsules_extra::ble_advertising_driver::DRIVER_NUM => f(Some(self.ble_radio)),
149 capsules_extra::ieee802154::DRIVER_NUM => f(Some(self.ieee802154_radio)),
150 capsules_extra::temperature::DRIVER_NUM => f(Some(self.temp)),
151 kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
152 capsules_core::i2c_master_slave_driver::DRIVER_NUM => f(Some(self.i2c_master_slave)),
153 _ => f(None),
154 }
155 }
156}
157
158impl KernelResources<nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>>
159 for Platform
160{
161 type SyscallDriverLookup = Self;
162 type SyscallFilter = ();
163 type ProcessFault = ();
164 type Scheduler = RoundRobinSched<'static>;
165 type SchedulerTimer = cortexm4::systick::SysTick;
166 type WatchDog = ();
167 type ContextSwitchCallback = ();
168
169 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
170 self
171 }
172 fn syscall_filter(&self) -> &Self::SyscallFilter {
173 &()
174 }
175 fn process_fault(&self) -> &Self::ProcessFault {
176 &()
177 }
178 fn scheduler(&self) -> &Self::Scheduler {
179 self.scheduler
180 }
181 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
182 &self.systick
183 }
184 fn watchdog(&self) -> &Self::WatchDog {
185 &()
186 }
187 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
188 &()
189 }
190}
191
192#[inline(never)]
196unsafe fn create_peripherals() -> &'static mut Nrf52840DefaultPeripherals<'static> {
197 let ieee802154_ack_buf = static_init!(
198 [u8; nrf52840::ieee802154_radio::ACK_BUF_SIZE],
199 [0; nrf52840::ieee802154_radio::ACK_BUF_SIZE]
200 );
201 let nrf52840_peripherals = static_init!(
203 Nrf52840DefaultPeripherals,
204 Nrf52840DefaultPeripherals::new(ieee802154_ack_buf)
205 );
206
207 nrf52840_peripherals
208}
209
210#[inline(never)]
214pub unsafe fn start_particle_boron() -> (
215 &'static kernel::Kernel,
216 Platform,
217 &'static nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>,
218) {
219 nrf52840::init();
220
221 kernel::deferred_call::initialize_deferred_call_state::<
223 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
224 >();
225
226 let nrf52840_peripherals = create_peripherals();
227
228 nrf52840_peripherals.init();
230 let base_peripherals = &nrf52840_peripherals.nrf52;
231
232 NRF52_POWER = Some(&base_peripherals.pwr_clk);
235
236 let processes = components::process_array::ProcessArrayComponent::new()
238 .finalize(components::process_array_component_static!(NUM_PROCS));
239 PROCESSES = Some(processes);
240
241 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
243
244 let process_management_capability =
251 create_capability!(capabilities::ProcessManagementCapability);
252 let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
253
254 let gpio_port = &nrf52840_peripherals.gpio_port;
259 let debug_gpios = static_init!(
263 [&'static dyn kernel::hil::gpio::Pin; 1],
264 [&gpio_port[LED2_R_PIN]]
265 );
266 kernel::debug::initialize_debug_gpio::<
267 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
268 >();
269 kernel::debug::assign_gpios(debug_gpios);
270
271 let uart_channel = UartChannel::Pins(UartPins::new(None, UART_TXD, None, UART_RXD));
272
273 let gpio = components::gpio::GpioComponent::new(
278 board_kernel,
279 capsules_core::gpio::DRIVER_NUM,
280 components::gpio_component_helper!(
281 nrf52840::gpio::GPIOPin,
282 6 => &nrf52840_peripherals.gpio_port[Pin::P1_15],
297 7 => &nrf52840_peripherals.gpio_port[Pin::P1_13],
299 8 => &nrf52840_peripherals.gpio_port[Pin::P1_14],
301 9 => &nrf52840_peripherals.gpio_port[Pin::P0_08],
303 10 => &nrf52840_peripherals.gpio_port[Pin::P0_06],
305 11 => &nrf52840_peripherals.gpio_port[Pin::P1_03],
308 12 => &nrf52840_peripherals.gpio_port[Pin::P1_12],
310 13 => &nrf52840_peripherals.gpio_port[Pin::P1_11],
312 14 => &nrf52840_peripherals.gpio_port[Pin::P1_10],
314 15 => &nrf52840_peripherals.gpio_port[Pin::P1_08],
316 16 => &nrf52840_peripherals.gpio_port[Pin::P1_02],
318 17 => &nrf52840_peripherals.gpio_port[Pin::P0_01],
320 18 => &nrf52840_peripherals.gpio_port[Pin::P0_27],
322 19 => &nrf52840_peripherals.gpio_port[Pin::P0_26],
324 ),
325 )
326 .finalize(components::gpio_component_static!(nrf52840::gpio::GPIOPin));
327
328 let button = components::button::ButtonComponent::new(
333 board_kernel,
334 capsules_core::button::DRIVER_NUM,
335 components::button_component_helper!(
336 nrf52840::gpio::GPIOPin,
337 (
338 &nrf52840_peripherals.gpio_port[BUTTON_PIN],
339 kernel::hil::gpio::ActivationMode::ActiveLow,
340 kernel::hil::gpio::FloatingState::PullUp
341 )
342 ),
343 )
344 .finalize(components::button_component_static!(
345 nrf52840::gpio::GPIOPin
346 ));
347
348 let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
353 LedLow<'static, nrf52840::gpio::GPIOPin>,
354 LedLow::new(&nrf52840_peripherals.gpio_port[LED_USR_PIN]),
355 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_R_PIN]),
356 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_G_PIN]),
357 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_B_PIN]),
358 ));
359
360 nrf52_components::startup::NrfStartupComponent::new(
361 false,
362 BUTTON_RST_PIN,
363 nrf52840::uicr::Regulator0Output::V3_0,
364 &base_peripherals.nvmc,
365 )
366 .finalize(());
367
368 let rtc = &base_peripherals.rtc;
373 let _ = rtc.start();
374 let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
375 .finalize(components::alarm_mux_component_static!(nrf52840::rtc::Rtc));
376 let alarm = components::alarm::AlarmDriverComponent::new(
377 board_kernel,
378 capsules_core::alarm::DRIVER_NUM,
379 mux_alarm,
380 )
381 .finalize(components::alarm_component_static!(nrf52840::rtc::Rtc));
382
383 let uart_channel = nrf52_components::UartChannelComponent::new(
388 uart_channel,
389 mux_alarm,
390 &base_peripherals.uarte0,
391 )
392 .finalize(nrf52_components::uart_channel_component_static!(
393 nrf52840::rtc::Rtc
394 ));
395
396 let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
398 .finalize(components::process_printer_text_component_static!());
399 PROCESS_PRINTER = Some(process_printer);
400
401 let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
403 .finalize(components::uart_mux_component_static!(132));
404
405 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!(132, 132));
412 components::debug_writer::DebugWriterComponent::new::<
414 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
415 >(
416 uart_mux,
417 create_capability!(capabilities::SetDebugWriterCapability),
418 )
419 .finalize(components::debug_writer_component_static!());
420
421 let ble_radio = components::ble::BLEComponent::new(
426 board_kernel,
427 capsules_extra::ble_advertising_driver::DRIVER_NUM,
428 &base_peripherals.ble_radio,
429 mux_alarm,
430 )
431 .finalize(components::ble_component_static!(
432 nrf52840::rtc::Rtc,
433 nrf52840::ble_radio::Radio
434 ));
435
436 let aes_mux = static_init!(
437 MuxAES128CCM<'static, nrf52840::aes::AesECB>,
438 MuxAES128CCM::new(&base_peripherals.ecb,)
439 );
440 base_peripherals.ecb.set_client(aes_mux);
441 aes_mux.register();
442
443 let (ieee802154_radio, _mux_mac) = components::ieee802154::Ieee802154Component::new(
444 board_kernel,
445 capsules_extra::ieee802154::DRIVER_NUM,
446 &nrf52840_peripherals.ieee802154_radio,
447 aes_mux,
448 PAN_ID,
449 SRC_MAC,
450 DEFAULT_EXT_SRC_MAC,
451 )
452 .finalize(components::ieee802154_component_static!(
453 nrf52840::ieee802154_radio::Radio,
454 nrf52840::aes::AesECB<'static>
455 ));
456
457 let temp = components::temperature::TemperatureComponent::new(
462 board_kernel,
463 capsules_extra::temperature::DRIVER_NUM,
464 &base_peripherals.temp,
465 )
466 .finalize(components::temperature_component_static!(
467 nrf52840::temperature::Temp
468 ));
469
470 let rng = components::rng::RngComponent::new(
475 board_kernel,
476 capsules_core::rng::DRIVER_NUM,
477 &base_peripherals.trng,
478 )
479 .finalize(components::rng_component_static!(nrf52840::trng::Trng));
480
481 base_peripherals.adc.calibrate();
486
487 let adc_mux = components::adc::AdcMuxComponent::new(&base_peripherals.adc)
488 .finalize(components::adc_mux_component_static!(nrf52840::adc::Adc));
489
490 let adc_syscall =
491 components::adc::AdcVirtualComponent::new(board_kernel, capsules_core::adc::DRIVER_NUM)
492 .finalize(components::adc_syscall_component_helper!(
493 components::adc::AdcComponent::new(
495 adc_mux,
496 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput1)
497 )
498 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
499 components::adc::AdcComponent::new(
501 adc_mux,
502 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput2)
503 )
504 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
505 components::adc::AdcComponent::new(
507 adc_mux,
508 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput4)
509 )
510 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
511 components::adc::AdcComponent::new(
513 adc_mux,
514 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput5)
515 )
516 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
517 components::adc::AdcComponent::new(
519 adc_mux,
520 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput6)
521 )
522 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
523 components::adc::AdcComponent::new(
525 adc_mux,
526 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput7)
527 )
528 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
529 ));
530
531 let i2c_master_buffer = static_init!([u8; 128], [0; 128]);
536 let i2c_slave_buffer1 = static_init!([u8; 128], [0; 128]);
537 let i2c_slave_buffer2 = static_init!([u8; 128], [0; 128]);
538
539 let i2c_master_slave = static_init!(
540 I2CMasterSlaveDriver<nrf52840::i2c::TWI<'static>>,
541 I2CMasterSlaveDriver::new(
542 &base_peripherals.twi1,
543 i2c_master_buffer,
544 i2c_slave_buffer1,
545 i2c_slave_buffer2,
546 board_kernel.create_grant(
547 capsules_core::i2c_master_slave_driver::DRIVER_NUM,
548 &memory_allocation_capability
549 ),
550 )
551 );
552 base_peripherals.twi1.configure(
553 nrf52840::pinmux::Pinmux::new(I2C_SCL_PIN as u32),
554 nrf52840::pinmux::Pinmux::new(I2C_SDA_PIN as u32),
555 );
556 base_peripherals.twi1.set_master_client(i2c_master_slave);
557 base_peripherals.twi1.set_slave_client(i2c_master_slave);
558 base_peripherals.twi1.set_speed(nrf52840::i2c::Speed::K400);
561
562 nrf52840_peripherals.gpio_port[I2C_SDA_PIN].set_i2c_pin_cfg();
564 nrf52840_peripherals.gpio_port[I2C_SCL_PIN].set_i2c_pin_cfg();
565 nrf52840_peripherals.gpio_port[I2C_SDA_PIN].set_floating_state(FloatingState::PullUp);
567 nrf52840_peripherals.gpio_port[I2C_SCL_PIN].set_floating_state(FloatingState::PullUp);
568
569 nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
574
575 let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
576 .finalize(components::round_robin_component_static!(NUM_PROCS));
577
578 let platform = Platform {
579 button,
580 ble_radio,
581 ieee802154_radio,
582 console,
583 led,
584 gpio,
585 adc: adc_syscall,
586 rng,
587 temp,
588 alarm,
589 ipc: kernel::ipc::IPC::new(
590 board_kernel,
591 kernel::ipc::DRIVER_NUM,
592 &memory_allocation_capability,
593 ),
594 i2c_master_slave,
595 scheduler,
596 systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
597 };
598
599 let chip = static_init!(
600 nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
601 nrf52840::chip::NRF52::new(nrf52840_peripherals)
602 );
603 CHIP = Some(chip);
604
605 debug!("Particle Boron: Initialization complete. Entering main loop\r");
606
607 extern "C" {
613 static _sapps: u8;
615 static _eapps: u8;
617 static mut _sappmem: u8;
619 static _eappmem: u8;
621 }
622
623 kernel::process::load_processes(
624 board_kernel,
625 chip,
626 core::slice::from_raw_parts(
627 core::ptr::addr_of!(_sapps),
628 core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
629 ),
630 core::slice::from_raw_parts_mut(
631 core::ptr::addr_of_mut!(_sappmem),
632 core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
633 ),
634 &FAULT_RESPONSE,
635 &process_management_capability,
636 )
637 .unwrap_or_else(|err| {
638 debug!("Error loading processes!");
639 debug!("{:?}", err);
640 });
641
642 (board_kernel, platform, chip)
643}
644
645#[no_mangle]
647pub unsafe fn main() {
648 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
649
650 let (board_kernel, platform, chip) = start_particle_boron();
651 board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
652}