Implemented auto port detection

This commit is contained in:
2025-09-19 11:14:13 +02:00
parent d298db44d2
commit 1dbbaab197
5 changed files with 129 additions and 13 deletions

111
Cargo.lock generated
View File

@@ -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"

View File

@@ -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"

View File

@@ -12,17 +12,11 @@ cargo install --git https://git.zumepro.cz/michal.prochazka/tulflash
## Flashing Intel HEX binaries
```
# Linux
tulflash --port /dev/ttyUSBx write <path-to-hex>
# macOS
tulflash --port /dev/tty.usbserial-xxx write <path-to-hex>
# Wokňous
tulflash --port COMx write <path-to-hex>
# All OSes, automatically detects all ports
tulflash write <path-to-hex>
```
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
```

View File

@@ -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<OsString>,
#[argp(switch)]
#[argp(description = "Try this if it does not work the first time.")]

View File

@@ -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),