1#![no_std]
11#![no_main]
12#![deny(missing_docs)]
13
14use core::ptr::{addr_of, addr_of_mut};
15
16use kernel::capabilities;
17use kernel::component::Component;
18use kernel::hil;
19use kernel::hil::led::LedLow;
20use kernel::hil::Controller;
21use kernel::platform::{KernelResources, SyscallDriverLookup};
22use kernel::scheduler::round_robin::RoundRobinSched;
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
39static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
41 [None; NUM_PROCS];
42
43static mut CHIP: Option<&'static sam4l::chip::Sam4l<Sam4lDefaultPeripherals>> = None;
44static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
45 None;
46
47#[no_mangle]
49#[link_section = ".stack_buffer"]
50pub static mut STACK_MEMORY: [u8; 0x1000] = [0; 0x1000];
51
52type SI7021Sensor = components::si7021::SI7021ComponentType<
53 capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, sam4l::ast::Ast<'static>>,
54 capsules_core::virtualizers::virtual_i2c::I2CDevice<'static, sam4l::i2c::I2CHw<'static>>,
55>;
56type TemperatureDriver = components::temperature::TemperatureComponentType<SI7021Sensor>;
57type HumidityDriver = components::humidity::HumidityComponentType<SI7021Sensor>;
58type RngDriver = components::rng::RngComponentType<sam4l::trng::Trng<'static>>;
59
60struct Hail {
63 console: &'static capsules_core::console::Console<'static>,
64 gpio: &'static capsules_core::gpio::GPIO<'static, sam4l::gpio::GPIOPin<'static>>,
65 alarm: &'static capsules_core::alarm::AlarmDriver<
66 'static,
67 capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<
68 'static,
69 sam4l::ast::Ast<'static>,
70 >,
71 >,
72 ambient_light: &'static capsules_extra::ambient_light::AmbientLight<'static>,
73 temp: &'static TemperatureDriver,
74 ninedof: &'static capsules_extra::ninedof::NineDof<'static>,
75 humidity: &'static HumidityDriver,
76 spi: &'static capsules_core::spi_controller::Spi<
77 'static,
78 capsules_core::virtualizers::virtual_spi::VirtualSpiMasterDevice<
79 'static,
80 sam4l::spi::SpiHw<'static>,
81 >,
82 >,
83 nrf51822: &'static capsules_extra::nrf51822_serialization::Nrf51822Serialization<'static>,
84 adc: &'static capsules_core::adc::AdcDedicated<'static, sam4l::adc::Adc<'static>>,
85 led: &'static capsules_core::led::LedDriver<
86 'static,
87 LedLow<'static, sam4l::gpio::GPIOPin<'static>>,
88 3,
89 >,
90 button: &'static capsules_core::button::Button<'static, sam4l::gpio::GPIOPin<'static>>,
91 rng: &'static RngDriver,
92 ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
93 crc: &'static capsules_extra::crc::CrcDriver<'static, sam4l::crccu::Crccu<'static>>,
94 dac: &'static capsules_extra::dac::Dac<'static>,
95 scheduler: &'static RoundRobinSched<'static>,
96 systick: cortexm4::systick::SysTick,
97}
98
99impl SyscallDriverLookup for Hail {
101 fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
102 where
103 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
104 {
105 match driver_num {
106 capsules_core::console::DRIVER_NUM => f(Some(self.console)),
107 capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
108
109 capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
110 capsules_core::spi_controller::DRIVER_NUM => f(Some(self.spi)),
111 capsules_extra::nrf51822_serialization::DRIVER_NUM => f(Some(self.nrf51822)),
112 capsules_extra::ambient_light::DRIVER_NUM => f(Some(self.ambient_light)),
113 capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
114 capsules_core::led::DRIVER_NUM => f(Some(self.led)),
115 capsules_core::button::DRIVER_NUM => f(Some(self.button)),
116 capsules_extra::humidity::DRIVER_NUM => f(Some(self.humidity)),
117 capsules_extra::temperature::DRIVER_NUM => f(Some(self.temp)),
118 capsules_extra::ninedof::DRIVER_NUM => f(Some(self.ninedof)),
119
120 capsules_core::rng::DRIVER_NUM => f(Some(self.rng)),
121
122 capsules_extra::crc::DRIVER_NUM => f(Some(self.crc)),
123
124 capsules_extra::dac::DRIVER_NUM => f(Some(self.dac)),
125
126 kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
127 _ => f(None),
128 }
129 }
130}
131
132impl KernelResources<sam4l::chip::Sam4l<Sam4lDefaultPeripherals>> for Hail {
133 type SyscallDriverLookup = Self;
134 type SyscallFilter = ();
135 type ProcessFault = ();
136 type Scheduler = RoundRobinSched<'static>;
137 type SchedulerTimer = cortexm4::systick::SysTick;
138 type WatchDog = ();
139 type ContextSwitchCallback = ();
140
141 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
142 self
143 }
144 fn syscall_filter(&self) -> &Self::SyscallFilter {
145 &()
146 }
147 fn process_fault(&self) -> &Self::ProcessFault {
148 &()
149 }
150 fn scheduler(&self) -> &Self::Scheduler {
151 self.scheduler
152 }
153 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
154 &self.systick
155 }
156 fn watchdog(&self) -> &Self::WatchDog {
157 &()
158 }
159 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
160 &()
161 }
162}
163
164unsafe fn set_pin_primary_functions(peripherals: &Sam4lDefaultPeripherals) {
166 use sam4l::gpio::PeripheralFunction::{A, B};
167
168 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); }
224
225#[inline(never)]
229unsafe fn start() -> (
230 &'static kernel::Kernel,
231 Hail,
232 &'static sam4l::chip::Sam4l<Sam4lDefaultPeripherals>,
233) {
234 sam4l::init();
235
236 let pm = static_init!(sam4l::pm::PowerManager, sam4l::pm::PowerManager::new());
237 let peripherals = static_init!(Sam4lDefaultPeripherals, Sam4lDefaultPeripherals::new(pm));
238
239 pm.setup_system_clock(
240 sam4l::pm::SystemClockSource::PllExternalOscillatorAt48MHz {
241 frequency: sam4l::pm::OscillatorFrequency::Frequency16MHz,
242 startup_mode: sam4l::pm::OscillatorStartup::SlowStart,
243 },
244 &peripherals.flash_controller,
245 );
246
247 sam4l::bpm::set_ck32source(sam4l::bpm::CK32Source::RC32K);
249
250 set_pin_primary_functions(peripherals);
251 peripherals.setup_circular_deps();
252 let chip = static_init!(
253 sam4l::chip::Sam4l<Sam4lDefaultPeripherals>,
254 sam4l::chip::Sam4l::new(pm, peripherals)
255 );
256 CHIP = Some(chip);
257
258 let process_management_capability =
261 create_capability!(capabilities::ProcessManagementCapability);
262 let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
263
264 kernel::debug::assign_gpios(
266 Some(&peripherals.pa[13]),
267 Some(&peripherals.pa[15]),
268 Some(&peripherals.pa[14]),
269 );
270
271 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&*addr_of!(PROCESSES)));
272
273 let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
274 .finalize(components::process_printer_text_component_static!());
275 PROCESS_PRINTER = Some(process_printer);
276
277 peripherals.usart0.set_mode(sam4l::usart::UsartMode::Uart);
279
280 let uart_mux = components::console::UartMuxComponent::new(&peripherals.usart0, 115200)
282 .finalize(components::uart_mux_component_static!());
283 uart_mux.initialize();
284
285 hil::uart::Transmit::set_transmit_client(&peripherals.usart0, uart_mux);
286 hil::uart::Receive::set_receive_client(&peripherals.usart0, uart_mux);
287
288 let mux_alarm = components::alarm::AlarmMuxComponent::new(&peripherals.ast)
289 .finalize(components::alarm_mux_component_static!(sam4l::ast::Ast));
290 peripherals.ast.configure(mux_alarm);
291
292 let console = components::console::ConsoleComponent::new(
294 board_kernel,
295 capsules_core::console::DRIVER_NUM,
296 uart_mux,
297 )
298 .finalize(components::console_component_static!());
299 let process_console = components::process_console::ProcessConsoleComponent::new(
300 board_kernel,
301 uart_mux,
302 mux_alarm,
303 process_printer,
304 Some(cortexm4::support::reset),
305 )
306 .finalize(components::process_console_component_static!(
307 sam4l::ast::Ast<'static>
308 ));
309 components::debug_writer::DebugWriterComponent::new(
310 uart_mux,
311 create_capability!(capabilities::SetDebugWriterCapability),
312 )
313 .finalize(components::debug_writer_component_static!());
314
315 peripherals.usart3.set_mode(sam4l::usart::UsartMode::Uart);
317 let nrf_serialization = components::nrf51822::Nrf51822Component::new(
320 board_kernel,
321 capsules_extra::nrf51822_serialization::DRIVER_NUM,
322 &peripherals.usart3,
323 &peripherals.pa[17],
324 )
325 .finalize(components::nrf51822_component_static!());
326
327 let sensors_i2c = components::i2c::I2CMuxComponent::new(&peripherals.i2c1, None)
328 .finalize(components::i2c_mux_component_static!(sam4l::i2c::I2CHw));
329
330 let si7021 = components::si7021::SI7021Component::new(sensors_i2c, mux_alarm, 0x40).finalize(
332 components::si7021_component_static!(sam4l::ast::Ast, sam4l::i2c::I2CHw),
333 );
334 let temp = components::temperature::TemperatureComponent::new(
335 board_kernel,
336 capsules_extra::temperature::DRIVER_NUM,
337 si7021,
338 )
339 .finalize(components::temperature_component_static!(SI7021Sensor));
340 let humidity = components::humidity::HumidityComponent::new(
341 board_kernel,
342 capsules_extra::humidity::DRIVER_NUM,
343 si7021,
344 )
345 .finalize(components::humidity_component_static!(SI7021Sensor));
346
347 let isl29035 = components::isl29035::Isl29035Component::new(sensors_i2c, mux_alarm).finalize(
349 components::isl29035_component_static!(sam4l::ast::Ast, sam4l::i2c::I2CHw),
350 );
351 let ambient_light = components::isl29035::AmbientLightComponent::new(
352 board_kernel,
353 capsules_extra::ambient_light::DRIVER_NUM,
354 isl29035,
355 )
356 .finalize(components::ambient_light_component_static!());
357
358 let alarm = components::alarm::AlarmDriverComponent::new(
360 board_kernel,
361 capsules_core::alarm::DRIVER_NUM,
362 mux_alarm,
363 )
364 .finalize(components::alarm_component_static!(sam4l::ast::Ast));
365
366 let fxos8700 =
368 components::fxos8700::Fxos8700Component::new(sensors_i2c, 0x1e, &peripherals.pa[9])
369 .finalize(components::fxos8700_component_static!(sam4l::i2c::I2CHw));
370
371 let ninedof = components::ninedof::NineDofComponent::new(
372 board_kernel,
373 capsules_extra::ninedof::DRIVER_NUM,
374 )
375 .finalize(components::ninedof_component_static!(fxos8700));
376
377 let mux_spi = components::spi::SpiMuxComponent::new(&peripherals.spi)
380 .finalize(components::spi_mux_component_static!(sam4l::spi::SpiHw));
381 let spi_syscalls = components::spi::SpiSyscallComponent::new(
383 board_kernel,
384 mux_spi,
385 sam4l::spi::Peripheral::Peripheral0,
386 capsules_core::spi_controller::DRIVER_NUM,
387 )
388 .finalize(components::spi_syscall_component_static!(sam4l::spi::SpiHw));
389
390 let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
392 LedLow<'static, sam4l::gpio::GPIOPin>,
393 LedLow::new(&peripherals.pa[13]), LedLow::new(&peripherals.pa[15]), LedLow::new(&peripherals.pa[14]), ));
397
398 let button = components::button::ButtonComponent::new(
400 board_kernel,
401 capsules_core::button::DRIVER_NUM,
402 components::button_component_helper!(
403 sam4l::gpio::GPIOPin,
404 (
405 &peripherals.pa[16],
406 kernel::hil::gpio::ActivationMode::ActiveLow,
407 kernel::hil::gpio::FloatingState::PullNone
408 )
409 ),
410 )
411 .finalize(components::button_component_static!(sam4l::gpio::GPIOPin));
412
413 let adc_channels = static_init!(
415 [sam4l::adc::AdcChannel; 6],
416 [
417 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), ]
424 );
425 let adc = components::adc::AdcDedicatedComponent::new(
426 &peripherals.adc,
427 adc_channels,
428 board_kernel,
429 capsules_core::adc::DRIVER_NUM,
430 )
431 .finalize(components::adc_dedicated_component_static!(sam4l::adc::Adc));
432
433 let rng = components::rng::RngComponent::new(
435 board_kernel,
436 capsules_core::rng::DRIVER_NUM,
437 &peripherals.trng,
438 )
439 .finalize(components::rng_component_static!(sam4l::trng::Trng));
440
441 let gpio = components::gpio::GpioComponent::new(
443 board_kernel,
444 capsules_core::gpio::DRIVER_NUM,
445 components::gpio_component_helper!(
446 sam4l::gpio::GPIOPin,
447 0 => &peripherals.pb[14], 1 => &peripherals.pb[15], 2 => &peripherals.pb[11], 3 => &peripherals.pb[12] ),
452 )
453 .finalize(components::gpio_component_static!(sam4l::gpio::GPIOPin));
454
455 let crc = components::crc::CrcComponent::new(
457 board_kernel,
458 capsules_extra::crc::DRIVER_NUM,
459 &peripherals.crccu,
460 )
461 .finalize(components::crc_component_static!(sam4l::crccu::Crccu));
462
463 let dac = components::dac::DacComponent::new(&peripherals.dac)
465 .finalize(components::dac_component_static!());
466
467 let fault_policy = static_init!(
489 capsules_system::process_policies::ThresholdRestartThenPanicFaultPolicy,
490 capsules_system::process_policies::ThresholdRestartThenPanicFaultPolicy::new(4)
491 );
492
493 let scheduler = components::sched::round_robin::RoundRobinComponent::new(&*addr_of!(PROCESSES))
494 .finalize(components::round_robin_component_static!(NUM_PROCS));
495
496 let hail = Hail {
497 console,
498 gpio,
499 alarm,
500 ambient_light,
501 temp,
502 humidity,
503 ninedof,
504 spi: spi_syscalls,
505 nrf51822: nrf_serialization,
506 adc,
507 led,
508 button,
509 rng,
510 ipc: kernel::ipc::IPC::new(
511 board_kernel,
512 kernel::ipc::DRIVER_NUM,
513 &memory_allocation_capability,
514 ),
515 crc,
516 dac,
517 scheduler,
518 systick: cortexm4::systick::SysTick::new(),
519 };
520
521 hail.nrf51822.initialize();
523
524 let _ = process_console.start();
525
526 debug!("Initialization complete. Entering main loop.");
530
531 extern "C" {
533 static _sapps: u8;
535 static _eapps: u8;
537 static mut _sappmem: u8;
539 static _eappmem: u8;
541 }
542
543 kernel::process::load_processes(
544 board_kernel,
545 chip,
546 core::slice::from_raw_parts(
547 core::ptr::addr_of!(_sapps),
548 core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
549 ),
550 core::slice::from_raw_parts_mut(
551 core::ptr::addr_of_mut!(_sappmem),
552 core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
553 ),
554 &mut *addr_of_mut!(PROCESSES),
555 fault_policy,
556 &process_management_capability,
557 )
558 .unwrap_or_else(|err| {
559 debug!("Error loading processes!");
560 debug!("{:?}", err);
561 });
562
563 (board_kernel, hail, chip)
564}
565
566#[no_mangle]
568pub unsafe fn main() {
569 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
570
571 let (board_kernel, platform, chip) = start();
572 board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
573}