From 72bbd4867876672ec689d8f46277843d20b6283c Mon Sep 17 00:00:00 2001 From: Sergey Matyukevich Date: Tue, 13 Nov 2018 23:35:49 +0300 Subject: [PATCH 1/4] cleanup: rust fmt Signed-off-by: Sergey Matyukevich --- examples/dlp-loopback-tester.rs | 9 ++-- src/lib.rs | 84 +++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 40 deletions(-) diff --git a/examples/dlp-loopback-tester.rs b/examples/dlp-loopback-tester.rs index ae4f6a1..f5ae496 100644 --- a/examples/dlp-loopback-tester.rs +++ b/examples/dlp-loopback-tester.rs @@ -6,7 +6,6 @@ extern crate ftdi; use std::io::{Read, Write}; - fn main() { println!("Starting tester..."); let mut context = ftdi::Context::new(); @@ -50,9 +49,11 @@ fn main() { context.read_to_end(&mut reply).unwrap(); let complement = 255 - num; if reply != vec![complement] { - println!("Wrong complement reply {:?} (expected {:?}", - reply, - vec![complement]); + println!( + "Wrong complement reply {:?} (expected {:?}", + reply, + vec![complement] + ); } } println!("Testing finished"); diff --git a/src/lib.rs b/src/lib.rs index 6e36f2a..782db25 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,11 +7,10 @@ extern crate num; extern crate libftdi1_sys as ffi; use std::io; -use std::io::{Read, Write, ErrorKind}; +use std::io::{ErrorKind, Read, Write}; use num::traits::ToPrimitive; - /// The target interface pub enum Interface { A, @@ -33,14 +32,15 @@ impl Into for Interface { } } - pub struct Context { native: ffi::ftdi_context, } impl Context { pub fn new() -> Context { - let mut context = Context { native: Default::default() }; + let mut context = Context { + native: Default::default(), + }; let result = unsafe { ffi::ftdi_init(&mut context.native) }; // Can be non-zero on either OOM or libusb_init failure assert!(result == 0); @@ -55,7 +55,10 @@ impl Context { -1 => Err(io::Error::new(ErrorKind::InvalidInput, "unknown interface")), -2 => Err(io::Error::new(ErrorKind::NotFound, "device not found")), -3 => Err(io::Error::new(ErrorKind::Other, "device already opened")), - _ => Err(io::Error::new(ErrorKind::Other, "unknown set latency error")), + _ => Err(io::Error::new( + ErrorKind::Other, + "unknown set latency error", + )), } } @@ -70,8 +73,14 @@ impl Context { -7 => Err(io::Error::new(ErrorKind::Other, "set baudrate failed")), -8 => Err(io::Error::new(ErrorKind::Other, "get description failed")), -9 => Err(io::Error::new(ErrorKind::Other, "get serial failed")), - -12 => Err(io::Error::new(ErrorKind::Other, "libusb_get_device_list failed")), - -13 => Err(io::Error::new(ErrorKind::Other, "libusb_get_device_descriptor failed")), + -12 => Err(io::Error::new( + ErrorKind::Other, + "libusb_get_device_list failed", + )), + -13 => Err(io::Error::new( + ErrorKind::Other, + "libusb_get_device_descriptor failed", + )), _ => Err(io::Error::new(ErrorKind::Other, "unknown usb_open error")), } } @@ -104,7 +113,10 @@ impl Context { -1 => Err(io::Error::new(ErrorKind::InvalidInput, "bad latency value")), -2 => Err(io::Error::new(ErrorKind::Other, "set latency failed")), -3 => Err(io::Error::new(ErrorKind::NotFound, "device not found")), - _ => Err(io::Error::new(ErrorKind::Other, "unknown set latency error")), + _ => Err(io::Error::new( + ErrorKind::Other, + "unknown set latency error", + )), } } @@ -115,49 +127,44 @@ impl Context { 0 => Ok(value), -1 => Err(io::Error::new(ErrorKind::Other, "set latency failed")), -2 => Err(io::Error::new(ErrorKind::NotFound, "device not found")), - _ => Err(io::Error::new(ErrorKind::Other, "unknown get latency error")), + _ => Err(io::Error::new( + ErrorKind::Other, + "unknown get latency error", + )), } } pub fn set_write_chunksize(&mut self, value: u32) { - let result = unsafe { - ffi::ftdi_write_data_set_chunksize(&mut self.native, value) - }; + let result = unsafe { ffi::ftdi_write_data_set_chunksize(&mut self.native, value) }; match result { 0 => (), - err => panic!("unknown set_write_chunksize retval {:?}", err) + err => panic!("unknown set_write_chunksize retval {:?}", err), } } pub fn write_chunksize(&mut self) -> u32 { let mut value = 0; - let result = unsafe { - ffi::ftdi_write_data_get_chunksize(&mut self.native, &mut value) - }; + let result = unsafe { ffi::ftdi_write_data_get_chunksize(&mut self.native, &mut value) }; match result { 0 => value, - err => panic!("unknown get_write_chunksize retval {:?}", err) + err => panic!("unknown get_write_chunksize retval {:?}", err), } } pub fn set_read_chunksize(&mut self, value: u32) { - let result = unsafe { - ffi::ftdi_read_data_set_chunksize(&mut self.native, value) - }; + let result = unsafe { ffi::ftdi_read_data_set_chunksize(&mut self.native, value) }; match result { 0 => (), - err => panic!("unknown set_write_chunksize retval {:?}", err) + err => panic!("unknown set_write_chunksize retval {:?}", err), } } pub fn read_chunksize(&mut self) -> u32 { let mut value = 0; - let result = unsafe { - ffi::ftdi_read_data_get_chunksize(&mut self.native, &mut value) - }; + let result = unsafe { ffi::ftdi_read_data_get_chunksize(&mut self.native, &mut value) }; match result { 0 => value, - err => panic!("unknown get_write_chunksize retval {:?}", err) + err => panic!("unknown get_write_chunksize retval {:?}", err), } } } @@ -174,11 +181,14 @@ impl Read for Context { let result = unsafe { ffi::ftdi_read_data(&mut self.native, buf.as_mut_ptr(), len) }; match result { count if count >= 0 => Ok(count as usize), - -666 => Err(io::Error::new(ErrorKind::NotFound, "device not found in read")), - libusb_error => { - Err(io::Error::new(ErrorKind::Other, - format!("libusb_bulk_transfer error {}", libusb_error))) - } + -666 => Err(io::Error::new( + ErrorKind::NotFound, + "device not found in read", + )), + libusb_error => Err(io::Error::new( + ErrorKind::Other, + format!("libusb_bulk_transfer error {}", libusb_error), + )), } } } @@ -189,11 +199,14 @@ impl Write for Context { let result = unsafe { ffi::ftdi_write_data(&mut self.native, buf.as_ptr(), len) }; match result { count if count >= 0 => Ok(count as usize), - -666 => Err(io::Error::new(ErrorKind::NotFound, "device not found in write")), - libusb_error => { - Err(io::Error::new(ErrorKind::Other, - format!("usb_bulk_write error {}", libusb_error))) - } + -666 => Err(io::Error::new( + ErrorKind::NotFound, + "device not found in write", + )), + libusb_error => Err(io::Error::new( + ErrorKind::Other, + format!("usb_bulk_write error {}", libusb_error), + )), } } @@ -202,7 +215,6 @@ impl Write for Context { } } - #[cfg(test)] mod test { #[test] From e06cb4bfa9f39d08983a87a546002d371706a482 Mon Sep 17 00:00:00 2001 From: Sergey Matyukevich Date: Tue, 13 Nov 2018 23:36:16 +0300 Subject: [PATCH 2/4] ftdi: add set_flow_control wrapper Signed-off-by: Sergey Matyukevich --- src/lib.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 782db25..be52cca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,6 +32,25 @@ impl Into for Interface { } } +#[allow(non_camel_case_types)] +pub enum FlowControl { + SIO_DISABLE_FLOW_CTRL, + SIO_RTS_CTS_HS, + SIO_DTR_DSR_HS, + SIO_XON_XOFF_HS, +} + +impl Into for FlowControl { + fn into(self) -> i32 { + match self { + FlowControl::SIO_DISABLE_FLOW_CTRL => 0x0, + FlowControl::SIO_RTS_CTS_HS => (0x1 << 8), + FlowControl::SIO_DTR_DSR_HS => (0x2 << 8), + FlowControl::SIO_XON_XOFF_HS => (0x4 << 8), + } + } +} + pub struct Context { native: ffi::ftdi_context, } @@ -167,6 +186,19 @@ impl Context { err => panic!("unknown get_write_chunksize retval {:?}", err), } } + + pub fn set_flow_control(&mut self, flowctrl: FlowControl) -> io::Result<()> { + let result = unsafe { ffi::ftdi_setflowctrl(&mut self.native, flowctrl.into()) }; + match result { + 0 => Ok(()), + -1 => Err(io::Error::new(ErrorKind::Other, "set flow control failed")), + -2 => Err(io::Error::new(ErrorKind::NotFound, "device not found")), + _ => Err(io::Error::new( + ErrorKind::Other, + "unknown set flow control error", + )), + } + } } impl Drop for Context { From 84d20b06e0f946542957ecb40ad50a4214f8eaaa Mon Sep 17 00:00:00 2001 From: Sergey Matyukevich Date: Wed, 14 Nov 2018 23:30:18 +0300 Subject: [PATCH 3/4] ftdi: add set_bitmode wrapper Signed-off-by: Sergey Matyukevich --- src/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index be52cca..9564cde 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,6 +32,36 @@ impl Into for Interface { } } +pub enum BitMode { + RESET, + BITBANG, + MPSSE, + SYNCBB, + MCU, + OPTO, + CBUS, + SYNCFF, + FT1284, +} + +impl Into for BitMode { + fn into(self) -> u8 { + let mode = match self { + BitMode::RESET => ffi::ftdi_mpsse_mode::BITMODE_RESET, + BitMode::BITBANG => ffi::ftdi_mpsse_mode::BITMODE_BITBANG, + BitMode::MPSSE => ffi::ftdi_mpsse_mode::BITMODE_MPSSE, + BitMode::SYNCBB => ffi::ftdi_mpsse_mode::BITMODE_SYNCBB, + BitMode::MCU => ffi::ftdi_mpsse_mode::BITMODE_MCU, + BitMode::OPTO => ffi::ftdi_mpsse_mode::BITMODE_OPTO, + BitMode::CBUS => ffi::ftdi_mpsse_mode::BITMODE_CBUS, + BitMode::SYNCFF => ffi::ftdi_mpsse_mode::BITMODE_SYNCFF, + BitMode::FT1284 => ffi::ftdi_mpsse_mode::BITMODE_FT1284, + }; + + mode as u8 + } +} + #[allow(non_camel_case_types)] pub enum FlowControl { SIO_DISABLE_FLOW_CTRL, @@ -199,6 +229,19 @@ impl Context { )), } } + + pub fn set_bitmode(&mut self, bitmask: u8, mode: BitMode) -> io::Result<()> { + let result = unsafe { ffi::ftdi_set_bitmode(&mut self.native, bitmask, mode.into()) }; + match result { + 0 => Ok(()), + -1 => Err(io::Error::new(ErrorKind::Other, "set bitmode failed")), + -2 => Err(io::Error::new(ErrorKind::NotFound, "device not found")), + _ => Err(io::Error::new( + ErrorKind::Other, + "unknown set bitmode error", + )), + } + } } impl Drop for Context { From 340c90d533990827522663b469f71ea3fe50409b Mon Sep 17 00:00:00 2001 From: Sergey Matyukevich Date: Sun, 9 Dec 2018 16:03:47 +0300 Subject: [PATCH 4/4] ftdi: add usb_close wrapper Signed-off-by: Sergey Matyukevich --- src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 9564cde..900f325 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -155,6 +155,16 @@ impl Context { } } + pub fn usb_close(&mut self) -> io::Result<()> { + let result = unsafe { ffi::ftdi_usb_close(&mut self.native) }; + match result { + 0 => Ok(()), + -1 => Err(io::Error::new(ErrorKind::Other, "usb release failed")), + -3 => Err(io::Error::new(ErrorKind::NotFound, "unknown ftdi context")), + _ => Err(io::Error::new(ErrorKind::Other, "unknown usb_open error")), + } + } + pub fn set_latency_timer(&mut self, value: u8) -> io::Result<()> { let result = unsafe { ffi::ftdi_set_latency_timer(&mut self.native, value) }; match result {