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