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