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