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