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::debug::PanicResources;
21use kernel::hil::gpio;
22use kernel::hil::led::LedLow;
23use kernel::hil::screen::ScreenRotation;
24use kernel::platform::{KernelResources, SyscallDriverLookup};
25use kernel::scheduler::round_robin::RoundRobinSched;
26use kernel::utilities::single_thread_value::SingleThreadValue;
27use kernel::{create_capability, debug, static_init};
28use stm32f412g::chip_specs::Stm32f412Specs;
29use stm32f412g::clocks::hsi::HSI_FREQUENCY_MHZ;
30use stm32f412g::interrupt_service::Stm32f412gDefaultPeripherals;
31use stm32f412g::rcc::PllSource;
32
33/// Support routines for debugging I/O.
34pub mod io;
35
36// Number of concurrent processes this platform supports.
37const NUM_PROCS: usize = 4;
38
39type ChipHw = stm32f412g::chip::Stm32f4xx<'static, Stm32f412gDefaultPeripherals<'static>>;
40type ProcessPrinterInUse = capsules_system::process_printer::ProcessPrinterText;
41
42/// Resources for when a board panics used by io.rs.
43static PANIC_RESOURCES: SingleThreadValue<PanicResources<ChipHw, ProcessPrinterInUse>> =
44    SingleThreadValue::new(PanicResources::new());
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    // Bind global variables to this thread.
408    PANIC_RESOURCES.bind_to_thread::<<ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider>();
409
410    let rcc = static_init!(stm32f412g::rcc::Rcc, stm32f412g::rcc::Rcc::new());
411    let clocks = static_init!(
412        stm32f412g::clocks::Clocks<Stm32f412Specs>,
413        stm32f412g::clocks::Clocks::new(rcc)
414    );
415
416    let syscfg = static_init!(
417        stm32f412g::syscfg::Syscfg,
418        stm32f412g::syscfg::Syscfg::new(clocks)
419    );
420
421    let exti = static_init!(stm32f412g::exti::Exti, stm32f412g::exti::Exti::new(syscfg));
422
423    let dma1 = static_init!(stm32f412g::dma::Dma1, stm32f412g::dma::Dma1::new(clocks));
424    let dma2 = static_init!(stm32f412g::dma::Dma2, stm32f412g::dma::Dma2::new(clocks));
425
426    let peripherals = static_init!(
427        Stm32f412gDefaultPeripherals,
428        Stm32f412gDefaultPeripherals::new(clocks, exti, dma1, dma2)
429    );
430
431    peripherals.init();
432
433    let _ = clocks.set_ahb_prescaler(stm32f412g::rcc::AHBPrescaler::DivideBy1);
434    let _ = clocks.set_apb1_prescaler(stm32f412g::rcc::APBPrescaler::DivideBy4);
435    let _ = clocks.set_apb2_prescaler(stm32f412g::rcc::APBPrescaler::DivideBy2);
436    let _ = clocks.set_pll_frequency_mhz(PllSource::HSI, 100);
437    let _ = clocks.pll.enable();
438    let _ = clocks.set_sys_clock_source(stm32f412g::rcc::SysClockSource::PLL);
439
440    let base_peripherals = &peripherals.stm32f4;
441    setup_peripherals(
442        &base_peripherals.tim2,
443        &base_peripherals.fsmc,
444        &peripherals.trng,
445    );
446
447    set_pin_primary_functions(
448        syscfg,
449        &base_peripherals.i2c1,
450        &base_peripherals.gpio_ports,
451        clocks.get_apb1_frequency_mhz(),
452    );
453
454    setup_dma(
455        dma1,
456        &base_peripherals.dma1_streams,
457        &base_peripherals.usart2,
458    );
459
460    // Create an array to hold process references.
461    let processes = components::process_array::ProcessArrayComponent::new()
462        .finalize(components::process_array_component_static!(NUM_PROCS));
463    PANIC_RESOURCES.get().map(|resources| {
464        resources.processes.put(processes.as_slice());
465    });
466
467    // Setup space to store the core kernel data structure.
468    let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
469
470    let chip = static_init!(
471        stm32f412g::chip::Stm32f4xx<Stm32f412gDefaultPeripherals>,
472        stm32f412g::chip::Stm32f4xx::new(peripherals)
473    );
474    PANIC_RESOURCES.get().map(|resources| {
475        resources.chip.put(chip);
476    });
477
478    // UART
479
480    // Create a shared UART channel for kernel debug.
481    base_peripherals.usart2.enable_clock();
482    let uart_mux = components::console::UartMuxComponent::new(&base_peripherals.usart2, 115200)
483        .finalize(components::uart_mux_component_static!());
484
485    (*addr_of_mut!(io::WRITER)).set_initialized();
486
487    // Create capabilities that the board needs to call certain protected kernel
488    // functions.
489    let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
490    let process_management_capability =
491        create_capability!(capabilities::ProcessManagementCapability);
492
493    // Setup the console.
494    let console = components::console::ConsoleComponent::new(
495        board_kernel,
496        capsules_core::console::DRIVER_NUM,
497        uart_mux,
498    )
499    .finalize(components::console_component_static!());
500    // Create the debugger object that handles calls to `debug!()`.
501    components::debug_writer::DebugWriterComponent::new::<
502        <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
503    >(
504        uart_mux,
505        create_capability!(capabilities::SetDebugWriterCapability),
506    )
507    .finalize(components::debug_writer_component_static!());
508
509    // LEDs
510
511    // Clock to Port A is enabled in `set_pin_primary_functions()`
512
513    let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
514        LedLow<'static, stm32f412g::gpio::Pin>,
515        LedLow::new(
516            base_peripherals
517                .gpio_ports
518                .get_pin(stm32f412g::gpio::PinId::PE00)
519                .unwrap()
520        ),
521        LedLow::new(
522            base_peripherals
523                .gpio_ports
524                .get_pin(stm32f412g::gpio::PinId::PE01)
525                .unwrap()
526        ),
527        LedLow::new(
528            base_peripherals
529                .gpio_ports
530                .get_pin(stm32f412g::gpio::PinId::PE02)
531                .unwrap()
532        ),
533        LedLow::new(
534            base_peripherals
535                .gpio_ports
536                .get_pin(stm32f412g::gpio::PinId::PE03)
537                .unwrap()
538        ),
539    ));
540
541    // BUTTONs
542    let button = components::button::ButtonComponent::new(
543        board_kernel,
544        capsules_core::button::DRIVER_NUM,
545        components::button_component_helper!(
546            stm32f412g::gpio::Pin,
547            // Select
548            (
549                base_peripherals
550                    .gpio_ports
551                    .get_pin(stm32f412g::gpio::PinId::PA00)
552                    .unwrap(),
553                kernel::hil::gpio::ActivationMode::ActiveHigh,
554                kernel::hil::gpio::FloatingState::PullNone
555            ),
556            // Down
557            (
558                base_peripherals
559                    .gpio_ports
560                    .get_pin(stm32f412g::gpio::PinId::PG01)
561                    .unwrap(),
562                kernel::hil::gpio::ActivationMode::ActiveHigh,
563                kernel::hil::gpio::FloatingState::PullNone
564            ),
565            // Left
566            (
567                base_peripherals
568                    .gpio_ports
569                    .get_pin(stm32f412g::gpio::PinId::PF15)
570                    .unwrap(),
571                kernel::hil::gpio::ActivationMode::ActiveHigh,
572                kernel::hil::gpio::FloatingState::PullNone
573            ),
574            // Right
575            (
576                base_peripherals
577                    .gpio_ports
578                    .get_pin(stm32f412g::gpio::PinId::PF14)
579                    .unwrap(),
580                kernel::hil::gpio::ActivationMode::ActiveHigh,
581                kernel::hil::gpio::FloatingState::PullNone
582            ),
583            // Up
584            (
585                base_peripherals
586                    .gpio_ports
587                    .get_pin(stm32f412g::gpio::PinId::PG00)
588                    .unwrap(),
589                kernel::hil::gpio::ActivationMode::ActiveHigh,
590                kernel::hil::gpio::FloatingState::PullNone
591            )
592        ),
593    )
594    .finalize(components::button_component_static!(stm32f412g::gpio::Pin));
595
596    // ALARM
597
598    let tim2 = &base_peripherals.tim2;
599    let mux_alarm = components::alarm::AlarmMuxComponent::new(tim2).finalize(
600        components::alarm_mux_component_static!(stm32f412g::tim2::Tim2),
601    );
602
603    let alarm = components::alarm::AlarmDriverComponent::new(
604        board_kernel,
605        capsules_core::alarm::DRIVER_NUM,
606        mux_alarm,
607    )
608    .finalize(components::alarm_component_static!(stm32f412g::tim2::Tim2));
609
610    // GPIO
611    let gpio = GpioComponent::new(
612        board_kernel,
613        capsules_core::gpio::DRIVER_NUM,
614        components::gpio_component_helper!(
615            stm32f412g::gpio::Pin,
616            // Arduino like RX/TX
617            0 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG09).unwrap(), //D0
618            1 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG14).unwrap(), //D1
619            2 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG13).unwrap(), //D2
620            3 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PF04).unwrap(), //D3
621            4 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG12).unwrap(), //D4
622            5 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PF10).unwrap(), //D5
623            6 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PF03).unwrap(), //D6
624            7 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG11).unwrap(), //D7
625            8 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG10).unwrap(), //D8
626            9 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PB08).unwrap(), //D9
627            // SPI Pins
628            10 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PA15).unwrap(), //D10
629            11 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PA07).unwrap(),  //D11
630            12 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PA06).unwrap(),  //D12
631            13 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PA15).unwrap()  //D13
632
633            // ADC Pins
634            // Enable the to use the ADC pins as GPIO
635            // 14 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PA01).unwrap(), //A0
636            // 15 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PC01).unwrap(), //A1
637            // 16 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PC03).unwrap(), //A2
638            // 17 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PC04).unwrap(), //A3
639            // 19 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PC05).unwrap(), //A4
640            // 20 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PB00).unwrap() //A5
641        ),
642    )
643    .finalize(components::gpio_component_static!(stm32f412g::gpio::Pin));
644
645    // RNG
646    let rng = RngComponent::new(
647        board_kernel,
648        capsules_core::rng::DRIVER_NUM,
649        &peripherals.trng,
650    )
651    .finalize(components::rng_component_static!(stm32f412g::trng::Trng));
652
653    // FT6206
654
655    let mux_i2c = components::i2c::I2CMuxComponent::new(&base_peripherals.i2c1, None)
656        .finalize(components::i2c_mux_component_static!(stm32f412g::i2c::I2C));
657
658    let ft6x06 = components::ft6x06::Ft6x06Component::new(
659        mux_i2c,
660        0x38,
661        base_peripherals
662            .gpio_ports
663            .get_pin(stm32f412g::gpio::PinId::PG05)
664            .unwrap(),
665    )
666    .finalize(components::ft6x06_component_static!(stm32f412g::i2c::I2C));
667
668    let bus = components::bus::Bus8080BusComponent::new(&base_peripherals.fsmc).finalize(
669        components::bus8080_bus_component_static!(stm32f412g::fsmc::Fsmc,),
670    );
671
672    let tft = components::st77xx::ST77XXComponent::new(
673        mux_alarm,
674        bus,
675        None,
676        base_peripherals
677            .gpio_ports
678            .get_pin(stm32f412g::gpio::PinId::PD11),
679        &capsules_extra::st77xx::ST7789H2,
680    )
681    .finalize(components::st77xx_component_static!(
682        // bus type
683        capsules_extra::bus::Bus8080Bus<'static, stm32f412g::fsmc::Fsmc>,
684        // timer type
685        stm32f412g::tim2::Tim2,
686        // pin type
687        stm32f412g::gpio::Pin,
688    ));
689
690    let _ = tft.init();
691
692    let screen = components::screen::ScreenComponent::new(
693        board_kernel,
694        capsules_extra::screen::screen::DRIVER_NUM,
695        tft,
696        Some(tft),
697    )
698    .finalize(components::screen_component_static!(1024));
699
700    let touch = components::touch::MultiTouchComponent::new(
701        board_kernel,
702        capsules_extra::touch::DRIVER_NUM,
703        ft6x06,
704        Some(ft6x06),
705        Some(tft),
706    )
707    .finalize(components::touch_component_static!());
708
709    touch.set_screen_rotation_offset(ScreenRotation::Rotated90);
710
711    // Uncomment this for multi touch support
712    // let touch =
713    //     components::touch::MultiTouchComponent::new(board_kernel, ft6x06, Some(ft6x06), None)
714    //         .finalize(());
715
716    // ADC
717    let adc_mux = components::adc::AdcMuxComponent::new(&base_peripherals.adc1)
718        .finalize(components::adc_mux_component_static!(stm32f412g::adc::Adc));
719
720    let temp_sensor = components::temperature_stm::TemperatureSTMComponent::new(
721        adc_mux,
722        stm32f412g::adc::Channel::Channel18,
723        2.5,
724        0.76,
725    )
726    .finalize(components::temperature_stm_adc_component_static!(
727        stm32f412g::adc::Adc
728    ));
729
730    let temp = components::temperature::TemperatureComponent::new(
731        board_kernel,
732        capsules_extra::temperature::DRIVER_NUM,
733        temp_sensor,
734    )
735    .finalize(components::temperature_component_static!(
736        TemperatureSTMSensor
737    ));
738
739    let adc_channel_0 =
740        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel1)
741            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
742
743    let adc_channel_1 =
744        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel11)
745            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
746
747    let adc_channel_2 =
748        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel13)
749            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
750
751    let adc_channel_3 =
752        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel14)
753            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
754
755    let adc_channel_4 =
756        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel15)
757            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
758
759    let adc_channel_5 =
760        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel8)
761            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
762
763    let adc_syscall =
764        components::adc::AdcVirtualComponent::new(board_kernel, capsules_core::adc::DRIVER_NUM)
765            .finalize(components::adc_syscall_component_helper!(
766                adc_channel_0,
767                adc_channel_1,
768                adc_channel_2,
769                adc_channel_3,
770                adc_channel_4,
771                adc_channel_5
772            ));
773
774    let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
775        .finalize(components::process_printer_text_component_static!());
776    PANIC_RESOURCES.get().map(|resources| {
777        resources.printer.put(process_printer);
778    });
779
780    // PROCESS CONSOLE
781    let process_console = components::process_console::ProcessConsoleComponent::new(
782        board_kernel,
783        uart_mux,
784        mux_alarm,
785        process_printer,
786        Some(cortexm4::support::reset),
787    )
788    .finalize(components::process_console_component_static!(
789        stm32f412g::tim2::Tim2
790    ));
791    let _ = process_console.start();
792
793    let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
794        .finalize(components::round_robin_component_static!(NUM_PROCS));
795
796    let stm32f412g = STM32F412GDiscovery {
797        console,
798        ipc: kernel::ipc::IPC::new(
799            board_kernel,
800            kernel::ipc::DRIVER_NUM,
801            &memory_allocation_capability,
802        ),
803        led,
804        button,
805        alarm,
806        gpio,
807        adc: adc_syscall,
808        touch,
809        screen,
810        temperature: temp,
811        rng,
812
813        scheduler,
814        systick: cortexm4::systick::SysTick::new_with_calibration(
815            (HSI_FREQUENCY_MHZ * 1_000_000) as u32,
816        ),
817    };
818
819    // // Optional kernel tests
820    // //
821    // // See comment in `boards/imix/src/main.rs`
822    // virtual_uart_rx_test::run_virtual_uart_receive(mux_uart);
823    // base_peripherals.fsmc.write(0x04, 120);
824    // debug!("id {}", base_peripherals.fsmc.read(0x05));
825
826    debug!("Initialization complete. Entering main loop");
827
828    extern "C" {
829        /// Beginning of the ROM region containing app images.
830        ///
831        /// This symbol is defined in the linker script.
832        static _sapps: u8;
833
834        /// End of the ROM region containing app images.
835        ///
836        /// This symbol is defined in the linker script.
837        static _eapps: u8;
838
839        /// Beginning of the RAM region for app memory.
840        ///
841        /// This symbol is defined in the linker script.
842        static mut _sappmem: u8;
843
844        /// End of the RAM region for app memory.
845        ///
846        /// This symbol is defined in the linker script.
847        static _eappmem: u8;
848    }
849
850    kernel::process::load_processes(
851        board_kernel,
852        chip,
853        core::slice::from_raw_parts(
854            core::ptr::addr_of!(_sapps),
855            core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
856        ),
857        core::slice::from_raw_parts_mut(
858            core::ptr::addr_of_mut!(_sappmem),
859            core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
860        ),
861        &FAULT_RESPONSE,
862        &process_management_capability,
863    )
864    .unwrap_or_else(|err| {
865        debug!("Error loading processes!");
866        debug!("{:?}", err);
867    });
868
869    //Uncomment to run multi alarm test
870    /*components::test::multi_alarm_test::MultiAlarmTestComponent::new(mux_alarm)
871    .finalize(components::multi_alarm_test_component_buf!(stm32f412g::tim2::Tim2))
872    .run();*/
873
874    (board_kernel, stm32f412g, chip)
875}
876
877/// Main function called after RAM initialized.
878#[no_mangle]
879pub unsafe fn main() {
880    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
881
882    let (board_kernel, platform, chip) = start();
883    board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
884}