Skip to content

Commit

Permalink
Allow listing and selecting probe
Browse files Browse the repository at this point in the history
This provides options similar to cargo-flash to list all connected probes
and to specify which one to use:
  probe-run --list-probes
  probe-run --probe '1366:0101' --chip nrf52 bin
  probe-run --probe '1366:0101:123456' --chip nrf52 bin

This also alows specifying the probe as en env-var:
  PROBE_RUN_PROBE='1366:0101:123456' cargo run

It fixes knurling-rs#14.
  • Loading branch information
Aurélien Jacobs committed Sep 1, 2020
1 parent baa06f2 commit daeb73e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ To support multiple devices, or permit overriding default behavior, you may pref
`${PROBE_RUN_CHIP}` environment variable, and set `runner` (or
`CARGO_TARGET_${TARGET_ARCH}_RUNNER`) to `probe-run`.

If you have several probes connected, you can specify which one to use adding
the --probe option to the `runner` or setting the `${PROBE_RUN_PROBE}` environment
variable:

```console
PROBE_RUN_PROBE='1366:0101:123456' cargo run
```

To list all connected probes run `probe-run --list-probes`.

2. Enable debug info

Next check that debug info is enabled for all profiles.
Expand Down
39 changes: 36 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,25 @@ struct Opts {
#[structopt(long)]
list_chips: bool,

/// Lists all the connected probes and exit.
#[structopt(long)]
list_probes: bool,

/// Enable defmt decoding.
#[cfg(feature = "defmt")]
#[structopt(long, conflicts_with = "no_flash")]
defmt: bool,

/// The chip to program.
#[structopt(long, required_unless("list-chips"), env = "PROBE_RUN_CHIP")]
#[structopt(long, required_unless_one(&["list-chips", "list-probes"]), env = "PROBE_RUN_CHIP")]
chip: Option<String>,

/// The probe to use (eg. VID:PID or VID:PID:Serial).
#[structopt(long, env = "PROBE_RUN_PROBE")]
probe: Option<String>,

/// Path to an ELF firmware file.
#[structopt(name = "ELF", parse(from_os_str), required_unless("list-chips"))]
#[structopt(name = "ELF", parse(from_os_str), required_unless_one(&["list-chips", "list-probes"]))]
elf: Option<PathBuf>,

/// Skip writing the application binary to flash.
Expand All @@ -73,6 +81,10 @@ fn notmain() -> Result<i32, anyhow::Error> {

let opts: Opts = Opts::from_args();

if opts.list_probes {
return print_probes();
}

if opts.list_chips {
return print_chips();
}
Expand Down Expand Up @@ -221,7 +233,12 @@ fn notmain() -> Result<i32, anyhow::Error> {
bail!("no probe was found")
}
log::debug!("found {} probes", probes.len());
let probe = probes[0].open()?;
let probe = if let Some(probe_opt) = opts.probe.as_deref() {
let selector: probe_rs::DebugProbeSelector = probe_opt.try_into()?;
probe_rs::Probe::open(selector)?
} else {
probes[0].open()?
};
log::info!("opened probe");
let mut sess = probe.attach(target)?;
log::info!("started session");
Expand Down Expand Up @@ -624,6 +641,22 @@ fn backtrace(
Ok(top_exception)
}

fn print_probes() -> Result<i32, anyhow::Error> {
let probes = Probe::list_all();

if !probes.is_empty() {
println!("The following devices were found:");
probes
.iter()
.enumerate()
.for_each(|(num, link)| println!("[{}]: {:?}", num, link));
} else {
println!("No devices were found.");
}

Ok(0)
}

fn print_chips() -> Result<i32, anyhow::Error> {
let registry = registry::families().expect("Could not retrieve chip family registry");
for chip_family in registry {
Expand Down

0 comments on commit daeb73e

Please sign in to comment.