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
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 let nrf52840_peripherals = create_peripherals();
220
221 nrf52840_peripherals.init();
223 let base_peripherals = &nrf52840_peripherals.nrf52;
224
225 NRF52_POWER = Some(&base_peripherals.pwr_clk);
228
229 let processes = components::process_array::ProcessArrayComponent::new()
231 .finalize(components::process_array_component_static!(NUM_PROCS));
232 PROCESSES = Some(processes);
233
234 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
236
237 let process_management_capability =
244 create_capability!(capabilities::ProcessManagementCapability);
245 let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
246
247 let gpio_port = &nrf52840_peripherals.gpio_port;
252 kernel::debug::assign_gpios(Some(&gpio_port[LED2_R_PIN]), None, None);
256
257 let uart_channel = UartChannel::Pins(UartPins::new(None, UART_TXD, None, UART_RXD));
258
259 let gpio = components::gpio::GpioComponent::new(
264 board_kernel,
265 capsules_core::gpio::DRIVER_NUM,
266 components::gpio_component_helper!(
267 nrf52840::gpio::GPIOPin,
268 6 => &nrf52840_peripherals.gpio_port[Pin::P1_15],
283 7 => &nrf52840_peripherals.gpio_port[Pin::P1_13],
285 8 => &nrf52840_peripherals.gpio_port[Pin::P1_14],
287 9 => &nrf52840_peripherals.gpio_port[Pin::P0_08],
289 10 => &nrf52840_peripherals.gpio_port[Pin::P0_06],
291 11 => &nrf52840_peripherals.gpio_port[Pin::P1_03],
294 12 => &nrf52840_peripherals.gpio_port[Pin::P1_12],
296 13 => &nrf52840_peripherals.gpio_port[Pin::P1_11],
298 14 => &nrf52840_peripherals.gpio_port[Pin::P1_10],
300 15 => &nrf52840_peripherals.gpio_port[Pin::P1_08],
302 16 => &nrf52840_peripherals.gpio_port[Pin::P1_02],
304 17 => &nrf52840_peripherals.gpio_port[Pin::P0_01],
306 18 => &nrf52840_peripherals.gpio_port[Pin::P0_27],
308 19 => &nrf52840_peripherals.gpio_port[Pin::P0_26],
310 ),
311 )
312 .finalize(components::gpio_component_static!(nrf52840::gpio::GPIOPin));
313
314 let button = components::button::ButtonComponent::new(
319 board_kernel,
320 capsules_core::button::DRIVER_NUM,
321 components::button_component_helper!(
322 nrf52840::gpio::GPIOPin,
323 (
324 &nrf52840_peripherals.gpio_port[BUTTON_PIN],
325 kernel::hil::gpio::ActivationMode::ActiveLow,
326 kernel::hil::gpio::FloatingState::PullUp
327 )
328 ),
329 )
330 .finalize(components::button_component_static!(
331 nrf52840::gpio::GPIOPin
332 ));
333
334 let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
339 LedLow<'static, nrf52840::gpio::GPIOPin>,
340 LedLow::new(&nrf52840_peripherals.gpio_port[LED_USR_PIN]),
341 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_R_PIN]),
342 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_G_PIN]),
343 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_B_PIN]),
344 ));
345
346 nrf52_components::startup::NrfStartupComponent::new(
347 false,
348 BUTTON_RST_PIN,
349 nrf52840::uicr::Regulator0Output::V3_0,
350 &base_peripherals.nvmc,
351 )
352 .finalize(());
353
354 let rtc = &base_peripherals.rtc;
359 let _ = rtc.start();
360 let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
361 .finalize(components::alarm_mux_component_static!(nrf52840::rtc::Rtc));
362 let alarm = components::alarm::AlarmDriverComponent::new(
363 board_kernel,
364 capsules_core::alarm::DRIVER_NUM,
365 mux_alarm,
366 )
367 .finalize(components::alarm_component_static!(nrf52840::rtc::Rtc));
368
369 let uart_channel = nrf52_components::UartChannelComponent::new(
374 uart_channel,
375 mux_alarm,
376 &base_peripherals.uarte0,
377 )
378 .finalize(nrf52_components::uart_channel_component_static!(
379 nrf52840::rtc::Rtc
380 ));
381
382 let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
384 .finalize(components::process_printer_text_component_static!());
385 PROCESS_PRINTER = Some(process_printer);
386
387 let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
389 .finalize(components::uart_mux_component_static!(132));
390
391 let console = components::console::ConsoleComponent::new(
393 board_kernel,
394 capsules_core::console::DRIVER_NUM,
395 uart_mux,
396 )
397 .finalize(components::console_component_static!(132, 132));
398 components::debug_writer::DebugWriterComponent::new(
400 uart_mux,
401 create_capability!(capabilities::SetDebugWriterCapability),
402 )
403 .finalize(components::debug_writer_component_static!());
404
405 let ble_radio = components::ble::BLEComponent::new(
410 board_kernel,
411 capsules_extra::ble_advertising_driver::DRIVER_NUM,
412 &base_peripherals.ble_radio,
413 mux_alarm,
414 )
415 .finalize(components::ble_component_static!(
416 nrf52840::rtc::Rtc,
417 nrf52840::ble_radio::Radio
418 ));
419
420 let aes_mux = static_init!(
421 MuxAES128CCM<'static, nrf52840::aes::AesECB>,
422 MuxAES128CCM::new(&base_peripherals.ecb,)
423 );
424 base_peripherals.ecb.set_client(aes_mux);
425 aes_mux.register();
426
427 let (ieee802154_radio, _mux_mac) = components::ieee802154::Ieee802154Component::new(
428 board_kernel,
429 capsules_extra::ieee802154::DRIVER_NUM,
430 &nrf52840_peripherals.ieee802154_radio,
431 aes_mux,
432 PAN_ID,
433 SRC_MAC,
434 DEFAULT_EXT_SRC_MAC,
435 )
436 .finalize(components::ieee802154_component_static!(
437 nrf52840::ieee802154_radio::Radio,
438 nrf52840::aes::AesECB<'static>
439 ));
440
441 let temp = components::temperature::TemperatureComponent::new(
446 board_kernel,
447 capsules_extra::temperature::DRIVER_NUM,
448 &base_peripherals.temp,
449 )
450 .finalize(components::temperature_component_static!(
451 nrf52840::temperature::Temp
452 ));
453
454 let rng = components::rng::RngComponent::new(
459 board_kernel,
460 capsules_core::rng::DRIVER_NUM,
461 &base_peripherals.trng,
462 )
463 .finalize(components::rng_component_static!(nrf52840::trng::Trng));
464
465 base_peripherals.adc.calibrate();
470
471 let adc_mux = components::adc::AdcMuxComponent::new(&base_peripherals.adc)
472 .finalize(components::adc_mux_component_static!(nrf52840::adc::Adc));
473
474 let adc_syscall =
475 components::adc::AdcVirtualComponent::new(board_kernel, capsules_core::adc::DRIVER_NUM)
476 .finalize(components::adc_syscall_component_helper!(
477 components::adc::AdcComponent::new(
479 adc_mux,
480 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput1)
481 )
482 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
483 components::adc::AdcComponent::new(
485 adc_mux,
486 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput2)
487 )
488 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
489 components::adc::AdcComponent::new(
491 adc_mux,
492 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput4)
493 )
494 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
495 components::adc::AdcComponent::new(
497 adc_mux,
498 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput5)
499 )
500 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
501 components::adc::AdcComponent::new(
503 adc_mux,
504 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput6)
505 )
506 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
507 components::adc::AdcComponent::new(
509 adc_mux,
510 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput7)
511 )
512 .finalize(components::adc_component_static!(nrf52840::adc::Adc)),
513 ));
514
515 let i2c_master_buffer = static_init!([u8; 128], [0; 128]);
520 let i2c_slave_buffer1 = static_init!([u8; 128], [0; 128]);
521 let i2c_slave_buffer2 = static_init!([u8; 128], [0; 128]);
522
523 let i2c_master_slave = static_init!(
524 I2CMasterSlaveDriver<nrf52840::i2c::TWI<'static>>,
525 I2CMasterSlaveDriver::new(
526 &base_peripherals.twi1,
527 i2c_master_buffer,
528 i2c_slave_buffer1,
529 i2c_slave_buffer2,
530 board_kernel.create_grant(
531 capsules_core::i2c_master_slave_driver::DRIVER_NUM,
532 &memory_allocation_capability
533 ),
534 )
535 );
536 base_peripherals.twi1.configure(
537 nrf52840::pinmux::Pinmux::new(I2C_SCL_PIN as u32),
538 nrf52840::pinmux::Pinmux::new(I2C_SDA_PIN as u32),
539 );
540 base_peripherals.twi1.set_master_client(i2c_master_slave);
541 base_peripherals.twi1.set_slave_client(i2c_master_slave);
542 base_peripherals.twi1.set_speed(nrf52840::i2c::Speed::K400);
545
546 nrf52840_peripherals.gpio_port[I2C_SDA_PIN].set_i2c_pin_cfg();
548 nrf52840_peripherals.gpio_port[I2C_SCL_PIN].set_i2c_pin_cfg();
549 nrf52840_peripherals.gpio_port[I2C_SDA_PIN].set_floating_state(FloatingState::PullUp);
551 nrf52840_peripherals.gpio_port[I2C_SCL_PIN].set_floating_state(FloatingState::PullUp);
552
553 nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
558
559 let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
560 .finalize(components::round_robin_component_static!(NUM_PROCS));
561
562 let platform = Platform {
563 button,
564 ble_radio,
565 ieee802154_radio,
566 console,
567 led,
568 gpio,
569 adc: adc_syscall,
570 rng,
571 temp,
572 alarm,
573 ipc: kernel::ipc::IPC::new(
574 board_kernel,
575 kernel::ipc::DRIVER_NUM,
576 &memory_allocation_capability,
577 ),
578 i2c_master_slave,
579 scheduler,
580 systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
581 };
582
583 let chip = static_init!(
584 nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
585 nrf52840::chip::NRF52::new(nrf52840_peripherals)
586 );
587 CHIP = Some(chip);
588
589 debug!("Particle Boron: Initialization complete. Entering main loop\r");
590
591 extern "C" {
597 static _sapps: u8;
599 static _eapps: u8;
601 static mut _sappmem: u8;
603 static _eappmem: u8;
605 }
606
607 kernel::process::load_processes(
608 board_kernel,
609 chip,
610 core::slice::from_raw_parts(
611 core::ptr::addr_of!(_sapps),
612 core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
613 ),
614 core::slice::from_raw_parts_mut(
615 core::ptr::addr_of_mut!(_sappmem),
616 core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
617 ),
618 &FAULT_RESPONSE,
619 &process_management_capability,
620 )
621 .unwrap_or_else(|err| {
622 debug!("Error loading processes!");
623 debug!("{:?}", err);
624 });
625
626 (board_kernel, platform, chip)
627}
628
629#[no_mangle]
631pub unsafe fn main() {
632 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
633
634 let (board_kernel, platform, chip) = start_particle_boron();
635 board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
636}