stm32f3discovery/
main.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Board file for STM32F3Discovery Kit development board
6//!
7//! - <https://www.st.com/en/evaluation-tools/stm32f3discovery.html>
8
9#![no_std]
10// Disable this attribute when documenting, as a workaround for
11// https://github.com/rust-lang/rust/issues/62184.
12#![cfg_attr(not(doc), no_main)]
13#![deny(missing_docs)]
14
15use core::ptr::addr_of;
16use core::ptr::addr_of_mut;
17
18use capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm;
19use capsules_extra::lsm303xx;
20use capsules_system::process_printer::ProcessPrinterText;
21use components::gpio::GpioComponent;
22use kernel::capabilities;
23use kernel::component::Component;
24use kernel::hil::gpio::Configure;
25use kernel::hil::gpio::Output;
26use kernel::hil::led::LedHigh;
27use kernel::hil::time::Counter;
28use kernel::platform::{KernelResources, SyscallDriverLookup};
29use kernel::scheduler::round_robin::RoundRobinSched;
30use kernel::{create_capability, debug, static_init};
31use stm32f303xc::chip::Stm32f3xxDefaultPeripherals;
32use stm32f303xc::wdt;
33
34/// Support routines for debugging I/O.
35pub mod io;
36
37// Unit Tests for drivers.
38#[allow(dead_code)]
39mod virtual_uart_rx_test;
40
41// Number of concurrent processes this platform supports.
42const NUM_PROCS: usize = 4;
43
44// Actual memory for holding the active process structures.
45static mut PROCESSES: [Option<&'static dyn kernel::process::Process>; NUM_PROCS] =
46    [None, None, None, None];
47
48// Static reference to chip for panic dumps.
49static mut CHIP: Option<&'static stm32f303xc::chip::Stm32f3xx<Stm32f3xxDefaultPeripherals>> = None;
50// Static reference to process printer for panic dumps.
51static mut PROCESS_PRINTER: Option<&'static ProcessPrinterText> = None;
52
53// How should the kernel respond when a process faults.
54const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
55    capsules_system::process_policies::PanicFaultPolicy {};
56
57/// Dummy buffer that causes the linker to reserve enough space for the stack.
58#[no_mangle]
59#[link_section = ".stack_buffer"]
60pub static mut STACK_MEMORY: [u8; 0x1700] = [0; 0x1700];
61
62type L3GD20Sensor = components::l3gd20::L3gd20ComponentType<
63    capsules_core::virtualizers::virtual_spi::VirtualSpiMasterDevice<
64        'static,
65        stm32f303xc::spi::Spi<'static>,
66    >,
67>;
68type TemperatureDriver = components::temperature::TemperatureComponentType<L3GD20Sensor>;
69
70/// A structure representing this platform that holds references to all
71/// capsules for this platform.
72struct STM32F3Discovery {
73    console: &'static capsules_core::console::Console<'static>,
74    ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
75    gpio: &'static capsules_core::gpio::GPIO<'static, stm32f303xc::gpio::Pin<'static>>,
76    led: &'static capsules_core::led::LedDriver<
77        'static,
78        LedHigh<'static, stm32f303xc::gpio::Pin<'static>>,
79        8,
80    >,
81    button: &'static capsules_core::button::Button<'static, stm32f303xc::gpio::Pin<'static>>,
82    ninedof: &'static capsules_extra::ninedof::NineDof<'static>,
83    l3gd20: &'static L3GD20Sensor,
84    lsm303dlhc: &'static capsules_extra::lsm303dlhc::Lsm303dlhcI2C<
85        'static,
86        capsules_core::virtualizers::virtual_i2c::I2CDevice<
87            'static,
88            stm32f303xc::i2c::I2C<'static>,
89        >,
90    >,
91    temp: &'static TemperatureDriver,
92    alarm: &'static capsules_core::alarm::AlarmDriver<
93        'static,
94        VirtualMuxAlarm<'static, stm32f303xc::tim2::Tim2<'static>>,
95    >,
96    adc: &'static capsules_core::adc::AdcVirtualized<'static>,
97    nonvolatile_storage:
98        &'static capsules_extra::nonvolatile_storage_driver::NonvolatileStorage<'static>,
99
100    scheduler: &'static RoundRobinSched<'static>,
101    systick: cortexm4::systick::SysTick,
102    watchdog: &'static wdt::WindoWdg<'static>,
103}
104
105/// Mapping of integer syscalls to objects that implement syscalls.
106impl SyscallDriverLookup for STM32F3Discovery {
107    fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
108    where
109        F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
110    {
111        match driver_num {
112            capsules_core::console::DRIVER_NUM => f(Some(self.console)),
113            capsules_core::led::DRIVER_NUM => f(Some(self.led)),
114            capsules_core::button::DRIVER_NUM => f(Some(self.button)),
115            capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
116            capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
117            capsules_extra::l3gd20::DRIVER_NUM => f(Some(self.l3gd20)),
118            capsules_extra::lsm303dlhc::DRIVER_NUM => f(Some(self.lsm303dlhc)),
119            capsules_extra::ninedof::DRIVER_NUM => f(Some(self.ninedof)),
120            capsules_extra::temperature::DRIVER_NUM => f(Some(self.temp)),
121            kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
122            capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
123            capsules_extra::nonvolatile_storage_driver::DRIVER_NUM => {
124                f(Some(self.nonvolatile_storage))
125            }
126            _ => f(None),
127        }
128    }
129}
130
131impl
132    KernelResources<
133        stm32f303xc::chip::Stm32f3xx<
134            'static,
135            stm32f303xc::chip::Stm32f3xxDefaultPeripherals<'static>,
136        >,
137    > for STM32F3Discovery
138{
139    type SyscallDriverLookup = Self;
140    type SyscallFilter = ();
141    type ProcessFault = ();
142    type Scheduler = RoundRobinSched<'static>;
143    type SchedulerTimer = cortexm4::systick::SysTick;
144    type WatchDog = wdt::WindoWdg<'static>;
145    type ContextSwitchCallback = ();
146
147    fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
148        self
149    }
150    fn syscall_filter(&self) -> &Self::SyscallFilter {
151        &()
152    }
153    fn process_fault(&self) -> &Self::ProcessFault {
154        &()
155    }
156    fn scheduler(&self) -> &Self::Scheduler {
157        self.scheduler
158    }
159    fn scheduler_timer(&self) -> &Self::SchedulerTimer {
160        &self.systick
161    }
162    fn watchdog(&self) -> &Self::WatchDog {
163        self.watchdog
164    }
165    fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
166        &()
167    }
168}
169
170/// Helper function called during bring-up that configures multiplexed I/O.
171unsafe fn set_pin_primary_functions(
172    syscfg: &stm32f303xc::syscfg::Syscfg,
173    spi1: &stm32f303xc::spi::Spi,
174    i2c1: &stm32f303xc::i2c::I2C,
175    gpio_ports: &'static stm32f303xc::gpio::GpioPorts<'static>,
176) {
177    use stm32f303xc::gpio::{AlternateFunction, Mode, PinId, PortId};
178
179    syscfg.enable_clock();
180
181    gpio_ports.get_port_from_port_id(PortId::A).enable_clock();
182    gpio_ports.get_port_from_port_id(PortId::B).enable_clock();
183    gpio_ports.get_port_from_port_id(PortId::C).enable_clock();
184    gpio_ports.get_port_from_port_id(PortId::D).enable_clock();
185    gpio_ports.get_port_from_port_id(PortId::E).enable_clock();
186    gpio_ports.get_port_from_port_id(PortId::F).enable_clock();
187
188    gpio_ports.get_pin(PinId::PE14).map(|pin| {
189        pin.make_output();
190        pin.set();
191    });
192
193    // User LD3 is connected to PE09. Configure PE09 as `debug_gpio!(0, ...)`
194    gpio_ports.get_pin(PinId::PE09).map(|pin| {
195        pin.make_output();
196
197        // Configure kernel debug gpios as early as possible
198        kernel::debug::assign_gpios(Some(pin), None, None);
199    });
200
201    // pc4 and pc5 (USART1) is connected to ST-LINK virtual COM port
202    gpio_ports.get_pin(PinId::PC04).map(|pin| {
203        pin.set_mode(Mode::AlternateFunctionMode);
204        // AF7 is USART1_TX
205        pin.set_alternate_function(AlternateFunction::AF7);
206    });
207    gpio_ports.get_pin(PinId::PC05).map(|pin| {
208        pin.set_mode(Mode::AlternateFunctionMode);
209        // AF7 is USART1_RX
210        pin.set_alternate_function(AlternateFunction::AF7);
211    });
212
213    // button is connected on pa00
214    gpio_ports.get_pin(PinId::PA00).map(|pin| {
215        pin.enable_interrupt();
216    });
217
218    // enable interrupt for gpio 0
219    gpio_ports.get_pin(PinId::PC01).map(|pin| {
220        pin.enable_interrupt();
221    });
222
223    // SPI1 has the l3gd20 sensor connected
224    gpio_ports.get_pin(PinId::PA06).map(|pin| {
225        pin.set_mode(Mode::AlternateFunctionMode);
226        pin.set_floating_state(kernel::hil::gpio::FloatingState::PullNone);
227        // AF5 is SPI1/SPI2
228        pin.set_alternate_function(AlternateFunction::AF5);
229    });
230    gpio_ports.get_pin(PinId::PA07).map(|pin| {
231        pin.make_output();
232        pin.set_floating_state(kernel::hil::gpio::FloatingState::PullNone);
233        pin.set_mode(Mode::AlternateFunctionMode);
234        // AF5 is SPI1/SPI2
235        pin.set_alternate_function(AlternateFunction::AF5);
236    });
237    gpio_ports.get_pin(PinId::PA05).map(|pin| {
238        pin.make_output();
239        pin.set_floating_state(kernel::hil::gpio::FloatingState::PullNone);
240        pin.set_mode(Mode::AlternateFunctionMode);
241        // AF5 is SPI1/SPI2
242        pin.set_alternate_function(AlternateFunction::AF5);
243    });
244    // PE03 is the chip select pin from the l3gd20 sensor
245    gpio_ports.get_pin(PinId::PE03).map(|pin| {
246        pin.make_output();
247        pin.set_floating_state(kernel::hil::gpio::FloatingState::PullNone);
248        pin.set();
249    });
250
251    spi1.enable_clock();
252
253    // I2C1 has the LSM303DLHC sensor connected
254    gpio_ports.get_pin(PinId::PB06).map(|pin| {
255        pin.set_mode(Mode::AlternateFunctionMode);
256        pin.set_floating_state(kernel::hil::gpio::FloatingState::PullNone);
257        // AF4 is I2C
258        pin.set_alternate_function(AlternateFunction::AF4);
259    });
260    gpio_ports.get_pin(PinId::PB07).map(|pin| {
261        pin.make_output();
262        pin.set_floating_state(kernel::hil::gpio::FloatingState::PullNone);
263        pin.set_mode(Mode::AlternateFunctionMode);
264        // AF4 is I2C
265        pin.set_alternate_function(AlternateFunction::AF4);
266    });
267
268    // ADC1
269    // channel 1 - shared with button
270    // gpio_ports.get_pin(PinId::PA00).map(|pin| {
271    //     pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
272    // });
273
274    // channel 2
275    gpio_ports.get_pin(PinId::PA01).map(|pin| {
276        pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
277    });
278
279    // channel 3
280    gpio_ports.get_pin(PinId::PA02).map(|pin| {
281        pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
282    });
283
284    // channel 4
285    gpio_ports.get_pin(PinId::PA03).map(|pin| {
286        pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
287    });
288
289    // channel 5
290    gpio_ports.get_pin(PinId::PF04).map(|pin| {
291        pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
292    });
293
294    // ADC2
295    // gpio_ports.get_pin(PinId::PA04).map(|pin| {
296    //     pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
297    // });
298
299    // gpio_ports.get_pin(PinId::PA05).map(|pin| {
300    //     pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
301    // });
302
303    // gpio_ports.get_pin(PinId::PA06).map(|pin| {
304    //     pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
305    // });
306
307    // gpio_ports.get_pin(PinId::PA07).map(|pin| {
308    //     pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
309    // });
310
311    // ADC3
312    // gpio_ports.get_pin(PinId::PB01).map(|pin| {
313    //     pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
314    // });
315
316    // gpio_ports.get_pin(PinId::PE09).map(|pin| {
317    //     pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
318    // });
319
320    // gpio_ports.get_pin(PinId::PE13).map(|pin| {
321    //     pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
322    // });
323
324    // gpio_ports.get_pin(PinId::PB13).map(|pin| {
325    //     pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
326    // });
327
328    // ADC4
329    // gpio_ports.get_pin(PinId::PE14).map(|pin| {
330    //     pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
331    // });
332
333    // gpio_ports.get_pin(PinId::PE15).map(|pin| {
334    //     pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
335    // });
336
337    // gpio_ports.get_pin(PinId::PB12).map(|pin| {
338    //     pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
339    // });
340
341    // gpio_ports.get_pin(PinId::PB14).map(|pin| {
342    //     pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
343    // });
344
345    // gpio_ports.get_pin(PinId::PB15).map(|pin| {
346    //     pin.set_mode(stm32f303xc::gpio::Mode::AnalogMode);
347    // });
348
349    i2c1.enable_clock();
350    i2c1.set_speed(stm32f303xc::i2c::I2CSpeed::Speed400k, 8);
351}
352
353/// Helper function for miscellaneous peripheral functions
354unsafe fn setup_peripherals(tim2: &stm32f303xc::tim2::Tim2) {
355    // USART1 IRQn is 37
356    cortexm4::nvic::Nvic::new(stm32f303xc::nvic::USART1).enable();
357    // USART2 IRQn is 38
358    cortexm4::nvic::Nvic::new(stm32f303xc::nvic::USART2).enable();
359
360    // TIM2 IRQn is 28
361    tim2.enable_clock();
362    let _ = tim2.start();
363    cortexm4::nvic::Nvic::new(stm32f303xc::nvic::TIM2).enable();
364}
365
366/// Main function.
367///
368/// This is in a separate, inline(never) function so that its stack frame is
369/// removed when this function returns. Otherwise, the stack space used for
370/// these static_inits is wasted.
371#[inline(never)]
372unsafe fn start() -> (
373    &'static kernel::Kernel,
374    STM32F3Discovery,
375    &'static stm32f303xc::chip::Stm32f3xx<'static, Stm32f3xxDefaultPeripherals<'static>>,
376) {
377    stm32f303xc::init();
378
379    // We use the default HSI 8Mhz clock
380    let rcc = static_init!(stm32f303xc::rcc::Rcc, stm32f303xc::rcc::Rcc::new());
381    let syscfg = static_init!(
382        stm32f303xc::syscfg::Syscfg,
383        stm32f303xc::syscfg::Syscfg::new(rcc)
384    );
385    let exti = static_init!(
386        stm32f303xc::exti::Exti,
387        stm32f303xc::exti::Exti::new(syscfg)
388    );
389
390    let peripherals = static_init!(
391        Stm32f3xxDefaultPeripherals,
392        Stm32f3xxDefaultPeripherals::new(rcc, exti)
393    );
394
395    peripherals.setup_circular_deps();
396
397    set_pin_primary_functions(
398        syscfg,
399        &peripherals.spi1,
400        &peripherals.i2c1,
401        &peripherals.gpio_ports,
402    );
403
404    setup_peripherals(&peripherals.tim2);
405
406    let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(&*addr_of!(PROCESSES)));
407
408    let chip = static_init!(
409        stm32f303xc::chip::Stm32f3xx<Stm32f3xxDefaultPeripherals>,
410        stm32f303xc::chip::Stm32f3xx::new(peripherals)
411    );
412    CHIP = Some(chip);
413
414    // UART
415
416    // Create a shared UART channel for kernel debug.
417    peripherals.usart1.enable_clock();
418    peripherals.usart2.enable_clock();
419
420    let uart_mux = components::console::UartMuxComponent::new(&peripherals.usart1, 115200)
421        .finalize(components::uart_mux_component_static!());
422
423    // `finalize()` configures the underlying USART, so we need to
424    // tell `send_byte()` not to configure the USART again.
425    (*addr_of_mut!(io::WRITER)).set_initialized();
426
427    // Create capabilities that the board needs to call certain protected kernel
428    // functions.
429    let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
430    let process_management_capability =
431        create_capability!(capabilities::ProcessManagementCapability);
432
433    // Setup the console.
434    let console = components::console::ConsoleComponent::new(
435        board_kernel,
436        capsules_core::console::DRIVER_NUM,
437        uart_mux,
438    )
439    .finalize(components::console_component_static!());
440    // Create the debugger object that handles calls to `debug!()`.
441    components::debug_writer::DebugWriterComponent::new(uart_mux)
442        .finalize(components::debug_writer_component_static!());
443
444    // LEDs
445
446    // Clock to Port E is enabled in `set_pin_primary_functions()`
447
448    let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
449        LedHigh<'static, stm32f303xc::gpio::Pin<'static>>,
450        LedHigh::new(
451            peripherals
452                .gpio_ports
453                .get_pin(stm32f303xc::gpio::PinId::PE09)
454                .unwrap()
455        ),
456        LedHigh::new(
457            peripherals
458                .gpio_ports
459                .get_pin(stm32f303xc::gpio::PinId::PE08)
460                .unwrap()
461        ),
462        LedHigh::new(
463            peripherals
464                .gpio_ports
465                .get_pin(stm32f303xc::gpio::PinId::PE10)
466                .unwrap()
467        ),
468        LedHigh::new(
469            peripherals
470                .gpio_ports
471                .get_pin(stm32f303xc::gpio::PinId::PE15)
472                .unwrap()
473        ),
474        LedHigh::new(
475            peripherals
476                .gpio_ports
477                .get_pin(stm32f303xc::gpio::PinId::PE11)
478                .unwrap()
479        ),
480        LedHigh::new(
481            peripherals
482                .gpio_ports
483                .get_pin(stm32f303xc::gpio::PinId::PE14)
484                .unwrap()
485        ),
486        LedHigh::new(
487            peripherals
488                .gpio_ports
489                .get_pin(stm32f303xc::gpio::PinId::PE12)
490                .unwrap()
491        ),
492        LedHigh::new(
493            peripherals
494                .gpio_ports
495                .get_pin(stm32f303xc::gpio::PinId::PE13)
496                .unwrap()
497        ),
498    ));
499
500    // BUTTONs
501    let button = components::button::ButtonComponent::new(
502        board_kernel,
503        capsules_core::button::DRIVER_NUM,
504        components::button_component_helper!(
505            stm32f303xc::gpio::Pin<'static>,
506            (
507                peripherals
508                    .gpio_ports
509                    .get_pin(stm32f303xc::gpio::PinId::PA00)
510                    .unwrap(),
511                kernel::hil::gpio::ActivationMode::ActiveHigh,
512                kernel::hil::gpio::FloatingState::PullNone
513            )
514        ),
515    )
516    .finalize(components::button_component_static!(
517        stm32f303xc::gpio::Pin<'static>
518    ));
519
520    // ALARM
521
522    let tim2 = &peripherals.tim2;
523    let mux_alarm = components::alarm::AlarmMuxComponent::new(tim2).finalize(
524        components::alarm_mux_component_static!(stm32f303xc::tim2::Tim2),
525    );
526
527    let alarm = components::alarm::AlarmDriverComponent::new(
528        board_kernel,
529        capsules_core::alarm::DRIVER_NUM,
530        mux_alarm,
531    )
532    .finalize(components::alarm_component_static!(stm32f303xc::tim2::Tim2));
533
534    let gpio_ports = &peripherals.gpio_ports;
535    // GPIO
536    let gpio = GpioComponent::new(
537        board_kernel,
538        capsules_core::gpio::DRIVER_NUM,
539        components::gpio_component_helper!(
540            stm32f303xc::gpio::Pin<'static>,
541            // Left outer connector
542            0 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC01).unwrap(),
543            1 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC03).unwrap(),
544            // 2 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA01).unwrap(),
545            // 3 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA03).unwrap(),
546            // 4 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PF04).unwrap(),
547            // 5 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA05).unwrap(),
548            // 6 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA07).unwrap(),
549            // 7 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC05).unwrap(),
550            // 8 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB01).unwrap(),
551            9 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE07).unwrap(),
552            // 10 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE09).unwrap(),
553            11 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE11).unwrap(),
554            // 12 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE13).unwrap(),
555            // 13 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE15).unwrap(),
556            14 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB11).unwrap(),
557            // 15 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB13).unwrap(),
558            // 16 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB15).unwrap(),
559            17 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD09).unwrap(),
560            18 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD11).unwrap(),
561            19 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD13).unwrap(),
562            20 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD15).unwrap(),
563            21 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC06).unwrap(),
564            // Left inner connector
565            22 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC00).unwrap(),
566            23 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC02).unwrap(),
567            24 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PF02).unwrap(),
568            // 25 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA00).unwrap(),
569            // 26 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA02).unwrap(),
570            // 27 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA04).unwrap(),
571            // 28 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA06).unwrap(),
572            // 29 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC04).unwrap(),
573            30 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB00).unwrap(),
574            31 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB02).unwrap(),
575            32 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE08).unwrap(),
576            33 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE10).unwrap(),
577            34 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE12).unwrap(),
578            // 35 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE14).unwrap(),
579            36 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB10).unwrap(),
580            // 37 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB12).unwrap(),
581            // 38 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB14).unwrap(),
582            39 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD08).unwrap(),
583            40 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD10).unwrap(),
584            41 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD12).unwrap(),
585            42 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD14).unwrap(),
586            43 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC07).unwrap(),
587            // Right inner connector
588            44 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PF09).unwrap(),
589            45 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PF00).unwrap(),
590            46 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC14).unwrap(),
591            47 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE06).unwrap(),
592            48 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE04).unwrap(),
593            49 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE02).unwrap(),
594            50 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE00).unwrap(),
595            51 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB08).unwrap(),
596            // 52 => &gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB06).unwrap(),
597            53 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB04).unwrap(),
598            54 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD07).unwrap(),
599            55 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD05).unwrap(),
600            56 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD03).unwrap(),
601            57 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD01).unwrap(),
602            58 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC12).unwrap(),
603            59 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC10).unwrap(),
604            60 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA14).unwrap(),
605            61 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PF06).unwrap(),
606            62 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA12).unwrap(),
607            63 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA10).unwrap(),
608            64 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA08).unwrap(),
609            65 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC08).unwrap(),
610            // Right outer connector
611            66 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PF10).unwrap(),
612            67 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PF01).unwrap(),
613            68 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC15).unwrap(),
614            69 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC13).unwrap(),
615            70 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE05).unwrap(),
616            71 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE03).unwrap(),
617            72 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE01).unwrap(),
618            73 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB09).unwrap(),
619            // 74 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB07).unwrap(),
620            75 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB05).unwrap(),
621            76 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PB03).unwrap(),
622            77 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD06).unwrap(),
623            78 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD04).unwrap(),
624            79 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD02).unwrap(),
625            80 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PD00).unwrap(),
626            81 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC11).unwrap(),
627            82 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA15).unwrap(),
628            83 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA13).unwrap(),
629            84 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA11).unwrap(),
630            85 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PA09).unwrap(),
631            86 => gpio_ports.get_pin(stm32f303xc::gpio::PinId::PC09).unwrap()
632        ),
633    )
634    .finalize(components::gpio_component_static!(
635        stm32f303xc::gpio::Pin<'static>
636    ));
637
638    // L3GD20 sensor
639    let spi_mux = components::spi::SpiMuxComponent::new(&peripherals.spi1)
640        .finalize(components::spi_mux_component_static!(stm32f303xc::spi::Spi));
641
642    let l3gd20 = components::l3gd20::L3gd20Component::new(
643        spi_mux,
644        gpio_ports.get_pin(stm32f303xc::gpio::PinId::PE03).unwrap(),
645        board_kernel,
646        capsules_extra::l3gd20::DRIVER_NUM,
647    )
648    .finalize(components::l3gd20_component_static!(
649        // spi type
650        stm32f303xc::spi::Spi
651    ));
652
653    l3gd20.power_on();
654
655    // Comment this if you want to use the ADC MCU temp sensor
656    let temp = components::temperature::TemperatureComponent::new(
657        board_kernel,
658        capsules_extra::temperature::DRIVER_NUM,
659        l3gd20,
660    )
661    .finalize(components::temperature_component_static!(L3GD20Sensor));
662
663    // LSM303DLHC
664
665    let mux_i2c = components::i2c::I2CMuxComponent::new(&peripherals.i2c1, None)
666        .finalize(components::i2c_mux_component_static!(stm32f303xc::i2c::I2C));
667
668    let lsm303dlhc = components::lsm303dlhc::Lsm303dlhcI2CComponent::new(
669        mux_i2c,
670        None,
671        None,
672        board_kernel,
673        capsules_extra::lsm303dlhc::DRIVER_NUM,
674    )
675    .finalize(components::lsm303dlhc_component_static!(
676        stm32f303xc::i2c::I2C
677    ));
678
679    if let Err(error) = lsm303dlhc.configure(
680        lsm303xx::Lsm303AccelDataRate::DataRate25Hz,
681        false,
682        lsm303xx::Lsm303Scale::Scale2G,
683        false,
684        true,
685        lsm303xx::Lsm303MagnetoDataRate::DataRate3_0Hz,
686        lsm303xx::Lsm303Range::Range1_9G,
687    ) {
688        debug!("Failed to configure LSM303DLHC sensor ({:?})", error);
689    }
690
691    let ninedof = components::ninedof::NineDofComponent::new(
692        board_kernel,
693        capsules_extra::ninedof::DRIVER_NUM,
694    )
695    .finalize(components::ninedof_component_static!(l3gd20, lsm303dlhc));
696
697    let adc_mux = components::adc::AdcMuxComponent::new(&peripherals.adc1)
698        .finalize(components::adc_mux_component_static!(stm32f303xc::adc::Adc));
699
700    // Uncomment this if you want to use ADC MCU temp sensor
701    // let temp_sensor = components::temperature_stm::TemperatureSTMComponent::new(4.3, 1.43)
702    //     .finalize(components::temperaturestm_adc_component_static!(
703    //         // spi type
704    //         stm32f303xc::adc::Adc,
705    //         // chip select
706    //         stm32f303xc::adc::Channel::Channel18,
707    //         // spi mux
708    //         adc_mux
709    //     ));
710    // let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
711    // let grant_temperature = board_kernel.create_grant(&grant_cap);
712
713    // let temp = static_init!(
714    //     capsules_extra::temperature::TemperatureSensor<'static>,
715    //     capsules_extra::temperature::TemperatureSensor::new(temp_sensor, grant_temperature)
716    // );
717    // kernel::hil::sensors::TemperatureDriver::set_client(temp_sensor, temp);
718
719    // shared with button
720    // let adc_channel_1 =
721    //     components::adc::AdcComponent::new(&adc_mux, stm32f303xc::adc::Channel::Channel1)
722    //         .finalize(components::adc_component_static!(stm32f303xc::adc::Adc));
723
724    let adc_channel_2 =
725        components::adc::AdcComponent::new(adc_mux, stm32f303xc::adc::Channel::Channel2)
726            .finalize(components::adc_component_static!(stm32f303xc::adc::Adc));
727
728    let adc_channel_3 =
729        components::adc::AdcComponent::new(adc_mux, stm32f303xc::adc::Channel::Channel3)
730            .finalize(components::adc_component_static!(stm32f303xc::adc::Adc));
731
732    let adc_channel_4 =
733        components::adc::AdcComponent::new(adc_mux, stm32f303xc::adc::Channel::Channel4)
734            .finalize(components::adc_component_static!(stm32f303xc::adc::Adc));
735
736    let adc_channel_5 =
737        components::adc::AdcComponent::new(adc_mux, stm32f303xc::adc::Channel::Channel5)
738            .finalize(components::adc_component_static!(stm32f303xc::adc::Adc));
739
740    let adc_syscall =
741        components::adc::AdcVirtualComponent::new(board_kernel, capsules_core::adc::DRIVER_NUM)
742            .finalize(components::adc_syscall_component_helper!(
743                adc_channel_2,
744                adc_channel_3,
745                adc_channel_4,
746                adc_channel_5,
747            ));
748
749    // Kernel storage region, allocated with the storage_volume!
750    // macro in common/utils.rs
751    extern "C" {
752        /// Beginning on the ROM region containing app images.
753        static _sstorage: u8;
754        static _estorage: u8;
755    }
756
757    let nonvolatile_storage = components::nonvolatile_storage::NonvolatileStorageComponent::new(
758        board_kernel,
759        capsules_extra::nonvolatile_storage_driver::DRIVER_NUM,
760        &peripherals.flash,
761        0x08038000, // Start address for userspace accesible region
762        0x8000,     // Length of userspace accesible region (16 pages)
763        core::ptr::addr_of!(_sstorage) as usize,
764        core::ptr::addr_of!(_estorage) as usize - core::ptr::addr_of!(_sstorage) as usize,
765    )
766    .finalize(components::nonvolatile_storage_component_static!(
767        stm32f303xc::flash::Flash
768    ));
769
770    let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
771        .finalize(components::process_printer_text_component_static!());
772    PROCESS_PRINTER = Some(process_printer);
773
774    // PROCESS CONSOLE
775    let process_console = components::process_console::ProcessConsoleComponent::new(
776        board_kernel,
777        uart_mux,
778        mux_alarm,
779        process_printer,
780        Some(cortexm4::support::reset),
781    )
782    .finalize(components::process_console_component_static!(
783        stm32f303xc::tim2::Tim2
784    ));
785    let _ = process_console.start();
786
787    let scheduler = components::sched::round_robin::RoundRobinComponent::new(&*addr_of!(PROCESSES))
788        .finalize(components::round_robin_component_static!(NUM_PROCS));
789
790    let stm32f3discovery = STM32F3Discovery {
791        console,
792        ipc: kernel::ipc::IPC::new(
793            board_kernel,
794            kernel::ipc::DRIVER_NUM,
795            &memory_allocation_capability,
796        ),
797        gpio,
798        led,
799        button,
800        alarm,
801        l3gd20,
802        lsm303dlhc,
803        ninedof,
804        temp,
805        adc: adc_syscall,
806        nonvolatile_storage,
807
808        scheduler,
809        // Systick uses the HSI, which runs at 8MHz
810        systick: cortexm4::systick::SysTick::new_with_calibration(8_000_000),
811        watchdog: &peripherals.watchdog,
812    };
813
814    // // Optional kernel tests
815    // //
816    // // See comment in `boards/imix/src/main.rs`
817    // virtual_uart_rx_test::run_virtual_uart_receive(mux_uart);
818
819    debug!("Initialization complete. Entering main loop");
820
821    // These symbols are defined in the linker script.
822    extern "C" {
823        /// Beginning of the ROM region containing app images.
824        static _sapps: u8;
825        /// End of the ROM region containing app images.
826        static _eapps: u8;
827        /// Beginning of the RAM region for app memory.
828        static mut _sappmem: u8;
829        /// End of the RAM region for app memory.
830        static _eappmem: u8;
831    }
832
833    kernel::process::load_processes(
834        board_kernel,
835        chip,
836        core::slice::from_raw_parts(
837            core::ptr::addr_of!(_sapps),
838            core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
839        ),
840        core::slice::from_raw_parts_mut(
841            core::ptr::addr_of_mut!(_sappmem),
842            core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
843        ),
844        &mut *addr_of_mut!(PROCESSES),
845        &FAULT_RESPONSE,
846        &process_management_capability,
847    )
848    .unwrap_or_else(|err| {
849        debug!("Error loading processes!");
850        debug!("{:?}", err);
851    });
852
853    // Uncomment this to enable the watchdog
854    peripherals.watchdog.enable();
855
856    //Uncomment to run multi alarm test
857    /*components::test::multi_alarm_test::MultiAlarmTestComponent::new(mux_alarm)
858    .finalize(components::multi_alarm_test_component_buf!(stm32f303xc::tim2::Tim2))
859    .run();*/
860
861    (board_kernel, stm32f3discovery, chip)
862}
863
864/// Main function called after RAM initialized.
865#[no_mangle]
866pub unsafe fn main() {
867    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
868
869    let (board_kernel, platform, chip) = start();
870    board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
871}