stm32f412gdiscovery/
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 STM32F412GDiscovery Discovery kit development board
6//!
7//! - <https://www.st.com/en/evaluation-tools/32f412gdiscovery.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 components::gpio::GpioComponent;
17use components::rng::RngComponent;
18use kernel::capabilities;
19use kernel::component::Component;
20use kernel::hil::gpio;
21use kernel::hil::led::LedLow;
22use kernel::hil::screen::ScreenRotation;
23use kernel::platform::{KernelResources, SyscallDriverLookup};
24use kernel::process::ProcessArray;
25use kernel::scheduler::round_robin::RoundRobinSched;
26use kernel::{create_capability, debug, static_init};
27use stm32f412g::chip_specs::Stm32f412Specs;
28use stm32f412g::clocks::hsi::HSI_FREQUENCY_MHZ;
29use stm32f412g::interrupt_service::Stm32f412gDefaultPeripherals;
30use stm32f412g::rcc::PllSource;
31
32/// Support routines for debugging I/O.
33pub mod io;
34
35// Number of concurrent processes this platform supports.
36const NUM_PROCS: usize = 4;
37
38type ChipHw = stm32f412g::chip::Stm32f4xx<'static, Stm32f412gDefaultPeripherals<'static>>;
39
40/// Static variables used by io.rs.
41static mut PROCESSES: Option<&'static ProcessArray<NUM_PROCS>> = None;
42static mut CHIP: Option<&'static ChipHw> = None;
43static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
44    None;
45
46// How should the kernel respond when a process faults.
47const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
48    capsules_system::process_policies::PanicFaultPolicy {};
49
50kernel::stack_size! {0x2000}
51
52type TemperatureSTMSensor = components::temperature_stm::TemperatureSTMComponentType<
53    capsules_core::virtualizers::virtual_adc::AdcDevice<'static, stm32f412g::adc::Adc<'static>>,
54>;
55type TemperatureDriver = components::temperature::TemperatureComponentType<TemperatureSTMSensor>;
56type RngDriver = components::rng::RngComponentType<stm32f412g::trng::Trng<'static>>;
57type ScreenDriver = components::screen::ScreenComponentType;
58
59/// A structure representing this platform that holds references to all
60/// capsules for this platform.
61struct STM32F412GDiscovery {
62    console: &'static capsules_core::console::Console<'static>,
63    ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
64    led: &'static capsules_core::led::LedDriver<
65        'static,
66        LedLow<'static, stm32f412g::gpio::Pin<'static>>,
67        4,
68    >,
69    button: &'static capsules_core::button::Button<'static, stm32f412g::gpio::Pin<'static>>,
70    alarm: &'static capsules_core::alarm::AlarmDriver<
71        'static,
72        VirtualMuxAlarm<'static, stm32f412g::tim2::Tim2<'static>>,
73    >,
74    gpio: &'static capsules_core::gpio::GPIO<'static, stm32f412g::gpio::Pin<'static>>,
75    adc: &'static capsules_core::adc::AdcVirtualized<'static>,
76    touch: &'static capsules_extra::touch::Touch<'static>,
77    screen: &'static ScreenDriver,
78    temperature: &'static TemperatureDriver,
79    rng: &'static RngDriver,
80
81    scheduler: &'static RoundRobinSched<'static>,
82    systick: cortexm4::systick::SysTick,
83}
84
85/// Mapping of integer syscalls to objects that implement syscalls.
86impl SyscallDriverLookup for STM32F412GDiscovery {
87    fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
88    where
89        F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
90    {
91        match driver_num {
92            capsules_core::console::DRIVER_NUM => f(Some(self.console)),
93            capsules_core::led::DRIVER_NUM => f(Some(self.led)),
94            capsules_core::button::DRIVER_NUM => f(Some(self.button)),
95            capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
96            kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
97            capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
98            capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
99            capsules_extra::touch::DRIVER_NUM => f(Some(self.touch)),
100            capsules_extra::screen::screen::DRIVER_NUM => f(Some(self.screen)),
101            capsules_extra::temperature::DRIVER_NUM => f(Some(self.temperature)),
102            capsules_core::rng::DRIVER_NUM => f(Some(self.rng)),
103            _ => f(None),
104        }
105    }
106}
107
108impl
109    KernelResources<
110        stm32f412g::chip::Stm32f4xx<
111            'static,
112            stm32f412g::interrupt_service::Stm32f412gDefaultPeripherals<'static>,
113        >,
114    > for STM32F412GDiscovery
115{
116    type SyscallDriverLookup = Self;
117    type SyscallFilter = ();
118    type ProcessFault = ();
119    type Scheduler = RoundRobinSched<'static>;
120    type SchedulerTimer = cortexm4::systick::SysTick;
121    type WatchDog = ();
122    type ContextSwitchCallback = ();
123
124    fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
125        self
126    }
127    fn syscall_filter(&self) -> &Self::SyscallFilter {
128        &()
129    }
130    fn process_fault(&self) -> &Self::ProcessFault {
131        &()
132    }
133    fn scheduler(&self) -> &Self::Scheduler {
134        self.scheduler
135    }
136    fn scheduler_timer(&self) -> &Self::SchedulerTimer {
137        &self.systick
138    }
139    fn watchdog(&self) -> &Self::WatchDog {
140        &()
141    }
142    fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
143        &()
144    }
145}
146
147/// Helper function called during bring-up that configures DMA.
148unsafe fn setup_dma(
149    dma: &stm32f412g::dma::Dma1,
150    dma_streams: &'static [stm32f412g::dma::Stream<stm32f412g::dma::Dma1>; 8],
151    usart2: &'static stm32f412g::usart::Usart<stm32f412g::dma::Dma1>,
152) {
153    use stm32f412g::dma::Dma1Peripheral;
154    use stm32f412g::usart;
155
156    dma.enable_clock();
157
158    let usart2_tx_stream = &dma_streams[Dma1Peripheral::USART2_TX.get_stream_idx()];
159    let usart2_rx_stream = &dma_streams[Dma1Peripheral::USART2_RX.get_stream_idx()];
160
161    usart2.set_dma(
162        usart::TxDMA(usart2_tx_stream),
163        usart::RxDMA(usart2_rx_stream),
164    );
165
166    usart2_tx_stream.set_client(usart2);
167    usart2_rx_stream.set_client(usart2);
168
169    usart2_tx_stream.setup(Dma1Peripheral::USART2_TX);
170    usart2_rx_stream.setup(Dma1Peripheral::USART2_RX);
171
172    cortexm4::nvic::Nvic::new(Dma1Peripheral::USART2_TX.get_stream_irqn()).enable();
173    cortexm4::nvic::Nvic::new(Dma1Peripheral::USART2_RX.get_stream_irqn()).enable();
174}
175
176/// Helper function called during bring-up that configures multiplexed I/O.
177unsafe fn set_pin_primary_functions(
178    syscfg: &stm32f412g::syscfg::Syscfg,
179    i2c1: &stm32f412g::i2c::I2C,
180    gpio_ports: &'static stm32f412g::gpio::GpioPorts<'static>,
181    peripheral_clock_frequency: usize,
182) {
183    use kernel::hil::gpio::Configure;
184    use stm32f412g::gpio::{AlternateFunction, Mode, PinId, PortId};
185
186    syscfg.enable_clock();
187
188    gpio_ports.get_port_from_port_id(PortId::E).enable_clock();
189
190    // User LD3 is connected to PE02. Configure PE02 as `debug_gpio!(0, ...)`
191    gpio_ports.get_pin(PinId::PE02).map(|pin| {
192        pin.make_output();
193
194        // Configure kernel debug gpios as early as possible
195        let debug_gpios = static_init!([&'static dyn kernel::hil::gpio::Pin; 1], [pin]);
196        kernel::debug::initialize_debug_gpio::<
197            <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
198        >();
199        kernel::debug::assign_gpios(debug_gpios);
200    });
201
202    gpio_ports.get_port_from_port_id(PortId::A).enable_clock();
203
204    // pa2 and pa3 (USART2) is connected to ST-LINK virtual COM port
205    gpio_ports.get_pin(PinId::PA02).map(|pin| {
206        pin.set_mode(Mode::AlternateFunctionMode);
207        // AF7 is USART2_TX
208        pin.set_alternate_function(AlternateFunction::AF7);
209    });
210    gpio_ports.get_pin(PinId::PA03).map(|pin| {
211        pin.set_mode(Mode::AlternateFunctionMode);
212        // AF7 is USART2_RX
213        pin.set_alternate_function(AlternateFunction::AF7);
214    });
215
216    // uncomment this if you do not plan to use the joystick up, as they both use Exti0
217    // joystick selection is connected on pa00
218    // gpio_ports.get_pin(PinId::PA00).map(|pin| {
219    //     pin.enable_interrupt();
220    // });
221
222    // joystick down is connected on pg01
223    gpio_ports.get_pin(PinId::PG01).map(|pin| {
224        pin.enable_interrupt();
225    });
226
227    // joystick left is connected on pf15
228    gpio_ports.get_pin(PinId::PF15).map(|pin| {
229        pin.enable_interrupt();
230    });
231
232    // joystick right is connected on pf14
233    gpio_ports.get_pin(PinId::PF14).map(|pin| {
234        pin.enable_interrupt();
235    });
236
237    // joystick up is connected on pg00
238    gpio_ports.get_pin(PinId::PG00).map(|pin| {
239        pin.enable_interrupt();
240    });
241
242    // enable interrupt for D0
243    gpio_ports.get_pin(PinId::PG09).map(|pin| {
244        pin.enable_interrupt();
245    });
246
247    // Enable clocks for GPIO Ports
248    // Disable some of them if you don't need some of the GPIOs
249    gpio_ports.get_port_from_port_id(PortId::B).enable_clock();
250    // Ports A and E are already enabled
251    gpio_ports.get_port_from_port_id(PortId::C).enable_clock();
252    gpio_ports.get_port_from_port_id(PortId::D).enable_clock();
253    gpio_ports.get_port_from_port_id(PortId::F).enable_clock();
254    gpio_ports.get_port_from_port_id(PortId::G).enable_clock();
255    gpio_ports.get_port_from_port_id(PortId::H).enable_clock();
256
257    // I2C1 has the TouchPanel connected
258    gpio_ports.get_pin(PinId::PB06).map(|pin| {
259        // pin.make_output();
260        pin.set_mode_output_opendrain();
261        pin.set_mode(Mode::AlternateFunctionMode);
262        pin.set_floating_state(kernel::hil::gpio::FloatingState::PullNone);
263        // AF4 is I2C
264        pin.set_alternate_function(AlternateFunction::AF4);
265    });
266    gpio_ports.get_pin(PinId::PB07).map(|pin| {
267        // pin.make_output();
268        pin.set_mode_output_opendrain();
269        pin.set_floating_state(kernel::hil::gpio::FloatingState::PullNone);
270        pin.set_mode(Mode::AlternateFunctionMode);
271        // AF4 is I2C
272        pin.set_alternate_function(AlternateFunction::AF4);
273    });
274
275    i2c1.enable_clock();
276    i2c1.set_speed(
277        stm32f412g::i2c::I2CSpeed::Speed400k,
278        peripheral_clock_frequency,
279    );
280
281    // FT6206 interrupt
282    gpio_ports.get_pin(PinId::PG05).map(|pin| {
283        pin.enable_interrupt();
284    });
285
286    // ADC
287
288    // Arduino A0
289    gpio_ports.get_pin(PinId::PA01).map(|pin| {
290        pin.set_mode(stm32f412g::gpio::Mode::AnalogMode);
291    });
292
293    // Arduino A1
294    gpio_ports.get_pin(PinId::PC01).map(|pin| {
295        pin.set_mode(stm32f412g::gpio::Mode::AnalogMode);
296    });
297
298    // Arduino A2
299    gpio_ports.get_pin(PinId::PC03).map(|pin| {
300        pin.set_mode(stm32f412g::gpio::Mode::AnalogMode);
301    });
302
303    // Arduino A3
304    gpio_ports.get_pin(PinId::PC04).map(|pin| {
305        pin.set_mode(stm32f412g::gpio::Mode::AnalogMode);
306    });
307
308    // Arduino A4
309    gpio_ports.get_pin(PinId::PC05).map(|pin| {
310        pin.set_mode(stm32f412g::gpio::Mode::AnalogMode);
311    });
312
313    // Arduino A5
314    gpio_ports.get_pin(PinId::PB00).map(|pin| {
315        pin.set_mode(stm32f412g::gpio::Mode::AnalogMode);
316    });
317
318    // EXTI9_5 interrupts is delivered at IRQn 23 (EXTI9_5)
319    cortexm4::nvic::Nvic::new(stm32f412g::nvic::EXTI9_5).enable();
320
321    // LCD
322
323    let pins = [
324        PinId::PD00,
325        PinId::PD01,
326        PinId::PD04,
327        PinId::PD05,
328        PinId::PD08,
329        PinId::PD09,
330        PinId::PD10,
331        PinId::PD14,
332        PinId::PD15,
333        PinId::PD07,
334        PinId::PE07,
335        PinId::PE08,
336        PinId::PE09,
337        PinId::PE10,
338        PinId::PE11,
339        PinId::PE12,
340        PinId::PE13,
341        PinId::PE14,
342        PinId::PE15,
343        PinId::PF00,
344    ];
345
346    for pin in pins.iter() {
347        gpio_ports.get_pin(*pin).map(|pin| {
348            pin.set_mode(stm32f412g::gpio::Mode::AlternateFunctionMode);
349            pin.set_floating_state(gpio::FloatingState::PullUp);
350            pin.set_speed();
351            pin.set_alternate_function(stm32f412g::gpio::AlternateFunction::AF12);
352        });
353    }
354
355    use kernel::hil::gpio::Output;
356
357    gpio_ports.get_pin(PinId::PF05).map(|pin| {
358        pin.make_output();
359        pin.set_floating_state(gpio::FloatingState::PullNone);
360        pin.set();
361    });
362
363    gpio_ports.get_pin(PinId::PG04).map(|pin| {
364        pin.make_input();
365    });
366}
367
368/// Helper function for miscellaneous peripheral functions
369unsafe fn setup_peripherals(
370    tim2: &stm32f412g::tim2::Tim2,
371    fsmc: &stm32f412g::fsmc::Fsmc,
372    trng: &stm32f412g::trng::Trng,
373) {
374    // USART2 IRQn is 38
375    cortexm4::nvic::Nvic::new(stm32f412g::nvic::USART2).enable();
376
377    // TIM2 IRQn is 28
378    tim2.enable_clock();
379    tim2.start();
380    cortexm4::nvic::Nvic::new(stm32f412g::nvic::TIM2).enable();
381
382    // FSMC
383    fsmc.enable();
384
385    // RNG
386    trng.enable_clock();
387}
388
389/// Main function.
390///
391/// This is in a separate, inline(never) function so that its stack frame is
392/// removed when this function returns. Otherwise, the stack space used for
393/// these static_inits is wasted.
394#[inline(never)]
395unsafe fn start() -> (
396    &'static kernel::Kernel,
397    STM32F412GDiscovery,
398    &'static stm32f412g::chip::Stm32f4xx<'static, Stm32f412gDefaultPeripherals<'static>>,
399) {
400    stm32f412g::init();
401
402    // Initialize deferred calls very early.
403    kernel::deferred_call::initialize_deferred_call_state::<
404        <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
405    >();
406
407    let rcc = static_init!(stm32f412g::rcc::Rcc, stm32f412g::rcc::Rcc::new());
408    let clocks = static_init!(
409        stm32f412g::clocks::Clocks<Stm32f412Specs>,
410        stm32f412g::clocks::Clocks::new(rcc)
411    );
412
413    let syscfg = static_init!(
414        stm32f412g::syscfg::Syscfg,
415        stm32f412g::syscfg::Syscfg::new(clocks)
416    );
417
418    let exti = static_init!(stm32f412g::exti::Exti, stm32f412g::exti::Exti::new(syscfg));
419
420    let dma1 = static_init!(stm32f412g::dma::Dma1, stm32f412g::dma::Dma1::new(clocks));
421    let dma2 = static_init!(stm32f412g::dma::Dma2, stm32f412g::dma::Dma2::new(clocks));
422
423    let peripherals = static_init!(
424        Stm32f412gDefaultPeripherals,
425        Stm32f412gDefaultPeripherals::new(clocks, exti, dma1, dma2)
426    );
427
428    peripherals.init();
429
430    let _ = clocks.set_ahb_prescaler(stm32f412g::rcc::AHBPrescaler::DivideBy1);
431    let _ = clocks.set_apb1_prescaler(stm32f412g::rcc::APBPrescaler::DivideBy4);
432    let _ = clocks.set_apb2_prescaler(stm32f412g::rcc::APBPrescaler::DivideBy2);
433    let _ = clocks.set_pll_frequency_mhz(PllSource::HSI, 100);
434    let _ = clocks.pll.enable();
435    let _ = clocks.set_sys_clock_source(stm32f412g::rcc::SysClockSource::PLL);
436
437    let base_peripherals = &peripherals.stm32f4;
438    setup_peripherals(
439        &base_peripherals.tim2,
440        &base_peripherals.fsmc,
441        &peripherals.trng,
442    );
443
444    set_pin_primary_functions(
445        syscfg,
446        &base_peripherals.i2c1,
447        &base_peripherals.gpio_ports,
448        clocks.get_apb1_frequency_mhz(),
449    );
450
451    setup_dma(
452        dma1,
453        &base_peripherals.dma1_streams,
454        &base_peripherals.usart2,
455    );
456
457    // Create an array to hold process references.
458    let processes = components::process_array::ProcessArrayComponent::new()
459        .finalize(components::process_array_component_static!(NUM_PROCS));
460    PROCESSES = Some(processes);
461
462    // Setup space to store the core kernel data structure.
463    let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
464
465    let chip = static_init!(
466        stm32f412g::chip::Stm32f4xx<Stm32f412gDefaultPeripherals>,
467        stm32f412g::chip::Stm32f4xx::new(peripherals)
468    );
469    CHIP = Some(chip);
470
471    // UART
472
473    // Create a shared UART channel for kernel debug.
474    base_peripherals.usart2.enable_clock();
475    let uart_mux = components::console::UartMuxComponent::new(&base_peripherals.usart2, 115200)
476        .finalize(components::uart_mux_component_static!());
477
478    (*addr_of_mut!(io::WRITER)).set_initialized();
479
480    // Create capabilities that the board needs to call certain protected kernel
481    // functions.
482    let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
483    let process_management_capability =
484        create_capability!(capabilities::ProcessManagementCapability);
485
486    // Setup the console.
487    let console = components::console::ConsoleComponent::new(
488        board_kernel,
489        capsules_core::console::DRIVER_NUM,
490        uart_mux,
491    )
492    .finalize(components::console_component_static!());
493    // Create the debugger object that handles calls to `debug!()`.
494    components::debug_writer::DebugWriterComponent::new::<
495        <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
496    >(
497        uart_mux,
498        create_capability!(capabilities::SetDebugWriterCapability),
499    )
500    .finalize(components::debug_writer_component_static!());
501
502    // LEDs
503
504    // Clock to Port A is enabled in `set_pin_primary_functions()`
505
506    let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
507        LedLow<'static, stm32f412g::gpio::Pin>,
508        LedLow::new(
509            base_peripherals
510                .gpio_ports
511                .get_pin(stm32f412g::gpio::PinId::PE00)
512                .unwrap()
513        ),
514        LedLow::new(
515            base_peripherals
516                .gpio_ports
517                .get_pin(stm32f412g::gpio::PinId::PE01)
518                .unwrap()
519        ),
520        LedLow::new(
521            base_peripherals
522                .gpio_ports
523                .get_pin(stm32f412g::gpio::PinId::PE02)
524                .unwrap()
525        ),
526        LedLow::new(
527            base_peripherals
528                .gpio_ports
529                .get_pin(stm32f412g::gpio::PinId::PE03)
530                .unwrap()
531        ),
532    ));
533
534    // BUTTONs
535    let button = components::button::ButtonComponent::new(
536        board_kernel,
537        capsules_core::button::DRIVER_NUM,
538        components::button_component_helper!(
539            stm32f412g::gpio::Pin,
540            // Select
541            (
542                base_peripherals
543                    .gpio_ports
544                    .get_pin(stm32f412g::gpio::PinId::PA00)
545                    .unwrap(),
546                kernel::hil::gpio::ActivationMode::ActiveHigh,
547                kernel::hil::gpio::FloatingState::PullNone
548            ),
549            // Down
550            (
551                base_peripherals
552                    .gpio_ports
553                    .get_pin(stm32f412g::gpio::PinId::PG01)
554                    .unwrap(),
555                kernel::hil::gpio::ActivationMode::ActiveHigh,
556                kernel::hil::gpio::FloatingState::PullNone
557            ),
558            // Left
559            (
560                base_peripherals
561                    .gpio_ports
562                    .get_pin(stm32f412g::gpio::PinId::PF15)
563                    .unwrap(),
564                kernel::hil::gpio::ActivationMode::ActiveHigh,
565                kernel::hil::gpio::FloatingState::PullNone
566            ),
567            // Right
568            (
569                base_peripherals
570                    .gpio_ports
571                    .get_pin(stm32f412g::gpio::PinId::PF14)
572                    .unwrap(),
573                kernel::hil::gpio::ActivationMode::ActiveHigh,
574                kernel::hil::gpio::FloatingState::PullNone
575            ),
576            // Up
577            (
578                base_peripherals
579                    .gpio_ports
580                    .get_pin(stm32f412g::gpio::PinId::PG00)
581                    .unwrap(),
582                kernel::hil::gpio::ActivationMode::ActiveHigh,
583                kernel::hil::gpio::FloatingState::PullNone
584            )
585        ),
586    )
587    .finalize(components::button_component_static!(stm32f412g::gpio::Pin));
588
589    // ALARM
590
591    let tim2 = &base_peripherals.tim2;
592    let mux_alarm = components::alarm::AlarmMuxComponent::new(tim2).finalize(
593        components::alarm_mux_component_static!(stm32f412g::tim2::Tim2),
594    );
595
596    let alarm = components::alarm::AlarmDriverComponent::new(
597        board_kernel,
598        capsules_core::alarm::DRIVER_NUM,
599        mux_alarm,
600    )
601    .finalize(components::alarm_component_static!(stm32f412g::tim2::Tim2));
602
603    // GPIO
604    let gpio = GpioComponent::new(
605        board_kernel,
606        capsules_core::gpio::DRIVER_NUM,
607        components::gpio_component_helper!(
608            stm32f412g::gpio::Pin,
609            // Arduino like RX/TX
610            0 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG09).unwrap(), //D0
611            1 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG14).unwrap(), //D1
612            2 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG13).unwrap(), //D2
613            3 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PF04).unwrap(), //D3
614            4 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG12).unwrap(), //D4
615            5 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PF10).unwrap(), //D5
616            6 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PF03).unwrap(), //D6
617            7 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG11).unwrap(), //D7
618            8 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG10).unwrap(), //D8
619            9 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PB08).unwrap(), //D9
620            // SPI Pins
621            10 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PA15).unwrap(), //D10
622            11 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PA07).unwrap(),  //D11
623            12 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PA06).unwrap(),  //D12
624            13 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PA15).unwrap()  //D13
625
626            // ADC Pins
627            // Enable the to use the ADC pins as GPIO
628            // 14 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PA01).unwrap(), //A0
629            // 15 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PC01).unwrap(), //A1
630            // 16 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PC03).unwrap(), //A2
631            // 17 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PC04).unwrap(), //A3
632            // 19 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PC05).unwrap(), //A4
633            // 20 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PB00).unwrap() //A5
634        ),
635    )
636    .finalize(components::gpio_component_static!(stm32f412g::gpio::Pin));
637
638    // RNG
639    let rng = RngComponent::new(
640        board_kernel,
641        capsules_core::rng::DRIVER_NUM,
642        &peripherals.trng,
643    )
644    .finalize(components::rng_component_static!(stm32f412g::trng::Trng));
645
646    // FT6206
647
648    let mux_i2c = components::i2c::I2CMuxComponent::new(&base_peripherals.i2c1, None)
649        .finalize(components::i2c_mux_component_static!(stm32f412g::i2c::I2C));
650
651    let ft6x06 = components::ft6x06::Ft6x06Component::new(
652        mux_i2c,
653        0x38,
654        base_peripherals
655            .gpio_ports
656            .get_pin(stm32f412g::gpio::PinId::PG05)
657            .unwrap(),
658    )
659    .finalize(components::ft6x06_component_static!(stm32f412g::i2c::I2C));
660
661    let bus = components::bus::Bus8080BusComponent::new(&base_peripherals.fsmc).finalize(
662        components::bus8080_bus_component_static!(stm32f412g::fsmc::Fsmc,),
663    );
664
665    let tft = components::st77xx::ST77XXComponent::new(
666        mux_alarm,
667        bus,
668        None,
669        base_peripherals
670            .gpio_ports
671            .get_pin(stm32f412g::gpio::PinId::PD11),
672        &capsules_extra::st77xx::ST7789H2,
673    )
674    .finalize(components::st77xx_component_static!(
675        // bus type
676        capsules_extra::bus::Bus8080Bus<'static, stm32f412g::fsmc::Fsmc>,
677        // timer type
678        stm32f412g::tim2::Tim2,
679        // pin type
680        stm32f412g::gpio::Pin,
681    ));
682
683    let _ = tft.init();
684
685    let screen = components::screen::ScreenComponent::new(
686        board_kernel,
687        capsules_extra::screen::screen::DRIVER_NUM,
688        tft,
689        Some(tft),
690    )
691    .finalize(components::screen_component_static!(1024));
692
693    let touch = components::touch::MultiTouchComponent::new(
694        board_kernel,
695        capsules_extra::touch::DRIVER_NUM,
696        ft6x06,
697        Some(ft6x06),
698        Some(tft),
699    )
700    .finalize(components::touch_component_static!());
701
702    touch.set_screen_rotation_offset(ScreenRotation::Rotated90);
703
704    // Uncomment this for multi touch support
705    // let touch =
706    //     components::touch::MultiTouchComponent::new(board_kernel, ft6x06, Some(ft6x06), None)
707    //         .finalize(());
708
709    // ADC
710    let adc_mux = components::adc::AdcMuxComponent::new(&base_peripherals.adc1)
711        .finalize(components::adc_mux_component_static!(stm32f412g::adc::Adc));
712
713    let temp_sensor = components::temperature_stm::TemperatureSTMComponent::new(
714        adc_mux,
715        stm32f412g::adc::Channel::Channel18,
716        2.5,
717        0.76,
718    )
719    .finalize(components::temperature_stm_adc_component_static!(
720        stm32f412g::adc::Adc
721    ));
722
723    let temp = components::temperature::TemperatureComponent::new(
724        board_kernel,
725        capsules_extra::temperature::DRIVER_NUM,
726        temp_sensor,
727    )
728    .finalize(components::temperature_component_static!(
729        TemperatureSTMSensor
730    ));
731
732    let adc_channel_0 =
733        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel1)
734            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
735
736    let adc_channel_1 =
737        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel11)
738            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
739
740    let adc_channel_2 =
741        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel13)
742            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
743
744    let adc_channel_3 =
745        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel14)
746            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
747
748    let adc_channel_4 =
749        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel15)
750            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
751
752    let adc_channel_5 =
753        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel8)
754            .finalize(components::adc_component_static!(stm32f412g::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_0,
760                adc_channel_1,
761                adc_channel_2,
762                adc_channel_3,
763                adc_channel_4,
764                adc_channel_5
765            ));
766
767    let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
768        .finalize(components::process_printer_text_component_static!());
769    PROCESS_PRINTER = Some(process_printer);
770
771    // PROCESS CONSOLE
772    let process_console = components::process_console::ProcessConsoleComponent::new(
773        board_kernel,
774        uart_mux,
775        mux_alarm,
776        process_printer,
777        Some(cortexm4::support::reset),
778    )
779    .finalize(components::process_console_component_static!(
780        stm32f412g::tim2::Tim2
781    ));
782    let _ = process_console.start();
783
784    let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
785        .finalize(components::round_robin_component_static!(NUM_PROCS));
786
787    let stm32f412g = STM32F412GDiscovery {
788        console,
789        ipc: kernel::ipc::IPC::new(
790            board_kernel,
791            kernel::ipc::DRIVER_NUM,
792            &memory_allocation_capability,
793        ),
794        led,
795        button,
796        alarm,
797        gpio,
798        adc: adc_syscall,
799        touch,
800        screen,
801        temperature: temp,
802        rng,
803
804        scheduler,
805        systick: cortexm4::systick::SysTick::new_with_calibration(
806            (HSI_FREQUENCY_MHZ * 1_000_000) as u32,
807        ),
808    };
809
810    // // Optional kernel tests
811    // //
812    // // See comment in `boards/imix/src/main.rs`
813    // virtual_uart_rx_test::run_virtual_uart_receive(mux_uart);
814    // base_peripherals.fsmc.write(0x04, 120);
815    // debug!("id {}", base_peripherals.fsmc.read(0x05));
816
817    debug!("Initialization complete. Entering main loop");
818
819    extern "C" {
820        /// Beginning of the ROM region containing app images.
821        ///
822        /// This symbol is defined in the linker script.
823        static _sapps: u8;
824
825        /// End of the ROM region containing app images.
826        ///
827        /// This symbol is defined in the linker script.
828        static _eapps: u8;
829
830        /// Beginning of the RAM region for app memory.
831        ///
832        /// This symbol is defined in the linker script.
833        static mut _sappmem: u8;
834
835        /// End of the RAM region for app memory.
836        ///
837        /// This symbol is defined in the linker script.
838        static _eappmem: u8;
839    }
840
841    kernel::process::load_processes(
842        board_kernel,
843        chip,
844        core::slice::from_raw_parts(
845            core::ptr::addr_of!(_sapps),
846            core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
847        ),
848        core::slice::from_raw_parts_mut(
849            core::ptr::addr_of_mut!(_sappmem),
850            core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
851        ),
852        &FAULT_RESPONSE,
853        &process_management_capability,
854    )
855    .unwrap_or_else(|err| {
856        debug!("Error loading processes!");
857        debug!("{:?}", err);
858    });
859
860    //Uncomment to run multi alarm test
861    /*components::test::multi_alarm_test::MultiAlarmTestComponent::new(mux_alarm)
862    .finalize(components::multi_alarm_test_component_buf!(stm32f412g::tim2::Tim2))
863    .run();*/
864
865    (board_kernel, stm32f412g, chip)
866}
867
868/// Main function called after RAM initialized.
869#[no_mangle]
870pub unsafe fn main() {
871    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
872
873    let (board_kernel, platform, chip) = start();
874    board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
875}