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