nucleo_f429zi/
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 Nucleo-F429ZI development board
6//!
7//! - <https://www.st.com/en/evaluation-tools/nucleo-f429zi.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 kernel::capabilities;
18use kernel::component::Component;
19use kernel::debug::PanicResources;
20use kernel::hil::led::LedHigh;
21use kernel::platform::{KernelResources, SyscallDriverLookup};
22use kernel::scheduler::round_robin::RoundRobinSched;
23use kernel::utilities::single_thread_value::SingleThreadValue;
24use kernel::{create_capability, debug, static_init};
25
26use stm32f429zi::chip_specs::Stm32f429Specs;
27use stm32f429zi::clocks::hsi::HSI_FREQUENCY_MHZ;
28use stm32f429zi::gpio::{AlternateFunction, Mode, PinId, PortId};
29use stm32f429zi::interrupt_service::Stm32f429ziDefaultPeripherals;
30
31/// Support routines for debugging I/O.
32pub mod io;
33
34// Number of concurrent processes this platform supports.
35const NUM_PROCS: usize = 4;
36
37type ChipHw = stm32f429zi::chip::Stm32f4xx<'static, Stm32f429ziDefaultPeripherals<'static>>;
38type ProcessPrinterInUse = capsules_system::process_printer::ProcessPrinterText;
39
40/// Resources for when a board panics used by io.rs.
41static PANIC_RESOURCES: SingleThreadValue<PanicResources<ChipHw, ProcessPrinterInUse>> =
42    SingleThreadValue::new(PanicResources::new());
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
50//------------------------------------------------------------------------------
51// SYSCALL DRIVER TYPE DEFINITIONS
52//------------------------------------------------------------------------------
53
54type TemperatureSTMSensor = components::temperature_stm::TemperatureSTMComponentType<
55    capsules_core::virtualizers::virtual_adc::AdcDevice<'static, stm32f429zi::adc::Adc<'static>>,
56>;
57type TemperatureDriver = components::temperature::TemperatureComponentType<TemperatureSTMSensor>;
58type RngDriver = components::rng::RngComponentType<stm32f429zi::trng::Trng<'static>>;
59
60/// Nucleo F429ZI HSE frequency in MHz
61pub const NUCLEO_F429ZI_HSE_FREQUENCY_MHZ: usize = 8;
62
63/// A structure representing this platform that holds references to all
64/// capsules for this platform.
65struct NucleoF429ZI {
66    console: &'static capsules_core::console::Console<'static>,
67    ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
68    led: &'static capsules_core::led::LedDriver<
69        'static,
70        LedHigh<'static, stm32f429zi::gpio::Pin<'static>>,
71        3,
72    >,
73    button: &'static capsules_core::button::Button<'static, stm32f429zi::gpio::Pin<'static>>,
74    adc: &'static capsules_core::adc::AdcVirtualized<'static>,
75    dac: &'static capsules_extra::dac::Dac<'static>,
76    alarm: &'static capsules_core::alarm::AlarmDriver<
77        'static,
78        VirtualMuxAlarm<'static, stm32f429zi::tim2::Tim2<'static>>,
79    >,
80    temperature: &'static TemperatureDriver,
81    gpio: &'static capsules_core::gpio::GPIO<'static, stm32f429zi::gpio::Pin<'static>>,
82    rng: &'static RngDriver,
83
84    scheduler: &'static RoundRobinSched<'static>,
85    systick: cortexm4::systick::SysTick,
86    can: &'static capsules_extra::can::CanCapsule<'static, stm32f429zi::can::Can<'static>>,
87    date_time: &'static capsules_extra::date_time::DateTimeCapsule<
88        'static,
89        stm32f429zi::rtc::Rtc<'static>,
90    >,
91}
92
93/// Mapping of integer syscalls to objects that implement syscalls.
94impl SyscallDriverLookup for NucleoF429ZI {
95    fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
96    where
97        F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
98    {
99        match driver_num {
100            capsules_core::console::DRIVER_NUM => f(Some(self.console)),
101            capsules_core::led::DRIVER_NUM => f(Some(self.led)),
102            capsules_core::button::DRIVER_NUM => f(Some(self.button)),
103            capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
104            capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
105            capsules_extra::temperature::DRIVER_NUM => f(Some(self.temperature)),
106            kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
107            capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
108            capsules_core::rng::DRIVER_NUM => f(Some(self.rng)),
109            capsules_extra::can::DRIVER_NUM => f(Some(self.can)),
110            capsules_extra::dac::DRIVER_NUM => f(Some(self.dac)),
111            capsules_extra::date_time::DRIVER_NUM => f(Some(self.date_time)),
112            _ => f(None),
113        }
114    }
115}
116
117impl
118    KernelResources<
119        stm32f429zi::chip::Stm32f4xx<
120            'static,
121            stm32f429zi::interrupt_service::Stm32f429ziDefaultPeripherals<'static>,
122        >,
123    > for NucleoF429ZI
124{
125    type SyscallDriverLookup = Self;
126    type SyscallFilter = ();
127    type ProcessFault = ();
128    type Scheduler = RoundRobinSched<'static>;
129    type SchedulerTimer = cortexm4::systick::SysTick;
130    type WatchDog = ();
131    type ContextSwitchCallback = ();
132
133    fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
134        self
135    }
136    fn syscall_filter(&self) -> &Self::SyscallFilter {
137        &()
138    }
139    fn process_fault(&self) -> &Self::ProcessFault {
140        &()
141    }
142    fn scheduler(&self) -> &Self::Scheduler {
143        self.scheduler
144    }
145    fn scheduler_timer(&self) -> &Self::SchedulerTimer {
146        &self.systick
147    }
148    fn watchdog(&self) -> &Self::WatchDog {
149        &()
150    }
151    fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
152        &()
153    }
154}
155
156/// Helper function called during bring-up that configures DMA.
157unsafe fn setup_dma(
158    dma: &stm32f429zi::dma::Dma1,
159    dma_streams: &'static [stm32f429zi::dma::Stream<stm32f429zi::dma::Dma1>; 8],
160    usart3: &'static stm32f429zi::usart::Usart<stm32f429zi::dma::Dma1>,
161) {
162    use stm32f429zi::dma::Dma1Peripheral;
163    use stm32f429zi::usart;
164
165    dma.enable_clock();
166
167    let usart3_tx_stream = &dma_streams[Dma1Peripheral::USART3_TX.get_stream_idx()];
168    let usart3_rx_stream = &dma_streams[Dma1Peripheral::USART3_RX.get_stream_idx()];
169
170    usart3.set_dma(
171        usart::TxDMA(usart3_tx_stream),
172        usart::RxDMA(usart3_rx_stream),
173    );
174
175    usart3_tx_stream.set_client(usart3);
176    usart3_rx_stream.set_client(usart3);
177
178    usart3_tx_stream.setup(Dma1Peripheral::USART3_TX);
179    usart3_rx_stream.setup(Dma1Peripheral::USART3_RX);
180
181    cortexm4::nvic::Nvic::new(Dma1Peripheral::USART3_TX.get_stream_irqn()).enable();
182    cortexm4::nvic::Nvic::new(Dma1Peripheral::USART3_RX.get_stream_irqn()).enable();
183}
184
185/// Helper function called during bring-up that configures multiplexed I/O.
186unsafe fn set_pin_primary_functions(
187    syscfg: &stm32f429zi::syscfg::Syscfg,
188    gpio_ports: &'static stm32f429zi::gpio::GpioPorts<'static>,
189) {
190    use kernel::hil::gpio::Configure;
191
192    syscfg.enable_clock();
193
194    gpio_ports.get_port_from_port_id(PortId::B).enable_clock();
195
196    // User LD2 is connected to PB07. Configure PB07 as `debug_gpio!(0, ...)`
197    gpio_ports.get_pin(PinId::PB07).map(|pin| {
198        pin.make_output();
199
200        // Configure kernel debug gpios as early as possible
201        let debug_gpios = static_init!([&'static dyn kernel::hil::gpio::Pin; 1], [pin]);
202        kernel::debug::initialize_debug_gpio::<
203            <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
204        >();
205        kernel::debug::assign_gpios(debug_gpios);
206    });
207
208    gpio_ports.get_port_from_port_id(PortId::D).enable_clock();
209
210    // pd8 and pd9 (USART3) is connected to ST-LINK virtual COM port
211    gpio_ports.get_pin(PinId::PD08).map(|pin| {
212        pin.set_mode(Mode::AlternateFunctionMode);
213        // AF7 is USART2_TX
214        pin.set_alternate_function(AlternateFunction::AF7);
215    });
216    gpio_ports.get_pin(PinId::PD09).map(|pin| {
217        pin.set_mode(Mode::AlternateFunctionMode);
218        // AF7 is USART2_RX
219        pin.set_alternate_function(AlternateFunction::AF7);
220    });
221
222    gpio_ports.get_port_from_port_id(PortId::C).enable_clock();
223
224    // button is connected on pc13
225    gpio_ports.get_pin(PinId::PC13).map(|pin| {
226        pin.enable_interrupt();
227    });
228
229    // set interrupt for pin D0
230    gpio_ports.get_pin(PinId::PG09).map(|pin| {
231        pin.enable_interrupt();
232    });
233
234    // Enable clocks for GPIO Ports
235    // Disable some of them if you don't need some of the GPIOs
236    gpio_ports.get_port_from_port_id(PortId::A).enable_clock();
237    // Ports B, C and D are already enabled
238    gpio_ports.get_port_from_port_id(PortId::E).enable_clock();
239    gpio_ports.get_port_from_port_id(PortId::F).enable_clock();
240    gpio_ports.get_port_from_port_id(PortId::G).enable_clock();
241    gpio_ports.get_port_from_port_id(PortId::H).enable_clock();
242
243    // Arduino A0
244    gpio_ports.get_pin(PinId::PA03).map(|pin| {
245        pin.set_mode(stm32f429zi::gpio::Mode::AnalogMode);
246    });
247
248    // Arduino A1
249    gpio_ports.get_pin(PinId::PC00).map(|pin| {
250        pin.set_mode(stm32f429zi::gpio::Mode::AnalogMode);
251    });
252
253    // Arduino A2
254    gpio_ports.get_pin(PinId::PC03).map(|pin| {
255        pin.set_mode(stm32f429zi::gpio::Mode::AnalogMode);
256    });
257
258    // Arduino A6
259    gpio_ports.get_pin(PinId::PB01).map(|pin| {
260        pin.set_mode(stm32f429zi::gpio::Mode::AnalogMode);
261    });
262
263    // Arduino A7
264    gpio_ports.get_pin(PinId::PC02).map(|pin| {
265        pin.set_mode(stm32f429zi::gpio::Mode::AnalogMode);
266    });
267
268    gpio_ports.get_pin(PinId::PD00).map(|pin| {
269        pin.set_mode(Mode::AlternateFunctionMode);
270        // AF9 is CAN_RX
271        pin.set_alternate_function(AlternateFunction::AF9);
272        pin.set_floating_state(kernel::hil::gpio::FloatingState::PullDown);
273    });
274    gpio_ports.get_pin(PinId::PD01).map(|pin| {
275        pin.set_mode(Mode::AlternateFunctionMode);
276        // AF9 is CAN_TX
277        pin.set_alternate_function(AlternateFunction::AF9);
278    });
279
280    // DAC Channel 1
281    gpio_ports.get_pin(PinId::PA04).map(|pin| {
282        pin.set_mode(stm32f429zi::gpio::Mode::AnalogMode);
283    });
284}
285
286/// Helper function for miscellaneous peripheral functions
287unsafe fn setup_peripherals(
288    tim2: &stm32f429zi::tim2::Tim2,
289    trng: &stm32f429zi::trng::Trng,
290    can1: &'static stm32f429zi::can::Can,
291    rtc: &'static stm32f429zi::rtc::Rtc,
292) {
293    // USART3 IRQn is 39
294    cortexm4::nvic::Nvic::new(stm32f429zi::nvic::USART3).enable();
295
296    // TIM2 IRQn is 28
297    tim2.enable_clock();
298    tim2.start();
299    cortexm4::nvic::Nvic::new(stm32f429zi::nvic::TIM2).enable();
300
301    // RNG
302    trng.enable_clock();
303
304    // CAN
305    can1.enable_clock();
306
307    // RTC
308    rtc.enable_clock();
309}
310
311/// This is in a separate, inline(never) function so that its stack frame is
312/// removed when this function returns. Otherwise, the stack space used for
313/// these static_inits is wasted.
314#[inline(never)]
315unsafe fn start() -> (
316    &'static kernel::Kernel,
317    NucleoF429ZI,
318    &'static stm32f429zi::chip::Stm32f4xx<'static, Stm32f429ziDefaultPeripherals<'static>>,
319) {
320    stm32f429zi::init();
321
322    // Initialize deferred calls very early.
323    kernel::deferred_call::initialize_deferred_call_state::<
324        <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
325    >();
326
327    // Bind global variables to this thread.
328    PANIC_RESOURCES.bind_to_thread::<<ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider>();
329
330    // We use the default HSI 16Mhz clock
331    let rcc = static_init!(stm32f429zi::rcc::Rcc, stm32f429zi::rcc::Rcc::new());
332    let clocks = static_init!(
333        stm32f429zi::clocks::Clocks<Stm32f429Specs>,
334        stm32f429zi::clocks::Clocks::new(rcc)
335    );
336
337    let syscfg = static_init!(
338        stm32f429zi::syscfg::Syscfg,
339        stm32f429zi::syscfg::Syscfg::new(clocks)
340    );
341    let exti = static_init!(
342        stm32f429zi::exti::Exti,
343        stm32f429zi::exti::Exti::new(syscfg)
344    );
345    let dma1 = static_init!(stm32f429zi::dma::Dma1, stm32f429zi::dma::Dma1::new(clocks));
346    let dma2 = static_init!(stm32f429zi::dma::Dma2, stm32f429zi::dma::Dma2::new(clocks));
347
348    let peripherals = static_init!(
349        Stm32f429ziDefaultPeripherals,
350        Stm32f429ziDefaultPeripherals::new(clocks, exti, dma1, dma2)
351    );
352    peripherals.init();
353    let base_peripherals = &peripherals.stm32f4;
354
355    setup_peripherals(
356        &base_peripherals.tim2,
357        &peripherals.trng,
358        &peripherals.can1,
359        &peripherals.rtc,
360    );
361
362    set_pin_primary_functions(syscfg, &base_peripherals.gpio_ports);
363
364    setup_dma(
365        dma1,
366        &base_peripherals.dma1_streams,
367        &base_peripherals.usart3,
368    );
369
370    // Create an array to hold process references.
371    let processes = components::process_array::ProcessArrayComponent::new()
372        .finalize(components::process_array_component_static!(NUM_PROCS));
373    PANIC_RESOURCES.get().map(|resources| {
374        resources.processes.put(processes.as_slice());
375    });
376
377    // Setup space to store the core kernel data structure.
378    let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
379
380    let chip = static_init!(
381        stm32f429zi::chip::Stm32f4xx<Stm32f429ziDefaultPeripherals>,
382        stm32f429zi::chip::Stm32f4xx::new(peripherals)
383    );
384    PANIC_RESOURCES.get().map(|resources| {
385        resources.chip.put(chip);
386    });
387
388    // UART
389
390    // Create a shared UART channel for kernel debug.
391    base_peripherals.usart3.enable_clock();
392    let uart_mux = components::console::UartMuxComponent::new(&base_peripherals.usart3, 115200)
393        .finalize(components::uart_mux_component_static!());
394
395    (*addr_of_mut!(io::WRITER)).set_initialized();
396
397    // Create capabilities that the board needs to call certain protected kernel
398    // functions.
399    let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
400    let process_management_capability =
401        create_capability!(capabilities::ProcessManagementCapability);
402
403    // Setup the console.
404    let console = components::console::ConsoleComponent::new(
405        board_kernel,
406        capsules_core::console::DRIVER_NUM,
407        uart_mux,
408    )
409    .finalize(components::console_component_static!());
410    // Create the debugger object that handles calls to `debug!()`.
411    components::debug_writer::DebugWriterComponent::new::<
412        <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
413    >(
414        uart_mux,
415        create_capability!(capabilities::SetDebugWriterCapability),
416    )
417    .finalize(components::debug_writer_component_static!());
418
419    // LEDs
420
421    // Clock to Port A is enabled in `set_pin_primary_functions()`
422    let gpio_ports = &base_peripherals.gpio_ports;
423
424    let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
425        LedHigh<'static, stm32f429zi::gpio::Pin>,
426        LedHigh::new(gpio_ports.get_pin(stm32f429zi::gpio::PinId::PB00).unwrap()),
427        LedHigh::new(gpio_ports.get_pin(stm32f429zi::gpio::PinId::PB07).unwrap()),
428        LedHigh::new(gpio_ports.get_pin(stm32f429zi::gpio::PinId::PB14).unwrap()),
429    ));
430
431    // BUTTONs
432    let button = components::button::ButtonComponent::new(
433        board_kernel,
434        capsules_core::button::DRIVER_NUM,
435        components::button_component_helper!(
436            stm32f429zi::gpio::Pin,
437            (
438                gpio_ports.get_pin(stm32f429zi::gpio::PinId::PC13).unwrap(),
439                kernel::hil::gpio::ActivationMode::ActiveHigh,
440                kernel::hil::gpio::FloatingState::PullNone
441            )
442        ),
443    )
444    .finalize(components::button_component_static!(stm32f429zi::gpio::Pin));
445
446    // ALARM
447
448    let tim2 = &base_peripherals.tim2;
449    let mux_alarm = components::alarm::AlarmMuxComponent::new(tim2).finalize(
450        components::alarm_mux_component_static!(stm32f429zi::tim2::Tim2),
451    );
452
453    let alarm = components::alarm::AlarmDriverComponent::new(
454        board_kernel,
455        capsules_core::alarm::DRIVER_NUM,
456        mux_alarm,
457    )
458    .finalize(components::alarm_component_static!(stm32f429zi::tim2::Tim2));
459
460    // GPIO
461    let gpio = GpioComponent::new(
462        board_kernel,
463        capsules_core::gpio::DRIVER_NUM,
464        components::gpio_component_helper!(
465            stm32f429zi::gpio::Pin,
466            // Arduino like RX/TX
467            0 => gpio_ports.get_pin(PinId::PG09).unwrap(), //D0
468            1 => gpio_ports.pins[6][14].as_ref().unwrap(), //D1
469            2 => gpio_ports.pins[5][15].as_ref().unwrap(), //D2
470            3 => gpio_ports.pins[4][13].as_ref().unwrap(), //D3
471            4 => gpio_ports.pins[5][14].as_ref().unwrap(), //D4
472            5 => gpio_ports.pins[4][11].as_ref().unwrap(), //D5
473            6 => gpio_ports.pins[4][9].as_ref().unwrap(), //D6
474            7 => gpio_ports.pins[5][13].as_ref().unwrap(), //D7
475            8 => gpio_ports.pins[5][12].as_ref().unwrap(), //D8
476            9 => gpio_ports.pins[3][15].as_ref().unwrap(), //D9
477            // SPI Pins
478            10 => gpio_ports.pins[3][14].as_ref().unwrap(), //D10
479            11 => gpio_ports.pins[0][7].as_ref().unwrap(),  //D11
480            12 => gpio_ports.pins[0][6].as_ref().unwrap(),  //D12
481            13 => gpio_ports.pins[0][5].as_ref().unwrap(),  //D13
482            // I2C Pins
483            14 => gpio_ports.pins[1][9].as_ref().unwrap(), //D14
484            15 => gpio_ports.pins[1][8].as_ref().unwrap(), //D15
485            16 => gpio_ports.pins[2][6].as_ref().unwrap(), //D16
486            17 => gpio_ports.pins[1][15].as_ref().unwrap(), //D17
487            18 => gpio_ports.pins[1][13].as_ref().unwrap(), //D18
488            19 => gpio_ports.pins[1][12].as_ref().unwrap(), //D19
489            20 => gpio_ports.pins[0][15].as_ref().unwrap(), //D20
490            21 => gpio_ports.pins[2][7].as_ref().unwrap(), //D21
491            // SPI B Pins
492            // 22 => gpio_ports.pins[1][5].as_ref().unwrap(), //D22
493            // 23 => gpio_ports.pins[1][3].as_ref().unwrap(), //D23
494            // 24 => gpio_ports.pins[0][4].as_ref().unwrap(), //D24
495            // 24 => gpio_ports.pins[1][4].as_ref().unwrap(), //D25
496            // QSPI
497            26 => gpio_ports.pins[1][6].as_ref().unwrap(), //D26
498            27 => gpio_ports.pins[1][2].as_ref().unwrap(), //D27
499            28 => gpio_ports.pins[3][13].as_ref().unwrap(), //D28
500            29 => gpio_ports.pins[3][12].as_ref().unwrap(), //D29
501            30 => gpio_ports.pins[3][11].as_ref().unwrap(), //D30
502            31 => gpio_ports.pins[4][2].as_ref().unwrap(), //D31
503            // Timer Pins
504            32 => gpio_ports.pins[0][0].as_ref().unwrap(), //D32
505            33 => gpio_ports.pins[1][0].as_ref().unwrap(), //D33
506            34 => gpio_ports.pins[4][0].as_ref().unwrap(), //D34
507            35 => gpio_ports.pins[1][11].as_ref().unwrap(), //D35
508            36 => gpio_ports.pins[1][10].as_ref().unwrap(), //D36
509            37 => gpio_ports.pins[4][15].as_ref().unwrap(), //D37
510            38 => gpio_ports.pins[4][14].as_ref().unwrap(), //D38
511            39 => gpio_ports.pins[4][12].as_ref().unwrap(), //D39
512            40 => gpio_ports.pins[4][10].as_ref().unwrap(), //D40
513            41 => gpio_ports.pins[4][7].as_ref().unwrap(), //D41
514            42 => gpio_ports.pins[4][8].as_ref().unwrap(), //D42
515            // SDMMC
516            43 => gpio_ports.pins[2][8].as_ref().unwrap(), //D43
517            44 => gpio_ports.pins[2][9].as_ref().unwrap(), //D44
518            45 => gpio_ports.pins[2][10].as_ref().unwrap(), //D45
519            46 => gpio_ports.pins[2][11].as_ref().unwrap(), //D46
520            47 => gpio_ports.pins[2][12].as_ref().unwrap(), //D47
521            48 => gpio_ports.pins[3][2].as_ref().unwrap(), //D48
522            49 => gpio_ports.pins[6][2].as_ref().unwrap(), //D49
523            50 => gpio_ports.pins[6][3].as_ref().unwrap(), //D50
524            // USART
525            51 => gpio_ports.pins[3][7].as_ref().unwrap(), //D51
526            52 => gpio_ports.pins[3][6].as_ref().unwrap(), //D52
527            53 => gpio_ports.pins[3][5].as_ref().unwrap(), //D53
528            54 => gpio_ports.pins[3][4].as_ref().unwrap(), //D54
529            55 => gpio_ports.pins[3][3].as_ref().unwrap(), //D55
530            56 => gpio_ports.pins[4][2].as_ref().unwrap(), //D56
531            57 => gpio_ports.pins[4][4].as_ref().unwrap(), //D57
532            58 => gpio_ports.pins[4][5].as_ref().unwrap(), //D58
533            59 => gpio_ports.pins[4][6].as_ref().unwrap(), //D59
534            60 => gpio_ports.pins[4][3].as_ref().unwrap(), //D60
535            61 => gpio_ports.pins[5][8].as_ref().unwrap(), //D61
536            62 => gpio_ports.pins[5][7].as_ref().unwrap(), //D62
537            63 => gpio_ports.pins[5][9].as_ref().unwrap(), //D63
538            64 => gpio_ports.pins[6][1].as_ref().unwrap(), //D64
539            65 => gpio_ports.pins[6][0].as_ref().unwrap(), //D65
540            66 => gpio_ports.pins[3][1].as_ref().unwrap(), //D66
541            67 => gpio_ports.pins[3][0].as_ref().unwrap(), //D67
542            68 => gpio_ports.pins[5][0].as_ref().unwrap(), //D68
543            69 => gpio_ports.pins[5][1].as_ref().unwrap(), //D69
544            70 => gpio_ports.pins[5][2].as_ref().unwrap(), //D70
545            71 => gpio_ports.pins[0][7].as_ref().unwrap(),  //D71
546
547            // ADC Pins
548            // Enable the to use the ADC pins as GPIO
549            // 72 => gpio_ports.pins[0][3].as_ref().unwrap(), //A0
550            // 73 => gpio_ports.pins[2][0].as_ref().unwrap(), //A1
551            // 74 => gpio_ports.pins[2][3].as_ref().unwrap(), //A2
552            75 => gpio_ports.pins[5][3].as_ref().unwrap(), //A3
553            76 => gpio_ports.pins[5][5].as_ref().unwrap(), //A4
554            77 => gpio_ports.pins[5][10].as_ref().unwrap(), //A5
555            // 78 => gpio_ports.pins[1][1].as_ref().unwrap(), //A6
556            // 79 => gpio_ports.pins[2][2].as_ref().unwrap(), //A7
557            80 => gpio_ports.pins[5][4].as_ref().unwrap()  //A8
558        ),
559    )
560    .finalize(components::gpio_component_static!(stm32f429zi::gpio::Pin));
561
562    // ADC
563    let adc_mux = components::adc::AdcMuxComponent::new(&base_peripherals.adc1)
564        .finalize(components::adc_mux_component_static!(stm32f429zi::adc::Adc));
565
566    let temp_sensor = components::temperature_stm::TemperatureSTMComponent::new(
567        adc_mux,
568        stm32f429zi::adc::Channel::Channel18,
569        2.5,
570        0.76,
571    )
572    .finalize(components::temperature_stm_adc_component_static!(
573        stm32f429zi::adc::Adc
574    ));
575
576    let temp = components::temperature::TemperatureComponent::new(
577        board_kernel,
578        capsules_extra::temperature::DRIVER_NUM,
579        temp_sensor,
580    )
581    .finalize(components::temperature_component_static!(
582        TemperatureSTMSensor
583    ));
584
585    let adc_channel_0 =
586        components::adc::AdcComponent::new(adc_mux, stm32f429zi::adc::Channel::Channel3)
587            .finalize(components::adc_component_static!(stm32f429zi::adc::Adc));
588
589    let adc_channel_1 =
590        components::adc::AdcComponent::new(adc_mux, stm32f429zi::adc::Channel::Channel10)
591            .finalize(components::adc_component_static!(stm32f429zi::adc::Adc));
592
593    let adc_channel_2 =
594        components::adc::AdcComponent::new(adc_mux, stm32f429zi::adc::Channel::Channel13)
595            .finalize(components::adc_component_static!(stm32f429zi::adc::Adc));
596
597    let adc_channel_3 =
598        components::adc::AdcComponent::new(adc_mux, stm32f429zi::adc::Channel::Channel9)
599            .finalize(components::adc_component_static!(stm32f429zi::adc::Adc));
600
601    let adc_channel_4 =
602        components::adc::AdcComponent::new(adc_mux, stm32f429zi::adc::Channel::Channel12)
603            .finalize(components::adc_component_static!(stm32f429zi::adc::Adc));
604
605    let adc_syscall =
606        components::adc::AdcVirtualComponent::new(board_kernel, capsules_core::adc::DRIVER_NUM)
607            .finalize(components::adc_syscall_component_helper!(
608                adc_channel_0,
609                adc_channel_1,
610                adc_channel_2,
611                adc_channel_3,
612                adc_channel_4,
613            ));
614
615    let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
616        .finalize(components::process_printer_text_component_static!());
617    PANIC_RESOURCES.get().map(|resources| {
618        resources.printer.put(process_printer);
619    });
620
621    // DAC
622    let dac = components::dac::DacComponent::new(&base_peripherals.dac)
623        .finalize(components::dac_component_static!());
624
625    // RNG
626    let rng = components::rng::RngComponent::new(
627        board_kernel,
628        capsules_core::rng::DRIVER_NUM,
629        &peripherals.trng,
630    )
631    .finalize(components::rng_component_static!(stm32f429zi::trng::Trng));
632
633    // CAN
634    let can = components::can::CanComponent::new(
635        board_kernel,
636        capsules_extra::can::DRIVER_NUM,
637        &peripherals.can1,
638    )
639    .finalize(components::can_component_static!(
640        stm32f429zi::can::Can<'static>
641    ));
642
643    // RTC DATE TIME
644    if let Err(e) = peripherals.rtc.rtc_init() {
645        debug!("{:?}", e)
646    }
647
648    let date_time = components::date_time::DateTimeComponent::new(
649        board_kernel,
650        capsules_extra::date_time::DRIVER_NUM,
651        &peripherals.rtc,
652    )
653    .finalize(components::date_time_component_static!(
654        stm32f429zi::rtc::Rtc<'static>
655    ));
656
657    // PROCESS CONSOLE
658    let process_console = components::process_console::ProcessConsoleComponent::new(
659        board_kernel,
660        uart_mux,
661        mux_alarm,
662        process_printer,
663        Some(cortexm4::support::reset),
664    )
665    .finalize(components::process_console_component_static!(
666        stm32f429zi::tim2::Tim2
667    ));
668    let _ = process_console.start();
669
670    let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
671        .finalize(components::round_robin_component_static!(NUM_PROCS));
672
673    let nucleo_f429zi = NucleoF429ZI {
674        console,
675        ipc: kernel::ipc::IPC::new(
676            board_kernel,
677            kernel::ipc::DRIVER_NUM,
678            &memory_allocation_capability,
679        ),
680        adc: adc_syscall,
681        dac,
682        led,
683        temperature: temp,
684        button,
685        alarm,
686        gpio,
687        rng,
688
689        scheduler,
690        systick: cortexm4::systick::SysTick::new_with_calibration(
691            (HSI_FREQUENCY_MHZ * 1_000_000) as u32,
692        ),
693        can,
694        date_time,
695    };
696
697    // // Optional kernel tests
698    // //
699    // // See comment in `boards/imix/src/main.rs`
700    // virtual_uart_rx_test::run_virtual_uart_receive(mux_uart);
701
702    debug!("Initialization complete. Entering main loop");
703
704    // These symbols are defined in the linker script.
705    extern "C" {
706        /// Beginning of the ROM region containing app images.
707        static _sapps: u8;
708        /// End of the ROM region containing app images.
709        static _eapps: u8;
710        /// Beginning of the RAM region for app memory.
711        static mut _sappmem: u8;
712        /// End of the RAM region for app memory.
713        static _eappmem: u8;
714    }
715
716    kernel::process::load_processes(
717        board_kernel,
718        chip,
719        core::slice::from_raw_parts(
720            core::ptr::addr_of!(_sapps),
721            core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
722        ),
723        core::slice::from_raw_parts_mut(
724            core::ptr::addr_of_mut!(_sappmem),
725            core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
726        ),
727        &FAULT_RESPONSE,
728        &process_management_capability,
729    )
730    .unwrap_or_else(|err| {
731        debug!("Error loading processes!");
732        debug!("{:?}", err);
733    });
734
735    //Uncomment to run multi alarm test
736    /*components::test::multi_alarm_test::MultiAlarmTestComponent::new(mux_alarm)
737    .finalize(components::multi_alarm_test_component_buf!(stm32f429zi::tim2::Tim2))
738    .run();*/
739
740    (board_kernel, nucleo_f429zi, chip)
741}
742
743/// Main function called after RAM initialized.
744#[no_mangle]
745pub unsafe fn main() {
746    let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
747
748    let (board_kernel, platform, chip) = start();
749    board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
750}