Skip to content

Commit

Permalink
use nscope link for firmware updates
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjmeyer committed Jan 3, 2024
1 parent 5b4a5f0 commit 0a6d1f9
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 89 deletions.
3 changes: 0 additions & 3 deletions examples/list_all_nscopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,5 @@ fn main() -> Result<(), Box<dyn Error>> {
for nscope_link in bench.list() {
println!(" {:?}", nscope_link)
}
for nscope_dfu in bench.scopes_in_dfu() {
println!(" {:?}", nscope_dfu)
}
Ok(())
}
15 changes: 6 additions & 9 deletions examples/update_firmware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
**************************************************************************************************/

use nscope::{LabBench, NscopeDFU};
use nscope::LabBench;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
Expand All @@ -20,14 +20,11 @@ fn main() -> Result<(), Box<dyn Error>> {
// Print the bench to show a list of detected nScopes
println!("{:?}", bench);

// Get a list of all the nScopes that are detected in DFU mode
let scopes_in_dfu: Vec<NscopeDFU> = bench.scopes_in_dfu().collect();

// If we have a scope in DFU mode, update the first one we found
if let Some(dfu) = scopes_in_dfu.first() {
dfu.update()?;
} else {
println!("Cannot find any nScopes in DFU mode");
// Update any scope that is in DFU mode
for nscope_link in bench.list() {
if let Err(e) = nscope_link.update() {
println!("Encountered an error updating nScope: {e}")
}
}

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion src/firmware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
*
**************************************************************************************************/

pub(crate) static FIRMWARE: &[u8] = include_bytes!("firmware/v2.0");
pub(crate) static FIRMWARE: &[u8] = include_bytes!("firmware/v2");
pub(crate) static FIRMWARE_VERSION: u16 = 0x0200;
Binary file renamed src/firmware/v2.0 → src/firmware/v2
Binary file not shown.
90 changes: 67 additions & 23 deletions src/lab_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
**************************************************************************************************/

use crate::scope::Nscope;
use crate::scope_dfu::NscopeDFU;
use std::{fmt, io};
use std::error::Error;
use std::sync::{Arc, RwLock};
use crate::firmware::{FIRMWARE, FIRMWARE_VERSION};

pub(crate) enum NscopeDevice {
HidApiDevice { info: hidapi::DeviceInfo, api: Arc<RwLock<hidapi::HidApi>> },
Expand All @@ -29,6 +29,8 @@ pub struct LabBench {
/// A detected link between the computer and an nScope, used to open and retrieve an nScope
pub struct NscopeLink {
available: bool,
in_dfu: bool,
needs_update: bool,
device: NscopeDevice,
}

Expand Down Expand Up @@ -72,14 +74,6 @@ impl LabBench {
v2_nscopes.chain(v1_nscopes)
}

pub fn scopes_in_dfu(&self) -> impl Iterator<Item=NscopeDFU> + '_ {
self.rusb_devices
.iter()
.filter_map(|d| {
NscopeDFU::new(d)
})
}

/// Returns a vector containing all nScopes that are available
pub fn open_all_available(&self) -> Vec<Nscope> {
self.list().filter_map(|nsl| nsl.open(false).ok()).collect()
Expand Down Expand Up @@ -113,14 +107,20 @@ impl NscopeLink {
let available = info.open_device(&hid_api).is_ok();
return Some(NscopeLink {
available,
in_dfu: false,
needs_update: false,
device: NscopeDevice::HidApiDevice { info: info.clone(), api: Arc::clone(&api) },
});
}
None
}
NscopeDevice::RusbDevice(device) => {
if let Ok(device_desc) = device.device_descriptor() {
if device_desc.vendor_id() == 0x0483 && device_desc.product_id() == 0xA4AA {
let vendor_id = device_desc.vendor_id();
let product_id = device_desc.product_id();
let firmware_version = device_desc.device_version();

if vendor_id == 0x0483 && product_id == 0xA4AA {
let mut available = false;
if let Ok(mut dev) = device.open() {
if let Ok(()) = dev.claim_interface(0) {
Expand All @@ -129,6 +129,15 @@ impl NscopeLink {
}
return Some(NscopeLink {
available,
in_dfu: false,
needs_update: firmware_version != rusb::Version::from_bcd(FIRMWARE_VERSION),
device: NscopeDevice::RusbDevice(device),
});
} else if device_desc.vendor_id() == 0x0483 && device_desc.product_id() == 0xA4AB {
return Some(NscopeLink {
available: false,
in_dfu: true,
needs_update: false,
device: NscopeDevice::RusbDevice(device),
});
}
Expand All @@ -142,17 +151,37 @@ impl NscopeLink {
pub fn open(&self, power_on: bool) -> Result<Nscope, Box<dyn Error>> {
Nscope::new(&self.device, power_on)
}

/// Update the nScope at the link
pub fn update(&self) -> Result<(), Box<dyn Error>> {
if !self.in_dfu {
return Err("nScope is not in DFU mode".into());
}

match &self.device {
NscopeDevice::HidApiDevice { .. } => {
return Err("Cannot update nScope v1".into());
}
NscopeDevice::RusbDevice(device) => {
let mut dfu = dfu_libusb::DfuLibusb::from_usb_device(
device.clone(),
device.open()?,
0, 0)?;

dfu.override_address(0x08008000);
dfu.download_from_slice(FIRMWARE)?;
dfu.detach()?;
println!("Resetting device");
dfu.usb_reset()?;
}
};
Ok(())
}
}

impl fmt::Debug for LabBench {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Connected nScopes: {:#?} \n\
nScopes in DFU mode: {:#?}",
self.list().collect::<Vec<NscopeLink>>(),
self.scopes_in_dfu().collect::<Vec<NscopeDFU>>()
)
write!(f, "{:#?}", self.list().collect::<Vec<NscopeLink>>())
}
}

Expand All @@ -162,11 +191,26 @@ impl fmt::Debug for NscopeLink {
NscopeDevice::HidApiDevice { .. } => { "nScope v1" }
NscopeDevice::RusbDevice(_) => { "nScope v2" }
};
write!(
f,
"Link to {} [ available: {} ]",
device_name,
self.available
)
if self.in_dfu {
write!(
f,
"Link to {} [ in DFU mode ]",
device_name,
)
} else if self.needs_update {
write!(
f,
"Link to {} [ REQUIRES FIRMWARE UPDATE ] [ available: {} ]",
device_name,
self.available,
)
} else {
write!(
f,
"Link to {} [ available: {} ]",
device_name,
self.available,
)
}
}
}
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,11 @@

mod lab_bench;
mod scope;
mod scope_dfu;
mod version;
mod firmware;

pub use lab_bench::LabBench;
pub use lab_bench::NscopeLink;
pub use scope_dfu::NscopeDFU;
pub use scope::Nscope;
pub use scope::power::*;
pub use scope::pulse_output::*;
Expand Down
51 changes: 0 additions & 51 deletions src/scope_dfu.rs

This file was deleted.

0 comments on commit 0a6d1f9

Please sign in to comment.