1#![no_std]
10#![no_main]
11#![deny(missing_docs)]
12
13use core::ptr::addr_of_mut;
14
15use capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm;
16use capsules_extra::lsm303xx;
17use capsules_system::process_printer::ProcessPrinterText;
18use components::gpio::GpioComponent;
19use kernel::capabilities;
20use kernel::component::Component;
21use kernel::hil::gpio::Configure;
22use kernel::hil::gpio::Output;
23use kernel::hil::led::LedHigh;
24use kernel::hil::time::Counter;
25use kernel::platform::{KernelResources, SyscallDriverLookup};
26use kernel::process::ProcessArray;
27use kernel::scheduler::round_robin::RoundRobinSched;
28use kernel::{create_capability, debug, static_init};
29use stm32f303xc::chip::Stm32f3xxDefaultPeripherals;
30use stm32f303xc::wdt;
31
32pub mod io;
34
35#[allow(dead_code)]
37mod virtual_uart_rx_test;
38
39const NUM_PROCS: usize = 4;
41
42type ChipHw = stm32f303xc::chip::Stm32f3xx<'static, Stm32f3xxDefaultPeripherals<'static>>;
43
44static mut PROCESSES: Option<&'static ProcessArray<NUM_PROCS>> = None;
46
47static mut CHIP: Option<&'static ChipHw> = None;
49static mut PROCESS_PRINTER: Option<&'static ProcessPrinterText> = None;
51
52const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
54    capsules_system::process_policies::PanicFaultPolicy {};
55
56kernel::stack_size! {0x1700}
57
58type L3GD20Sensor = components::l3gd20::L3gd20ComponentType<
59    capsules_core::virtualizers::virtual_spi::VirtualSpiMasterDevice<
60        'static,
61        stm32f303xc::spi::Spi<'static>,
62    >,
63>;
64type TemperatureDriver = components::temperature::TemperatureComponentType<L3GD20Sensor>;
65
66struct STM32F3Discovery {
69    console: &'static capsules_core::console::Console<'static>,
70    ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
71    gpio: &'static capsules_core::gpio::GPIO<'static, stm32f303xc::gpio::Pin<'static>>,
72    led: &'static capsules_core::led::LedDriver<
73        'static,
74        LedHigh<'static, stm32f303xc::gpio::Pin<'static>>,
75        8,
76    >,
77    button: &'static capsules_core::button::Button<'static, stm32f303xc::gpio::Pin<'static>>,
78    ninedof: &'static capsules_extra::ninedof::NineDof<'static>,
79    l3gd20: &'static L3GD20Sensor,
80    lsm303dlhc: &'static capsules_extra::lsm303dlhc::Lsm303dlhcI2C<
81        'static,
82        capsules_core::virtualizers::virtual_i2c::I2CDevice<
83            'static,
84            stm32f303xc::i2c::I2C<'static>,
85        >,
86    >,
87    temp: &'static TemperatureDriver,
88    alarm: &'static capsules_core::alarm::AlarmDriver<
89        'static,
90        VirtualMuxAlarm<'static, stm32f303xc::tim2::Tim2<'static>>,
91    >,
92    adc: &'static capsules_core::adc::AdcVirtualized<'static>,
93    nonvolatile_storage:
94        &'static capsules_extra::nonvolatile_storage_driver::NonvolatileStorage<'static>,
95
96    scheduler: &'static RoundRobinSched<'static>,
97    systick: cortexm4::systick::SysTick,
98    watchdog: &'static wdt::WindoWdg<'static>,
99}
100
101impl SyscallDriverLookup for STM32F3Discovery {
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::led::DRIVER_NUM => f(Some(self.led)),
110            capsules_core::button::DRIVER_NUM => f(Some(self.button)),
111            capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
112            capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
113            capsules_extra::l3gd20::DRIVER_NUM => f(Some(self.l3gd20)),
114            capsules_extra::lsm303dlhc::DRIVER_NUM => f(Some(self.lsm303dlhc)),
115            capsules_extra::ninedof::DRIVER_NUM => f(Some(self.ninedof)),
116            capsules_extra::temperature::DRIVER_NUM => f(Some(self.temp)),
117            kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
118            capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
119            capsules_extra::nonvolatile_storage_driver::DRIVER_NUM => {
120                f(Some(self.nonvolatile_storage))
121            }
122            _ => f(None),
123        }
124    }
125}
126
127impl
128    KernelResources<
129        stm32f303xc::chip::Stm32f3xx<
130            'static,
131            stm32f303xc::chip::Stm32f3xxDefaultPeripherals<'static>,
132        >,
133    > for STM32F3Discovery
134{
135    type SyscallDriverLookup = Self;
136    type SyscallFilter = ();
137    type ProcessFault = ();
138    type Scheduler = RoundRobinSched<'static>;
139    type SchedulerTimer = cortexm4::systick::SysTick;
140    type WatchDog = wdt::WindoWdg<'static>;
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        self.watchdog
160    }
161    fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
162        &()
163    }
164}
165
166unsafe fn set_pin_primary_functions(
168    syscfg: &stm32f303xc::syscfg::Syscfg,
169    spi1: &stm32f303xc::spi::Spi,
170    i2c1: &stm32f303xc::i2c::I2C,
171    gpio_ports: &'static stm32f303xc::gpio::GpioPorts<'static>,
172) {
173    use stm32f303xc::gpio::{AlternateFunction, Mode, PinId, PortId};
174
175    syscfg.enable_clock();
176
177    gpio_ports.get_port_from_port_id(PortId::A).enable_clock();
178    gpio_ports.get_port_from_port_id(PortId::B).enable_clock();
179    gpio_ports.get_port_from_port_id(PortId::C).enable_clock();
180    gpio_ports.get_port_from_port_id(PortId::D).enable_clock();
181    gpio_ports.get_port_from_port_id(PortId::E).enable_clock();
182    gpio_ports.get_port_from_port_id(PortId::F).enable_clock();
183
184    gpio_ports.get_pin(PinId::PE14).map(|pin| {
185        pin.make_output();
186        pin.set();
187    });
188
189    gpio_ports.get_pin(PinId::PE09).map(|pin| {
191        pin.make_output();
192
193        kernel::debug::assign_gpios(Some(pin), None, None);
195    });
196
197    gpio_ports.get_pin(PinId::PC04).map(|pin| {
199        pin.set_mode(Mode::AlternateFunctionMode);
200        pin.set_alternate_function(AlternateFunction::AF7);
202    });
203    gpio_ports.get_pin(PinId::PC05).map(|pin| {
204        pin.set_mode(Mode::AlternateFunctionMode);
205        pin.set_alternate_function(AlternateFunction::AF7);
207    });
208
209    gpio_ports.get_pin(PinId::PA00).map(|pin| {
211        pin.enable_interrupt();
212    });
213
214    gpio_ports.get_pin(PinId::PC01).map(|pin| {
216        pin.enable_interrupt();
217    });
218
219    gpio_ports.get_pin(PinId::PA06).map(|pin| {
221        pin.set_mode(Mode::AlternateFunctionMode);
222        pin.set_floating_state(kernel::hil::gpio::FloatingState::PullNone);
223        pin.set_alternate_function(AlternateFunction::AF5);
225    });
226    gpio_ports.get_pin(PinId::PA07).map(|pin| {
227        pin.make_output();
228        pin.set_floating_state(kernel::hil::gpio::FloatingState::PullNone);
229        pin.set_mode(Mode::AlternateFunctionMode);
230        pin.set_alternate_function(AlternateFunction::AF5);
232    });
233    gpio_ports.get_pin(PinId::PA05).map(|pin| {
234        pin.make_output();
235        pin.set_floating_state(kernel::hil::gpio::FloatingState::PullNone);
236        pin.set_mode(Mode::AlternateFunctionMode);
237        pin.set_alternate_function(AlternateFunction::AF5);
239    });
240    gpio_ports.get_pin(PinId::PE03).map(|pin| {
242        pin.make_output();
243        pin.set_floating_state(kernel::hil::gpio::FloatingState::PullNone);
244        pin.set();
245    });
246
247    spi1.enable_clock();
248
249    gpio_ports.get_pin(PinId::PB06).map(|pin| {
251        pin.set_mode(Mode::AlternateFunctionMode);
252        pin.set_floating_state(kernel::hil::gpio::FloatingState::PullNone);
253        pin.set_alternate_function(AlternateFunction::AF4);
255    });
256    gpio_ports.get_pin(PinId::PB07).map(|pin| {
257        pin.make_output();
258        pin.set_floating_state(kernel::hil::gpio::FloatingState::PullNone);
259        pin.set_mode(Mode::AlternateFunctionMode);
260        pin.set_alternate_function(AlternateFunction::AF4);
262    });
263
264    gpio_ports.get_pin(PinId::PA01).map(|pin| {
272        pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
273    });
274
275    gpio_ports.get_pin(PinId::PA02).map(|pin| {
277        pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
278    });
279
280    gpio_ports.get_pin(PinId::PA03).map(|pin| {
282        pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
283    });
284
285    gpio_ports.get_pin(PinId::PF04).map(|pin| {
287        pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
288    });
289
290    i2c1.enable_clock();
346    i2c1.set_speed(stm32f303xc::i2c::I2CSpeed::Speed400k, 8);
347}
348
349unsafe fn setup_peripherals(tim2: &stm32f303xc::tim2::Tim2) {
351    cortexm4::nvic::Nvic::new(stm32f303xc::nvic::USART1).enable();
353    cortexm4::nvic::Nvic::new(stm32f303xc::nvic::USART2).enable();
355
356    tim2.enable_clock();
358    let _ = tim2.start();
359    cortexm4::nvic::Nvic::new(stm32f303xc::nvic::TIM2).enable();
360}
361
362#[inline(never)]
368unsafe fn start() -> (
369    &'static kernel::Kernel,
370    STM32F3Discovery,
371    &'static stm32f303xc::chip::Stm32f3xx<'static, Stm32f3xxDefaultPeripherals<'static>>,
372) {
373    stm32f303xc::init();
374
375    let rcc = static_init!(stm32f303xc::rcc::Rcc, stm32f303xc::rcc::Rcc::new());
377    let syscfg = static_init!(
378        stm32f303xc::syscfg::Syscfg,
379        stm32f303xc::syscfg::Syscfg::new(rcc)
380    );
381    let exti = static_init!(
382        stm32f303xc::exti::Exti,
383        stm32f303xc::exti::Exti::new(syscfg)
384    );
385
386    let peripherals = static_init!(
387        Stm32f3xxDefaultPeripherals,
388        Stm32f3xxDefaultPeripherals::new(rcc, exti)
389    );
390
391    peripherals.setup_circular_deps();
392
393    set_pin_primary_functions(
394        syscfg,
395        &peripherals.spi1,
396        &peripherals.i2c1,
397        &peripherals.gpio_ports,
398    );
399
400    setup_peripherals(&peripherals.tim2);
401
402    let processes = components::process_array::ProcessArrayComponent::new()
404        .finalize(components::process_array_component_static!(NUM_PROCS));
405    PROCESSES = Some(processes);
406
407    let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
409
410    let chip = static_init!(
411        stm32f303xc::chip::Stm32f3xx<Stm32f3xxDefaultPeripherals>,
412        stm32f303xc::chip::Stm32f3xx::new(peripherals)
413    );
414    CHIP = Some(chip);
415
416    peripherals.usart1.enable_clock();
420    peripherals.usart2.enable_clock();
421
422    let uart_mux = components::console::UartMuxComponent::new(&peripherals.usart1, 115200)
423        .finalize(components::uart_mux_component_static!());
424
425    (*addr_of_mut!(io::WRITER)).set_initialized();
428
429    let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
432    let process_management_capability =
433        create_capability!(capabilities::ProcessManagementCapability);
434
435    let console = components::console::ConsoleComponent::new(
437        board_kernel,
438        capsules_core::console::DRIVER_NUM,
439        uart_mux,
440    )
441    .finalize(components::console_component_static!());
442    components::debug_writer::DebugWriterComponent::new::<
444        <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
445    >(
446        uart_mux,
447        create_capability!(capabilities::SetDebugWriterCapability),
448    )
449    .finalize(components::debug_writer_component_static!());
450
451    let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
456        LedHigh<'static, stm32f303xc::gpio::Pin<'static>>,
457        LedHigh::new(
458            peripherals
459                .gpio_ports
460                .get_pin(stm32f303xc::gpio::PinId::PE09)
461                .unwrap()
462        ),
463        LedHigh::new(
464            peripherals
465                .gpio_ports
466                .get_pin(stm32f303xc::gpio::PinId::PE08)
467                .unwrap()
468        ),
469        LedHigh::new(
470            peripherals
471                .gpio_ports
472                .get_pin(stm32f303xc::gpio::PinId::PE10)
473                .unwrap()
474        ),
475        LedHigh::new(
476            peripherals
477                .gpio_ports
478                .get_pin(stm32f303xc::gpio::PinId::PE15)
479                .unwrap()
480        ),
481        LedHigh::new(
482            peripherals
483                .gpio_ports
484                .get_pin(stm32f303xc::gpio::PinId::PE11)
485                .unwrap()
486        ),
487        LedHigh::new(
488            peripherals
489                .gpio_ports
490                .get_pin(stm32f303xc::gpio::PinId::PE14)
491                .unwrap()
492        ),
493        LedHigh::new(
494            peripherals
495                .gpio_ports
496                .get_pin(stm32f303xc::gpio::PinId::PE12)
497                .unwrap()
498        ),
499        LedHigh::new(
500            peripherals
501                .gpio_ports
502                .get_pin(stm32f303xc::gpio::PinId::PE13)
503                .unwrap()
504        ),
505    ));
506
507    let button = components::button::ButtonComponent::new(
509        board_kernel,
510        capsules_core::button::DRIVER_NUM,
511        components::button_component_helper!(
512            stm32f303xc::gpio::Pin<'static>,
513            (
514                peripherals
515                    .gpio_ports
516                    .get_pin(stm32f303xc::gpio::PinId::PA00)
517                    .unwrap(),
518                kernel::hil::gpio::ActivationMode::ActiveHigh,
519                kernel::hil::gpio::FloatingState::PullNone
520            )
521        ),
522    )
523    .finalize(components::button_component_static!(
524        stm32f303xc::gpio::Pin<'static>
525    ));
526
527    let tim2 = &peripherals.tim2;
530    let mux_alarm = components::alarm::AlarmMuxComponent::new(tim2).finalize(
531        components::alarm_mux_component_static!(stm32f303xc::tim2::Tim2),
532    );
533
534    let alarm = components::alarm::AlarmDriverComponent::new(
535        board_kernel,
536        capsules_core::alarm::DRIVER_NUM,
537        mux_alarm,
538    )
539    .finalize(components::alarm_component_static!(stm32f303xc::tim2::Tim2));
540
541    let gpio_ports = &peripherals.gpio_ports;
542    let gpio = GpioComponent::new(
544        board_kernel,
545        capsules_core::gpio::DRIVER_NUM,
546        components::gpio_component_helper!(
547            stm32f303xc::gpio::Pin<'static>,
548            0 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC01).unwrap(),
550            1 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC03).unwrap(),
551            9 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE07).unwrap(),
559            11 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE11).unwrap(),
561            14 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB11).unwrap(),
564            17 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD09).unwrap(),
567            18 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD11).unwrap(),
568            19 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD13).unwrap(),
569            20 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD15).unwrap(),
570            21 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC06).unwrap(),
571            22 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC00).unwrap(),
573            23 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC02).unwrap(),
574            24 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PF02).unwrap(),
575            30 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB00).unwrap(),
581            31 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB02).unwrap(),
582            32 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE08).unwrap(),
583            33 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE10).unwrap(),
584            34 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE12).unwrap(),
585            36 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB10).unwrap(),
587            39 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD08).unwrap(),
590            40 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD10).unwrap(),
591            41 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD12).unwrap(),
592            42 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD14).unwrap(),
593            43 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC07).unwrap(),
594            44 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PF09).unwrap(),
596            45 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PF00).unwrap(),
597            46 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC14).unwrap(),
598            47 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE06).unwrap(),
599            48 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE04).unwrap(),
600            49 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE02).unwrap(),
601            50 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE00).unwrap(),
602            51 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB08).unwrap(),
603            53 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB04).unwrap(),
605            54 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD07).unwrap(),
606            55 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD05).unwrap(),
607            56 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD03).unwrap(),
608            57 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD01).unwrap(),
609            58 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC12).unwrap(),
610            59 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC10).unwrap(),
611            60 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA14).unwrap(),
612            61 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PF06).unwrap(),
613            62 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA12).unwrap(),
614            63 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA10).unwrap(),
615            64 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA08).unwrap(),
616            65 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC08).unwrap(),
617            66 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PF10).unwrap(),
619            67 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PF01).unwrap(),
620            68 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC15).unwrap(),
621            69 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC13).unwrap(),
622            70 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE05).unwrap(),
623            71 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE03).unwrap(),
624            72 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE01).unwrap(),
625            73 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB09).unwrap(),
626            75 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB05).unwrap(),
628            76 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB03).unwrap(),
629            77 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD06).unwrap(),
630            78 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD04).unwrap(),
631            79 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD02).unwrap(),
632            80 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD00).unwrap(),
633            81 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC11).unwrap(),
634            82 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA15).unwrap(),
635            83 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA13).unwrap(),
636            84 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA11).unwrap(),
637            85 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA09).unwrap(),
638            86 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC09).unwrap()
639        ),
640    )
641    .finalize(components::gpio_component_static!(
642        stm32f303xc::gpio::Pin<'static>
643    ));
644
645    let spi_mux = components::spi::SpiMuxComponent::new(&peripherals.spi1)
647        .finalize(components::spi_mux_component_static!(stm32f303xc::spi::Spi));
648
649    let l3gd20 = components::l3gd20::L3gd20Component::new(
650        spi_mux,
651        gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE03).unwrap(),
652        board_kernel,
653        capsules_extra::l3gd20::DRIVER_NUM,
654    )
655    .finalize(components::l3gd20_component_static!(
656        stm32f303xc::spi::Spi
658    ));
659
660    l3gd20.power_on();
661
662    let temp = components::temperature::TemperatureComponent::new(
664        board_kernel,
665        capsules_extra::temperature::DRIVER_NUM,
666        l3gd20,
667    )
668    .finalize(components::temperature_component_static!(L3GD20Sensor));
669
670    let mux_i2c = components::i2c::I2CMuxComponent::new(&peripherals.i2c1, None)
673        .finalize(components::i2c_mux_component_static!(stm32f303xc::i2c::I2C));
674
675    let lsm303dlhc = components::lsm303dlhc::Lsm303dlhcI2CComponent::new(
676        mux_i2c,
677        None,
678        None,
679        board_kernel,
680        capsules_extra::lsm303dlhc::DRIVER_NUM,
681    )
682    .finalize(components::lsm303dlhc_component_static!(
683        stm32f303xc::i2c::I2C
684    ));
685
686    if let Err(error) = lsm303dlhc.configure(
687        lsm303xx::Lsm303AccelDataRate::DataRate25Hz,
688        false,
689        lsm303xx::Lsm303Scale::Scale2G,
690        false,
691        true,
692        lsm303xx::Lsm303MagnetoDataRate::DataRate3_0Hz,
693        lsm303xx::Lsm303Range::Range1_9G,
694    ) {
695        debug!("Failed to configure LSM303DLHC sensor ({:?})", error);
696    }
697
698    let ninedof = components::ninedof::NineDofComponent::new(
699        board_kernel,
700        capsules_extra::ninedof::DRIVER_NUM,
701    )
702    .finalize(components::ninedof_component_static!(l3gd20, lsm303dlhc));
703
704    let adc_mux = components::adc::AdcMuxComponent::new(&peripherals.adc1)
705        .finalize(components::adc_mux_component_static!(stm32f303xc::adc::Adc));
706
707    let adc_channel_2 =
732        components::adc::AdcComponent::new(adc_mux, stm32f303xc::adc::Channel::Channel2)
733            .finalize(components::adc_component_static!(stm32f303xc::adc::Adc));
734
735    let adc_channel_3 =
736        components::adc::AdcComponent::new(adc_mux, stm32f303xc::adc::Channel::Channel3)
737            .finalize(components::adc_component_static!(stm32f303xc::adc::Adc));
738
739    let adc_channel_4 =
740        components::adc::AdcComponent::new(adc_mux, stm32f303xc::adc::Channel::Channel4)
741            .finalize(components::adc_component_static!(stm32f303xc::adc::Adc));
742
743    let adc_channel_5 =
744        components::adc::AdcComponent::new(adc_mux, stm32f303xc::adc::Channel::Channel5)
745            .finalize(components::adc_component_static!(stm32f303xc::adc::Adc));
746
747    let adc_syscall =
748        components::adc::AdcVirtualComponent::new(board_kernel, capsules_core::adc::DRIVER_NUM)
749            .finalize(components::adc_syscall_component_helper!(
750                adc_channel_2,
751                adc_channel_3,
752                adc_channel_4,
753                adc_channel_5,
754            ));
755
756    extern "C" {
759        static _sstorage: u8;
761        static _estorage: u8;
762    }
763
764    let nonvolatile_storage = components::nonvolatile_storage::NonvolatileStorageComponent::new(
765        board_kernel,
766        capsules_extra::nonvolatile_storage_driver::DRIVER_NUM,
767        &peripherals.flash,
768        0x08038000, 0x8000,     core::ptr::addr_of!(_sstorage) as usize,
771        core::ptr::addr_of!(_estorage) as usize - core::ptr::addr_of!(_sstorage) as usize,
772    )
773    .finalize(components::nonvolatile_storage_component_static!(
774        stm32f303xc::flash::Flash
775    ));
776
777    let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
778        .finalize(components::process_printer_text_component_static!());
779    PROCESS_PRINTER = Some(process_printer);
780
781    let process_console = components::process_console::ProcessConsoleComponent::new(
783        board_kernel,
784        uart_mux,
785        mux_alarm,
786        process_printer,
787        Some(cortexm4::support::reset),
788    )
789    .finalize(components::process_console_component_static!(
790        stm32f303xc::tim2::Tim2
791    ));
792    let _ = process_console.start();
793
794    let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
795        .finalize(components::round_robin_component_static!(NUM_PROCS));
796
797    let stm32f3discovery = STM32F3Discovery {
798        console,
799        ipc: kernel::ipc::IPC::new(
800            board_kernel,
801            kernel::ipc::DRIVER_NUM,
802            &memory_allocation_capability,
803        ),
804        gpio,
805        led,
806        button,
807        alarm,
808        l3gd20,
809        lsm303dlhc,
810        ninedof,
811        temp,
812        adc: adc_syscall,
813        nonvolatile_storage,
814
815        scheduler,
816        systick: cortexm4::systick::SysTick::new_with_calibration(8_000_000),
818        watchdog: &peripherals.watchdog,
819    };
820
821    debug!("Initialization complete. Entering main loop");
827
828    extern "C" {
830        static _sapps: u8;
832        static _eapps: u8;
834        static mut _sappmem: u8;
836        static _eappmem: u8;
838    }
839
840    kernel::process::load_processes(
841        board_kernel,
842        chip,
843        core::slice::from_raw_parts(
844            core::ptr::addr_of!(_sapps),
845            core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
846        ),
847        core::slice::from_raw_parts_mut(
848            core::ptr::addr_of_mut!(_sappmem),
849            core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
850        ),
851        &FAULT_RESPONSE,
852        &process_management_capability,
853    )
854    .unwrap_or_else(|err| {
855        debug!("Error loading processes!");
856        debug!("{:?}", err);
857    });
858
859    peripherals.watchdog.enable();
861
862    (board_kernel, stm32f3discovery, chip)
868}
869
870#[no_mangle]
872pub unsafe fn main() {
873    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
874
875    let (board_kernel, platform, chip) = start();
876    board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
877}