-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.rs
63 lines (53 loc) · 1.7 KB
/
runner.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! Simple Teensy 4 program loader, for use as a Cargo runner.
//!
//! # Dependencies
//!
//! - `rust-objcopy`
//! - `teensy_loader_cli`
//!
//! See the teensy4-rs documentation for more information.
use std::{env, error, path::PathBuf, process::Command};
/// Loader configurations.
///
/// You may override these values using environment variables.
struct Configuration {
/// `objcopy` program name.
objcopy: String,
/// `teensy_loader_cli` program name.
loader: String,
}
impl Configuration {
fn new() -> Self {
let objcopy = env::var("TEENSY4RS_OBJCOPY").unwrap_or_else(|_| "rust-objcopy".into());
// MacOS
// let loader = env::var("TEENSY4RS_LOADER")
// .unwrap_or_else(|_| "./teensy_loader_cli/teensy_loader_cli_macos".into());
// Windows
// let loader = env::var("TEENSY4RS_LOADER")
// .unwrap_or_else(|_| "./teensy_loader_cli/teensy_loader_cli_windows.exe".into());
// Linux
let loader = env::var("TEENSY4RS_LOADER")
.unwrap_or_else(|_| "./teensy_loader_cli/teensy_loader_cli_linux".into());
Self { objcopy, loader }
}
}
fn main() -> Result<(), Box<dyn error::Error>> {
let elf_path = env::args()
.nth(1)
.map(PathBuf::from)
.ok_or("Supply the path to a Teensy 4 ELF program")?;
let mut hex_path = elf_path.clone();
hex_path.set_extension("hex");
let cfg = Configuration::new();
Command::new(cfg.objcopy)
.args(["-O", "ihex"])
.arg(&elf_path)
.arg(&hex_path)
.output()?;
Command::new(cfg.loader)
.args(["-w", "-v", "--mcu=imxrt1062"])
.arg(&hex_path)
.spawn()?
.wait()?;
Ok(())
}