opentitanlib/io/
console.rs

1// Copyright lowRISC contributors (OpenTitan project).
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
5use anyhow::Result;
6use serde::{Deserialize, Serialize};
7use std::rc::Rc;
8use thiserror::Error;
9
10use crate::impl_serializable_error;
11use crate::io::gpio::GpioPin;
12
13/// Errors related to the console interface.
14#[derive(Error, Debug, Serialize, Deserialize)]
15pub enum ConsoleError {
16    #[error("Unsupported: {0}")]
17    UnsupportedError(String),
18    #[error("{0}")]
19    GenericError(String),
20}
21impl_serializable_error!(ConsoleError);
22
23pub trait ConsoleDevice {
24    /// Reads data from the UART to print to the console (used when this UART is the console device).
25    fn console_poll_read(
26        &self,
27        _cx: &mut std::task::Context<'_>,
28        _buf: &mut [u8],
29    ) -> std::task::Poll<Result<usize>> {
30        Err(ConsoleError::UnsupportedError(
31            "console_poll_read() not implemented.".into(),
32        ))?
33    }
34
35    /// Writes console input data to the UART (used when this UART is the console device).
36    fn console_write(&self, _buf: &[u8]) -> Result<()> {
37        Err(ConsoleError::UnsupportedError("console_write() not implemented.".into()).into())
38    }
39
40    fn set_break(&self, _enable: bool) -> Result<()> {
41        Err(ConsoleError::GenericError("break unsupported".into()).into())
42    }
43
44    /// Query if TX-ready pin non-polling mode is supported.
45    fn get_tx_ready_pin(&self) -> Result<Option<&Rc<dyn GpioPin>>> {
46        Ok(None)
47    }
48}