1#![no_std]
11#![cfg_attr(not(doc), no_main)]
14#![deny(missing_docs)]
15
16use core::ptr::addr_of;
17use core::ptr::addr_of_mut;
18
19use capsules_core::i2c_master_slave_driver::I2CMasterSlaveDriver;
20use capsules_core::virtualizers::virtual_aes_ccm::MuxAES128CCM;
21use capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm;
22use kernel::component::Component;
23use kernel::deferred_call::DeferredCallClient;
24use kernel::hil::gpio::Configure;
25use kernel::hil::gpio::FloatingState;
26use kernel::hil::i2c::{I2CMaster, I2CSlave};
27use kernel::hil::led::LedLow;
28use kernel::hil::symmetric_encryption::AES128;
29use kernel::hil::time::Counter;
30use kernel::platform::{KernelResources, SyscallDriverLookup};
31use kernel::scheduler::round_robin::RoundRobinSched;
32#[allow(unused_imports)]
33use kernel::{capabilities, create_capability, debug, debug_gpio, debug_verbose, static_init};
34use nrf52840::gpio::Pin;
35use nrf52840::interrupt_service::Nrf52840DefaultPeripherals;
36#[allow(unused_imports)]
37use nrf52_components::{self, UartChannel, UartPins};
38
39const LED_USR_PIN: Pin = Pin::P1_12;
41const LED2_R_PIN: Pin = Pin::P0_13;
42const LED2_G_PIN: Pin = Pin::P0_14;
43const LED2_B_PIN: Pin = Pin::P0_15;
44
45const BUTTON_PIN: Pin = Pin::P0_11;
47const BUTTON_RST_PIN: Pin = Pin::P0_18;
48
49const _UART_RTS: Option<Pin> = Some(Pin::P0_30);
51const _UART_CTS: Option<Pin> = Some(Pin::P0_31);
52const UART_TXD: Pin = Pin::P0_06;
53const UART_RXD: Pin = Pin::P0_08;
54
55const _SPI_MOSI: Pin = Pin::P1_13;
57const _SPI_MISO: Pin = Pin::P1_14;
58const _SPI_CLK: Pin = Pin::P1_15;
59
60const I2C_SDA_PIN: Pin = Pin::P0_26;
62const I2C_SCL_PIN: Pin = Pin::P0_27;
63
64const SRC_MAC: u16 = 0xf00f;
67const PAN_ID: u16 = 0xABCD;
68const DEFAULT_EXT_SRC_MAC: [u8; 8] = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77];
69
70pub mod io;
72
73const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
76 capsules_system::process_policies::PanicFaultPolicy {};
77
78const NUM_PROCS: usize = 8;
80
81static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
82 [None; NUM_PROCS];
83
84static mut CHIP: Option<&'static nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>> = None;
86static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
88 None;
89static mut NRF52_POWER: Option<&'static nrf52840::power::Power> = None;
90
91#[no_mangle]
93#[link_section = ".stack_buffer"]
94pub static mut STACK_MEMORY: [u8; 0x1000] = [0; 0x1000];
95
96type TemperatureDriver =
97 components::temperature::TemperatureComponentType<nrf52840::temperature::Temp<'static>>;
98type RngDriver = components::rng::RngComponentType<nrf52840::trng::Trng<'static>>;
99
100type Ieee802154Driver = components::ieee802154::Ieee802154ComponentType<
101 nrf52840::ieee802154_radio::Radio<'static>,
102 nrf52840::aes::AesECB<'static>,
103>;
104
105pub struct Platform {
107 ble_radio: &'static capsules_extra::ble_advertising_driver::BLE<
108 'static,
109 nrf52840::ble_radio::Radio<'static>,
110 VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>,
111 >,
112 ieee802154_radio: &'static Ieee802154Driver,
113 button: &'static capsules_core::button::Button<'static, nrf52840::gpio::GPIOPin<'static>>,
114 console: &'static capsules_core::console::Console<'static>,
115 gpio: &'static capsules_core::gpio::GPIO<'static, nrf52840::gpio::GPIOPin<'static>>,
116 led: &'static capsules_core::led::LedDriver<
117 'static,
118 LedLow<'static, nrf52840::gpio::GPIOPin<'static>>,
119 4,
120 >,
121 adc: &'static capsules_core::adc::AdcVirtualized<'static>,
122 rng: &'static RngDriver,
123 temp: &'static TemperatureDriver,
124 ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
125 i2c_master_slave: &'static capsules_core::i2c_master_slave_driver::I2CMasterSlaveDriver<
126 'static,
127 nrf52840::i2c::TWI<'static>,
128 >,
129 alarm: &'static capsules_core::alarm::AlarmDriver<
130 'static,
131 capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<
132 'static,
133 nrf52840::rtc::Rtc<'static>,
134 >,
135 >,
136 scheduler: &'static RoundRobinSched<'static>,
137 systick: cortexm4::systick::SysTick,
138}
139
140impl SyscallDriverLookup for Platform {
141 fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
142 where
143 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
144 {
145 match driver_num {
146 capsules_core::console::DRIVER_NUM => f(Some(self.console)),
147 capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
148 capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
149 capsules_core::led::DRIVER_NUM => f(Some(self.led)),
150 capsules_core::button::DRIVER_NUM => f(Some(self.button)),
151 capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
152 capsules_core::rng::DRIVER_NUM => f(Some(self.rng)),
153 capsules_extra::ble_advertising_driver::DRIVER_NUM => f(Some(self.ble_radio)),
154 capsules_extra::ieee802154::DRIVER_NUM => f(Some(self.ieee802154_radio)),
155 capsules_extra::temperature::DRIVER_NUM => f(Some(self.temp)),
156 kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
157 capsules_core::i2c_master_slave_driver::DRIVER_NUM => f(Some(self.i2c_master_slave)),
158 _ => f(None),
159 }
160 }
161}
162
163impl KernelResources<nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>>
164 for Platform
165{
166 type SyscallDriverLookup = Self;
167 type SyscallFilter = ();
168 type ProcessFault = ();
169 type Scheduler = RoundRobinSched<'static>;
170 type SchedulerTimer = cortexm4::systick::SysTick;
171 type WatchDog = ();
172 type ContextSwitchCallback = ();
173
174 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
175 self
176 }
177 fn syscall_filter(&self) -> &Self::SyscallFilter {
178 &()
179 }
180 fn process_fault(&self) -> &Self::ProcessFault {
181 &()
182 }
183 fn scheduler(&self) -> &Self::Scheduler {
184 self.scheduler
185 }
186 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
187 &self.systick
188 }
189 fn watchdog(&self) -> &Self::WatchDog {
190 &()
191 }
192 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
193 &()
194 }
195}
196
197#[inline(never)]
201unsafe fn create_peripherals() -> &'static mut Nrf52840DefaultPeripherals<'static> {
202 let ieee802154_ack_buf = static_init!(
203 [u8; nrf52840::ieee802154_radio::ACK_BUF_SIZE],
204 [0; nrf52840::ieee802154_radio::ACK_BUF_SIZE]
205 );
206 let nrf52840_peripherals = static_init!(
208 Nrf52840DefaultPeripherals,
209 Nrf52840DefaultPeripherals::new(ieee802154_ack_buf)
210 );
211
212 nrf52840_peripherals
213}
214
215#[inline(never)]
219pub unsafe fn start_particle_boron() -> (
220 &'static kernel::Kernel,
221 Platform,
222 &'static nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>,
223) {
224 nrf52840::init();
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 board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&*addr_of!(PROCESSES)));
237
238 let process_management_capability =
245 create_capability!(capabilities::ProcessManagementCapability);
246 let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
247
248 let gpio_port = &nrf52840_peripherals.gpio_port;
253 kernel::debug::assign_gpios(Some(&gpio_port[LED2_R_PIN]), None, None);
257
258 let uart_channel = UartChannel::Pins(UartPins::new(None, UART_TXD, None, UART_RXD));
259
260 let gpio = components::gpio::GpioComponent::new(
265 board_kernel,
266 capsules_core::gpio::DRIVER_NUM,
267 components::gpio_component_helper!(
268 nrf52840::gpio::GPIOPin,
269 6 => &nrf52840_peripherals.gpio_port[Pin::P1_15],
284 7 => &nrf52840_peripherals.gpio_port[Pin::P1_13],
286 8 => &nrf52840_peripherals.gpio_port[Pin::P1_14],
288 9 => &nrf52840_peripherals.gpio_port[Pin::P0_08],
290 10 => &nrf52840_peripherals.gpio_port[Pin::P0_06],
292 11 => &nrf52840_peripherals.gpio_port[Pin::P1_03],
295 12 => &nrf52840_peripherals.gpio_port[Pin::P1_12],
297 13 => &nrf52840_peripherals.gpio_port[Pin::P1_11],
299 14 => &nrf52840_peripherals.gpio_port[Pin::P1_10],
301 15 => &nrf52840_peripherals.gpio_port[Pin::P1_08],
303 16 => &nrf52840_peripherals.gpio_port[Pin::P1_02],
305 17 => &nrf52840_peripherals.gpio_port[Pin::P0_01],
307 18 => &nrf52840_peripherals.gpio_port[Pin::P0_27],
309 19 => &nrf52840_peripherals.gpio_port[Pin::P0_26],
311 ),
312 )
313 .finalize(components::gpio_component_static!(nrf52840::gpio::GPIOPin));
314
315 let button = components::button::ButtonComponent::new(
320 board_kernel,
321 capsules_core::button::DRIVER_NUM,
322 components::button_component_helper!(
323 nrf52840::gpio::GPIOPin,
324 (
325 &nrf52840_peripherals.gpio_port[BUTTON_PIN],
326 kernel::hil::gpio::ActivationMode::ActiveLow,
327 kernel::hil::gpio::FloatingState::PullUp
328 )
329 ),
330 )
331 .finalize(components::button_component_static!(
332 nrf52840::gpio::GPIOPin
333 ));
334
335 let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
340 LedLow<'static, nrf52840::gpio::GPIOPin>,
341 LedLow::new(&nrf52840_peripherals.gpio_port[LED_USR_PIN]),
342 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_R_PIN]),
343 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_G_PIN]),
344 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_B_PIN]),
345 ));
346
347 nrf52_components::startup::NrfStartupComponent::new(
348 false,
349 BUTTON_RST_PIN,
350 nrf52840::uicr::Regulator0Output::V3_0,
351 &base_peripherals.nvmc,
352 )
353 .finalize(());
354
355 let rtc = &base_peripherals.rtc;
360 let _ = rtc.start();
361 let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
362 .finalize(components::alarm_mux_component_static!(nrf52840::rtc::Rtc));
363 let alarm = components::alarm::AlarmDriverComponent::new(
364 board_kernel,
365 capsules_core::alarm::DRIVER_NUM,
366 mux_alarm,
367 )
368 .finalize(components::alarm_component_static!(nrf52840::rtc::Rtc));
369
370 let uart_channel = nrf52_components::UartChannelComponent::new(
375 uart_channel,
376 mux_alarm,
377 &base_peripherals.uarte0,
378 )
379 .finalize(nrf52_components::uart_channel_component_static!(
380 nrf52840::rtc::Rtc
381 ));
382
383 let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
385 .finalize(components::process_printer_text_component_static!());
386 PROCESS_PRINTER = Some(process_printer);
387
388 let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
390 .finalize(components::uart_mux_component_static!(132));
391
392 let console = components::console::ConsoleComponent::new(
394 board_kernel,
395 capsules_core::console::DRIVER_NUM,
396 uart_mux,
397 )
398 .finalize(components::console_component_static!(132, 132));
399 components::debug_writer::DebugWriterComponent::new(uart_mux)
401 .finalize(components::debug_writer_component_static!());
402
403 let ble_radio = components::ble::BLEComponent::new(
408 board_kernel,
409 capsules_extra::ble_advertising_driver::DRIVER_NUM,
410 &base_peripherals.ble_radio,
411 mux_alarm,
412 )
413 .finalize(components::ble_component_static!(
414 nrf52840::rtc::Rtc,
415 nrf52840::ble_radio::Radio
416 ));
417
418 let aes_mux = static_init!(
419 MuxAES128CCM<'static, nrf52840::aes::AesECB>,
420 MuxAES128CCM::new(&base_peripherals.ecb,)
421 );
422 base_peripherals.ecb.set_client(aes_mux);
423 aes_mux.register();
424
425 let (ieee802154_radio, _mux_mac) = components::ieee802154::Ieee802154Component::new(
426 board_kernel,
427 capsules_extra::ieee802154::DRIVER_NUM,
428 &nrf52840_peripherals.ieee802154_radio,
429 aes_mux,
430 PAN_ID,
431 SRC_MAC,
432 DEFAULT_EXT_SRC_MAC,
433 )
434 .finalize(components::ieee802154_component_static!(
435 nrf52840::ieee802154_radio::Radio,
436 nrf52840::aes::AesECB<'static>
437 ));
438
439 let temp = components::temperature::TemperatureComponent::new(
444 board_kernel,
445 capsules_extra::temperature::DRIVER_NUM,
446 &base_peripherals.temp,
447 )
448 .finalize(components::temperature_component_static!(
449 nrf52840::temperature::Temp
450 ));
451
452 let rng = components::rng::RngComponent::new(
457 board_kernel,
458 capsules_core::rng::DRIVER_NUM,
459 &base_peripherals.trng,
460 )
461 .finalize(components::rng_component_static!(nrf52840::trng::Trng));
462
463 base_peripherals.adc.calibrate();
468
469 let adc_mux = components::adc::AdcMuxComponent::new(&base_peripherals.adc)
470 .finalize(components::adc_mux_component_static!(nrf52840::adc::Adc));
471
472 let adc_syscall =
473 components::adc::AdcVirtualComponent::new(board_kernel, capsules_core::adc::DRIVER_NUM)
474 .finalize(components::adc_syscall_component_helper!(
475 components::adc::AdcComponent::new(
477 adc_mux,
478 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput1)
479 )
480 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
481 components::adc::AdcComponent::new(
483 adc_mux,
484 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput2)
485 )
486 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
487 components::adc::AdcComponent::new(
489 adc_mux,
490 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput4)
491 )
492 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
493 components::adc::AdcComponent::new(
495 adc_mux,
496 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput5)
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::AnalogInput6)
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::AnalogInput7)
509 )
510 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
511 ));
512
513 let i2c_master_buffer = static_init!([u8; 128], [0; 128]);
518 let i2c_slave_buffer1 = static_init!([u8; 128], [0; 128]);
519 let i2c_slave_buffer2 = static_init!([u8; 128], [0; 128]);
520
521 let i2c_master_slave = static_init!(
522 I2CMasterSlaveDriver<nrf52840::i2c::TWI<'static>>,
523 I2CMasterSlaveDriver::new(
524 &base_peripherals.twi1,
525 i2c_master_buffer,
526 i2c_slave_buffer1,
527 i2c_slave_buffer2,
528 board_kernel.create_grant(
529 capsules_core::i2c_master_slave_driver::DRIVER_NUM,
530 &memory_allocation_capability
531 ),
532 )
533 );
534 base_peripherals.twi1.configure(
535 nrf52840::pinmux::Pinmux::new(I2C_SCL_PIN as u32),
536 nrf52840::pinmux::Pinmux::new(I2C_SDA_PIN as u32),
537 );
538 base_peripherals.twi1.set_master_client(i2c_master_slave);
539 base_peripherals.twi1.set_slave_client(i2c_master_slave);
540 base_peripherals.twi1.set_speed(nrf52840::i2c::Speed::K400);
543
544 nrf52840_peripherals.gpio_port[I2C_SDA_PIN].set_i2c_pin_cfg();
546 nrf52840_peripherals.gpio_port[I2C_SCL_PIN].set_i2c_pin_cfg();
547 nrf52840_peripherals.gpio_port[I2C_SDA_PIN].set_floating_state(FloatingState::PullUp);
549 nrf52840_peripherals.gpio_port[I2C_SCL_PIN].set_floating_state(FloatingState::PullUp);
550
551 nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
556
557 let scheduler = components::sched::round_robin::RoundRobinComponent::new(&*addr_of!(PROCESSES))
558 .finalize(components::round_robin_component_static!(NUM_PROCS));
559
560 let platform = Platform {
561 button,
562 ble_radio,
563 ieee802154_radio,
564 console,
565 led,
566 gpio,
567 adc: adc_syscall,
568 rng,
569 temp,
570 alarm,
571 ipc: kernel::ipc::IPC::new(
572 board_kernel,
573 kernel::ipc::DRIVER_NUM,
574 &memory_allocation_capability,
575 ),
576 i2c_master_slave,
577 scheduler,
578 systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
579 };
580
581 let chip = static_init!(
582 nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
583 nrf52840::chip::NRF52::new(nrf52840_peripherals)
584 );
585 CHIP = Some(chip);
586
587 debug!("Particle Boron: Initialization complete. Entering main loop\r");
588
589 extern "C" {
595 static _sapps: u8;
597 static _eapps: u8;
599 static mut _sappmem: u8;
601 static _eappmem: u8;
603 }
604
605 kernel::process::load_processes(
606 board_kernel,
607 chip,
608 core::slice::from_raw_parts(
609 core::ptr::addr_of!(_sapps),
610 core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
611 ),
612 core::slice::from_raw_parts_mut(
613 core::ptr::addr_of_mut!(_sappmem),
614 core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
615 ),
616 &mut *addr_of_mut!(PROCESSES),
617 &FAULT_RESPONSE,
618 &process_management_capability,
619 )
620 .unwrap_or_else(|err| {
621 debug!("Error loading processes!");
622 debug!("{:?}", err);
623 });
624
625 (board_kernel, platform, chip)
626}
627
628#[no_mangle]
630pub unsafe fn main() {
631 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
632
633 let (board_kernel, platform, chip) = start_particle_boron();
634 board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
635}