1#![no_std]
14#![no_main]
15#![deny(missing_docs)]
16
17use core::ptr::{addr_of, addr_of_mut};
18
19use capsules_core::virtualizers::virtual_aes_ccm::MuxAES128CCM;
20use capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm;
21use capsules_core::virtualizers::virtual_spi::VirtualSpiMasterDevice;
22use kernel::component::Component;
23use kernel::deferred_call::DeferredCallClient;
24use kernel::hil::i2c::I2CMaster;
25use kernel::hil::led::LedHigh;
26use kernel::hil::screen::Screen;
27use kernel::hil::symmetric_encryption::AES128;
28use kernel::hil::time::Counter;
29use kernel::platform::{KernelResources, SyscallDriverLookup};
30use kernel::scheduler::round_robin::RoundRobinSched;
31#[allow(unused_imports)]
32use kernel::{capabilities, create_capability, debug, debug_gpio, debug_verbose, static_init};
33use nrf52840::gpio::Pin;
34use nrf52840::interrupt_service::Nrf52840DefaultPeripherals;
35
36const LED1_PIN: Pin = Pin::P0_08;
38
39const VIBRA1_PIN: Pin = Pin::P0_19;
41
42const BUTTON_PIN: Pin = Pin::P0_17;
44
45const I2C_TEMP_SDA_PIN: Pin = Pin::P1_15;
47const I2C_TEMP_SCL_PIN: Pin = Pin::P0_02;
48
49const SRC_MAC: u16 = 0xf00f;
52const PAN_ID: u16 = 0xABCD;
53const DEFAULT_EXT_SRC_MAC: [u8; 8] = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77];
54
55pub mod io;
57
58const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
61 capsules_system::process_policies::PanicFaultPolicy {};
62
63const NUM_PROCS: usize = 8;
65
66static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
67 [None; NUM_PROCS];
68
69static mut CHIP: Option<&'static nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>> = None;
71static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
73 None;
74
75#[no_mangle]
77#[link_section = ".stack_buffer"]
78pub static mut STACK_MEMORY: [u8; 0x1000] = [0; 0x1000];
79
80type Bmp280Sensor = components::bmp280::Bmp280ComponentType<
81 VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>,
82 capsules_core::virtualizers::virtual_i2c::I2CDevice<'static, nrf52840::i2c::TWI<'static>>,
83>;
84type TemperatureDriver = components::temperature::TemperatureComponentType<Bmp280Sensor>;
85type RngDriver = components::rng::RngComponentType<nrf52840::trng::Trng<'static>>;
86
87type Ieee802154Driver = components::ieee802154::Ieee802154ComponentType<
88 nrf52840::ieee802154_radio::Radio<'static>,
89 nrf52840::aes::AesECB<'static>,
90>;
91
92pub struct Platform {
94 temperature: &'static TemperatureDriver,
95 ble_radio: &'static capsules_extra::ble_advertising_driver::BLE<
96 'static,
97 nrf52840::ble_radio::Radio<'static>,
98 VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>,
99 >,
100 ieee802154_radio: &'static Ieee802154Driver,
101 button: &'static capsules_core::button::Button<'static, nrf52840::gpio::GPIOPin<'static>>,
102 pconsole: &'static capsules_core::process_console::ProcessConsole<
103 'static,
104 { capsules_core::process_console::DEFAULT_COMMAND_HISTORY_LEN },
105 VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>,
106 components::process_console::Capability,
107 >,
108 console: &'static capsules_core::console::Console<'static>,
109 gpio: &'static capsules_core::gpio::GPIO<'static, nrf52840::gpio::GPIOPin<'static>>,
110 led: &'static capsules_core::led::LedDriver<
111 'static,
112 LedHigh<'static, nrf52840::gpio::GPIOPin<'static>>,
113 2,
114 >,
115 rng: &'static RngDriver,
116 ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
117 analog_comparator: &'static capsules_extra::analog_comparator::AnalogComparator<
118 'static,
119 nrf52840::acomp::Comparator<'static>,
120 >,
121 alarm: &'static capsules_core::alarm::AlarmDriver<
122 'static,
123 capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<
124 'static,
125 nrf52840::rtc::Rtc<'static>,
126 >,
127 >,
128 screen: &'static capsules_extra::screen::Screen<'static>,
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::rng::DRIVER_NUM => f(Some(self.rng)),
145 capsules_extra::ble_advertising_driver::DRIVER_NUM => f(Some(self.ble_radio)),
146 capsules_extra::ieee802154::DRIVER_NUM => f(Some(self.ieee802154_radio)),
147 capsules_extra::temperature::DRIVER_NUM => f(Some(self.temperature)),
148 capsules_extra::analog_comparator::DRIVER_NUM => f(Some(self.analog_comparator)),
149 capsules_extra::screen::DRIVER_NUM => f(Some(self.screen)),
150 kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
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)]
194pub unsafe fn start() -> (
195 &'static kernel::Kernel,
196 Platform,
197 &'static nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>,
198) {
199 nrf52840::init();
200
201 let ieee802154_ack_buf = static_init!(
202 [u8; nrf52840::ieee802154_radio::ACK_BUF_SIZE],
203 [0; nrf52840::ieee802154_radio::ACK_BUF_SIZE]
204 );
205 let nrf52840_peripherals = static_init!(
207 Nrf52840DefaultPeripherals,
208 Nrf52840DefaultPeripherals::new(ieee802154_ack_buf)
209 );
210
211 nrf52840_peripherals.init();
213 let base_peripherals = &nrf52840_peripherals.nrf52;
214
215 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&*addr_of!(PROCESSES)));
216
217 let gpio = components::gpio::GpioComponent::new(
219 board_kernel,
220 capsules_core::gpio::DRIVER_NUM,
221 components::gpio_component_helper!(
222 nrf52840::gpio::GPIOPin,
223 0 => &nrf52840_peripherals.gpio_port[Pin::P0_29],
224 ),
225 )
226 .finalize(components::gpio_component_static!(nrf52840::gpio::GPIOPin));
227
228 let button = components::button::ButtonComponent::new(
229 board_kernel,
230 capsules_core::button::DRIVER_NUM,
231 components::button_component_helper!(
232 nrf52840::gpio::GPIOPin,
233 (
234 &nrf52840_peripherals.gpio_port[BUTTON_PIN],
235 kernel::hil::gpio::ActivationMode::ActiveLow,
236 kernel::hil::gpio::FloatingState::PullUp
237 )
238 ),
239 )
240 .finalize(components::button_component_static!(
241 nrf52840::gpio::GPIOPin
242 ));
243
244 let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
245 LedHigh<'static, nrf52840::gpio::GPIOPin>,
246 LedHigh::new(&nrf52840_peripherals.gpio_port[LED1_PIN]),
247 LedHigh::new(&nrf52840_peripherals.gpio_port[VIBRA1_PIN]),
248 ));
249
250 let chip = static_init!(
251 nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
252 nrf52840::chip::NRF52::new(nrf52840_peripherals)
253 );
254 CHIP = Some(chip);
255
256 nrf52_components::startup::NrfStartupComponent::new(
257 false,
258 BUTTON_PIN,
262 nrf52840::uicr::Regulator0Output::V3_0,
263 &base_peripherals.nvmc,
264 )
265 .finalize(());
266
267 let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
271
272 let gpio_port = &nrf52840_peripherals.gpio_port;
273
274 kernel::debug::assign_gpios(Some(&gpio_port[LED1_PIN]), None, None);
276
277 let rtc = &base_peripherals.rtc;
278 let _ = rtc.start();
279 let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
280 .finalize(components::alarm_mux_component_static!(nrf52840::rtc::Rtc));
281 let alarm = components::alarm::AlarmDriverComponent::new(
282 board_kernel,
283 capsules_core::alarm::DRIVER_NUM,
284 mux_alarm,
285 )
286 .finalize(components::alarm_component_static!(nrf52840::rtc::Rtc));
287
288 let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
289 .finalize(components::process_printer_text_component_static!());
290 PROCESS_PRINTER = Some(process_printer);
291
292 let uart_channel = {
294 let rtt_memory = components::segger_rtt::SeggerRttMemoryComponent::new()
296 .finalize(components::segger_rtt_memory_component_static!());
297
298 self::io::set_rtt_memory(&*core::ptr::from_mut(rtt_memory.rtt_memory));
302
303 components::segger_rtt::SeggerRttComponent::new(mux_alarm, rtt_memory)
304 .finalize(components::segger_rtt_component_static!(nrf52840::rtc::Rtc))
305 };
306
307 let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
309 .finalize(components::uart_mux_component_static!());
310
311 let pconsole = components::process_console::ProcessConsoleComponent::new(
312 board_kernel,
313 uart_mux,
314 mux_alarm,
315 process_printer,
316 Some(cortexm4::support::reset),
317 )
318 .finalize(components::process_console_component_static!(
319 nrf52840::rtc::Rtc<'static>
320 ));
321
322 let console = components::console::ConsoleComponent::new(
324 board_kernel,
325 capsules_core::console::DRIVER_NUM,
326 uart_mux,
327 )
328 .finalize(components::console_component_static!());
329 components::debug_writer::DebugWriterComponent::new(
331 uart_mux,
332 create_capability!(capabilities::SetDebugWriterCapability),
333 )
334 .finalize(components::debug_writer_component_static!());
335
336 let ble_radio = components::ble::BLEComponent::new(
337 board_kernel,
338 capsules_extra::ble_advertising_driver::DRIVER_NUM,
339 &base_peripherals.ble_radio,
340 mux_alarm,
341 )
342 .finalize(components::ble_component_static!(
343 nrf52840::rtc::Rtc,
344 nrf52840::ble_radio::Radio
345 ));
346
347 let aes_mux = static_init!(
348 MuxAES128CCM<'static, nrf52840::aes::AesECB>,
349 MuxAES128CCM::new(&base_peripherals.ecb,)
350 );
351 base_peripherals.ecb.set_client(aes_mux);
352 aes_mux.register();
353
354 let (ieee802154_radio, _mux_mac) = components::ieee802154::Ieee802154Component::new(
355 board_kernel,
356 capsules_extra::ieee802154::DRIVER_NUM,
357 &nrf52840_peripherals.ieee802154_radio,
358 aes_mux,
359 PAN_ID,
360 SRC_MAC,
361 DEFAULT_EXT_SRC_MAC,
362 )
363 .finalize(components::ieee802154_component_static!(
364 nrf52840::ieee802154_radio::Radio,
365 nrf52840::aes::AesECB<'static>
366 ));
367
368 let _temp = components::temperature::TemperatureComponent::new(
371 board_kernel,
372 capsules_extra::temperature::DRIVER_NUM,
373 &base_peripherals.temp,
374 )
375 .finalize(components::temperature_component_static!(
376 nrf52840::temperature::Temp
377 ));
378
379 let sensors_i2c_bus = static_init!(
380 capsules_core::virtualizers::virtual_i2c::MuxI2C<'static, nrf52840::i2c::TWI>,
381 capsules_core::virtualizers::virtual_i2c::MuxI2C::new(&base_peripherals.twi1, None,)
382 );
383 sensors_i2c_bus.register();
384
385 base_peripherals.twi1.configure(
386 nrf52840::pinmux::Pinmux::new(I2C_TEMP_SCL_PIN as u32),
387 nrf52840::pinmux::Pinmux::new(I2C_TEMP_SDA_PIN as u32),
388 );
389 base_peripherals.twi1.set_master_client(sensors_i2c_bus);
390
391 let bmp280 = components::bmp280::Bmp280Component::new(
392 sensors_i2c_bus,
393 capsules_extra::bmp280::BASE_ADDR,
394 mux_alarm,
395 )
396 .finalize(components::bmp280_component_static!(
397 nrf52840::rtc::Rtc<'static>,
398 nrf52840::i2c::TWI
399 ));
400
401 let temperature = components::temperature::TemperatureComponent::new(
402 board_kernel,
403 capsules_extra::temperature::DRIVER_NUM,
404 bmp280,
405 )
406 .finalize(components::temperature_component_static!(Bmp280Sensor));
407
408 let rng = components::rng::RngComponent::new(
409 board_kernel,
410 capsules_core::rng::DRIVER_NUM,
411 &base_peripherals.trng,
412 )
413 .finalize(components::rng_component_static!(nrf52840::trng::Trng));
414
415 let analog_comparator = components::analog_comparator::AnalogComparatorComponent::new(
418 &base_peripherals.acomp,
419 components::analog_comparator_component_helper!(
420 nrf52840::acomp::Channel,
421 &*addr_of!(nrf52840::acomp::CHANNEL_AC0)
422 ),
423 board_kernel,
424 capsules_extra::analog_comparator::DRIVER_NUM,
425 )
426 .finalize(components::analog_comparator_component_static!(
427 nrf52840::acomp::Comparator
428 ));
429
430 nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
431
432 let scheduler = components::sched::round_robin::RoundRobinComponent::new(&*addr_of!(PROCESSES))
433 .finalize(components::round_robin_component_static!(NUM_PROCS));
434
435 let periodic_virtual_alarm = static_init!(
436 capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, nrf52840::rtc::Rtc>,
437 capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm::new(mux_alarm)
438 );
439 periodic_virtual_alarm.setup();
440
441 let screen = {
442 let mux_spi = components::spi::SpiMuxComponent::new(&base_peripherals.spim2)
443 .finalize(components::spi_mux_component_static!(nrf52840::spi::SPIM));
444
445 use kernel::hil::spi::SpiMaster;
446 base_peripherals
447 .spim2
448 .set_rate(1_000_000)
449 .expect("SPIM2 set rate");
450
451 base_peripherals.spim2.configure(
452 nrf52840::pinmux::Pinmux::new(Pin::P0_27 as u32),
453 nrf52840::pinmux::Pinmux::new(Pin::P0_28 as u32),
454 nrf52840::pinmux::Pinmux::new(Pin::P0_26 as u32),
455 );
456
457 let disp_pin = &nrf52840_peripherals.gpio_port[Pin::P0_07];
458 let cs_pin = &nrf52840_peripherals.gpio_port[Pin::P0_05];
459
460 let display = components::lpm013m126::Lpm013m126Component::new(
461 mux_spi,
462 cs_pin,
463 disp_pin,
464 &nrf52840_peripherals.gpio_port[Pin::P0_06],
465 mux_alarm,
466 )
467 .finalize(components::lpm013m126_component_static!(
468 nrf52840::rtc::Rtc<'static>,
469 nrf52840::gpio::GPIOPin,
470 nrf52840::spi::SPIM
471 ));
472
473 let screen = components::screen::ScreenComponent::new(
474 board_kernel,
475 capsules_extra::screen::DRIVER_NUM,
476 display,
477 None,
478 )
479 .finalize(components::screen_component_static!(4096));
480 let _ = display.set_power(true);
482 screen
483 };
484
485 let platform = Platform {
486 temperature,
487 button,
488 ble_radio,
489 ieee802154_radio,
490 pconsole,
491 console,
492 led,
493 gpio,
494 rng,
495 alarm,
496 analog_comparator,
497 screen,
498 ipc: kernel::ipc::IPC::new(
499 board_kernel,
500 kernel::ipc::DRIVER_NUM,
501 &memory_allocation_capability,
502 ),
503 scheduler,
504 systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
505 };
506
507 fn load_processes(
518 board_kernel: &'static kernel::Kernel,
519 chip: &'static nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>,
520 ) {
521 let process_management_capability =
522 create_capability!(capabilities::ProcessManagementCapability);
523 unsafe {
524 kernel::process::load_processes(
525 board_kernel,
526 chip,
527 core::slice::from_raw_parts(
528 core::ptr::addr_of!(_sapps),
529 core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
530 ),
531 core::slice::from_raw_parts_mut(
532 core::ptr::addr_of_mut!(_sappmem),
533 core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
534 ),
535 &mut *addr_of_mut!(PROCESSES),
536 &FAULT_RESPONSE,
537 &process_management_capability,
538 )
539 .unwrap_or_else(|err| {
540 debug!("Error loading processes!");
541 debug!("{:?}", err);
542 });
543 }
544 }
545
546 let _ = platform.pconsole.start();
547 debug!("Initialization complete. Entering main loop\r");
548 debug!("{}", &*addr_of!(nrf52840::ficr::FICR_INSTANCE));
549
550 load_processes(board_kernel, chip);
551 extern "C" {
553 static _sapps: u8;
555 static _eapps: u8;
557 static mut _sappmem: u8;
559 static _eappmem: u8;
561 }
562
563 (board_kernel, platform, chip)
564}
565
566#[no_mangle]
568pub unsafe fn main() {
569 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
570
571 let (board_kernel, platform, chip) = start();
572 board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
573}