Skip to content

Commit

Permalink
Add overflow management support
Browse files Browse the repository at this point in the history
  • Loading branch information
aMarcireau committed Apr 22, 2024
1 parent a8af09f commit bec6d26
Show file tree
Hide file tree
Showing 29 changed files with 875 additions and 665 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,17 @@ The fields of the USB configuration are identical across devices, but the defaul
```py
@dataclasses.dataclass
class UsbConfiguration:
buffer_size: serde.type.uint64 = 131072 # size of each buffer in the ring, in bytes
ring_size: serde.type.uint64 = 4096 # number of buffers in the ring, the total size is ring_size * buffer_size
transfer_queue_size: serde.type.uint64 = 32 # number of libusb transfers submitted in parallel
buffer_length: serde.type.uint64 = 131072 # size of each buffer in the ring, in bytes
ring_length: serde.type.uint64 = 4096 # number of buffers in the ring, the total size is ring_length * buffer_length
transfer_queue_length: serde.type.uint64 = 32 # number of libusb transfers submitted in parallel
allow_dma: bool = False # whether to enable Direct Memory Access
```

## More examples

See _python/examples_ for different usage examples.

_python/examples/any_display.py_ implements a live event viewer with exponential decays caculated by the GPU. It requires vispy and glfw (`pip install vispy glfw`).
_python/examples/any_display.py_ implements a live event viewer with exponential decays caculated by the GPU. It requires vispy and glfw (`pip install vispy glfw pyopengl`).

_python/examples/evk4_plot_hot_pixels_ generates plots and require Plotly (`pip install plotly pandas kaleido`).

Expand Down
9 changes: 5 additions & 4 deletions drivers/src/device.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error;
use crate::flag;
use crate::usb;
use rusb::UsbContext;

