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