Skip to content

Commit

Permalink
Improve synchronize_mpsse timeout handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
newAM committed Oct 23, 2020
1 parent 93fed45 commit 0f38ae0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.23.0] - 2020-10-23
### Changed
- `synchronize_mpsse` will now timeout if no read data is recieved and a read
timeout has been set.

## [0.22.0] - 2020-10-15
### Added
- Added `list_devices_fs` to work around vendor driver bug.
Expand Down Expand Up @@ -51,7 +56,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Prior releases
A changelog was not kept for prior releases.

[Unreleased]: https://github.com/newAM/libftd2xx-rs/compare/0.21.1...HEAD
[Unreleased]: https://github.com/newAM/libftd2xx-rs/compare/0.23.0...HEAD
[0.23.0]: https://github.com/newAM/libftd2xx-rs/releases/tag/0.23.0
[0.22.0]: https://github.com/newAM/libftd2xx-rs/releases/tag/0.22.0
[0.21.1]: https://github.com/newAM/libftd2xx-rs/releases/tag/0.21.1
[0.21.0]: https://github.com/newAM/libftd2xx-rs/releases/tag/0.21.0
[0.20.0]: https://github.com/newAM/libftd2xx-rs/releases/tag/0.20.0
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libftd2xx"
version = "0.22.0" # remember to update html_root_url
version = "0.23.0" # remember to update html_root_url
authors = ["Alex M. <[email protected]>"]
edition = "2018"
description = "Rust safe wrapper around the libftd2xx-ffi crate."
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ permission from FTDI.

```toml
[dependencies]
libftd2xx = "~0.22.0"
libftd2xx = "~0.23.0"
```

This is a basic example to get your started.
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//!
//! ```toml
//! [dependencies]
//! libftd2xx = "~0.22.0"
//! libftd2xx = "~0.23.0"
//! ```
//!
//! This is a basic example to get your started.
Expand Down Expand Up @@ -74,7 +74,7 @@
//! [libftd2xx-ffi]: https://github.com/newAM/libftd2xx-ffi-rs
//! [setup executable]: https://www.ftdichip.com/Drivers/CDM/CDM21228_Setup.zip
//! [udev]: https://en.wikipedia.org/wiki/Udev
#![doc(html_root_url = "https://docs.rs/libftd2xx/0.22.0")]
#![doc(html_root_url = "https://docs.rs/libftd2xx/0.23.0")]
#![deny(missing_docs)]

mod errors;
Expand Down
33 changes: 14 additions & 19 deletions src/mpsse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,10 +513,14 @@ pub trait FtdiMpsse: FtdiCommon {
self.set_bit_mode(settings.mask, BitMode::Mpsse)?;
self.enable_loopback()?;
self.synchronize_mpsse()?;
self.disable_loopback()?;

// bundle the disable loopback and clock divisor writes together
// to save some time
let mut mpsse_cmd = MpsseCmdBuilder::new().disable_loopback();
if let Some(frequency) = settings.clock_frequency {
self.set_clock(frequency)?;
mpsse_cmd = mpsse_cmd.set_clock(frequency, Self::DEVICE_TYPE);
}
self.write(mpsse_cmd.as_slice())?;

Ok(())
}
Expand Down Expand Up @@ -550,28 +554,19 @@ pub trait FtdiMpsse: FtdiCommon {
/// [FTDI MPSSE Basics]: https://www.ftdichip.com/Support/Documents/AppNotes/AN_135_MPSSE_Basics.pdf
fn synchronize_mpsse(&mut self) -> Result<(), TimeoutError> {
self.purge_rx()?;
debug_assert_eq!(self.queue_status()?, 0);
self.write(&[ECHO_CMD_2])?;

let mut num_bytes: usize = 0;
while num_bytes == 0 {
num_bytes = self.queue_status()?;
}

let mut buf = vec![0; num_bytes].into_boxed_slice();
// the FTDI MPSSE basics polls the queue status here
// we purged the RX buffer so the response should always be 2 bytes
// this allows us to leverage the timeout built into read
let mut buf: [u8; 2] = [0; 2];
self.read(&mut buf)?;

let mut command_echoed = false;
for count in 0..(num_bytes - 1) {
if buf[count] == 0xFA && buf[count + 1] == ECHO_CMD_2 {
command_echoed = true;
break;
}
}

if !command_echoed {
Err(TimeoutError::from(FtStatus::OTHER_ERROR))
} else {
if buf[0] == 0xFA && buf[1] == ECHO_CMD_2 {
Ok(())
} else {
Err(TimeoutError::from(FtStatus::OTHER_ERROR))
}
}

Expand Down

0 comments on commit 0f38ae0

Please sign in to comment.