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