add change-channel tool

This commit is contained in:
Jorge Aparicio 2020-07-13 16:27:13 +02:00
parent 8f5f89d782
commit 09507369af
8 changed files with 81 additions and 7 deletions

8
common/pids/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
authors = ["Jorge Aparicio <jorge.aparicio@ferrous-systems.com>"]
edition = "2018"
license = "MIT OR Apache-2.0"
name = "pids"
version = "0.0.0"
[dependencies]

4
common/pids/src/lib.rs Normal file
View file

@ -0,0 +1,4 @@
#![no_std]
pub const LOOPBACK: u16 = 0x0309;
pub const PUZZLE: u16 = 0x0310;

25
tools/Cargo.lock generated
View file

@ -122,9 +122,19 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "change-channel"
version = "0.0.0"
dependencies = [
"anyhow",
"consts",
"hidapi",
"pids",
]
[[package]]
name = "consts"
version = "0.1.0"
version = "0.0.0"
[[package]]
name = "crc32fast"
@ -158,7 +168,7 @@ dependencies = [
[[package]]
name = "dk-run"
version = "0.1.0"
version = "0.0.0"
dependencies = [
"anyhow",
"arrayref",
@ -174,7 +184,7 @@ dependencies = [
[[package]]
name = "dongle-flash"
version = "0.1.0"
version = "0.0.0"
dependencies = [
"anyhow",
"ihex",
@ -516,6 +526,10 @@ dependencies = [
"wasmparser",
]
[[package]]
name = "pids"
version = "0.0.0"
[[package]]
name = "pkg-config"
version = "0.3.17"
@ -768,7 +782,7 @@ dependencies = [
[[package]]
name = "serial-term"
version = "0.1.0"
version = "0.0.0"
dependencies = [
"anyhow",
"consts",
@ -932,9 +946,10 @@ checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
[[package]]
name = "usb-list"
version = "0.1.0"
version = "0.0.0"
dependencies = [
"consts",
"pids",
"rusb",
]

View file

@ -1,5 +1,6 @@
[workspace]
members = [
"change-channel",
"dk-run",
"dongle-flash",
"serial-term",

View file

@ -0,0 +1,12 @@
[package]
authors = ["Jorge Aparicio <jorge.aparicio@ferrous-systems.com>"]
edition = "2018"
license = "MIT OR Apache-2.0"
name = "change-channel"
version = "0.0.0"
[dependencies]
anyhow = "1.0.27"
consts = { path = "../../advanced/common/consts" }
hidapi = "1.2.2"
pids = { path = "../../common/pids" }

View file

@ -0,0 +1,33 @@
use std::env;
use anyhow::{anyhow, bail, ensure};
use hidapi::HidApi;
fn main() -> Result<(), anyhow::Error> {
let args = env::args()
.skip(1) // skip program name
.collect::<Vec<_>>();
ensure!(!args.is_empty(), "expected exactly one argument");
let api = HidApi::new()?;
let dev = api
.device_list()
.filter(|dev| dev.vendor_id() == consts::VID && check_pid(dev.product_id()))
.next()
.ok_or_else(|| anyhow!("device not found"))?
.open_device(&api)?;
let chan = args[0].parse::<u8>()?;
if chan < 11 || chan > 26 {
bail!("channel is out of range (`11..=26`)")
}
const REPORT_ID: u8 = 0;
dev.write(&[REPORT_ID, chan])?;
println!("requested channel change to channel {}", chan);
Ok(())
}
fn check_pid(pid: u16) -> bool {
pid == pids::LOOPBACK || pid == pids::PUZZLE
}

View file

@ -7,4 +7,5 @@ version = "0.0.0"
[dependencies]
consts = { path = "../../advanced/common/consts" }
pids = { path = "../../common/pids" }
rusb = "0.5.5"

View file

@ -6,8 +6,8 @@ fn main() -> Result<(), Box<dyn Error>> {
let suffix = match (desc.vendor_id(), desc.product_id()) {
(0x1366, 0x1015) => " <- J-Link on the nRF52840 Development Kit",
(0x1915, 0x521f) => " <- nRF52840 Dongle (in bootloader mode)",
(0x2020, 0x0309) => " <- nRF52840 Dongle (loopback.hex)",
(0x2020, 0x0310) => " <- nRF52840 Dongle (puzzle.hex)",
(0x2020, pids::LOOPBACK) => " <- nRF52840 Dongle (loopback.hex)",
(0x2020, pids::PUZZLE) => " <- nRF52840 Dongle (puzzle.hex)",
(consts::VID, consts::PID) => " <- nRF52840 on the nRF52840 Development Kit",
_ => "",
};