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