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
38type ChipHw = sam4l::chip::Sam4l<Sam4lDefaultPeripherals>;
39
40static mut PROCESSES: Option<&'static ProcessArray<NUM_PROCS>> = None;
42static mut CHIP: Option<&'static sam4l::chip::Sam4l<Sam4lDefaultPeripherals>> = None;
43static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
44    None;
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    let pm = static_init!(sam4l::pm::PowerManager, sam4l::pm::PowerManager::new());
233    let peripherals = static_init!(Sam4lDefaultPeripherals, Sam4lDefaultPeripherals::new(pm));
234
235    pm.setup_system_clock(
236        sam4l::pm::SystemClockSource::PllExternalOscillatorAt48MHz {
237            frequency: sam4l::pm::OscillatorFrequency::Frequency16MHz,
238            startup_mode: sam4l::pm::OscillatorStartup::SlowStart,
239        },
240        &peripherals.flash_controller,
241    );
242
243    sam4l::bpm::set_ck32source(sam4l::bpm::CK32Source::RC32K);
245
246    set_pin_primary_functions(peripherals);
247    peripherals.setup_circular_deps();
248    let chip = static_init!(
249        sam4l::chip::Sam4l<Sam4lDefaultPeripherals>,
250        sam4l::chip::Sam4l::new(pm, peripherals)
251    );
252    CHIP = Some(chip);
253
254    let process_management_capability =
257        create_capability!(capabilities::ProcessManagementCapability);
258    let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
259
260    kernel::debug::assign_gpios(
262        Some(&peripherals.pa[13]),
263        Some(&peripherals.pa[15]),
264        Some(&peripherals.pa[14]),
265    );
266
267    let processes = components::process_array::ProcessArrayComponent::new()
269        .finalize(components::process_array_component_static!(NUM_PROCS));
270    PROCESSES = Some(processes);
271
272    let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
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::<
312        <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
313    >(
314        uart_mux,
315        create_capability!(capabilities::SetDebugWriterCapability),
316    )
317    .finalize(components::debug_writer_component_static!());
318
319    peripherals.usart3.set_mode(sam4l::usart::UsartMode::Uart);
321    let nrf_serialization = components::nrf51822::Nrf51822Component::new(
324        board_kernel,
325        capsules_extra::nrf51822_serialization::DRIVER_NUM,
326        &peripherals.usart3,
327        &peripherals.pa[17],
328    )
329    .finalize(components::nrf51822_component_static!());
330
331    let sensors_i2c = components::i2c::I2CMuxComponent::new(&peripherals.i2c1, None)
332        .finalize(components::i2c_mux_component_static!(sam4l::i2c::I2CHw));
333
334    let si7021 = components::si7021::SI7021Component::new(sensors_i2c, mux_alarm, 0x40).finalize(
336        components::si7021_component_static!(sam4l::ast::Ast, sam4l::i2c::I2CHw),
337    );
338    let temp = components::temperature::TemperatureComponent::new(
339        board_kernel,
340        capsules_extra::temperature::DRIVER_NUM,
341        si7021,
342    )
343    .finalize(components::temperature_component_static!(SI7021Sensor));
344    let humidity = components::humidity::HumidityComponent::new(
345        board_kernel,
346        capsules_extra::humidity::DRIVER_NUM,
347        si7021,
348    )
349    .finalize(components::humidity_component_static!(SI7021Sensor));
350
351    let isl29035 = components::isl29035::Isl29035Component::new(sensors_i2c, mux_alarm).finalize(
353        components::isl29035_component_static!(sam4l::ast::Ast, sam4l::i2c::I2CHw),
354    );
355    let ambient_light = components::isl29035::AmbientLightComponent::new(
356        board_kernel,
357        capsules_extra::ambient_light::DRIVER_NUM,
358        isl29035,
359    )
360    .finalize(components::ambient_light_component_static!());
361
362    let alarm = components::alarm::AlarmDriverComponent::new(
364        board_kernel,
365        capsules_core::alarm::DRIVER_NUM,
366        mux_alarm,
367    )
368    .finalize(components::alarm_component_static!(sam4l::ast::Ast));
369
370    let fxos8700 =
372        components::fxos8700::Fxos8700Component::new(sensors_i2c, 0x1e, &peripherals.pa[9])
373            .finalize(components::fxos8700_component_static!(sam4l::i2c::I2CHw));
374
375    let ninedof = components::ninedof::NineDofComponent::new(
376        board_kernel,
377        capsules_extra::ninedof::DRIVER_NUM,
378    )
379    .finalize(components::ninedof_component_static!(fxos8700));
380
381    let mux_spi = components::spi::SpiMuxComponent::new(&peripherals.spi)
384        .finalize(components::spi_mux_component_static!(sam4l::spi::SpiHw));
385    let spi_syscalls = components::spi::SpiSyscallComponent::new(
387        board_kernel,
388        mux_spi,
389        sam4l::spi::Peripheral::Peripheral0,
390        capsules_core::spi_controller::DRIVER_NUM,
391    )
392    .finalize(components::spi_syscall_component_static!(sam4l::spi::SpiHw));
393
394    let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
396        LedLow<'static, sam4l::gpio::GPIOPin>,
397        LedLow::new(&peripherals.pa[13]), LedLow::new(&peripherals.pa[15]), LedLow::new(&peripherals.pa[14]), ));
401
402    let button = components::button::ButtonComponent::new(
404        board_kernel,
405        capsules_core::button::DRIVER_NUM,
406        components::button_component_helper!(
407            sam4l::gpio::GPIOPin,
408            (
409                &peripherals.pa[16],
410                kernel::hil::gpio::ActivationMode::ActiveLow,
411                kernel::hil::gpio::FloatingState::PullNone
412            )
413        ),
414    )
415    .finalize(components::button_component_static!(sam4l::gpio::GPIOPin));
416
417    let adc_channels = static_init!(
419        [sam4l::adc::AdcChannel; 6],
420        [
421            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), ]
428    );
429    let adc = components::adc::AdcDedicatedComponent::new(
430        &peripherals.adc,
431        adc_channels,
432        board_kernel,
433        capsules_core::adc::DRIVER_NUM,
434    )
435    .finalize(components::adc_dedicated_component_static!(sam4l::adc::Adc));
436
437    let rng = components::rng::RngComponent::new(
439        board_kernel,
440        capsules_core::rng::DRIVER_NUM,
441        &peripherals.trng,
442    )
443    .finalize(components::rng_component_static!(sam4l::trng::Trng));
444
445    let gpio = components::gpio::GpioComponent::new(
447        board_kernel,
448        capsules_core::gpio::DRIVER_NUM,
449        components::gpio_component_helper!(
450            sam4l::gpio::GPIOPin,
451            0 => &peripherals.pb[14], 1 => &peripherals.pb[15], 2 => &peripherals.pb[11], 3 => &peripherals.pb[12]  ),
456    )
457    .finalize(components::gpio_component_static!(sam4l::gpio::GPIOPin));
458
459    let crc = components::crc::CrcComponent::new(
461        board_kernel,
462        capsules_extra::crc::DRIVER_NUM,
463        &peripherals.crccu,
464    )
465    .finalize(components::crc_component_static!(sam4l::crccu::Crccu));
466
467    let dac = components::dac::DacComponent::new(&peripherals.dac)
469        .finalize(components::dac_component_static!());
470
471    let fault_policy = static_init!(
493        capsules_system::process_policies::ThresholdRestartThenPanicFaultPolicy,
494        capsules_system::process_policies::ThresholdRestartThenPanicFaultPolicy::new(4)
495    );
496
497    let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
498        .finalize(components::round_robin_component_static!(NUM_PROCS));
499
500    let hail = Hail {
501        console,
502        gpio,
503        alarm,
504        ambient_light,
505        temp,
506        humidity,
507        ninedof,
508        spi: spi_syscalls,
509        nrf51822: nrf_serialization,
510        adc,
511        led,
512        button,
513        rng,
514        ipc: kernel::ipc::IPC::new(
515            board_kernel,
516            kernel::ipc::DRIVER_NUM,
517            &memory_allocation_capability,
518        ),
519        crc,
520        dac,
521        scheduler,
522        systick: cortexm4::systick::SysTick::new(),
523    };
524
525    hail.nrf51822.initialize();
527
528    let _ = process_console.start();
529
530    debug!("Initialization complete. Entering main loop.");
534
535    extern "C" {
537        static _sapps: u8;
539        static _eapps: u8;
541        static mut _sappmem: u8;
543        static _eappmem: u8;
545    }
546
547    kernel::process::load_processes(
548        board_kernel,
549        chip,
550        core::slice::from_raw_parts(
551            core::ptr::addr_of!(_sapps),
552            core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
553        ),
554        core::slice::from_raw_parts_mut(
555            core::ptr::addr_of_mut!(_sappmem),
556            core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
557        ),
558        fault_policy,
559        &process_management_capability,
560    )
561    .unwrap_or_else(|err| {
562        debug!("Error loading processes!");
563        debug!("{:?}", err);
564    });
565
566    (board_kernel, hail, chip)
567}
568
569#[no_mangle]
571pub unsafe fn main() {
572    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
573
574    let (board_kernel, platform, chip) = start();
575    board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
576}