Skip to content

Commit

Permalink
phy: serial: Automatically configure low latency on Linux
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Rahix committed Dec 26, 2024
1 parent aff61a8 commit 2b7a5ce
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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/",
Expand Down
12 changes: 9 additions & 3 deletions src/phy/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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,
}
Expand Down

0 comments on commit 2b7a5ce

Please sign in to comment.