serial_driver/src/main.rs

106 lines
2.7 KiB
Rust
Raw Permalink Normal View History

2024-11-16 11:54:45 -06:00
use std::os::mikros::{ipc, syscalls, Errno, FileOpenMode};
2024-06-09 18:31:30 -05:00
use parking_lot::Mutex;
use uart::{
Baud, Data, DataBits, FifoSize, InterruptEnable, LineControl, LineStatus, ModemControl,
ParityMode, Uart, UartAddress,
};
2024-06-09 18:31:30 -05:00
struct DevServ;
impl dev_driver_rpc::Server for DevServ {
2024-11-16 11:54:45 -06:00
fn open(&self, _path: &std::path::Path, _mode: FileOpenMode) -> Result<u64, Errno> {
2024-06-09 18:31:30 -05:00
Ok(0)
}
}
struct UartWrp(Uart<Data>);
impl std::ops::DerefMut for UartWrp {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl std::ops::Deref for UartWrp {
type Target = Uart<Data>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
unsafe impl Send for UartWrp {}
unsafe impl Sync for UartWrp {}
struct FileServ(Mutex<UartWrp>);
impl file_rpc::Server for FileServ {
fn read(
&self,
_fd: u64,
_len: usize,
2024-10-04 12:36:26 -05:00
) -> std::result::Result<std::borrow::Cow<'_, [u8]>, Errno> {
Err(Errno::ENOSYS)
2024-06-09 18:31:30 -05:00
}
2024-10-04 12:36:26 -05:00
fn write(&self, _fd: u64, data: &[u8]) -> Result<(), Errno> {
let mut uart = self.0.lock();
for &byte in data {
while !uart.read_line_status().contains(LineStatus::TRANSMIT_EMPTY) {}
uart.write_data(byte);
}
2024-06-09 18:31:30 -05:00
Ok(())
}
2024-10-04 12:36:26 -05:00
fn close(&self, _fd: u64) -> Result<(), Errno> { Ok(()) }
2024-06-09 18:32:13 -05:00
2024-10-04 12:36:26 -05:00
fn size(&self, _fd: u64) -> Result<u64, Errno> {
Err(Errno::ENOSYS)
2024-06-09 18:32:13 -05:00
}
2024-09-17 11:40:28 -05:00
2024-10-04 12:36:26 -05:00
fn dup(&self, fd: u64) -> Result<u64, Errno> {
Ok(fd)
2024-09-17 11:40:28 -05:00
}
2024-09-30 13:33:10 -05:00
2024-10-04 12:36:26 -05:00
fn seek(&self, _fd: u64, _pos: file_rpc::SeekFrom) -> Result<u64, Errno> { Err(Errno::ESPIPE) }
2024-06-09 18:31:30 -05:00
}
fn main() {
let uart = unsafe { init_uart(uart::COM1) };
dev_driver_rpc::register_server(Box::new(DevServ));
file_rpc::register_server(Box::new(FileServ(Mutex::new(UartWrp(uart)))));
2024-06-09 18:31:30 -05:00
let devfs_pid;
loop {
if let Some(pid) = syscalls::try_get_registered(1) {
devfs_pid = pid;
break;
}
}
devfs_rpc::Client::new(devfs_pid)
.register_dev("ttyS0")
.unwrap();
2024-06-09 18:31:30 -05:00
loop {
ipc::process_messages()
}
}
/// Sets up the given UART for 115200 baud 8N1, no interrupts
unsafe fn init_uart(address: UartAddress) -> Uart<Data> {
let mut uart = unsafe { Uart::new(address) }.configure_mode();
uart.set_baud(Baud::B115200);
let mut uart = uart.data_mode();
let line_control = LineControl {
bits: DataBits::Eight,
parity: ParityMode::None,
extra_stop: false,
break_signal: false,
};
uart.write_line_control(line_control);
uart.enable_fifo(true, true, false, FifoSize::Fourteen);
// UART library misspelled modem here
uart.write_model_control(ModemControl::TERMINAL_READY | ModemControl::REQUEST_TO_SEND);
uart.write_interrupt_enable(InterruptEnable::empty());
uart
}