1#![no_std]
11#![no_main]
12#![deny(missing_docs)]
13
14use core::ptr::addr_of;
15
16use capsules_core::virtualizers::virtual_aes_ccm::MuxAES128CCM;
17use capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm;
18use kernel::component::Component;
19use kernel::deferred_call::DeferredCallClient;
20use kernel::hil::led::LedLow;
21use kernel::hil::symmetric_encryption::AES128;
22use kernel::hil::time::Counter;
23use kernel::platform::{KernelResources, SyscallDriverLookup};
24use kernel::process::ProcessArray;
25use kernel::scheduler::round_robin::RoundRobinSched;
26#[allow(unused_imports)]
27use kernel::{capabilities, create_capability, debug, debug_gpio, debug_verbose, static_init};
28use nrf52840::gpio::Pin;
29use nrf52840::interrupt_service::Nrf52840DefaultPeripherals;
30use nrf52_components::{UartChannel, UartPins};
31
32const LED1_PIN: Pin = Pin::P0_06;
34const LED2_R_PIN: Pin = Pin::P0_08;
35const LED2_G_PIN: Pin = Pin::P1_09;
36const LED2_B_PIN: Pin = Pin::P0_12;
37
38const BUTTON_PIN: Pin = Pin::P1_06;
40const BUTTON_RST_PIN: Pin = Pin::P0_18;
41
42const UART_RTS: Option<Pin> = Some(Pin::P0_13);
43const UART_TXD: Pin = Pin::P0_15;
44const UART_CTS: Option<Pin> = Some(Pin::P0_17);
45const UART_RXD: Pin = Pin::P0_20;
46
47const _SPI_MOSI: Pin = Pin::P1_01;
49const _SPI_MISO: Pin = Pin::P1_02;
50const _SPI_CLK: Pin = Pin::P1_04;
51
52const SRC_MAC: u16 = 0xf00f;
55const PAN_ID: u16 = 0xABCD;
56const DEFAULT_EXT_SRC_MAC: [u8; 8] = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77];
57
58pub mod io;
60
61const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
64 capsules_system::process_policies::PanicFaultPolicy {};
65
66const NUM_PROCS: usize = 8;
68
69type ChipHw = nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>;
70
71static mut PROCESSES: Option<&'static ProcessArray<NUM_PROCS>> = None;
73static mut CHIP: Option<&'static nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>> = None;
75static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
77 None;
78
79kernel::stack_size! {0x1000}
80
81type TemperatureDriver =
82 components::temperature::TemperatureComponentType<nrf52840::temperature::Temp<'static>>;
83type RngDriver = components::rng::RngComponentType<nrf52840::trng::Trng<'static>>;
84
85type Ieee802154Driver = components::ieee802154::Ieee802154ComponentType<
86 nrf52840::ieee802154_radio::Radio<'static>,
87 nrf52840::aes::AesECB<'static>,
88>;
89
90pub struct Platform {
92 ble_radio: &'static capsules_extra::ble_advertising_driver::BLE<
93 'static,
94 nrf52840::ble_radio::Radio<'static>,
95 VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>,
96 >,
97 ieee802154_radio: &'static Ieee802154Driver,
98 button: &'static capsules_core::button::Button<'static, nrf52840::gpio::GPIOPin<'static>>,
99 pconsole: &'static capsules_core::process_console::ProcessConsole<
100 'static,
101 { capsules_core::process_console::DEFAULT_COMMAND_HISTORY_LEN },
102 VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>,
103 components::process_console::Capability,
104 >,
105 console: &'static capsules_core::console::Console<'static>,
106 gpio: &'static capsules_core::gpio::GPIO<'static, nrf52840::gpio::GPIOPin<'static>>,
107 led: &'static capsules_core::led::LedDriver<
108 'static,
109 LedLow<'static, nrf52840::gpio::GPIOPin<'static>>,
110 4,
111 >,
112 rng: &'static RngDriver,
113 temp: &'static TemperatureDriver,
114 ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
115 analog_comparator: &'static capsules_extra::analog_comparator::AnalogComparator<
116 'static,
117 nrf52840::acomp::Comparator<'static>,
118 >,
119 alarm: &'static capsules_core::alarm::AlarmDriver<
120 'static,
121 capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<
122 'static,
123 nrf52840::rtc::Rtc<'static>,
124 >,
125 >,
126 scheduler: &'static RoundRobinSched<'static>,
127 systick: cortexm4::systick::SysTick,
128}
129
130impl SyscallDriverLookup for Platform {
131 fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
132 where
133 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
134 {
135 match driver_num {
136 capsules_core::console::DRIVER_NUM => f(Some(self.console)),
137 capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
138 capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
139 capsules_core::led::DRIVER_NUM => f(Some(self.led)),
140 capsules_core::button::DRIVER_NUM => f(Some(self.button)),
141 capsules_core::rng::DRIVER_NUM => f(Some(self.rng)),
142 capsules_extra::ble_advertising_driver::DRIVER_NUM => f(Some(self.ble_radio)),
143 capsules_extra::ieee802154::DRIVER_NUM => f(Some(self.ieee802154_radio)),
144 capsules_extra::temperature::DRIVER_NUM => f(Some(self.temp)),
145 capsules_extra::analog_comparator::DRIVER_NUM => f(Some(self.analog_comparator)),
146 kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
147 _ => f(None),
148 }
149 }
150}
151
152impl KernelResources<nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>>
153 for Platform
154{
155 type SyscallDriverLookup = Self;
156 type SyscallFilter = ();
157 type ProcessFault = ();
158 type Scheduler = RoundRobinSched<'static>;
159 type SchedulerTimer = cortexm4::systick::SysTick;
160 type WatchDog = ();
161 type ContextSwitchCallback = ();
162
163 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
164 self
165 }
166 fn syscall_filter(&self) -> &Self::SyscallFilter {
167 &()
168 }
169 fn process_fault(&self) -> &Self::ProcessFault {
170 &()
171 }
172 fn scheduler(&self) -> &Self::Scheduler {
173 self.scheduler
174 }
175 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
176 &self.systick
177 }
178 fn watchdog(&self) -> &Self::WatchDog {
179 &()
180 }
181 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
182 &()
183 }
184}
185
186#[inline(never)]
190pub unsafe fn start() -> (
191 &'static kernel::Kernel,
192 Platform,
193 &'static nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>,
194) {
195 nrf52840::init();
196
197 kernel::deferred_call::initialize_deferred_call_state::<
199 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
200 >();
201
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.init();
214 let base_peripherals = &nrf52840_peripherals.nrf52;
215
216 let processes = components::process_array::ProcessArrayComponent::new()
218 .finalize(components::process_array_component_static!(NUM_PROCS));
219 PROCESSES = Some(processes);
220
221 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
223
224 let gpio = components::gpio::GpioComponent::new(
226 board_kernel,
227 capsules_core::gpio::DRIVER_NUM,
228 components::gpio_component_helper!(
229 nrf52840::gpio::GPIOPin,
230 0 => &nrf52840_peripherals.gpio_port[Pin::P0_13],
232 1 => &nrf52840_peripherals.gpio_port[Pin::P0_15],
233 2 => &nrf52840_peripherals.gpio_port[Pin::P0_17],
234 3 => &nrf52840_peripherals.gpio_port[Pin::P0_20],
235 4 => &nrf52840_peripherals.gpio_port[Pin::P0_22],
236 5 => &nrf52840_peripherals.gpio_port[Pin::P0_24],
237 6 => &nrf52840_peripherals.gpio_port[Pin::P1_00],
238 7 => &nrf52840_peripherals.gpio_port[Pin::P0_09],
239 8 => &nrf52840_peripherals.gpio_port[Pin::P0_10],
240 9 => &nrf52840_peripherals.gpio_port[Pin::P0_31],
242 10 => &nrf52840_peripherals.gpio_port[Pin::P0_29],
243 11 => &nrf52840_peripherals.gpio_port[Pin::P0_02],
244 12 => &nrf52840_peripherals.gpio_port[Pin::P1_15],
245 13 => &nrf52840_peripherals.gpio_port[Pin::P1_13],
246 14 => &nrf52840_peripherals.gpio_port[Pin::P1_10],
247 15 => &nrf52840_peripherals.gpio_port[Pin::P0_26],
249 16 => &nrf52840_peripherals.gpio_port[Pin::P0_04],
250 17 => &nrf52840_peripherals.gpio_port[Pin::P0_11],
251 18 => &nrf52840_peripherals.gpio_port[Pin::P0_14],
252 19 => &nrf52840_peripherals.gpio_port[Pin::P1_11],
253 20 => &nrf52840_peripherals.gpio_port[Pin::P1_07],
254 21 => &nrf52840_peripherals.gpio_port[Pin::P1_01],
255 22 => &nrf52840_peripherals.gpio_port[Pin::P1_04],
256 23 => &nrf52840_peripherals.gpio_port[Pin::P1_02]
257 ),
258 )
259 .finalize(components::gpio_component_static!(nrf52840::gpio::GPIOPin));
260
261 let button = components::button::ButtonComponent::new(
262 board_kernel,
263 capsules_core::button::DRIVER_NUM,
264 components::button_component_helper!(
265 nrf52840::gpio::GPIOPin,
266 (
267 &nrf52840_peripherals.gpio_port[BUTTON_PIN],
268 kernel::hil::gpio::ActivationMode::ActiveLow,
269 kernel::hil::gpio::FloatingState::PullUp
270 )
271 ),
272 )
273 .finalize(components::button_component_static!(
274 nrf52840::gpio::GPIOPin
275 ));
276
277 let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
278 LedLow<'static, nrf52840::gpio::GPIOPin>,
279 LedLow::new(&nrf52840_peripherals.gpio_port[LED1_PIN]),
280 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_R_PIN]),
281 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_G_PIN]),
282 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_B_PIN]),
283 ));
284
285 let chip = static_init!(
286 nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
287 nrf52840::chip::NRF52::new(nrf52840_peripherals)
288 );
289 CHIP = Some(chip);
290
291 nrf52_components::startup::NrfStartupComponent::new(
292 false,
293 BUTTON_RST_PIN,
294 nrf52840::uicr::Regulator0Output::V3_0,
295 &base_peripherals.nvmc,
296 )
297 .finalize(());
298
299 let process_management_capability =
302 create_capability!(capabilities::ProcessManagementCapability);
303 let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
304
305 let gpio_port = &nrf52840_peripherals.gpio_port;
306
307 let debug_gpios = static_init!(
309 [&'static dyn kernel::hil::gpio::Pin; 3],
310 [
311 &gpio_port[LED2_R_PIN],
312 &gpio_port[LED2_G_PIN],
313 &gpio_port[LED2_B_PIN]
314 ]
315 );
316 kernel::debug::initialize_debug_gpio::<
317 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
318 >();
319 kernel::debug::assign_gpios(debug_gpios);
320
321 let rtc = &base_peripherals.rtc;
322 let _ = rtc.start();
323 let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
324 .finalize(components::alarm_mux_component_static!(nrf52840::rtc::Rtc));
325 let alarm = components::alarm::AlarmDriverComponent::new(
326 board_kernel,
327 capsules_core::alarm::DRIVER_NUM,
328 mux_alarm,
329 )
330 .finalize(components::alarm_component_static!(nrf52840::rtc::Rtc));
331 let uart_channel = UartChannel::Pins(UartPins::new(UART_RTS, UART_TXD, UART_CTS, UART_RXD));
332 let channel = nrf52_components::UartChannelComponent::new(
333 uart_channel,
334 mux_alarm,
335 &base_peripherals.uarte0,
336 )
337 .finalize(nrf52_components::uart_channel_component_static!(
338 nrf52840::rtc::Rtc
339 ));
340
341 let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
342 .finalize(components::process_printer_text_component_static!());
343 PROCESS_PRINTER = Some(process_printer);
344
345 let uart_mux = components::console::UartMuxComponent::new(channel, 115200)
347 .finalize(components::uart_mux_component_static!());
348
349 let pconsole = components::process_console::ProcessConsoleComponent::new(
350 board_kernel,
351 uart_mux,
352 mux_alarm,
353 process_printer,
354 Some(cortexm4::support::reset),
355 )
356 .finalize(components::process_console_component_static!(
357 nrf52840::rtc::Rtc<'static>
358 ));
359
360 let console = components::console::ConsoleComponent::new(
362 board_kernel,
363 capsules_core::console::DRIVER_NUM,
364 uart_mux,
365 )
366 .finalize(components::console_component_static!());
367 components::debug_writer::DebugWriterComponent::new::<
369 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
370 >(
371 uart_mux,
372 create_capability!(capabilities::SetDebugWriterCapability),
373 )
374 .finalize(components::debug_writer_component_static!());
375
376 let ble_radio = components::ble::BLEComponent::new(
377 board_kernel,
378 capsules_extra::ble_advertising_driver::DRIVER_NUM,
379 &base_peripherals.ble_radio,
380 mux_alarm,
381 )
382 .finalize(components::ble_component_static!(
383 nrf52840::rtc::Rtc,
384 nrf52840::ble_radio::Radio
385 ));
386
387 let aes_mux = static_init!(
388 MuxAES128CCM<'static, nrf52840::aes::AesECB>,
389 MuxAES128CCM::new(&base_peripherals.ecb,)
390 );
391 aes_mux.register();
392 base_peripherals.ecb.set_client(aes_mux);
393
394 let (ieee802154_radio, _mux_mac) = components::ieee802154::Ieee802154Component::new(
395 board_kernel,
396 capsules_extra::ieee802154::DRIVER_NUM,
397 &nrf52840_peripherals.ieee802154_radio,
398 aes_mux,
399 PAN_ID,
400 SRC_MAC,
401 DEFAULT_EXT_SRC_MAC,
402 )
403 .finalize(components::ieee802154_component_static!(
404 nrf52840::ieee802154_radio::Radio,
405 nrf52840::aes::AesECB<'static>
406 ));
407
408 let temp = components::temperature::TemperatureComponent::new(
409 board_kernel,
410 capsules_extra::temperature::DRIVER_NUM,
411 &base_peripherals.temp,
412 )
413 .finalize(components::temperature_component_static!(
414 nrf52840::temperature::Temp
415 ));
416
417 let rng = components::rng::RngComponent::new(
418 board_kernel,
419 capsules_core::rng::DRIVER_NUM,
420 &base_peripherals.trng,
421 )
422 .finalize(components::rng_component_static!(nrf52840::trng::Trng));
423
424 let analog_comparator_channel = static_init!(
427 nrf52840::acomp::Channel,
428 nrf52840::acomp::Channel::new(nrf52840::acomp::ChannelNumber::AC0)
429 );
430 let analog_comparator = components::analog_comparator::AnalogComparatorComponent::new(
431 &base_peripherals.acomp,
432 components::analog_comparator_component_helper!(
433 nrf52840::acomp::Channel,
434 analog_comparator_channel,
435 ),
436 board_kernel,
437 capsules_extra::analog_comparator::DRIVER_NUM,
438 )
439 .finalize(components::analog_comparator_component_static!(
440 nrf52840::acomp::Comparator
441 ));
442
443 nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
444
445 let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
446 .finalize(components::round_robin_component_static!(NUM_PROCS));
447
448 let platform = Platform {
449 button,
450 ble_radio,
451 ieee802154_radio,
452 pconsole,
453 console,
454 led,
455 gpio,
456 rng,
457 temp,
458 alarm,
459 analog_comparator,
460 ipc: kernel::ipc::IPC::new(
461 board_kernel,
462 kernel::ipc::DRIVER_NUM,
463 &memory_allocation_capability,
464 ),
465 scheduler,
466 systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
467 };
468
469 let _ = platform.pconsole.start();
470 debug!("Initialization complete. Entering main loop\r");
471 debug!("{}", &*addr_of!(nrf52840::ficr::FICR_INSTANCE));
472
473 extern "C" {
475 static _sapps: u8;
477 static _eapps: u8;
479 static mut _sappmem: u8;
481 static _eappmem: u8;
483 }
484
485 kernel::process::load_processes(
486 board_kernel,
487 chip,
488 core::slice::from_raw_parts(
489 core::ptr::addr_of!(_sapps),
490 core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
491 ),
492 core::slice::from_raw_parts_mut(
493 core::ptr::addr_of_mut!(_sappmem),
494 core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
495 ),
496 &FAULT_RESPONSE,
497 &process_management_capability,
498 )
499 .unwrap_or_else(|err| {
500 debug!("Error loading processes!");
501 debug!("{:?}", err);
502 });
503
504 (board_kernel, platform, chip)
505}
506
507#[no_mangle]
509pub unsafe fn main() {
510 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
511
512 let (board_kernel, platform, chip) = start();
513 board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
514}