1#![no_std]
8#![no_main]
9#![deny(missing_docs)]
10
11use core::ptr::{addr_of, addr_of_mut};
12
13use kernel::component::Component;
14use kernel::hil::led::LedLow;
15use kernel::hil::time::Counter;
16use kernel::platform::{KernelResources, SyscallDriverLookup};
17use kernel::process::ProcessLoadingAsync;
18use kernel::scheduler::round_robin::RoundRobinSched;
19use kernel::{capabilities, create_capability, static_init};
20use nrf52840::gpio::Pin;
21use nrf52840::interrupt_service::Nrf52840DefaultPeripherals;
22use nrf52_components::{UartChannel, UartPins};
23
24const LED1_PIN: Pin = Pin::P0_13;
26const LED2_PIN: Pin = Pin::P0_14;
27const LED3_PIN: Pin = Pin::P0_15;
28const LED4_PIN: Pin = Pin::P0_16;
29
30const BUTTON1_PIN: Pin = Pin::P0_11;
32const BUTTON2_PIN: Pin = Pin::P0_12;
33const BUTTON3_PIN: Pin = Pin::P0_24;
34const BUTTON4_PIN: Pin = Pin::P0_25;
35const BUTTON_RST_PIN: Pin = Pin::P0_18;
36
37const UART_RTS: Option<Pin> = Some(Pin::P0_05);
38const UART_TXD: Pin = Pin::P0_06;
39const UART_CTS: Option<Pin> = Some(Pin::P0_07);
40const UART_RXD: Pin = Pin::P0_08;
41
42pub mod io;
44
45const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
48 capsules_system::process_policies::PanicFaultPolicy {};
49
50const NUM_PROCS: usize = 8;
52
53static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
54 [None; NUM_PROCS];
55
56static mut CHIP: Option<&'static nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>> = None;
57static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
59 None;
60
61#[no_mangle]
63#[link_section = ".stack_buffer"]
64pub static mut STACK_MEMORY: [u8; 0x2000] = [0; 0x2000];
65
66type AlarmDriver = components::alarm::AlarmDriverComponentType<nrf52840::rtc::Rtc<'static>>;
71
72type NonVolatilePages = components::dynamic_binary_storage::NVPages<nrf52840::nvmc::Nvmc>;
73type DynamicBinaryStorage<'a> = kernel::dynamic_binary_storage::SequentialDynamicBinaryStorage<
74 'static,
75 'static,
76 nrf52840::chip::NRF52<'a, Nrf52840DefaultPeripherals<'a>>,
77 kernel::process::ProcessStandardDebugFull,
78 NonVolatilePages,
79>;
80
81pub struct Platform {
83 console: &'static capsules_core::console::Console<'static>,
84 button: &'static capsules_core::button::Button<'static, nrf52840::gpio::GPIOPin<'static>>,
85 adc: &'static capsules_core::adc::AdcDedicated<'static, nrf52840::adc::Adc<'static>>,
86 led: &'static capsules_core::led::LedDriver<
87 'static,
88 kernel::hil::led::LedLow<'static, nrf52840::gpio::GPIOPin<'static>>,
89 4,
90 >,
91 alarm: &'static AlarmDriver,
92 scheduler: &'static RoundRobinSched<'static>,
93 systick: cortexm4::systick::SysTick,
94 processes: &'static [Option<&'static dyn kernel::process::Process>],
95 dynamic_app_loader: &'static capsules_extra::app_loader::AppLoader<
96 DynamicBinaryStorage<'static>,
97 DynamicBinaryStorage<'static>,
98 >,
99}
100
101impl SyscallDriverLookup for Platform {
102 fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
103 where
104 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
105 {
106 match driver_num {
107 capsules_core::console::DRIVER_NUM => f(Some(self.console)),
108 capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
109 capsules_core::led::DRIVER_NUM => f(Some(self.led)),
110 capsules_core::button::DRIVER_NUM => f(Some(self.button)),
111 capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
112 capsules_extra::app_loader::DRIVER_NUM => f(Some(self.dynamic_app_loader)),
113 _ => f(None),
114 }
115 }
116}
117
118#[inline(never)]
122unsafe fn create_peripherals() -> &'static mut Nrf52840DefaultPeripherals<'static> {
123 let ieee802154_ack_buf = static_init!(
124 [u8; nrf52840::ieee802154_radio::ACK_BUF_SIZE],
125 [0; nrf52840::ieee802154_radio::ACK_BUF_SIZE]
126 );
127 let nrf52840_peripherals = static_init!(
129 Nrf52840DefaultPeripherals,
130 Nrf52840DefaultPeripherals::new(ieee802154_ack_buf)
131 );
132
133 nrf52840_peripherals
134}
135
136impl KernelResources<nrf52840::chip::NRF52<'static, Nrf52840DefaultPeripherals<'static>>>
137 for Platform
138{
139 type SyscallDriverLookup = Self;
140 type SyscallFilter = ();
141 type ProcessFault = ();
142 type Scheduler = RoundRobinSched<'static>;
143 type SchedulerTimer = cortexm4::systick::SysTick;
144 type WatchDog = ();
145 type ContextSwitchCallback = ();
146
147 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
148 self
149 }
150 fn syscall_filter(&self) -> &Self::SyscallFilter {
151 &()
152 }
153 fn process_fault(&self) -> &Self::ProcessFault {
154 &()
155 }
156 fn scheduler(&self) -> &Self::Scheduler {
157 self.scheduler
158 }
159 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
160 &self.systick
161 }
162 fn watchdog(&self) -> &Self::WatchDog {
163 &()
164 }
165 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
166 &()
167 }
168}
169
170impl kernel::process::ProcessLoadingAsyncClient for Platform {
171 fn process_loaded(&self, _result: Result<(), kernel::process::ProcessLoadError>) {}
172
173 fn process_loading_finished(&self) {
174 kernel::debug!("Processes Loaded at Main:");
175
176 for (i, proc) in self.processes.iter().enumerate() {
177 proc.map(|p| {
178 kernel::debug!("[{}] {}", i, p.get_process_name());
179 kernel::debug!(" ShortId: {}", p.short_app_id());
180 });
181 }
182 }
183}
184
185#[no_mangle]
187pub unsafe fn main() {
188 nrf52840::init();
194
195 let nrf52840_peripherals = create_peripherals();
198
199 nrf52840_peripherals.init();
201 let base_peripherals = &nrf52840_peripherals.nrf52;
202
203 let processes = &*addr_of!(PROCESSES);
204
205 let uart_channel = UartChannel::Pins(UartPins::new(UART_RTS, UART_TXD, UART_CTS, UART_RXD));
209
210 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes));
212
213 let chip = static_init!(
216 nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
217 nrf52840::chip::NRF52::new(nrf52840_peripherals)
218 );
219 CHIP = Some(chip);
220
221 nrf52_components::startup::NrfStartupComponent::new(
224 false,
225 BUTTON_RST_PIN,
226 nrf52840::uicr::Regulator0Output::DEFAULT,
227 &base_peripherals.nvmc,
228 )
229 .finalize(());
230
231 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
238
239 let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
244 LedLow<'static, nrf52840::gpio::GPIOPin>,
245 LedLow::new(&nrf52840_peripherals.gpio_port[LED1_PIN]),
246 LedLow::new(&nrf52840_peripherals.gpio_port[LED2_PIN]),
247 LedLow::new(&nrf52840_peripherals.gpio_port[LED3_PIN]),
248 LedLow::new(&nrf52840_peripherals.gpio_port[LED4_PIN]),
249 ));
250
251 let rtc = &base_peripherals.rtc;
256 let _ = rtc.start();
257 let mux_alarm = components::alarm::AlarmMuxComponent::new(rtc)
258 .finalize(components::alarm_mux_component_static!(nrf52840::rtc::Rtc));
259 let alarm = components::alarm::AlarmDriverComponent::new(
260 board_kernel,
261 capsules_core::alarm::DRIVER_NUM,
262 mux_alarm,
263 )
264 .finalize(components::alarm_component_static!(nrf52840::rtc::Rtc));
265
266 let uart_channel = nrf52_components::UartChannelComponent::new(
271 uart_channel,
272 mux_alarm,
273 &base_peripherals.uarte0,
274 )
275 .finalize(nrf52_components::uart_channel_component_static!(
276 nrf52840::rtc::Rtc
277 ));
278
279 let uart_mux = components::console::UartMuxComponent::new(uart_channel, 115200)
281 .finalize(components::uart_mux_component_static!());
282
283 let console = components::console::ConsoleComponent::new(
285 board_kernel,
286 capsules_core::console::DRIVER_NUM,
287 uart_mux,
288 )
289 .finalize(components::console_component_static!());
290
291 let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
293 .finalize(components::process_printer_text_component_static!());
294 PROCESS_PRINTER = Some(process_printer);
295
296 let pconsole = components::process_console::ProcessConsoleComponent::new(
299 board_kernel,
300 uart_mux,
301 mux_alarm,
302 process_printer,
303 Some(cortexm4::support::reset),
304 )
305 .finalize(components::process_console_component_static!(
306 nrf52840::rtc::Rtc<'static>
307 ));
308
309 components::debug_writer::DebugWriterComponent::new(
311 uart_mux,
312 create_capability!(capabilities::SetDebugWriterCapability),
313 )
314 .finalize(components::debug_writer_component_static!());
315
316 let button = components::button::ButtonComponent::new(
321 board_kernel,
322 capsules_core::button::DRIVER_NUM,
323 components::button_component_helper!(
324 nrf52840::gpio::GPIOPin,
325 (
326 &nrf52840_peripherals.gpio_port[BUTTON1_PIN],
327 kernel::hil::gpio::ActivationMode::ActiveLow,
328 kernel::hil::gpio::FloatingState::PullUp
329 ),
330 (
331 &nrf52840_peripherals.gpio_port[BUTTON2_PIN],
332 kernel::hil::gpio::ActivationMode::ActiveLow,
333 kernel::hil::gpio::FloatingState::PullUp
334 ),
335 (
336 &nrf52840_peripherals.gpio_port[BUTTON3_PIN],
337 kernel::hil::gpio::ActivationMode::ActiveLow,
338 kernel::hil::gpio::FloatingState::PullUp
339 ),
340 (
341 &nrf52840_peripherals.gpio_port[BUTTON4_PIN],
342 kernel::hil::gpio::ActivationMode::ActiveLow,
343 kernel::hil::gpio::FloatingState::PullUp
344 )
345 ),
346 )
347 .finalize(components::button_component_static!(
348 nrf52840::gpio::GPIOPin
349 ));
350
351 let adc_channels = static_init!(
356 [nrf52840::adc::AdcChannelSetup; 6],
357 [
358 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput1),
359 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput2),
360 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput4),
361 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput5),
362 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput6),
363 nrf52840::adc::AdcChannelSetup::new(nrf52840::adc::AdcChannel::AnalogInput7),
364 ]
365 );
366 let adc = components::adc::AdcDedicatedComponent::new(
367 &base_peripherals.adc,
368 adc_channels,
369 board_kernel,
370 capsules_core::adc::DRIVER_NUM,
371 )
372 .finalize(components::adc_dedicated_component_static!(
373 nrf52840::adc::Adc
374 ));
375
376 nrf52_components::NrfClockComponent::new(&base_peripherals.clock).finalize(());
381
382 let checking_policy = components::appid::checker_null::AppCheckerNullComponent::new()
388 .finalize(components::app_checker_null_component_static!());
389
390 let assigner = components::appid::assigner_tbf::AppIdAssignerTbfHeaderComponent::new()
392 .finalize(components::appid_assigner_tbf_header_component_static!());
393
394 let checker = components::appid::checker::ProcessCheckerMachineComponent::new(checking_policy)
396 .finalize(components::process_checker_machine_component_static!());
397
398 let storage_permissions_policy =
403 components::storage_permissions::null::StoragePermissionsNullComponent::new().finalize(
404 components::storage_permissions_null_component_static!(
405 nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
406 kernel::process::ProcessStandardDebugFull,
407 ),
408 );
409
410 extern "C" {
412 static _sapps: u8;
414 static _eapps: u8;
416 static mut _sappmem: u8;
418 static _eappmem: u8;
420 }
421
422 let app_flash = core::slice::from_raw_parts(
423 core::ptr::addr_of!(_sapps),
424 core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
425 );
426 let app_memory = core::slice::from_raw_parts_mut(
427 core::ptr::addr_of_mut!(_sappmem),
428 core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
429 );
430
431 let loader = components::loader::sequential::ProcessLoaderSequentialComponent::new(
433 checker,
434 &mut *addr_of_mut!(PROCESSES),
435 board_kernel,
436 chip,
437 &FAULT_RESPONSE,
438 assigner,
439 storage_permissions_policy,
440 app_flash,
441 app_memory,
442 )
443 .finalize(components::process_loader_sequential_component_static!(
444 nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
445 kernel::process::ProcessStandardDebugFull,
446 NUM_PROCS
447 ));
448
449 let dynamic_binary_storage =
455 components::dynamic_binary_storage::SequentialBinaryStorageComponent::new(
456 &base_peripherals.nvmc,
457 loader,
458 )
459 .finalize(components::sequential_binary_storage_component_static!(
460 nrf52840::nvmc::Nvmc,
461 nrf52840::chip::NRF52<Nrf52840DefaultPeripherals>,
462 kernel::process::ProcessStandardDebugFull,
463 ));
464
465 let dynamic_app_loader = components::app_loader::AppLoaderComponent::new(
467 board_kernel,
468 capsules_extra::app_loader::DRIVER_NUM,
469 dynamic_binary_storage,
470 dynamic_binary_storage,
471 )
472 .finalize(components::app_loader_component_static!(
473 DynamicBinaryStorage<'static>,
474 DynamicBinaryStorage<'static>,
475 ));
476
477 let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
482 .finalize(components::round_robin_component_static!(NUM_PROCS));
483
484 let platform = static_init!(
485 Platform,
486 Platform {
487 console,
488 button,
489 adc,
490 led,
491 alarm,
492 scheduler,
493 systick: cortexm4::systick::SysTick::new_with_calibration(64000000),
494 processes,
495 dynamic_app_loader,
496 }
497 );
498 loader.set_client(platform);
499
500 let _ = pconsole.start();
501
502 board_kernel.kernel_loop(
503 platform,
504 chip,
505 None::<&kernel::ipc::IPC<0>>,
506 &main_loop_capability,
507 );
508}