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