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