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