nucleo_f446re/
io.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
5use core::fmt::Write;
6use core::panic::PanicInfo;
7use core::ptr::{addr_of, addr_of_mut};
8
9use kernel::debug;
10use kernel::debug::IoWrite;
11use kernel::hil::led;
12use kernel::hil::uart;
13use kernel::hil::uart::Configure;
14
15use stm32f446re::chip_specs::Stm32f446Specs;
16use stm32f446re::gpio::PinId;
17
18use crate::CHIP;
19use crate::PROCESSES;
20use crate::PROCESS_PRINTER;
21
22/// Writer is used by kernel::debug to panic message to the serial port.
23pub struct Writer {
24    initialized: bool,
25}
26
27/// Global static for debug writer
28pub static mut WRITER: Writer = Writer { initialized: false };
29
30impl Writer {
31    /// Indicate that USART has already been initialized. Trying to double
32    /// initialize USART2 causes STM32F446RE to go into in in-deterministic state.
33    pub fn set_initialized(&mut self) {
34        self.initialized = true;
35    }
36}
37
38impl Write for Writer {
39    fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
40        self.write(s.as_bytes());
41        Ok(())
42    }
43}
44
45impl IoWrite for Writer {
46    fn write(&mut self, buf: &[u8]) -> usize {
47        let rcc = stm32f446re::rcc::Rcc::new();
48        let clocks: stm32f446re::clocks::Clocks<Stm32f446Specs> =
49            stm32f446re::clocks::Clocks::new(&rcc);
50        let uart = stm32f446re::usart::Usart::new_usart2(&clocks);
51
52        if !self.initialized {
53            self.initialized = true;
54
55            let _ = uart.configure(uart::Parameters {
56                baud_rate: 115200,
57                stop_bits: uart::StopBits::One,
58                parity: uart::Parity::None,
59                hw_flow_control: false,
60                width: uart::Width::Eight,
61            });
62        }
63
64        for &c in buf {
65            uart.send_byte(c);
66        }
67        buf.len()
68    }
69}
70
71/// Panic handler.
72#[no_mangle]
73#[panic_handler]
74pub unsafe fn panic_fmt(info: &PanicInfo) -> ! {
75    // User LD2 is connected to PA05
76    // Have to reinitialize several peripherals because otherwise can't access them here.
77    let rcc = stm32f446re::rcc::Rcc::new();
78    let clocks: stm32f446re::clocks::Clocks<Stm32f446Specs> =
79        stm32f446re::clocks::Clocks::new(&rcc);
80    let syscfg = stm32f446re::syscfg::Syscfg::new(&clocks);
81    let exti = stm32f446re::exti::Exti::new(&syscfg);
82    let pin = stm32f446re::gpio::Pin::new(PinId::PA05, &exti);
83    let gpio_ports = stm32f446re::gpio::GpioPorts::new(&clocks, &exti);
84    pin.set_ports_ref(&gpio_ports);
85    let led = &mut led::LedHigh::new(&pin);
86
87    let writer = &mut *addr_of_mut!(WRITER);
88
89    debug::panic(
90        &mut [led],
91        writer,
92        info,
93        &cortexm4::support::nop,
94        &*addr_of!(PROCESSES),
95        &*addr_of!(CHIP),
96        &*addr_of!(PROCESS_PRINTER),
97    )
98}