1#![no_std]
11#![no_main]
12#![deny(missing_docs)]
13
14use kernel::capabilities;
15use kernel::component::Component;
16use kernel::hil;
17use kernel::hil::led::LedLow;
18use kernel::hil::Controller;
19use kernel::platform::{KernelResources, SyscallDriverLookup};
20use kernel::process::ProcessArray;
21use kernel::scheduler::round_robin::RoundRobinSched;
22#[allow(unused_imports)]
23use kernel::{create_capability, debug, debug_gpio, static_init};
24use sam4l::chip::Sam4lDefaultPeripherals;
25
26pub mod io;
30#[allow(dead_code)]
31mod test_take_map_cell;
32
33const NUM_PROCS: usize = 20;
37
38static mut PROCESSES: Option<&'static ProcessArray<NUM_PROCS>> = None;
40static mut CHIP: Option<&'static sam4l::chip::Sam4l<Sam4lDefaultPeripherals>> = None;
41static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
42 None;
43
44kernel::stack_size! {0x1000}
45
46type SI7021Sensor = components::si7021::SI7021ComponentType<
47 capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<'static, sam4l::ast::Ast<'static>>,
48 capsules_core::virtualizers::virtual_i2c::I2CDevice<'static, sam4l::i2c::I2CHw<'static>>,
49>;
50type TemperatureDriver = components::temperature::TemperatureComponentType<SI7021Sensor>;
51type HumidityDriver = components::humidity::HumidityComponentType<SI7021Sensor>;
52type RngDriver = components::rng::RngComponentType<sam4l::trng::Trng<'static>>;
53
54struct Hail {
57 console: &'static capsules_core::console::Console<'static>,
58 gpio: &'static capsules_core::gpio::GPIO<'static, sam4l::gpio::GPIOPin<'static>>,
59 alarm: &'static capsules_core::alarm::AlarmDriver<
60 'static,
61 capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<
62 'static,
63 sam4l::ast::Ast<'static>,
64 >,
65 >,
66 ambient_light: &'static capsules_extra::ambient_light::AmbientLight<'static>,
67 temp: &'static TemperatureDriver,
68 ninedof: &'static capsules_extra::ninedof::NineDof<'static>,
69 humidity: &'static HumidityDriver,
70 spi: &'static capsules_core::spi_controller::Spi<
71 'static,
72 capsules_core::virtualizers::virtual_spi::VirtualSpiMasterDevice<
73 'static,
74 sam4l::spi::SpiHw<'static>,
75 >,
76 >,
77 nrf51822: &'static capsules_extra::nrf51822_serialization::Nrf51822Serialization<'static>,
78 adc: &'static capsules_core::adc::AdcDedicated<'static, sam4l::adc::Adc<'static>>,
79 led: &'static capsules_core::led::LedDriver<
80 'static,
81 LedLow<'static, sam4l::gpio::GPIOPin<'static>>,
82 3,
83 >,
84 button: &'static capsules_core::button::Button<'static, sam4l::gpio::GPIOPin<'static>>,
85 rng: &'static RngDriver,
86 ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
87 crc: &'static capsules_extra::crc::CrcDriver<'static, sam4l::crccu::Crccu<'static>>,
88 dac: &'static capsules_extra::dac::Dac<'static>,
89 scheduler: &'static RoundRobinSched<'static>,
90 systick: cortexm4::systick::SysTick,
91}
92
93impl SyscallDriverLookup for Hail {
95 fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
96 where
97 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
98 {
99 match driver_num {
100 capsules_core::console::DRIVER_NUM => f(Some(self.console)),
101 capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
102
103 capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
104 capsules_core::spi_controller::DRIVER_NUM => f(Some(self.spi)),
105 capsules_extra::nrf51822_serialization::DRIVER_NUM => f(Some(self.nrf51822)),
106 capsules_extra::ambient_light::DRIVER_NUM => f(Some(self.ambient_light)),
107 capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
108 capsules_core::led::DRIVER_NUM => f(Some(self.led)),
109 capsules_core::button::DRIVER_NUM => f(Some(self.button)),
110 capsules_extra::humidity::DRIVER_NUM => f(Some(self.humidity)),
111 capsules_extra::temperature::DRIVER_NUM => f(Some(self.temp)),
112 capsules_extra::ninedof::DRIVER_NUM => f(Some(self.ninedof)),
113
114 capsules_core::rng::DRIVER_NUM => f(Some(self.rng)),
115
116 capsules_extra::crc::DRIVER_NUM => f(Some(self.crc)),
117
118 capsules_extra::dac::DRIVER_NUM => f(Some(self.dac)),
119
120 kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
121 _ => f(None),
122 }
123 }
124}
125
126impl KernelResources<sam4l::chip::Sam4l<Sam4lDefaultPeripherals>> for Hail {
127 type SyscallDriverLookup = Self;
128 type SyscallFilter = ();
129 type ProcessFault = ();
130 type Scheduler = RoundRobinSched<'static>;
131 type SchedulerTimer = cortexm4::systick::SysTick;
132 type WatchDog = ();
133 type ContextSwitchCallback = ();
134
135 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
136 self
137 }
138 fn syscall_filter(&self) -> &Self::SyscallFilter {
139 &()
140 }
141 fn process_fault(&self) -> &Self::ProcessFault {
142 &()
143 }
144 fn scheduler(&self) -> &Self::Scheduler {
145 self.scheduler
146 }
147 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
148 &self.systick
149 }
150 fn watchdog(&self) -> &Self::WatchDog {
151 &()
152 }
153 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
154 &()
155 }
156}
157
158unsafe fn set_pin_primary_functions(peripherals: &Sam4lDefaultPeripherals) {
160 use sam4l::gpio::PeripheralFunction::{A, B};
161
162 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); }
218
219#[inline(never)]
223unsafe fn start() -> (
224 &'static kernel::Kernel,
225 Hail,
226 &'static sam4l::chip::Sam4l<Sam4lDefaultPeripherals>,
227) {
228 sam4l::init();
229
230 let pm = static_init!(sam4l::pm::PowerManager, sam4l::pm::PowerManager::new());
231 let peripherals = static_init!(Sam4lDefaultPeripherals, Sam4lDefaultPeripherals::new(pm));
232
233 pm.setup_system_clock(
234 sam4l::pm::SystemClockSource::PllExternalOscillatorAt48MHz {
235 frequency: sam4l::pm::OscillatorFrequency::Frequency16MHz,
236 startup_mode: sam4l::pm::OscillatorStartup::SlowStart,
237 },
238 &peripherals.flash_controller,
239 );
240
241 sam4l::bpm::set_ck32source(sam4l::bpm::CK32Source::RC32K);
243
244 set_pin_primary_functions(peripherals);
245 peripherals.setup_circular_deps();
246 let chip = static_init!(
247 sam4l::chip::Sam4l<Sam4lDefaultPeripherals>,
248 sam4l::chip::Sam4l::new(pm, peripherals)
249 );
250 CHIP = Some(chip);
251
252 let process_management_capability =
255 create_capability!(capabilities::ProcessManagementCapability);
256 let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
257
258 kernel::debug::assign_gpios(
260 Some(&peripherals.pa[13]),
261 Some(&peripherals.pa[15]),
262 Some(&peripherals.pa[14]),
263 );
264
265 let processes = components::process_array::ProcessArrayComponent::new()
267 .finalize(components::process_array_component_static!(NUM_PROCS));
268 PROCESSES = Some(processes);
269
270 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
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(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 fault_policy,
555 &process_management_capability,
556 )
557 .unwrap_or_else(|err| {
558 debug!("Error loading processes!");
559 debug!("{:?}", err);
560 });
561
562 (board_kernel, hail, chip)
563}
564
565#[no_mangle]
567pub unsafe fn main() {
568 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
569
570 let (board_kernel, platform, chip) = start();
571 board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
572}