diff --git a/Cargo.lock b/Cargo.lock index ba9af37..299ba72 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,27 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "CoreFoundation-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0e9889e6db118d49d88d84728d0e964d973a5680befb5f85f55141beea5c20b" +dependencies = [ + "libc", + "mach", +] + +[[package]] +name = "IOKit-sys" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99696c398cbaf669d2368076bdb3d627fb0ce51a26899d7c61228c5c0af3bf4a" +dependencies = [ + "CoreFoundation-sys", + "libc", + "mach", +] + [[package]] name = "argp" version = "0.4.0" @@ -50,6 +71,12 @@ version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" +[[package]] +name = "defer-lite" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6a1ef0750c0fcdb9bd00e7829fcad58e9a0a2ab7078b57c39743b973f3878ce" + [[package]] name = "getopts" version = "0.2.24" @@ -76,12 +103,37 @@ dependencies = [ "redox_syscall", ] +[[package]] +name = "mach" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fd13ee2dd61cc82833ba05ade5a30bb3d63f7ced605ef827063c63078302de9" +dependencies = [ + "libc", +] + [[package]] name = "memchr" version = "2.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "numtoa" version = "0.2.4" @@ -155,6 +207,21 @@ dependencies = [ "winapi", ] +[[package]] +name = "serial_enumerator" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "831ffc034580df26f33a70363cc92c6b1ebd5c6872ef7c191f9140cc0aafb29c" +dependencies = [ + "CoreFoundation-sys", + "IOKit-sys", + "defer-lite", + "libc", + "mach", + "nom", + "windows", +] + [[package]] name = "syn" version = "2.0.106" @@ -185,6 +252,7 @@ dependencies = [ "argp", "pbr", "serial2", + "serial_enumerator", "termion", ] @@ -227,3 +295,46 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aac7fef12f4b59cd0a29339406cc9203ab44e440ddff6b3f5a41455349fa9cf3" +dependencies = [ + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_msvc" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d027175d00b01e0cbeb97d6ab6ebe03b12330a35786cbaca5252b1c4bf5d9b" + +[[package]] +name = "windows_i686_gnu" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8793f59f7b8e8b01eda1a652b2697d87b93097198ae85f823b969ca5b89bba58" + +[[package]] +name = "windows_i686_msvc" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8602f6c418b67024be2996c512f5f995de3ba417f4c75af68401ab8756796ae4" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3d615f419543e0bd7d2b3323af0d86ff19cbc4f816e6453f36a2c2ce889c354" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11d95421d9ed3672c280884da53201a5c46b7b2765ca6faf34b0d71cf34a3561" diff --git a/Cargo.toml b/Cargo.toml index 17ca17a..a5d3d4a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ edition = "2024" argp = "0.4.0" pbr = "1.1.1" serial2 = "0.2.32" +serial_enumerator = "0.2.12" termion = "4.0.5" diff --git a/README.md b/README.md index e24b217..97559ac 100644 --- a/README.md +++ b/README.md @@ -12,17 +12,11 @@ cargo install --git https://git.zumepro.cz/michal.prochazka/tulflash ## Flashing Intel HEX binaries ``` -# Linux -tulflash --port /dev/ttyUSBx write - -# macOS -tulflash --port /dev/tty.usbserial-xxx write - -# Wokňous -tulflash --port COMx write +# All OSes, automatically detects all ports +tulflash write ``` -If the flash fails, try to add `--slow` before the `--port` argument (should not be needed though). +If the flash fails, try to add `--slow` before `write` (should not be needed though). The tool also allows reading and erasing the chip, figure that out on your own if you need that (although `tulflash --help` might guide you if you ask it nicely). @@ -32,6 +26,6 @@ You'll need to clone this repo (or just [download the file](example.c)) and inst ``` sdcc example.c -tulflash --port ... write example.ihx +tulflash write example.ihx ``` diff --git a/src/args.rs b/src/args.rs index 17dc2e3..689c80f 100644 --- a/src/args.rs +++ b/src/args.rs @@ -6,8 +6,8 @@ use argp::FromArgs; #[argp(description = "tulflash - Flashing utility for the TUL Výukový Přípravek v1.1™")] pub struct Args { #[argp(option)] - #[argp(description = "Path to the serial port. Probably will be `/dev/ttyUSBx` on Linux, `/dev/tty.usbserial-xxx` on macOS or `COMx` on Windows.")] - pub port: OsString, + #[argp(description = "Path to the serial port if auto detection fails. Probably will be `/dev/ttyUSBx` on Linux, `/dev/tty.usbserial-xxx` on macOS or `COMx` on Windows.")] + pub port: Option, #[argp(switch)] #[argp(description = "Try this if it does not work the first time.")] diff --git a/src/main.rs b/src/main.rs index d62d9e5..440ee4f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -216,7 +216,17 @@ fn main() { let start = SystemTime::now(); - let mut target = PhysicalTarget::init(args.port, if args.slow { 57600 } else { 312500 }).unwrap(); + let port = args.port.or_else(|| { + for port in serial_enumerator::get_serial_list() { + if let Some(vendor) = port.vendor && vendor.as_str() == "TUL" { + return Some(port.name.into()) + } + } + + None + }).unwrap(); + + let mut target = PhysicalTarget::init(port, if args.slow { 57600 } else { 312500 }).unwrap(); let res = match args.command { ArgCommand::Read(args) => read_chip(args, &mut target),