stm32f412gdiscovery/
main.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Board file for STM32F412GDiscovery Discovery kit development board
6//!
7//! - <https://www.st.com/en/evaluation-tools/32f412gdiscovery.html>
8
9#![no_std]
10#![no_main]
11#![deny(missing_docs)]
12
13use core::ptr::addr_of_mut;
14
15use capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm;
16use components::gpio::GpioComponent;
17use components::rng::RngComponent;
18use kernel::capabilities;
19use kernel::component::Component;
20use kernel::hil::gpio;
21use kernel::hil::led::LedLow;
22use kernel::hil::screen::ScreenRotation;
23use kernel::platform::{KernelResources, SyscallDriverLookup};
24use kernel::process::ProcessArray;
25use kernel::scheduler::round_robin::RoundRobinSched;
26use kernel::{create_capability, debug, static_init};
27use stm32f412g::chip_specs::Stm32f412Specs;
28use stm32f412g::clocks::hsi::HSI_FREQUENCY_MHZ;
29use stm32f412g::interrupt_service::Stm32f412gDefaultPeripherals;
30use stm32f412g::rcc::PllSource;
31
32/// Support routines for debugging I/O.
33pub mod io;
34
35// Number of concurrent processes this platform supports.
36const NUM_PROCS: usize = 4;
37
38type ChipHw = stm32f412g::chip::Stm32f4xx<'static, Stm32f412gDefaultPeripherals<'static>>;
39
40/// Static variables used by io.rs.
41static mut PROCESSES: Option<&'static ProcessArray<NUM_PROCS>> = None;
42static mut CHIP: Option<&'static ChipHw> = None;
43static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
44    None;
45
46// How should the kernel respond when a process faults.
47const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
48    capsules_system::process_policies::PanicFaultPolicy {};
49
50kernel::stack_size! {0x2000}
51
52type TemperatureSTMSensor = components::temperature_stm::TemperatureSTMComponentType<
53    capsules_core::virtualizers::virtual_adc::AdcDevice<'static, stm32f412g::adc::Adc<'static>>,
54>;
55type TemperatureDriver = components::temperature::TemperatureComponentType<TemperatureSTMSensor>;
56type RngDriver = components::rng::RngComponentType<stm32f412g::trng::Trng<'static>>;
57type ScreenDriver = components::screen::ScreenComponentType;
58
59/// A structure representing this platform that holds references to all
60/// capsules for this platform.
61struct STM32F412GDiscovery {
62    console: &'static capsules_core::console::Console<'static>,
63    ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
64    led: &'static capsules_core::led::LedDriver<
65        'static,
66        LedLow<'static, stm32f412g::gpio::Pin<'static>>,
67        4,
68    >,
69    button: &'static capsules_core::button::Button<'static, stm32f412g::gpio::Pin<'static>>,
70    alarm: &'static capsules_core::alarm::AlarmDriver<
71        'static,
72        VirtualMuxAlarm<'static, stm32f412g::tim2::Tim2<'static>>,
73    >,
74    gpio: &'static capsules_core::gpio::GPIO<'static, stm32f412g::gpio::Pin<'static>>,
75    adc: &'static capsules_core::adc::AdcVirtualized<'static>,
76    touch: &'static capsules_extra::touch::Touch<'static>,
77    screen: &'static ScreenDriver,
78    temperature: &'static TemperatureDriver,
79    rng: &'static RngDriver,
80
81    scheduler: &'static RoundRobinSched<'static>,
82    systick: cortexm4::systick::SysTick,
83}
84
85/// Mapping of integer syscalls to objects that implement syscalls.
86impl SyscallDriverLookup for STM32F412GDiscovery {
87    fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
88    where
89        F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
90    {
91        match driver_num {
92            capsules_core::console::DRIVER_NUM => f(Some(self.console)),
93            capsules_core::led::DRIVER_NUM => f(Some(self.led)),
94            capsules_core::button::DRIVER_NUM => f(Some(self.button)),
95            capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
96            kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
97            capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
98            capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
99            capsules_extra::touch::DRIVER_NUM => f(Some(self.touch)),
100            capsules_extra::screen::screen::DRIVER_NUM => f(Some(self.screen)),
101            capsules_extra::temperature::DRIVER_NUM => f(Some(self.temperature)),
102            capsules_core::rng::DRIVER_NUM => f(Some(self.rng)),
103            _ => f(None),
104        }
105    }
106}
107
108impl
109    KernelResources<
110        stm32f412g::chip::Stm32f4xx<
111            'static,
112            stm32f412g::interrupt_service::Stm32f412gDefaultPeripherals<'static>,
113        >,
114    > for STM32F412GDiscovery
115{
116    type SyscallDriverLookup = Self;
117    type SyscallFilter = ();
118    type ProcessFault = ();
119    type Scheduler = RoundRobinSched<'static>;
120    type SchedulerTimer = cortexm4::systick::SysTick;
121    type WatchDog = ();
122    type ContextSwitchCallback = ();
123
124    fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
125        self
126    }
127    fn syscall_filter(&self) -> &Self::SyscallFilter {
128        &()
129    }
130    fn process_fault(&self) -> &Self::ProcessFault {
131        &()
132    }
133    fn scheduler(&self) -> &Self::Scheduler {
134        self.scheduler
135    }
136    fn scheduler_timer(&self) -> &Self::SchedulerTimer {
137        &self.systick
138    }
139    fn watchdog(&self) -> &Self::WatchDog {
140        &()
141    }
142    fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
143        &()
144    }
145}
146
147/// Helper function called during bring-up that configures DMA.
148unsafe fn setup_dma(
149    dma: &stm32f412g::dma::Dma1,
150    dma_streams: &'static [stm32f412g::dma::Stream<stm32f412g::dma::Dma1>; 8],
151    usart2: &'static stm32f412g::usart::Usart<stm32f412g::dma::Dma1>,
152) {
153    use stm32f412g::dma::Dma1Peripheral;
154    use stm32f412g::usart;
155
156    dma.enable_clock();
157
158    let usart2_tx_stream = &dma_streams[Dma1Peripheral::USART2_TX.get_stream_idx()];
159    let usart2_rx_stream = &dma_streams[Dma1Peripheral::USART2_RX.get_stream_idx()];
160
161    usart2.set_dma(
162        usart::TxDMA(usart2_tx_stream),
163        usart::RxDMA(usart2_rx_stream),
164    );
165
166    usart2_tx_stream.set_client(usart2);
167    usart2_rx_stream.set_client(usart2);
168
169    usart2_tx_stream.setup(Dma1Peripheral::USART2_TX);
170    usart2_rx_stream.setup(Dma1Peripheral::USART2_RX);
171
172    cortexm4::nvic::Nvic::new(Dma1Peripheral::USART2_TX.get_stream_irqn()).enable();
173    cortexm4::nvic::Nvic::new(Dma1Peripheral::USART2_RX.get_stream_irqn()).enable();
174}
175
176/// Helper function called during bring-up that configures multiplexed I/O.
177unsafe fn set_pin_primary_functions(
178    syscfg: &stm32f412g::syscfg::Syscfg,
179    i2c1: &stm32f412g::i2c::I2C,
180    gpio_ports: &'static stm32f412g::gpio::GpioPorts<'static>,
181    peripheral_clock_frequency: usize,
182) {
183    use kernel::hil::gpio::Configure;
184    use stm32f412g::gpio::{AlternateFunction, Mode, PinId, PortId};
185
186    syscfg.enable_clock();
187
188    gpio_ports.get_port_from_port_id(PortId::E).enable_clock();
189
190    // User LD3 is connected to PE02. Configure PE02 as `debug_gpio!(0, ...)`
191    gpio_ports.get_pin(PinId::PE02).map(|pin| {
192        pin.make_output();
193
194        // Configure kernel debug gpios as early as possible
195        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    // Create an array to hold process references.
449    let processes = components::process_array::ProcessArrayComponent::new()
450        .finalize(components::process_array_component_static!(NUM_PROCS));
451    PROCESSES = Some(processes);
452
453    // Setup space to store the core kernel data structure.
454    let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
455
456    let chip = static_init!(
457        stm32f412g::chip::Stm32f4xx<Stm32f412gDefaultPeripherals>,
458        stm32f412g::chip::Stm32f4xx::new(peripherals)
459    );
460    CHIP = Some(chip);
461
462    // UART
463
464    // Create a shared UART channel for kernel debug.
465    base_peripherals.usart2.enable_clock();
466    let uart_mux = components::console::UartMuxComponent::new(&base_peripherals.usart2, 115200)
467        .finalize(components::uart_mux_component_static!());
468
469    (*addr_of_mut!(io::WRITER)).set_initialized();
470
471    // Create capabilities that the board needs to call certain protected kernel
472    // functions.
473    let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
474    let process_management_capability =
475        create_capability!(capabilities::ProcessManagementCapability);
476
477    // Setup the console.
478    let console = components::console::ConsoleComponent::new(
479        board_kernel,
480        capsules_core::console::DRIVER_NUM,
481        uart_mux,
482    )
483    .finalize(components::console_component_static!());
484    // Create the debugger object that handles calls to `debug!()`.
485    components::debug_writer::DebugWriterComponent::new::<
486        <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
487    >(
488        uart_mux,
489        create_capability!(capabilities::SetDebugWriterCapability),
490    )
491    .finalize(components::debug_writer_component_static!());
492
493    // LEDs
494
495    // Clock to Port A is enabled in `set_pin_primary_functions()`
496
497    let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
498        LedLow<'static, stm32f412g::gpio::Pin>,
499        LedLow::new(
500            base_peripherals
501                .gpio_ports
502                .get_pin(stm32f412g::gpio::PinId::PE00)
503                .unwrap()
504        ),
505        LedLow::new(
506            base_peripherals
507                .gpio_ports
508                .get_pin(stm32f412g::gpio::PinId::PE01)
509                .unwrap()
510        ),
511        LedLow::new(
512            base_peripherals
513                .gpio_ports
514                .get_pin(stm32f412g::gpio::PinId::PE02)
515                .unwrap()
516        ),
517        LedLow::new(
518            base_peripherals
519                .gpio_ports
520                .get_pin(stm32f412g::gpio::PinId::PE03)
521                .unwrap()
522        ),
523    ));
524
525    // BUTTONs
526    let button = components::button::ButtonComponent::new(
527        board_kernel,
528        capsules_core::button::DRIVER_NUM,
529        components::button_component_helper!(
530            stm32f412g::gpio::Pin,
531            // Select
532            (
533                base_peripherals
534                    .gpio_ports
535                    .get_pin(stm32f412g::gpio::PinId::PA00)
536                    .unwrap(),
537                kernel::hil::gpio::ActivationMode::ActiveHigh,
538                kernel::hil::gpio::FloatingState::PullNone
539            ),
540            // Down
541            (
542                base_peripherals
543                    .gpio_ports
544                    .get_pin(stm32f412g::gpio::PinId::PG01)
545                    .unwrap(),
546                kernel::hil::gpio::ActivationMode::ActiveHigh,
547                kernel::hil::gpio::FloatingState::PullNone
548            ),
549            // Left
550            (
551                base_peripherals
552                    .gpio_ports
553                    .get_pin(stm32f412g::gpio::PinId::PF15)
554                    .unwrap(),
555                kernel::hil::gpio::ActivationMode::ActiveHigh,
556                kernel::hil::gpio::FloatingState::PullNone
557            ),
558            // Right
559            (
560                base_peripherals
561                    .gpio_ports
562                    .get_pin(stm32f412g::gpio::PinId::PF14)
563                    .unwrap(),
564                kernel::hil::gpio::ActivationMode::ActiveHigh,
565                kernel::hil::gpio::FloatingState::PullNone
566            ),
567            // Up
568            (
569                base_peripherals
570                    .gpio_ports
571                    .get_pin(stm32f412g::gpio::PinId::PG00)
572                    .unwrap(),
573                kernel::hil::gpio::ActivationMode::ActiveHigh,
574                kernel::hil::gpio::FloatingState::PullNone
575            )
576        ),
577    )
578    .finalize(components::button_component_static!(stm32f412g::gpio::Pin));
579
580    // ALARM
581
582    let tim2 = &base_peripherals.tim2;
583    let mux_alarm = components::alarm::AlarmMuxComponent::new(tim2).finalize(
584        components::alarm_mux_component_static!(stm32f412g::tim2::Tim2),
585    );
586
587    let alarm = components::alarm::AlarmDriverComponent::new(
588        board_kernel,
589        capsules_core::alarm::DRIVER_NUM,
590        mux_alarm,
591    )
592    .finalize(components::alarm_component_static!(stm32f412g::tim2::Tim2));
593
594    // GPIO
595    let gpio = GpioComponent::new(
596        board_kernel,
597        capsules_core::gpio::DRIVER_NUM,
598        components::gpio_component_helper!(
599            stm32f412g::gpio::Pin,
600            // Arduino like RX/TX
601            0 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG09).unwrap(), //D0
602            1 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG14).unwrap(), //D1
603            2 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG13).unwrap(), //D2
604            3 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PF04).unwrap(), //D3
605            4 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG12).unwrap(), //D4
606            5 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PF10).unwrap(), //D5
607            6 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PF03).unwrap(), //D6
608            7 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG11).unwrap(), //D7
609            8 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PG10).unwrap(), //D8
610            9 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PB08).unwrap(), //D9
611            // SPI Pins
612            10 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PA15).unwrap(), //D10
613            11 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PA07).unwrap(),  //D11
614            12 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PA06).unwrap(),  //D12
615            13 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PA15).unwrap()  //D13
616
617            // ADC Pins
618            // Enable the to use the ADC pins as GPIO
619            // 14 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PA01).unwrap(), //A0
620            // 15 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PC01).unwrap(), //A1
621            // 16 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PC03).unwrap(), //A2
622            // 17 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PC04).unwrap(), //A3
623            // 19 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PC05).unwrap(), //A4
624            // 20 => base_peripherals.gpio_ports.get_pin(stm32f412g::gpio::PinId::PB00).unwrap() //A5
625        ),
626    )
627    .finalize(components::gpio_component_static!(stm32f412g::gpio::Pin));
628
629    // RNG
630    let rng = RngComponent::new(
631        board_kernel,
632        capsules_core::rng::DRIVER_NUM,
633        &peripherals.trng,
634    )
635    .finalize(components::rng_component_static!(stm32f412g::trng::Trng));
636
637    // FT6206
638
639    let mux_i2c = components::i2c::I2CMuxComponent::new(&base_peripherals.i2c1, None)
640        .finalize(components::i2c_mux_component_static!(stm32f412g::i2c::I2C));
641
642    let ft6x06 = components::ft6x06::Ft6x06Component::new(
643        mux_i2c,
644        0x38,
645        base_peripherals
646            .gpio_ports
647            .get_pin(stm32f412g::gpio::PinId::PG05)
648            .unwrap(),
649    )
650    .finalize(components::ft6x06_component_static!(stm32f412g::i2c::I2C));
651
652    let bus = components::bus::Bus8080BusComponent::new(&base_peripherals.fsmc).finalize(
653        components::bus8080_bus_component_static!(stm32f412g::fsmc::Fsmc,),
654    );
655
656    let tft = components::st77xx::ST77XXComponent::new(
657        mux_alarm,
658        bus,
659        None,
660        base_peripherals
661            .gpio_ports
662            .get_pin(stm32f412g::gpio::PinId::PD11),
663        &capsules_extra::st77xx::ST7789H2,
664    )
665    .finalize(components::st77xx_component_static!(
666        // bus type
667        capsules_extra::bus::Bus8080Bus<'static, stm32f412g::fsmc::Fsmc>,
668        // timer type
669        stm32f412g::tim2::Tim2,
670        // pin type
671        stm32f412g::gpio::Pin,
672    ));
673
674    let _ = tft.init();
675
676    let screen = components::screen::ScreenComponent::new(
677        board_kernel,
678        capsules_extra::screen::screen::DRIVER_NUM,
679        tft,
680        Some(tft),
681    )
682    .finalize(components::screen_component_static!(1024));
683
684    let touch = components::touch::MultiTouchComponent::new(
685        board_kernel,
686        capsules_extra::touch::DRIVER_NUM,
687        ft6x06,
688        Some(ft6x06),
689        Some(tft),
690    )
691    .finalize(components::touch_component_static!());
692
693    touch.set_screen_rotation_offset(ScreenRotation::Rotated90);
694
695    // Uncomment this for multi touch support
696    // let touch =
697    //     components::touch::MultiTouchComponent::new(board_kernel, ft6x06, Some(ft6x06), None)
698    //         .finalize(());
699
700    // ADC
701    let adc_mux = components::adc::AdcMuxComponent::new(&base_peripherals.adc1)
702        .finalize(components::adc_mux_component_static!(stm32f412g::adc::Adc));
703
704    let temp_sensor = components::temperature_stm::TemperatureSTMComponent::new(
705        adc_mux,
706        stm32f412g::adc::Channel::Channel18,
707        2.5,
708        0.76,
709    )
710    .finalize(components::temperature_stm_adc_component_static!(
711        stm32f412g::adc::Adc
712    ));
713
714    let temp = components::temperature::TemperatureComponent::new(
715        board_kernel,
716        capsules_extra::temperature::DRIVER_NUM,
717        temp_sensor,
718    )
719    .finalize(components::temperature_component_static!(
720        TemperatureSTMSensor
721    ));
722
723    let adc_channel_0 =
724        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel1)
725            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
726
727    let adc_channel_1 =
728        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel11)
729            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
730
731    let adc_channel_2 =
732        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel13)
733            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
734
735    let adc_channel_3 =
736        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel14)
737            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
738
739    let adc_channel_4 =
740        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel15)
741            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
742
743    let adc_channel_5 =
744        components::adc::AdcComponent::new(adc_mux, stm32f412g::adc::Channel::Channel8)
745            .finalize(components::adc_component_static!(stm32f412g::adc::Adc));
746
747    let adc_syscall =
748        components::adc::AdcVirtualComponent::new(board_kernel, capsules_core::adc::DRIVER_NUM)
749            .finalize(components::adc_syscall_component_helper!(
750                adc_channel_0,
751                adc_channel_1,
752                adc_channel_2,
753                adc_channel_3,
754                adc_channel_4,
755                adc_channel_5
756            ));
757
758    let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
759        .finalize(components::process_printer_text_component_static!());
760    PROCESS_PRINTER = Some(process_printer);
761
762    // PROCESS CONSOLE
763    let process_console = components::process_console::ProcessConsoleComponent::new(
764        board_kernel,
765        uart_mux,
766        mux_alarm,
767        process_printer,
768        Some(cortexm4::support::reset),
769    )
770    .finalize(components::process_console_component_static!(
771        stm32f412g::tim2::Tim2
772    ));
773    let _ = process_console.start();
774
775    let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
776        .finalize(components::round_robin_component_static!(NUM_PROCS));
777
778    let stm32f412g = STM32F412GDiscovery {
779        console,
780        ipc: kernel::ipc::IPC::new(
781            board_kernel,
782            kernel::ipc::DRIVER_NUM,
783            &memory_allocation_capability,
784        ),
785        led,
786        button,
787        alarm,
788        gpio,
789        adc: adc_syscall,
790        touch,
791        screen,
792        temperature: temp,
793        rng,
794
795        scheduler,
796        systick: cortexm4::systick::SysTick::new_with_calibration(
797            (HSI_FREQUENCY_MHZ * 1_000_000) as u32,
798        ),
799    };
800
801    // // Optional kernel tests
802    // //
803    // // See comment in `boards/imix/src/main.rs`
804    // virtual_uart_rx_test::run_virtual_uart_receive(mux_uart);
805    // base_peripherals.fsmc.write(0x04, 120);
806    // debug!("id {}", base_peripherals.fsmc.read(0x05));
807
808    debug!("Initialization complete. Entering main loop");
809
810    extern "C" {
811        /// Beginning of the ROM region containing app images.
812        ///
813        /// This symbol is defined in the linker script.
814        static _sapps: u8;
815
816        /// End of the ROM region containing app images.
817        ///
818        /// This symbol is defined in the linker script.
819        static _eapps: u8;
820
821        /// Beginning of the RAM region for app memory.
822        ///
823        /// This symbol is defined in the linker script.
824        static mut _sappmem: u8;
825
826        /// End of the RAM region for app memory.
827        ///
828        /// This symbol is defined in the linker script.
829        static _eappmem: u8;
830    }
831
832    kernel::process::load_processes(
833        board_kernel,
834        chip,
835        core::slice::from_raw_parts(
836            core::ptr::addr_of!(_sapps),
837            core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
838        ),
839        core::slice::from_raw_parts_mut(
840            core::ptr::addr_of_mut!(_sappmem),
841            core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
842        ),
843        &FAULT_RESPONSE,
844        &process_management_capability,
845    )
846    .unwrap_or_else(|err| {
847        debug!("Error loading processes!");
848        debug!("{:?}", err);
849    });
850
851    //Uncomment to run multi alarm test
852    /*components::test::multi_alarm_test::MultiAlarmTestComponent::new(mux_alarm)
853    .finalize(components::multi_alarm_test_component_buf!(stm32f412g::tim2::Tim2))
854    .run();*/
855
856    (board_kernel, stm32f412g, chip)
857}
858
859/// Main function called after RAM initialized.
860#[no_mangle]
861pub unsafe fn main() {
862    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
863
864    let (board_kernel, platform, chip) = start();
865    board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
866}