Skip to content

Commit

Permalink
add ability to update nScope from driver
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjmeyer committed Dec 28, 2023
1 parent 30d6d58 commit 51c2779
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
.DS_Store
Cargo.lock
.idea
21 changes: 21 additions & 0 deletions .run/Update Firmware.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Update Firmware" type="CargoCommandRunConfiguration" factoryName="Cargo Command" folderName="Examples">
<option name="command" value="run --example update_firmware" />
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
<option name="emulateTerminal" value="false" />
<option name="channel" value="DEFAULT" />
<option name="requiredFeatures" value="true" />
<option name="allFeatures" value="false" />
<option name="withSudo" value="false" />
<option name="buildTarget" value="REMOTE" />
<option name="backtrace" value="SHORT" />
<envs>
<env name="RUST_LOG" value="trace" />
</envs>
<option name="isRedirectInput" value="false" />
<option name="redirectInputPath" value="" />
<method v="2">
<option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
</method>
</configuration>
</component>
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ hidapi = "2.3.3"
log = "~0.4"
regex = "~1"
rusb = "0.9.3"
dfu-libusb = "0.5.1"

[dev-dependencies]
env_logger = "0.10.0"
Expand Down
8 changes: 6 additions & 2 deletions examples/list_all_nscopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ fn main() -> Result<(), Box<dyn Error>> {
let bench = LabBench::new()?;

// Print the bench to show a list of detected nScopes
println!("{:?}", bench);
println!("Lab Bench: \n{:?}", bench);

println!("\nManual list of all detected nScopes:");
// Or loop over all nScope links in the list and print them
for nscope_link in bench.list() {
println!("{:?}", nscope_link)
println!(" {:?}", nscope_link)
}
for nscope_dfu in bench.scopes_in_dfu() {
println!(" {:?}", nscope_dfu)
}
Ok(())
}
34 changes: 34 additions & 0 deletions examples/update_firmware.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/***************************************************************************************************
*
* nLabs, LLC
* https://nscope.org
* Copyright(c) 2020. All Rights Reserved
*
* This file is part of the nScope API
*
**************************************************************************************************/

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

fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();

// Create a LabBench
let bench = LabBench::new()?;

// 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");
}

Ok(())
}
11 changes: 11 additions & 0 deletions src/firmware.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/***************************************************************************************************
*
* nLabs, LLC
* https://nscope.org
* Copyright(c) 2020. All Rights Reserved
*
* This file is part of the nScope API
*
**************************************************************************************************/

pub(crate) static FIRMWARE: &[u8] = include_bytes!("firmware/v2.0");
Binary file added src/firmware/v2.0
Binary file not shown.
15 changes: 13 additions & 2 deletions src/lab_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
**************************************************************************************************/

use crate::scope::Nscope;
use crate::scope_dfu::NscopeDFU;
use std::{fmt, io};
use std::error::Error;
use std::sync::{Arc, RwLock};
Expand Down Expand Up @@ -71,6 +72,14 @@ 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 @@ -139,8 +148,10 @@ impl fmt::Debug for LabBench {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"LabBench: {:#?}",
self.list().collect::<Vec<NscopeLink>>()
"Connected nScopes: {:#?} \n\
nScopes in DFU mode: {:#?}",
self.list().collect::<Vec<NscopeLink>>(),
self.scopes_in_dfu().collect::<Vec<NscopeDFU>>()
)
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@

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: 51 additions & 0 deletions src/scope_dfu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/***************************************************************************************************
*
* nLabs, LLC
* https://nscope.org
* Copyright(c) 2020. All Rights Reserved
*
* This file is part of the nScope API
*
**************************************************************************************************/

use std::error::Error;
use std::fmt;

use crate::firmware::FIRMWARE;

pub struct NscopeDFU {
device: rusb::Device<rusb::GlobalContext>,
}


impl NscopeDFU {
pub(crate) fn new(device: &rusb::Device<rusb::GlobalContext>) -> Option<Self> {
if let Ok(device_desc) = device.device_descriptor() {
if device_desc.vendor_id() == 0x0483 && device_desc.product_id() == 0xA4AB {
return Some(NscopeDFU { device: device.clone() });
}
}
None
}

pub fn update(&self) -> Result<(), Box<dyn Error>> {
let mut dfu = dfu_libusb::DfuLibusb::from_usb_device(
self.device.clone(),
self.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 NscopeDFU {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "nScope in DFU mode: {:?}", &self.device)
}
}

0 comments on commit 51c2779

Please sign in to comment.