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