From b56119d2d74db40adb2335ed65c984617b8f7a3b Mon Sep 17 00:00:00 2001 From: he2she Date: Wed, 7 Feb 2024 03:48:23 +0900 Subject: [PATCH] add eeprom implementation for 232R --- src/lib.rs | 84 ++++++++++++++++++++++++++++++++++++++++++++++++---- src/types.rs | 52 +++++++++++++++++++++++++++++++- 2 files changed, 130 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ce0344b..35c4f52 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -104,8 +104,8 @@ mod types; use types::{vid_pid_from_id, STRING_LEN}; pub use types::{ BitMode, BitsPerWord, ByteOrder, Cbus232h, Cbus232r, CbusX, ClockPolarity, DeviceInfo, - DeviceStatus, DeviceType, DriveCurrent, DriverType, Eeprom2232h, Eeprom232h, Eeprom4232h, - EepromHeader, EepromStrings, ModemStatus, Parity, Speed, StopBits, Version, + DeviceStatus, DeviceType, DriveCurrent, DriverType, Eeprom2232h, Eeprom232h, Eeprom232r, + Eeprom4232h, EepromHeader, EepromStrings, ModemStatus, Parity, Speed, StopBits, Version, }; mod util; @@ -125,9 +125,9 @@ use libftd2xx_ffi::{ FT_SetBreakOn, FT_SetChars, FT_SetDataCharacteristics, FT_SetDeadmanTimeout, FT_SetDtr, FT_SetFlowControl, FT_SetLatencyTimer, FT_SetRts, FT_SetTimeouts, FT_SetUSBParameters, FT_Write, FT_WriteEE, FT_DEVICE_LIST_INFO_NODE, FT_EEPROM_2232H, FT_EEPROM_232H, - FT_EEPROM_4232H, FT_FLOW_DTR_DSR, FT_FLOW_NONE, FT_FLOW_RTS_CTS, FT_FLOW_XON_XOFF, FT_HANDLE, - FT_LIST_NUMBER_ONLY, FT_OPEN_BY_DESCRIPTION, FT_OPEN_BY_SERIAL_NUMBER, FT_PURGE_RX, - FT_PURGE_TX, FT_STATUS, + FT_EEPROM_232R, FT_EEPROM_4232H, FT_FLOW_DTR_DSR, FT_FLOW_NONE, FT_FLOW_RTS_CTS, + FT_FLOW_XON_XOFF, FT_HANDLE, FT_LIST_NUMBER_ONLY, FT_OPEN_BY_DESCRIPTION, + FT_OPEN_BY_SERIAL_NUMBER, FT_PURGE_RX, FT_PURGE_TX, FT_STATUS, }; #[cfg(target_os = "windows")] @@ -520,6 +520,23 @@ pub struct Ft232h { ftdi: Ftdi, } +/// FT232R device. +/// +/// # Example +/// +/// Converting from an unknown FTDI device. +/// +/// ```no_run +/// use libftd2xx::{Ft232r, Ftdi}; +/// +/// let ft232r: Ft232r = Ftdi::new()?.try_into()?; +/// # Ok::<(), libftd2xx::DeviceTypeError>(()) +/// ``` +#[derive(Debug)] +pub struct Ft232r { + ftdi: Ftdi, +} + /// FT2232H device. /// /// # Example @@ -2096,6 +2113,59 @@ impl Ft232h { } } +impl Ft232r { + /// Open a `Ft232r` device and initialize the handle. + /// + /// # Safety + /// + /// This is **unchecked** meaning a device type check will not be performed. + /// Methods that require this specific device type may fail in unexpected + /// ways if the wrong device is used. + /// + /// # Example + /// + /// ```no_run + /// use libftd2xx::Ft232r; + /// + /// let mut ft = unsafe { Ft232r::with_serial_number_unchecked("AH06S0OE")? }; + /// # Ok::<(), libftd2xx::FtStatus>(()) + /// ``` + pub unsafe fn with_serial_number_unchecked(serial_number: &str) -> Result { + let handle = ft_open_ex(serial_number, FT_OPEN_BY_SERIAL_NUMBER)?; + Ok(Ft232r { + ftdi: Ftdi { handle }, + }) + } + + /// Open a `Ft232r` device and initialize the handle. + /// + /// # Example + /// + /// ```no_run + /// use libftd2xx::Ft232r; + /// + /// Ft232r::with_serial_number("AH06S0OE")?; + /// # Ok::<(), libftd2xx::DeviceTypeError>(()) + /// ``` + pub fn with_serial_number(serial_number: &str) -> Result { + Ftdi::with_serial_number(serial_number)?.try_into() + } + + /// Open a `Ft232r` device by its device description. + /// + /// # Example + /// + /// ```no_run + /// use libftd2xx::Ft232r; + /// + /// Ft232r::with_description("Hello")?; + /// # Ok::<(), libftd2xx::DeviceTypeError>(()) + /// ``` + pub fn with_description(description: &str) -> Result { + Ftdi::with_description(description)?.try_into() + } +} + impl Ft2232h { /// Open a `Ft2232h` device and initialize the handle. /// @@ -2278,18 +2348,22 @@ macro_rules! impl_try_from_for { } impl_boilerplate_for!(Ft232h, DeviceType::FT232H); +impl_boilerplate_for!(Ft232r, DeviceType::FT232R); impl_boilerplate_for!(Ft2232h, DeviceType::FT2232H); impl_boilerplate_for!(Ft4232h, DeviceType::FT4232H); impl_try_from_for!(Ft232h); +impl_try_from_for!(Ft232r); impl_try_from_for!(Ft2232h); impl_try_from_for!(Ft4232h); impl FtdiEeprom for Ft232h {} +impl FtdiEeprom for Ft232r {} impl FtdiEeprom for Ft2232h {} impl FtdiEeprom for Ft4232h {} impl FtdiMpsse for Ft232h {} +impl FtdiMpsse for Ft232r {} impl FtdiMpsse for Ft2232h {} impl FtdiMpsse for Ft4232h {} impl Ftx232hMpsse for Ft232h {} diff --git a/src/types.rs b/src/types.rs index f51f227..37c8e6d 100644 --- a/src/types.rs +++ b/src/types.rs @@ -49,7 +49,9 @@ use libftd2xx_ffi::{ use libftd2xx_ffi::{FT_DRIVER_TYPE_D2XX, FT_DRIVER_TYPE_VCP}; // FT_EEPROM_ -use libftd2xx_ffi::{FT_EEPROM_2232H, FT_EEPROM_232H, FT_EEPROM_4232H, FT_EEPROM_HEADER}; +use libftd2xx_ffi::{ + FT_EEPROM_2232H, FT_EEPROM_232H, FT_EEPROM_232R, FT_EEPROM_4232H, FT_EEPROM_HEADER, +}; use super::{EepromStringsError, EepromValueError}; use crate::util::slice_into_string; @@ -1089,6 +1091,54 @@ impl Default for Eeprom232h { } } +/// EEPROM structure for the FT232R. +/// +/// This is used by the [`eeprom_read`] and [`eeprom_program`] methods. +/// +/// [`eeprom_read`]: crate::FtdiEeprom::eeprom_read +/// [`eeprom_program`]: crate::FtdiEeprom::eeprom_program +#[derive(Debug, Copy, Clone)] +pub struct Eeprom232r(FT_EEPROM_232R); + +impl From for FT_EEPROM_232R { + fn from(val: Eeprom232r) -> FT_EEPROM_232R { + val.0 + } +} + +impl From for Eeprom232r { + fn from(val: FT_EEPROM_232R) -> Eeprom232r { + Eeprom232r(val) + } +} + +impl Default for Eeprom232r { + fn default() -> Self { + let mut header = EepromHeader::default(); + header.set_device_type(DeviceType::FT232R); + header.set_product_id(0x6001); + Self(FT_EEPROM_232R { + common: header.0, + IsHighCurrent: 0, + UseExtOsc: 0, + InvertTXD: 0, + InvertRXD: 0, + InvertRTS: 0, + InvertCTS: 0, + InvertDTR: 0, + InvertDSR: 0, + InvertDCD: 0, + InvertRI: 0, + Cbus0: 0, + Cbus1: 0, + Cbus2: 0, + Cbus3: 0, + Cbus4: 0, + DriverType: DriverType::D2XX.into(), + }) + } +} + /// EEPROM structure for the FT2232H. /// /// This is used by the [`eeprom_read`] and [`eeprom_program`] methods.