Serial driver is now independent of kernel

This commit is contained in:
pjht 2024-09-06 10:47:48 -05:00
parent c0934a85ca
commit e050cb41dc
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E
3 changed files with 137 additions and 11 deletions

70
Cargo.lock generated
View File

@ -17,6 +17,12 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
[[package]]
name = "bit_field"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61"
[[package]]
name = "bitflags"
version = "2.5.0"
@ -41,6 +47,26 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67ba02a97a2bd10f4b59b25c7973101c79642302776489e030cd13cdab09ed15"
[[package]]
name = "const_format"
version = "0.2.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3a214c7af3d04997541b18d432afaff4c455e79e2029079647e72fc2bd27673"
dependencies = [
"const_format_proc_macros",
]
[[package]]
name = "const_format_proc_macros"
version = "0.2.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7f6ff08fd20f4f299298a28e2dfa8a8ba1036e6cd2460ac1de7b425d76f2500"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "critical-section"
version = "1.1.2"
@ -149,11 +175,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a55c51ee6c0db07e68448e336cf8ea4131a620edefebf9893e759b2d793420f8"
dependencies = [
"cobs",
"const_format",
"embedded-io",
"heapless",
"postcard-derive",
"serde",
]
[[package]]
name = "postcard-derive"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc4b01218787dd4420daf63875163a787a78294ad48a24e9f6fa8c6507759a79"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "proc-macro2"
version = "1.0.85"
@ -219,7 +258,7 @@ checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.66",
]
[[package]]
@ -229,6 +268,8 @@ dependencies = [
"dev_driver_rpc",
"devfs_rpc",
"file_rpc",
"parking_lot",
"uart",
]
[[package]]
@ -252,6 +293,17 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.66"
@ -263,12 +315,28 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "uart"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf797cf07dd88b21255bcc3b1bb84606e9bae30e4f5b61e22e2565bdafa3e309"
dependencies = [
"bit_field",
"bitflags",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "unicode-xid"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
[[package]]
name = "windows-targets"
version = "0.52.5"

View File

@ -7,3 +7,5 @@ edition = "2021"
dev_driver_rpc = { version = "0.1.0", path = "../dev_driver_rpc" }
devfs_rpc = { version = "0.1.0", path = "../devfs/devfs_rpc" }
file_rpc = { version = "0.1.0", path = "../file_rpc" }
parking_lot = "0.12.3"
uart = "0.1.2"

View File

@ -1,25 +1,60 @@
use std::os::mikros::{ipc, syscalls};
struct Serv;
use parking_lot::Mutex;
use uart::{
Baud, Data, DataBits, FifoSize, InterruptEnable, LineControl, LineStatus, ModemControl,
ParityMode, Uart, UartAddress,
};
impl dev_driver_rpc::Server for Serv {
struct DevServ;
impl dev_driver_rpc::Server for DevServ {
fn open(&self, _path: &std::path::Path) -> Result<u64, ()> {
Ok(0)
}
}
impl file_rpc::Server for Serv {
fn read(&self, _fd: u64, _pos: u64, _len: usize) -> Result<Vec<u8>, ()> {
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,
_pos: u64,
_len: usize,
) -> std::result::Result<std::borrow::Cow<'_, [u8]>, ()> {
Err(())
}
fn write(&self, _fd: u64, _pos: u64, data: &[u8]) -> Result<(), ()> {
print!("{}", std::str::from_utf8(data).map_err(|_| ())?);
let mut uart = self.0.lock();
for &byte in data {
while !uart.read_line_status().contains(LineStatus::TRANSMIT_EMPTY) {}
uart.write_data(byte);
}
Ok(())
}
fn close(&self, _fd: u64) {
}
fn close(&self, _fd: u64) {}
fn size(&self, _fd: u64) -> Option<u64> {
None
@ -27,8 +62,9 @@ impl file_rpc::Server for Serv {
}
fn main() {
dev_driver_rpc::register_server(Box::new(Serv));
file_rpc::register_server(Box::new(Serv));
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)))));
let devfs_pid;
loop {
if let Some(pid) = syscalls::try_get_registered(1) {
@ -36,9 +72,29 @@ fn main() {
break;
}
}
devfs_rpc::Client::new(devfs_pid).register_dev("ttyS0").unwrap();
devfs_rpc::Client::new(devfs_pid)
.register_dev("ttyS0")
.unwrap();
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
}