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