1#![no_std]
11#![no_main]
12#![deny(missing_docs)]
13
14use kernel::capabilities;
15use kernel::component::Component;
16use kernel::debug::PanicResources;
17use kernel::hil;
18use kernel::hil::led::LedLow;
19use kernel::hil::Controller;
20use kernel::platform::{KernelResources, SyscallDriverLookup};
21use kernel::scheduler::round_robin::RoundRobinSched;
22use kernel::utilities::single_thread_value::SingleThreadValue;
23#[allow(unused_imports)]
24use kernel::{create_capability, debug, debug_gpio, static_init};
25use sam4l::chip::Sam4lDefaultPeripherals;
26
27pub mod io;
31#[allow(dead_code)]
32mod test_take_map_cell;
33
34const NUM_PROCS: usize = 20;
38
39type ChipHw = sam4l::chip::Sam4l<Sam4lDefaultPeripherals>;
40type ProcessPrinterInUse = capsules_system::process_printer::ProcessPrinterText;
41
42static PANIC_RESOURCES: SingleThreadValue<PanicResources<ChipHw, ProcessPrinterInUse>> =
44 SingleThreadValue::new(PanicResources::new());
45
46kernel::stack_size! {0x1000}
47
48type SI7021Sensor = components::si7021::SI7021ComponentType<
49 capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, sam4l::ast::Ast<'static>>,
50 capsules_core::virtualizers::virtual_i2c::I2CDevice<'static, sam4l::i2c::I2CHw<'static>>,
51>;
52type TemperatureDriver = components::temperature::TemperatureComponentType<SI7021Sensor>;
53type HumidityDriver = components::humidity::HumidityComponentType<SI7021Sensor>;
54type RngDriver = components::rng::RngComponentType<sam4l::trng::Trng<'static>>;
55
56struct Hail {
59 console: &'static capsules_core::console::Console<'static>,
60 gpio: &'static capsules_core::gpio::GPIO<'static, sam4l::gpio::GPIOPin<'static>>,
61 alarm: &'static capsules_core::alarm::AlarmDriver<
62 'static,
63 capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<
64 'static,
65 sam4l::ast::Ast<'static>,
66 >,
67 >,
68 ambient_light: &'static capsules_extra::ambient_light::AmbientLight<'static>,
69 temp: &'static TemperatureDriver,
70 ninedof: &'static capsules_extra::ninedof::NineDof<'static>,
71 humidity: &'static HumidityDriver,
72 spi: &'static capsules_core::spi_controller::Spi<
73 'static,
74 capsules_core::virtualizers::virtual_spi::VirtualSpiMasterDevice<
75 'static,
76 sam4l::spi::SpiHw<'static>,
77 >,
78 >,
79 nrf51822: &'static capsules_extra::nrf51822_serialization::Nrf51822Serialization<'static>,
80 adc: &'static capsules_core::adc::AdcDedicated<'static, sam4l::adc::Adc<'static>>,
81 led: &'static capsules_core::led::LedDriver<
82 'static,
83 LedLow<'static, sam4l::gpio::GPIOPin<'static>>,
84 3,
85 >,
86 button: &'static capsules_core::button::Button<'static, sam4l::gpio::GPIOPin<'static>>,
87 rng: &'static RngDriver,
88 ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
89 crc: &'static capsules_extra::crc::CrcDriver<'static, sam4l::crccu::Crccu<'static>>,
90 dac: &'static capsules_extra::dac::Dac<'static>,
91 scheduler: &'static RoundRobinSched<'static>,
92 systick: cortexm4::systick::SysTick,
93}
94
95impl SyscallDriverLookup for Hail {
97 fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
98 where
99 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
100 {
101 match driver_num {
102 capsules_core::console::DRIVER_NUM => f(Some(self.console)),
103 capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
104
105 capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
106 capsules_core::spi_controller::DRIVER_NUM => f(Some(self.spi)),
107 capsules_extra::nrf51822_serialization::DRIVER_NUM => f(Some(self.nrf51822)),
108 capsules_extra::ambient_light::DRIVER_NUM => f(Some(self.ambient_light)),
109 capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
110 capsules_core::led::DRIVER_NUM => f(Some(self.led)),
111 capsules_core::button::DRIVER_NUM => f(Some(self.button)),
112 capsules_extra::humidity::DRIVER_NUM => f(Some(self.humidity)),
113 capsules_extra::temperature::DRIVER_NUM => f(Some(self.temp)),
114 capsules_extra::ninedof::DRIVER_NUM => f(Some(self.ninedof)),
115
116 capsules_core::rng::DRIVER_NUM => f(Some(self.rng)),
117
118 capsules_extra::crc::DRIVER_NUM => f(Some(self.crc)),
119
120 capsules_extra::dac::DRIVER_NUM => f(Some(self.dac)),
121
122 kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
123 _ => f(None),
124 }
125 }
126}
127
128impl KernelResources<sam4l::chip::Sam4l<Sam4lDefaultPeripherals>> for Hail {
129 type SyscallDriverLookup = Self;
130 type SyscallFilter = ();
131 type ProcessFault = ();
132 type Scheduler = RoundRobinSched<'static>;
133 type SchedulerTimer = cortexm4::systick::SysTick;
134 type WatchDog = ();
135 type ContextSwitchCallback = ();
136
137 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
138 self
139 }
140 fn syscall_filter(&self) -> &Self::SyscallFilter {
141 &()
142 }
143 fn process_fault(&self) -> &Self::ProcessFault {
144 &()
145 }
146 fn scheduler(&self) -> &Self::Scheduler {
147 self.scheduler
148 }
149 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
150 &self.systick
151 }
152 fn watchdog(&self) -> &Self::WatchDog {
153 &()
154 }
155 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
156 &()
157 }
158}
159
160unsafe fn set_pin_primary_functions(peripherals: &Sam4lDefaultPeripherals) {
162 use sam4l::gpio::PeripheralFunction::{A, B};
163
164 peripherals.pa[04].configure(Some(A)); peripherals.pa[05].configure(Some(A)); peripherals.pa[06].configure(Some(A)); peripherals.pa[07].configure(None); peripherals.pa[08].configure(Some(A)); peripherals.pa[09].configure(None); peripherals.pa[10].configure(None); peripherals.pa[11].configure(Some(A)); peripherals.pa[12].configure(Some(A)); peripherals.pa[13].configure(None); peripherals.pa[14].configure(None); peripherals.pa[15].configure(None); peripherals.pa[16].configure(None); peripherals.pa[17].configure(None); peripherals.pa[18].configure(None); peripherals.pa[19].configure(None); peripherals.pa[20].configure(None); peripherals.pa[21].configure(Some(A)); peripherals.pa[22].configure(Some(A)); peripherals.pa[23].configure(Some(A)); peripherals.pa[24].configure(Some(A)); peripherals.pa[25].configure(Some(B)); peripherals.pa[26].configure(Some(B)); peripherals.pb[00].configure(Some(A)); peripherals.pb[01].configure(Some(A)); peripherals.pb[02].configure(Some(A)); peripherals.pb[03].configure(Some(A)); peripherals.pb[04].configure(Some(A)); peripherals.pb[05].configure(Some(A)); peripherals.pb[06].configure(Some(A)); peripherals.pb[07].configure(Some(A)); peripherals.pb[08].configure(None); peripherals.pb[09].configure(Some(A)); peripherals.pb[10].configure(Some(A)); peripherals.pb[11].configure(None); peripherals.pb[12].configure(None); peripherals.pb[13].configure(None); peripherals.pb[14].configure(None); peripherals.pb[15].configure(None); }
220
221#[inline(never)]
225unsafe fn start() -> (
226 &'static kernel::Kernel,
227 Hail,
228 &'static sam4l::chip::Sam4l<Sam4lDefaultPeripherals>,
229) {
230 sam4l::init();
231
232 kernel::deferred_call::initialize_deferred_call_state::<
234 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
235 >();
236
237 PANIC_RESOURCES.bind_to_thread::<<ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider>();
239
240 let pm = static_init!(sam4l::pm::PowerManager, sam4l::pm::PowerManager::new());
241 let peripherals = static_init!(Sam4lDefaultPeripherals, Sam4lDefaultPeripherals::new(pm));
242
243 pm.setup_system_clock(
244 sam4l::pm::SystemClockSource::PllExternalOscillatorAt48MHz {
245 frequency: sam4l::pm::OscillatorFrequency::Frequency16MHz,
246 startup_mode: sam4l::pm::OscillatorStartup::SlowStart,
247 },
248 &peripherals.flash_controller,
249 );
250
251 sam4l::bpm::set_ck32source(sam4l::bpm::CK32Source::RC32K);
253
254 set_pin_primary_functions(peripherals);
255 peripherals.setup_circular_deps();
256 let chip = static_init!(
257 sam4l::chip::Sam4l<Sam4lDefaultPeripherals>,
258 sam4l::chip::Sam4l::new(pm, peripherals)
259 );
260 PANIC_RESOURCES.get().map(|resources| {
261 resources.chip.put(chip);
262 });
263
264 let process_management_capability =
267 create_capability!(capabilities::ProcessManagementCapability);
268 let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
269
270 let debug_gpios = static_init!(
272 [&'static dyn kernel::hil::gpio::Pin; 3],
273 [
274 &peripherals.pa[13],
275 &peripherals.pa[14],
276 &peripherals.pa[15],
277 ]
278 );
279 kernel::debug::initialize_debug_gpio::<
280 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
281 >();
282 kernel::debug::assign_gpios(debug_gpios);
283
284 let processes = components::process_array::ProcessArrayComponent::new()
286 .finalize(components::process_array_component_static!(NUM_PROCS));
287 PANIC_RESOURCES.get().map(|resources| {
288 resources.processes.put(processes.as_slice());
289 });
290
291 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
293
294 let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
295 .finalize(components::process_printer_text_component_static!());
296 PANIC_RESOURCES.get().map(|resources| {
297 resources.printer.put(process_printer);
298 });
299
300 peripherals.usart0.set_mode(sam4l::usart::UsartMode::Uart);
302
303 let uart_mux = components::console::UartMuxComponent::new(&peripherals.usart0, 115200)
305 .finalize(components::uart_mux_component_static!());
306 uart_mux.initialize();
307
308 hil::uart::Transmit::set_transmit_client(&peripherals.usart0, uart_mux);
309 hil::uart::Receive::set_receive_client(&peripherals.usart0, uart_mux);
310
311 let mux_alarm = components::alarm::AlarmMuxComponent::new(&peripherals.ast)
312 .finalize(components::alarm_mux_component_static!(sam4l::ast::Ast));
313 peripherals.ast.configure(mux_alarm);
314
315 let console = components::console::ConsoleComponent::new(
317 board_kernel,
318 capsules_core::console::DRIVER_NUM,
319 uart_mux,
320 )
321 .finalize(components::console_component_static!());
322 let process_console = components::process_console::ProcessConsoleComponent::new(
323 board_kernel,
324 uart_mux,
325 mux_alarm,
326 process_printer,
327 Some(cortexm4::support::reset),
328 )
329 .finalize(components::process_console_component_static!(
330 sam4l::ast::Ast<'static>
331 ));
332 components::debug_writer::DebugWriterComponent::new::<
333 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
334 >(
335 uart_mux,
336 create_capability!(capabilities::SetDebugWriterCapability),
337 )
338 .finalize(components::debug_writer_component_static!());
339
340 peripherals.usart3.set_mode(sam4l::usart::UsartMode::Uart);
342 let nrf_serialization = components::nrf51822::Nrf51822Component::new(
345 board_kernel,
346 capsules_extra::nrf51822_serialization::DRIVER_NUM,
347 &peripherals.usart3,
348 &peripherals.pa[17],
349 )
350 .finalize(components::nrf51822_component_static!());
351
352 let sensors_i2c = components::i2c::I2CMuxComponent::new(&peripherals.i2c1, None)
353 .finalize(components::i2c_mux_component_static!(sam4l::i2c::I2CHw));
354
355 let si7021 = components::si7021::SI7021Component::new(sensors_i2c, mux_alarm, 0x40).finalize(
357 components::si7021_component_static!(sam4l::ast::Ast, sam4l::i2c::I2CHw),
358 );
359 let temp = components::temperature::TemperatureComponent::new(
360 board_kernel,
361 capsules_extra::temperature::DRIVER_NUM,
362 si7021,
363 )
364 .finalize(components::temperature_component_static!(SI7021Sensor));
365 let humidity = components::humidity::HumidityComponent::new(
366 board_kernel,
367 capsules_extra::humidity::DRIVER_NUM,
368 si7021,
369 )
370 .finalize(components::humidity_component_static!(SI7021Sensor));
371
372 let isl29035 = components::isl29035::Isl29035Component::new(sensors_i2c, mux_alarm).finalize(
374 components::isl29035_component_static!(sam4l::ast::Ast, sam4l::i2c::I2CHw),
375 );
376 let ambient_light = components::isl29035::AmbientLightComponent::new(
377 board_kernel,
378 capsules_extra::ambient_light::DRIVER_NUM,
379 isl29035,
380 )
381 .finalize(components::ambient_light_component_static!());
382
383 let alarm = components::alarm::AlarmDriverComponent::new(
385 board_kernel,
386 capsules_core::alarm::DRIVER_NUM,
387 mux_alarm,
388 )
389 .finalize(components::alarm_component_static!(sam4l::ast::Ast));
390
391 let fxos8700 =
393 components::fxos8700::Fxos8700Component::new(sensors_i2c, 0x1e, &peripherals.pa[9])
394 .finalize(components::fxos8700_component_static!(sam4l::i2c::I2CHw));
395
396 let ninedof = components::ninedof::NineDofComponent::new(
397 board_kernel,
398 capsules_extra::ninedof::DRIVER_NUM,
399 )
400 .finalize(components::ninedof_component_static!(fxos8700));
401
402 let mux_spi = components::spi::SpiMuxComponent::new(&peripherals.spi)
405 .finalize(components::spi_mux_component_static!(sam4l::spi::SpiHw));
406 let spi_syscalls = components::spi::SpiSyscallComponent::new(
408 board_kernel,
409 mux_spi,
410 sam4l::spi::Peripheral::Peripheral0,
411 capsules_core::spi_controller::DRIVER_NUM,
412 )
413 .finalize(components::spi_syscall_component_static!(sam4l::spi::SpiHw));
414
415 let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
417 LedLow<'static, sam4l::gpio::GPIOPin>,
418 LedLow::new(&peripherals.pa[13]), LedLow::new(&peripherals.pa[15]), LedLow::new(&peripherals.pa[14]), ));
422
423 let button = components::button::ButtonComponent::new(
425 board_kernel,
426 capsules_core::button::DRIVER_NUM,
427 components::button_component_helper!(
428 sam4l::gpio::GPIOPin,
429 (
430 &peripherals.pa[16],
431 kernel::hil::gpio::ActivationMode::ActiveLow,
432 kernel::hil::gpio::FloatingState::PullNone
433 )
434 ),
435 )
436 .finalize(components::button_component_static!(sam4l::gpio::GPIOPin));
437
438 let adc_channels = static_init!(
440 [sam4l::adc::AdcChannel; 6],
441 [
442 sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD0), sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD1), sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD3), sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD4), sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD5), sam4l::adc::AdcChannel::new(sam4l::adc::Channel::AD6), ]
449 );
450 let adc = components::adc::AdcDedicatedComponent::new(
451 &peripherals.adc,
452 adc_channels,
453 board_kernel,
454 capsules_core::adc::DRIVER_NUM,
455 )
456 .finalize(components::adc_dedicated_component_static!(sam4l::adc::Adc));
457
458 let rng = components::rng::RngComponent::new(
460 board_kernel,
461 capsules_core::rng::DRIVER_NUM,
462 &peripherals.trng,
463 )
464 .finalize(components::rng_component_static!(sam4l::trng::Trng));
465
466 let gpio = components::gpio::GpioComponent::new(
468 board_kernel,
469 capsules_core::gpio::DRIVER_NUM,
470 components::gpio_component_helper!(
471 sam4l::gpio::GPIOPin,
472 0 => &peripherals.pb[14], 1 => &peripherals.pb[15], 2 => &peripherals.pb[11], 3 => &peripherals.pb[12] ),
477 )
478 .finalize(components::gpio_component_static!(sam4l::gpio::GPIOPin));
479
480 let crc = components::crc::CrcComponent::new(
482 board_kernel,
483 capsules_extra::crc::DRIVER_NUM,
484 &peripherals.crccu,
485 )
486 .finalize(components::crc_component_static!(sam4l::crccu::Crccu));
487
488 let dac = components::dac::DacComponent::new(&peripherals.dac)
490 .finalize(components::dac_component_static!());
491
492 let fault_policy = static_init!(
514 capsules_system::process_policies::ThresholdRestartThenPanicFaultPolicy,
515 capsules_system::process_policies::ThresholdRestartThenPanicFaultPolicy::new(4)
516 );
517
518 let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
519 .finalize(components::round_robin_component_static!(NUM_PROCS));
520
521 let hail = Hail {
522 console,
523 gpio,
524 alarm,
525 ambient_light,
526 temp,
527 humidity,
528 ninedof,
529 spi: spi_syscalls,
530 nrf51822: nrf_serialization,
531 adc,
532 led,
533 button,
534 rng,
535 ipc: kernel::ipc::IPC::new(
536 board_kernel,
537 kernel::ipc::DRIVER_NUM,
538 &memory_allocation_capability,
539 ),
540 crc,
541 dac,
542 scheduler,
543 systick: cortexm4::systick::SysTick::new(),
544 };
545
546 hail.nrf51822.initialize();
548
549 let _ = process_console.start();
550
551 debug!("Initialization complete. Entering main loop.");
555
556 extern "C" {
558 static _sapps: u8;
560 static _eapps: u8;
562 static mut _sappmem: u8;
564 static _eappmem: u8;
566 }
567
568 kernel::process::load_processes(
569 board_kernel,
570 chip,
571 core::slice::from_raw_parts(
572 core::ptr::addr_of!(_sapps),
573 core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
574 ),
575 core::slice::from_raw_parts_mut(
576 core::ptr::addr_of_mut!(_sappmem),
577 core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
578 ),
579 fault_policy,
580 &process_management_capability,
581 )
582 .unwrap_or_else(|err| {
583 debug!("Error loading processes!");
584 debug!("{:?}", err);
585 });
586
587 (board_kernel, hail, chip)
588}
589
590#[no_mangle]
592pub unsafe fn main() {
593 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
594
595 let (board_kernel, platform, chip) = start();
596 board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
597}