Expand Down Expand Up @@ -31,15 +31,16 @@ pub trait Usb: Sized {

fn update_configuration(&self, configuration: Self::Configuration);

fn open<IntoError>(
fn open<IntoError, IntoWarning>(
serial: &Option<&str>,
configuration: Self::Configuration,
usb_configuration: &usb::Configuration,
event_loop: std::sync::Arc<usb::EventLoop>,
error_flag: error::Flag<IntoError>,
flag: flag::Flag<IntoError, IntoWarning>,
) -> Result<Self, Self::Error>
where
IntoError: From<Self::Error> + Clone + Send + 'static;
IntoError: From<Self::Error> + Clone + Send + 'static,
IntoWarning: From<crate::usb::Overflow> + Clone + Send + 'static;

fn next_with_timeout(&self, timeout: &std::time::Duration) -> Option<usb::BufferView>;

Expand Down
8 changes: 4 additions & 4 deletions drivers/src/devices.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::adapters;
use crate::device::TemperatureCelsius;
use crate::device::Usb;
use crate::error;
use crate::flag;
use crate::usb;
use rusb::UsbContext;

Expand Down Expand Up @@ -115,7 +115,7 @@ macro_rules! register {
configuration: Option<Configuration>,
usb_configuration: Option<usb::Configuration>,
event_loop: std::sync::Arc<usb::EventLoop>,
error_flag: error::Flag<Error>,
flag: flag::Flag<Error, usb::Overflow>,
) -> Result<Device, Error>
{
match configuration {
Expand All @@ -130,7 +130,7 @@ macro_rules! register {
.as_ref()
.unwrap_or(&$module::Device::DEFAULT_USB_CONFIGURATION),
event_loop.clone(),
error_flag.clone(),
flag.clone(),
)
.map(|device| paste::paste! {Device::[<$module:camel>](device)})
.map_err(|error| Error::from(error).unpack())?
Expand All @@ -147,7 +147,7 @@ macro_rules! register {
.as_ref()
.unwrap_or(&$module::Device::DEFAULT_USB_CONFIGURATION),
event_loop.clone(),
error_flag.clone(),
flag.clone(),
) {
Ok(device) => return Ok(Device::[<$module:camel>](device)),
Err(error) => match Error::from(error).unpack() {
Expand Down
49 changes: 37 additions & 12 deletions drivers/src/devices/prophesee_evk3_hd.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::adapters;
use crate::configuration;
use crate::device;
use crate::error;
use crate::flag;
use crate::properties;
use crate::usb;

use device::Usb;

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct Biases {
pub pr: u8,
Expand Down Expand Up @@ -64,6 +66,23 @@ pub struct Device {
serial: String,
}

pub const PROPERTIES: properties::Camera<Configuration> = Device::PROPERTIES;
pub const DEFAULT_CONFIGURATION: Configuration = Device::PROPERTIES.default_configuration;
pub const DEFAULT_USB_CONFIGURATION: usb::Configuration = Device::DEFAULT_USB_CONFIGURATION;
pub fn open<IntoError, IntoWarning>(
serial: &Option<&str>,
configuration: Configuration,
usb_configuration: &usb::Configuration,
event_loop: std::sync::Arc<usb::EventLoop>,
flag: flag::Flag<IntoError, IntoWarning>,
) -> Result<Device, Error>
where
IntoError: From<Error> + Clone + Send + 'static,
IntoWarning: From<usb::Overflow> + Clone + Send + 'static,
{
Device::open(serial, configuration, usb_configuration, event_loop, flag)
}

impl device::Usb for Device {
type Adapter = adapters::evt3::Adapter;

Expand Down Expand Up @@ -102,9 +121,9 @@ impl device::Usb for Device {
};

const DEFAULT_USB_CONFIGURATION: usb::Configuration = usb::Configuration {
buffer_size: 1 << 17,
ring_size: 1 << 12,
transfer_queue_size: 1 << 5,
buffer_length: 1 << 17,
ring_length: 1 << 12,
transfer_queue_length: 1 << 5,
allow_dma: false,
};

Expand All @@ -127,15 +146,16 @@ impl device::Usb for Device {
self.configuration_updater.update(configuration);
}

fn open<IntoError>(
fn open<IntoError, IntoWarning>(
serial: &Option<&str>,
configuration: Self::Configuration,
usb_configuration: &usb::Configuration,
event_loop: std::sync::Arc<usb::EventLoop>,
error_flag: error::Flag<IntoError>,
flag: flag::Flag<IntoError, IntoWarning>,
) -> Result<Self, Self::Error>
where
IntoError: From<Self::Error> + Clone + Send + 'static,
IntoWarning: From<crate::usb::Overflow> + Clone + Send + 'static,
{
let (handle, serial) = Self::handle_from_serial(event_loop.context(), serial)?;
std::thread::sleep(std::time::Duration::from_millis(150));
Expand Down Expand Up @@ -337,14 +357,18 @@ impl device::Usb for Device {
.write(&handle)?;

let handle = std::sync::Arc::new(handle);
let ring_error_flag = error_flag.clone();
let error_flag = flag.clone();
let warning_flag = flag.clone();
Ok(Device {
handle: handle.clone(),
ring: usb::Ring::new(
handle.clone(),
usb_configuration,
move |usb_error| {
ring_error_flag.store_if_not_set(Self::Error::from(usb_error));
error_flag.store_error_if_not_set(Self::Error::from(usb_error));
},
move |overflow| {
warning_flag.store_warning_if_not_set(overflow);
},
event_loop,
usb::TransferType::Bulk {
Expand All @@ -354,14 +378,14 @@ impl device::Usb for Device {
)?,
configuration_updater: configuration::Updater::new(
configuration,
ConfigurationUpdaterContext { handle, error_flag },
ConfigurationUpdaterContext { handle, flag },
|context, previous_configuration, configuration| {
if let Err(error) = update_configuration(
&context.handle,
Some(previous_configuration),
configuration,
) {
context.error_flag.store_if_not_set(error);
context.flag.store_error_if_not_set(error);
}
context
},
Expand Down Expand Up @@ -763,12 +787,13 @@ fn update_configuration(
Ok(())
}

struct ConfigurationUpdaterContext<IntoError>
struct ConfigurationUpdaterContext<IntoError, IntoWarning>
where
IntoError: From<Error> + Clone + Send,
IntoWarning: From<crate::usb::Overflow> + Clone + Send,
{
handle: std::sync::Arc<rusb::DeviceHandle<rusb::Context>>,
error_flag: error::Flag<IntoError>,
flag: flag::Flag<IntoError, IntoWarning>,
}

struct RuntimeRegister {
Expand Down
51 changes: 38 additions & 13 deletions drivers/src/devices/prophesee_evk4.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::adapters;
use crate::configuration;
use crate::device;
use crate::error;
use crate::flag;
use crate::properties;
use crate::usb;

use device::Usb;

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
pub struct Biases {
pub pr: u8,
Expand Down Expand Up @@ -87,6 +89,23 @@ impl From<rusb::Error> for Error {
}
}

pub const PROPERTIES: properties::Camera<Configuration> = Device::PROPERTIES;
pub const DEFAULT_CONFIGURATION: Configuration = Device::PROPERTIES.default_configuration;
pub const DEFAULT_USB_CONFIGURATION: usb::Configuration = Device::DEFAULT_USB_CONFIGURATION;
pub fn open<IntoError, IntoWarning>(
serial: &Option<&str>,
configuration: Configuration,
usb_configuration: &usb::Configuration,
event_loop: std::sync::Arc<usb::EventLoop>,
flag: flag::Flag<IntoError, IntoWarning>,
) -> Result<Device, Error>
where
IntoError: From<Error> + Clone + Send + 'static,
IntoWarning: From<usb::Overflow> + Clone + Send + 'static,
{
Device::open(serial, configuration, usb_configuration, event_loop, flag)
}

impl device::Usb for Device {
type Adapter = adapters::evt3::Adapter;

Expand Down Expand Up @@ -132,9 +151,9 @@ impl device::Usb for Device {
};

const DEFAULT_USB_CONFIGURATION: usb::Configuration = usb::Configuration {
buffer_size: 1 << 17,
ring_size: 1 << 12,
transfer_queue_size: 1 << 5,
buffer_length: 1 << 17,
ring_length: 1 << 12,
transfer_queue_length: 1 << 5,
allow_dma: false,
};

Expand All @@ -157,15 +176,16 @@ impl device::Usb for Device {
self.configuration_updater.update(configuration);
}

fn open<IntoError>(
fn open<IntoError, IntoWarning>(
serial: &Option<&str>,
configuration: Self::Configuration,
usb_configuration: &usb::Configuration,
event_loop: std::sync::Arc<usb::EventLoop>,
error_flag: error::Flag<IntoError>,
flag: flag::Flag<IntoError, IntoWarning>,
) -> Result<Self, Self::Error>
where
IntoError: From<Self::Error> + Clone + Send + 'static,
IntoWarning: From<usb::Overflow> + Clone + Send + 'static,
{
let (handle, serial) = Self::handle_from_serial(event_loop.context(), serial)?;
usb::assert_control_transfer(
Expand Down Expand Up @@ -688,7 +708,7 @@ impl device::Usb for Device {
}
.write(&handle)?;
loop {
let mut buffer = vec![0u8; Self::DEFAULT_USB_CONFIGURATION.buffer_size];
let mut buffer = vec![0u8; Self::DEFAULT_USB_CONFIGURATION.buffer_length];
match handle.read_bulk(0x81, &mut buffer, TIMEOUT) {
Ok(size) => {
if size == 0 {
Expand Down Expand Up @@ -786,15 +806,19 @@ impl device::Usb for Device {
// }

let handle = std::sync::Arc::new(handle);
let ring_error_flag = error_flag.clone();
let error_flag = flag.clone();
let warning_flag = flag.clone();
let register_mutex = std::sync::Arc::new(std::sync::Mutex::new(()));
Ok(Device {
handle: handle.clone(),
ring: usb::Ring::new(
handle.clone(),
usb_configuration,
move |usb_error| {
ring_error_flag.store_if_not_set(Self::Error::from(usb_error));
error_flag.store_error_if_not_set(Self::Error::from(usb_error));
},
move |overflow| {
warning_flag.store_warning_if_not_set(overflow);
},
event_loop,
usb::TransferType::Bulk {
Expand All @@ -806,7 +830,7 @@ impl device::Usb for Device {
configuration,
ConfigurationUpdaterContext {
handle,
error_flag,
flag,
register_mutex: register_mutex.clone(),
},
|context, previous_configuration, configuration| {
Expand All @@ -822,7 +846,7 @@ impl device::Usb for Device {
)
};
if let Err(error) = result {
context.error_flag.store_if_not_set(error);
context.flag.store_error_if_not_set(error);
}
context
},
Expand Down Expand Up @@ -1231,12 +1255,13 @@ fn update_configuration(
Ok(())
}

struct ConfigurationUpdaterContext<IntoError>
struct ConfigurationUpdaterContext<IntoError, IntoWarning>
where
IntoError: From<Error> + Clone + Send,
IntoWarning: From<usb::Overflow> + Clone + Send,
{
handle: std::sync::Arc<rusb::DeviceHandle<rusb::Context>>,
error_flag: error::Flag<IntoError>,
flag: flag::Flag<IntoError, IntoWarning>,
register_mutex: std::sync::Arc<std::sync::Mutex<()>>,
}

Expand Down
35 changes: 0 additions & 35 deletions drivers/src/error.rs

This file was deleted.

Loading

0 comments on commit bec6d26

Please sign in to comment.