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