cy8cproto_62_4243_w/
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 OxidOS Automotive 2025 SRL.
4
5use core::panic::PanicInfo;
6use kernel::utilities::cells::OptionalCell;
7
8use psoc62xa::gpio::GpioPin;
9use psoc62xa::scb::Scb;
10
11use kernel::debug::{self, IoWrite};
12use kernel::hil::led::LedHigh;
13
14/// Writer is used by kernel::debug to panic message to the serial port.
15pub struct Writer {
16    scb: OptionalCell<&'static Scb<'static>>,
17}
18
19impl Writer {
20    pub fn set_scb(&self, scb: &'static Scb) {
21        self.scb.set(scb);
22    }
23}
24
25impl core::fmt::Write for Writer {
26    fn write_str(&mut self, s: &str) -> core::fmt::Result {
27        self.scb.map(|scb| scb.transmit_uart_sync(s.as_bytes()));
28        Ok(())
29    }
30}
31
32impl IoWrite for Writer {
33    fn write(&mut self, buf: &[u8]) -> usize {
34        self.scb.map(|scb| scb.transmit_uart_sync(buf));
35        buf.len()
36    }
37}
38
39pub static mut WRITER: Writer = Writer {
40    scb: OptionalCell::empty(),
41};
42
43/// Panic handler for the CY8CPROTO-062-4343 board.
44#[panic_handler]
45pub unsafe fn panic_fmt(panic_info: &PanicInfo) -> ! {
46    use core::ptr::addr_of_mut;
47    let writer = &mut *addr_of_mut!(WRITER);
48    let led_kernel_pin = &GpioPin::new(psoc62xa::gpio::PsocPin::P13_7);
49    let led = &mut LedHigh::new(led_kernel_pin);
50
51    debug::panic(
52        &mut [led],
53        writer,
54        panic_info,
55        &cortexm0p::support::nop,
56        crate::PANIC_RESOURCES.get(),
57    );
58}