From 2b7a5cedaf47d4aa05ec589de01aad0c3275b697 Mon Sep 17 00:00:00 2001 From: Rahix Date: Thu, 26 Dec 2024 10:29:11 +0100 Subject: [PATCH] phy: serial: Automatically configure low latency on Linux Use the `serialport_low_latency` [1] crate to automatically configure serial ports for low latency if possible. Unfortunately, the crate only supports Linux for the time being - on other platforms, low latency has to be configured manually by other means. [1]: https://github.com/michaellass/serialport_low_latency --- Cargo.toml | 5 ++++- src/phy/serial.rs | 12 +++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 31bc055..4ab3b27 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ repository = "https://github.com/rahix/profirust" [features] phy-linux = ["rs485", "libc", "std"] -phy-serial = ["serialport", "std"] +phy-serial = ["serialport", "std", "serialport_low_latency"] phy-simulator = ["std"] phy-rp2040 = ["rp2040-hal", "fugit", "embedded-hal", "nb", "cortex-m"] std = ["managed/std"] @@ -37,6 +37,9 @@ rp2040-hal = { version = "0.9.0", optional = true } rs485 = { version = "0.1.0", optional = true } serialport = { version = "4.6.0", optional = true } +[target.'cfg(target_os = "linux")'.dependencies] +serialport_low_latency = { version = "0.1.1", optional = true } + [workspace] members = [ "gsd-parser/", diff --git a/src/phy/serial.rs b/src/phy/serial.rs index 573a3f3..7774f05 100644 --- a/src/phy/serial.rs +++ b/src/phy/serial.rs @@ -111,12 +111,15 @@ impl SerialPortPhy { } fn new_inner(serial_port: Cow<'_, str>, baudrate: crate::Baudrate) -> Self { - let port = serialport::new(serial_port, u32::try_from(baudrate.to_rate()).unwrap()) + use serialport::SerialPort; + + #[allow(unused_mut)] + let mut port = serialport::new(serial_port, u32::try_from(baudrate.to_rate()).unwrap()) .data_bits(serialport::DataBits::Eight) .flow_control(serialport::FlowControl::None) .parity(serialport::Parity::Even) .stop_bits(serialport::StopBits::One) - .open() + .open_native() .unwrap(); assert_eq!( @@ -125,10 +128,13 @@ impl SerialPortPhy { "baudrate not configured correctly" ); + #[cfg(target_os = "linux")] + serialport_low_latency::enable_low_latency(&mut port).unwrap(); + let buffer = crate::phy::BufferHandle::from(vec![0u8; 512]); Self { - port, + port: Box::new(port), data: PhyData::Rx { buffer, length: 0 }, last_rx: None, }