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
80static mut CHIP: Option<&'static nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>> = None;
82static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
84 None;
85static mut NRF52_POWER: Option<&'static nrf52840::power::Power> = None;
86
87#[no_mangle]
89#[link_section = ".stack_buffer"]
90static mut STACK_MEMORY: [u8; 0x1000] = [0; 0x1000];
91
92type TemperatureDriver =
93 components::temperature::TemperatureComponentType<nrf52840::temperature::Temp<'static>>;
94type RngDriver = components::rng::RngComponentType<nrf52840::trng::Trng<'static>>;
95
96type Ieee802154Driver = components::ieee802154::Ieee802154ComponentType<
97 nrf52840::ieee802154_radio::Radio<'static>,
98 nrf52840::aes::AesECB<'static>,
99>;
100
101pub struct Platform {
103 ble_radio: &'static capsules_extra::ble_advertising_driver::BLE<
104 'static,
105 nrf52840::ble_radio::Radio<'static>,
106 VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>,
107 >,
108 ieee802154_radio: &'static Ieee802154Driver,
109 button: &'static capsules_core::button::Button<'static, nrf52840::gpio::GPIOPin<'static>>,
110 console: &'static capsules_core::console::Console<'static>,
111 gpio: &'static capsules_core::gpio::GPIO<'static, nrf52840::gpio::GPIOPin<'static>>,
112 led: &'static capsules_core::led::LedDriver<
113 'static,
114 LedLow<'static, nrf52840::gpio::GPIOPin<'static>>,
115 4,
116 >,
117 adc: &'static capsules_core::adc::AdcVirtualized<'static>,
118 rng: &'static RngDriver,
119 temp: &'static TemperatureDriver,
120 ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
121 i2c_master_slave: &'static capsules_core::i2c_master_slave_driver::I2CMasterSlaveDriver<
122 'static,
123 nrf52840::i2c::TWI<'static>,
124 >,
125 alarm: &'static capsules_core::alarm::AlarmDriver<
126 'static,
127 capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<
128 'static,
129 nrf52840::rtc::Rtc<'static>,
130 >,
131 >,
132 scheduler: &'static RoundRobinSched<'static>,
133 systick: cortexm4::systick::SysTick,
134}
135
136impl SyscallDriverLookup for Platform {
137 fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
138 where
139 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
140 {
141 match driver_num {
142 capsules_core::console::DRIVER_NUM => f(Some(self.console)),
143 capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
144 capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
145 capsules_core::led::DRIVER_NUM => f(Some(self.led)),
146 capsules_core::button::DRIVER_NUM => f(Some(self.button)),
147 capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
148 capsules_core::rng::DRIVER_NUM => f(Some(self.rng)),
149 capsules_extra::ble_advertising_driver::DRIVER_NUM => f(Some(self.ble_radio)),
150 capsules_extra::ieee802154::DRIVER_NUM => f(Some(self.ieee802154_radio)),
151 capsules_extra::temperature::DRIVER_NUM => f(Some(self.temp)),
152 kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
153 capsules_core::i2c_master_slave_driver::DRIVER_NUM => f(Some(self.i2c_master_slave)),
154 _ => f(None),
155 }
156 }
157}
158
159impl KernelResources<nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>>
160 for Platform
161{
162 type SyscallDriverLookup = Self;
163 type SyscallFilter = ();
164 type ProcessFault = ();
165 type Scheduler = RoundRobinSched<'static>;
166 type SchedulerTimer = cortexm4::systick::SysTick;
167 type WatchDog = ();
168 type ContextSwitchCallback = ();
169
170 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
171 self
172 }
173 fn syscall_filter(&self) -> &Self::SyscallFilter {
174 &()
175 }
176 fn process_fault(&self) -> &Self::ProcessFault {
177 &()
178 }
179 fn scheduler(&self) -> &Self::Scheduler {
180 self.scheduler
181 }
182 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
183 &self.systick
184 }
185 fn watchdog(&self) -> &Self::WatchDog {
186 &()
187 }
188 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
189 &()
190 }
191}
192
193#[inline(never)]
197unsafe fn create_peripherals() -> &'static mut Nrf52840DefaultPeripherals<'static> {
198 let ieee802154_ack_buf = static_init!(
199 [u8; nrf52840::ieee802154_radio::ACK_BUF_SIZE],
200 [0; nrf52840::ieee802154_radio::ACK_BUF_SIZE]
201 );
202 let nrf52840_peripherals = static_init!(
204 Nrf52840DefaultPeripherals,
205 Nrf52840DefaultPeripherals::new(ieee802154_ack_buf)
206 );
207
208 nrf52840_peripherals
209}
210
211#[inline(never)]
215pub unsafe fn start_particle_boron() -> (
216 &'static kernel::Kernel,
217 Platform,
218 &'static nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>,
219) {
220 nrf52840::init();
221
222 let nrf52840_peripherals = create_peripherals();
223
224 nrf52840_peripherals.init();
226 let base_peripherals = &nrf52840_peripherals.nrf52;
227
228 NRF52_POWER = Some(&base_peripherals.pwr_clk);
231
232 let processes = components::process_array::ProcessArrayComponent::new()
234 .finalize(components::process_array_component_static!(NUM_PROCS));
235 PROCESSES = Some(processes);
236
237 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
239
240 let process_management_capability =
247 create_capability!(capabilities::ProcessManagementCapability);
248 let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
249
250 let gpio_port = &nrf52840_peripherals.gpio_port;
255 kernel::debug::assign_gpios(Some(&gpio_port[LED2_R_PIN]), None, None);
259
260 let uart_channel = UartChannel::Pins(UartPins::new(None, UART_TXD, None, UART_RXD));
261
262 let gpio = components::gpio::GpioComponent::new(
267 board_kernel,
268 capsules_core::gpio::DRIVER_NUM,
269 components::gpio_component_helper!(
270 nrf52840::gpio::GPIOPin,
271 6 => &nrf52840_peripherals.gpio_port[Pin::P1_15],
286 7 => &nrf52840_peripherals.gpio_port[Pin::P1_13],
288 8 => &nrf52840_peripherals.gpio_port[Pin::P1_14],
290 9 => &nrf52840_peripherals.gpio_port[Pin::P0_08],
292 10 => &nrf52840_peripherals.gpio_port[Pin::P0_06],
294 11 => &nrf52840_peripherals.gpio_port[Pin::P1_03],
297 12 => &nrf52840_peripherals.gpio_port[Pin::P1_12],
299 13 => &nrf52840_peripherals.gpio_port[Pin::P1_11],
301 14 => &nrf52840_peripherals.gpio_port[Pin::P1_10],
303 15 => &nrf52840_peripherals.gpio_port[Pin::P1_08],
305 16 => &nrf52840_peripherals.gpio_port[Pin::P1_02],
307 17 => &nrf52840_peripherals.gpio_port[Pin::P0_01],
309 18 => &nrf52840_peripherals.gpio_port[Pin::P0_27],
311 19 => &nrf52840_peripherals.gpio_port[Pin::P0_26],
313 ),
314 )
315 .finalize(components::gpio_component_static!(nrf52840::gpio::GPIOPin));
316
317 let button = components::button::ButtonComponent::new(
322 board_kernel,
323 capsules_core::button::DRIVER_NUM,
324 components::button_component_helper!(
325 nrf52840::gpio::GPIOPin,
326 (
327 &nrf52840_peripherals.gpio_port[BUTTON_PIN],
328 kernel::hil::gpio::ActivationMode::ActiveLow,
329 kernel::hil::gpio::FloatingState::PullUp
330 )
331 ),
332 )
333 .finalize(components::button_component_static!(
334 nrf52840::gpio::GPIOPin
335 ));
336
337 let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
342 LedLow<'static, nrf52840::gpio::GPIOPin>,
343 LedLow::new(&nrf52840_peripherals.gpio_port[LED_USR_PIN]),
344 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_R_PIN]),
345 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_G_PIN]),
346 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_B_PIN]),
347 ));
348
349 nrf52_components::startup::NrfStartupComponent::new(
350 false,
351 BUTTON_RST_PIN,
352 nrf52840::uicr::Regulator0Output::V3_0,
353 &base_peripherals.nvmc,
354 )
355 .finalize(());
356
357 let rtc = &base_peripherals.rtc;
362 let _ = rtc.start();
363 let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
364 .finalize(components::alarm_mux_component_static!(nrf52840::rtc::Rtc));
365 let alarm = components::alarm::AlarmDriverComponent::new(
366 board_kernel,
367 capsules_core::alarm::DRIVER_NUM,
368 mux_alarm,
369 )
370 .finalize(components::alarm_component_static!(nrf52840::rtc::Rtc));
371
372 let uart_channel = nrf52_components::UartChannelComponent::new(
377 uart_channel,
378 mux_alarm,
379 &base_peripherals.uarte0,
380 )
381 .finalize(nrf52_components::uart_channel_component_static!(
382 nrf52840::rtc::Rtc
383 ));
384
385 let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
387 .finalize(components::process_printer_text_component_static!());
388 PROCESS_PRINTER = Some(process_printer);
389
390 let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
392 .finalize(components::uart_mux_component_static!(132));
393
394 let console = components::console::ConsoleComponent::new(
396 board_kernel,
397 capsules_core::console::DRIVER_NUM,
398 uart_mux,
399 )
400 .finalize(components::console_component_static!(132, 132));
401 components::debug_writer::DebugWriterComponent::new(
403 uart_mux,
404 create_capability!(capabilities::SetDebugWriterCapability),
405 )
406 .finalize(components::debug_writer_component_static!());
407
408 let ble_radio = components::ble::BLEComponent::new(
413 board_kernel,
414 capsules_extra::ble_advertising_driver::DRIVER_NUM,
415 &base_peripherals.ble_radio,
416 mux_alarm,
417 )
418 .finalize(components::ble_component_static!(
419 nrf52840::rtc::Rtc,
420 nrf52840::ble_radio::Radio
421 ));
422
423 let aes_mux = static_init!(
424 MuxAES128CCM<'static, nrf52840::aes::AesECB>,
425 MuxAES128CCM::new(&base_peripherals.ecb,)
426 );
427 base_peripherals.ecb.set_client(aes_mux);
428 aes_mux.register();
429
430 let (ieee802154_radio, _mux_mac) = components::ieee802154::Ieee802154Component::new(
431 board_kernel,
432 capsules_extra::ieee802154::DRIVER_NUM,
433 &nrf52840_peripherals.ieee802154_radio,
434 aes_mux,
435 PAN_ID,
436 SRC_MAC,
437 DEFAULT_EXT_SRC_MAC,
438 )
439 .finalize(components::ieee802154_component_static!(
440 nrf52840::ieee802154_radio::Radio,
441 nrf52840::aes::AesECB<'static>
442 ));
443
444 let temp = components::temperature::TemperatureComponent::new(
449 board_kernel,
450 capsules_extra::temperature::DRIVER_NUM,
451 &base_peripherals.temp,
452 )
453 .finalize(components::temperature_component_static!(
454 nrf52840::temperature::Temp
455 ));
456
457 let rng = components::rng::RngComponent::new(
462 board_kernel,
463 capsules_core::rng::DRIVER_NUM,
464 &base_peripherals.trng,
465 )
466 .finalize(components::rng_component_static!(nrf52840::trng::Trng));
467
468 base_peripherals.adc.calibrate();
473
474 let adc_mux = components::adc::AdcMuxComponent::new(&base_peripherals.adc)
475 .finalize(components::adc_mux_component_static!(nrf52840::adc::Adc));
476
477 let adc_syscall =
478 components::adc::AdcVirtualComponent::new(board_kernel, capsules_core::adc::DRIVER_NUM)
479 .finalize(components::adc_syscall_component_helper!(
480 components::adc::AdcComponent::new(
482 adc_mux,
483 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput1)
484 )
485 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
486 components::adc::AdcComponent::new(
488 adc_mux,
489 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput2)
490 )
491 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
492 components::adc::AdcComponent::new(
494 adc_mux,
495 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput4)
496 )
497 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
498 components::adc::AdcComponent::new(
500 adc_mux,
501 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput5)
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::AnalogInput6)
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::AnalogInput7)
514 )
515 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
516 ));
517
518 let i2c_master_buffer = static_init!([u8; 128], [0; 128]);
523 let i2c_slave_buffer1 = static_init!([u8; 128], [0; 128]);
524 let i2c_slave_buffer2 = static_init!([u8; 128], [0; 128]);
525
526 let i2c_master_slave = static_init!(
527 I2CMasterSlaveDriver<nrf52840::i2c::TWI<'static>>,
528 I2CMasterSlaveDriver::new(
529 &base_peripherals.twi1,
530 i2c_master_buffer,
531 i2c_slave_buffer1,
532 i2c_slave_buffer2,
533 board_kernel.create_grant(
534 capsules_core::i2c_master_slave_driver::DRIVER_NUM,
535 &memory_allocation_capability
536 ),
537 )
538 );
539 base_peripherals.twi1.configure(
540 nrf52840::pinmux::Pinmux::new(I2C_SCL_PIN as u32),
541 nrf52840::pinmux::Pinmux::new(I2C_SDA_PIN as u32),
542 );
543 base_peripherals.twi1.set_master_client(i2c_master_slave);
544 base_peripherals.twi1.set_slave_client(i2c_master_slave);
545 base_peripherals.twi1.set_speed(nrf52840::i2c::Speed::K400);
548
549 nrf52840_peripherals.gpio_port[I2C_SDA_PIN].set_i2c_pin_cfg();
551 nrf52840_peripherals.gpio_port[I2C_SCL_PIN].set_i2c_pin_cfg();
552 nrf52840_peripherals.gpio_port[I2C_SDA_PIN].set_floating_state(FloatingState::PullUp);
554 nrf52840_peripherals.gpio_port[I2C_SCL_PIN].set_floating_state(FloatingState::PullUp);
555
556 nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
561
562 let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
563 .finalize(components::round_robin_component_static!(NUM_PROCS));
564
565 let platform = Platform {
566 button,
567 ble_radio,
568 ieee802154_radio,
569 console,
570 led,
571 gpio,
572 adc: adc_syscall,
573 rng,
574 temp,
575 alarm,
576 ipc: kernel::ipc::IPC::new(
577 board_kernel,
578 kernel::ipc::DRIVER_NUM,
579 &memory_allocation_capability,
580 ),
581 i2c_master_slave,
582 scheduler,
583 systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
584 };
585
586 let chip = static_init!(
587 nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
588 nrf52840::chip::NRF52::new(nrf52840_peripherals)
589 );
590 CHIP = Some(chip);
591
592 debug!("Particle Boron: Initialization complete. Entering main loop\r");
593
594 extern "C" {
600 static _sapps: u8;
602 static _eapps: u8;
604 static mut _sappmem: u8;
606 static _eappmem: u8;
608 }
609
610 kernel::process::load_processes(
611 board_kernel,
612 chip,
613 core::slice::from_raw_parts(
614 core::ptr::addr_of!(_sapps),
615 core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
616 ),
617 core::slice::from_raw_parts_mut(
618 core::ptr::addr_of_mut!(_sappmem),
619 core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
620 ),
621 &FAULT_RESPONSE,
622 &process_management_capability,
623 )
624 .unwrap_or_else(|err| {
625 debug!("Error loading processes!");
626 debug!("{:?}", err);
627 });
628
629 (board_kernel, platform, chip)
630}
631
632#[no_mangle]
634pub unsafe fn main() {
635 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
636
637 let (board_kernel, platform, chip) = start_particle_boron();
638 board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
639}