diff --git a/common/pids/Cargo.toml b/common/pids/Cargo.toml new file mode 100644 index 0000000..2668815 --- /dev/null +++ b/common/pids/Cargo.toml @@ -0,0 +1,8 @@ +[package] +authors = ["Jorge Aparicio "] +edition = "2018" +license = "MIT OR Apache-2.0" +name = "pids" +version = "0.0.0" + +[dependencies] diff --git a/common/pids/src/lib.rs b/common/pids/src/lib.rs new file mode 100644 index 0000000..05443fd --- /dev/null +++ b/common/pids/src/lib.rs @@ -0,0 +1,4 @@ +#![no_std] + +pub const LOOPBACK: u16 = 0x0309; +pub const PUZZLE: u16 = 0x0310; diff --git a/tools/Cargo.lock b/tools/Cargo.lock index 9d6fbe9..0ee2f71 100644 --- a/tools/Cargo.lock +++ b/tools/Cargo.lock @@ -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", ] diff --git a/tools/Cargo.toml b/tools/Cargo.toml index 363ef4e..cdf9600 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ + "change-channel", "dk-run", "dongle-flash", "serial-term", diff --git a/tools/change-channel/Cargo.toml b/tools/change-channel/Cargo.toml new file mode 100644 index 0000000..0a2cd35 --- /dev/null +++ b/tools/change-channel/Cargo.toml @@ -0,0 +1,12 @@ +[package] +authors = ["Jorge Aparicio "] +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" } \ No newline at end of file diff --git a/tools/change-channel/src/main.rs b/tools/change-channel/src/main.rs new file mode 100644 index 0000000..938def1 --- /dev/null +++ b/tools/change-channel/src/main.rs @@ -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::>(); + 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::()?; + 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 +} diff --git a/tools/usb-list/Cargo.toml b/tools/usb-list/Cargo.toml index d4c33fa..6025478 100644 --- a/tools/usb-list/Cargo.toml +++ b/tools/usb-list/Cargo.toml @@ -7,4 +7,5 @@ version = "0.0.0" [dependencies] consts = { path = "../../advanced/common/consts" } +pids = { path = "../../common/pids" } rusb = "0.5.5" diff --git a/tools/usb-list/src/main.rs b/tools/usb-list/src/main.rs index 0cbe641..4068a52 100644 --- a/tools/usb-list/src/main.rs +++ b/tools/usb-list/src/main.rs @@ -6,8 +6,8 @@ fn main() -> Result<(), Box> { 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", _ => "", };