Skip to main content

kernel/drivers/tty/
uart.rs

1// SPDX-License-Identifier: GPL-3.0-only
2//! Serial port abstraction: the [`_Serial`] trait and the [`SerialPort`] identifier enums.
3//!
4//! Authors: MarioS271
5
6#[cfg(target_arch = "x86_64")]
7pub use crate::arch::x86_64::drivers::tty::uart::Uart;
8
9/// Trait which defines standardized UART operations across different serial implementations
10pub trait UartOps {
11    /// Initialize the UART device
12    fn init(&mut self) -> UartResult<()>;
13
14    /// Read one byte from the UART device
15    fn read_byte(&mut self) -> UartResult<u8>;
16
17    /// Write one byte to the UART device
18    fn write_byte(&mut self, byte: u8) -> UartResult<()>;
19
20    /// Write a string to the serial device
21    /// Default implementation calls [`Self::write_byte`] for each byte in the string
22    fn write_string(&mut self, string: &str) -> UartResult<()> {
23        for byte in string.bytes() {
24            self.write_byte(byte)?;
25        }
26        Ok(())
27    }
28}
29
30pub type UartResult<T> = Result<T, UartError>;
31pub enum UartError {
32    AlreadyInitialized,
33    NotInitialized
34}
35impl UartError {
36    /// Return the name of `self` as a string slice
37    pub fn as_str(&self) -> &str {
38        use UartError::*;
39        match self {
40            AlreadyInitialized => "AlreadyInitialized",
41            NotInitialized => "NotInitialized"
42        }
43    }
44}