Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 68 additions & 5 deletions crates/luatos-cli/src/cmd_flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::Context;

use crate::{
event::{self, MessageLevel},
OutputFormat,
Ec718PortMode, OutputFormat,
};

/// 检查脚本镜像大小是否超过分区容量,超出则报错并给出详细信息
Expand All @@ -24,9 +24,13 @@ fn check_script_size(image_len: usize, partition_size: usize) -> anyhow::Result<
}

pub fn cmd_flash_run(
soc: &str,
soc: Option<&str>,
binpkg: Option<&str>,
port: &str,
baud: Option<u32>,
agent_baud: Option<u32>,
at_baud: Option<u32>,
port_mode: Ec718PortMode,
script_folders: Option<&[String]>,
step: u8,
format: &OutputFormat,
Expand All @@ -46,9 +50,58 @@ pub fn cmd_flash_run(

let on_progress = make_progress_callback(format, "flash.run", step);

if let Some(binpkg) = binpkg {
if soc.is_some() {
anyhow::bail!("--soc and --binpkg are mutually exclusive");
}
if baud.is_some() {
anyhow::bail!("--baud is only supported with --soc flashing");
}
if script_folders.is_some() {
anyhow::bail!("--script is only supported with --soc flashing");
}
if reset_config.is_some() {
anyhow::bail!("--auto-reset is not supported with EC718 --binpkg flashing");
}

let (boot_port, forced_port_type) = match port_mode {
Ec718PortMode::Auto => (luatos_flash::ec718::auto_enter_boot_mode(Some(port), &on_progress)?, None),
Ec718PortMode::Usb => (
luatos_flash::ec718::auto_enter_boot_mode(Some(port), &on_progress)?,
Some(luatos_flash::ec718::Ec718PortType::Usb),
),
Ec718PortMode::Uart => {
on_progress(&luatos_flash::FlashProgress::info("Fallback", 2.0, &format!("使用用户指定端口: {} (UART模式)", port)));
(port.to_string(), Some(luatos_flash::ec718::Ec718PortType::Uart))
}
};
luatos_flash::ec718::flash_ec718_binpkg(binpkg, &boot_port, forced_port_type, agent_baud, at_baud, &on_progress, cancel)?;
match format {
OutputFormat::Text => {
println!("EC718 BINPKG flash completed successfully.");
}
OutputFormat::Json | OutputFormat::Jsonl => event::emit_result(format, "flash.run", "ok", serde_json::json!({ "chip": "ec7xx", "format": "binpkg" }))?,
}
return Ok(());
}

let soc = soc.ok_or_else(|| anyhow::anyhow!("Either --soc or --binpkg must be specified"))?;
// Detect chip type from SOC info.json
let info = luatos_soc::read_soc_info(soc)?;
let chip = info.chip.chip_type.as_str();
let is_ec718_chip = matches!(chip, "ec7xx" | "air8000" | "air780epm" | "air780ehm" | "air780ehv" | "air780ehg");

if !is_ec718_chip {
if agent_baud.is_some() {
anyhow::bail!("--agent-baud/--agbaud is only supported with EC718 flashing");
}
if at_baud.is_some() {
anyhow::bail!("--at-baud is only supported with EC718 flashing");
}
if port_mode != Ec718PortMode::Auto {
anyhow::bail!("--port-mode is only supported with EC718 flashing");
}
}

match chip {
"bk72xx" | "air8101" => {
Expand Down Expand Up @@ -86,8 +139,18 @@ pub fn cmd_flash_run(
}
"ec7xx" | "air8000" | "air780epm" | "air780ehm" | "air780ehv" | "air780ehg" => {
// EC718 series: auto-detect boot mode, reboot if needed
let boot_port = luatos_flash::ec718::auto_enter_boot_mode(Some(port), &on_progress)?;
luatos_flash::ec718::flash_ec718(soc, &boot_port, &on_progress, cancel)?;
let (boot_port, forced_port_type) = match port_mode {
Ec718PortMode::Auto => (luatos_flash::ec718::auto_enter_boot_mode(Some(port), &on_progress)?, None),
Ec718PortMode::Usb => (
luatos_flash::ec718::auto_enter_boot_mode(Some(port), &on_progress)?,
Some(luatos_flash::ec718::Ec718PortType::Usb),
),
Ec718PortMode::Uart => {
on_progress(&luatos_flash::FlashProgress::info("Fallback", 2.0, &format!("使用用户指定端口: {} (UART模式)", port)));
(port.to_string(), Some(luatos_flash::ec718::Ec718PortType::Uart))
}
};
luatos_flash::ec718::flash_ec718(soc, &boot_port, forced_port_type, agent_baud, at_baud, &on_progress, cancel)?;
match format {
OutputFormat::Text => {
println!("EC718 flash completed successfully.");
Expand Down Expand Up @@ -428,7 +491,7 @@ pub fn cmd_flash_test(
"ec7xx" | "air8000" | "air780epm" | "air780ehm" | "air780ehv" | "air780ehg" => {
let on_progress2 = make_progress_callback(format, "flash.test", step);
let boot_port = luatos_flash::ec718::auto_enter_boot_mode(Some(port), &on_progress2)?;
luatos_flash::ec718::flash_ec718(soc, &boot_port, &on_progress2, cancel.clone())?;
luatos_flash::ec718::flash_ec718(soc, &boot_port, None, None, None, &on_progress2, cancel.clone())?;
Vec::new()
}
_ => {
Expand Down
47 changes: 44 additions & 3 deletions crates/luatos-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
// luatos-cli soc info <path> # Show SOC file info
// luatos-cli soc unpack <path> -o dir # Extract SOC file
// luatos-cli flash run --soc <path> --port COM6
// luatos-cli flash run --binpkg <path> --port COM6
// luatos-cli flash test --soc <path> --port COM6

use clap::{Parser, Subcommand};
use clap::{ArgGroup, Parser, Subcommand};

mod cmd_build;
mod cmd_device;
Expand Down Expand Up @@ -40,6 +41,13 @@ enum OutputFormat {
Jsonl,
}

#[derive(Clone, Copy, PartialEq, clap::ValueEnum)]
pub(crate) enum Ec718PortMode {
Auto,
Usb,
Uart,
}

#[derive(Subcommand)]
enum Commands {
/// Serial port tools
Expand Down Expand Up @@ -170,16 +178,33 @@ impl SignalLevel {
#[derive(Subcommand)]
enum FlashCommands {
/// Full firmware flash (ROM + optional script)
#[command(group(
ArgGroup::new("firmware")
.required(true)
.args(["soc", "binpkg"])
))]
Run {
/// Path to .soc file
#[arg(long)]
soc: String,
soc: Option<String>,
/// Path to EC718 .binpkg file
#[arg(long)]
binpkg: Option<String>,
/// Serial port (e.g. COM6)
#[arg(long)]
port: String,
/// Override baud rate
#[arg(long)]
baud: Option<u32>,
/// EC718 agentboot/download baud override (default: UART 921600, USB unchanged)
#[arg(long = "agent-baud", visible_alias = "agbaud", alias = "force-br")]
agent_baud: Option<u32>,
/// EC718 UART1 AT reset baud override (default: 115200)
#[arg(long = "at-baud")]
at_baud: Option<u32>,
/// EC718 download port mode for --binpkg/--soc
#[arg(long, value_enum, default_value = "auto")]
port_mode: Ec718PortMode,
/// Script folder (optional, can specify multiple)
#[arg(long)]
script: Vec<String>,
Expand Down Expand Up @@ -638,8 +663,12 @@ fn main() {
Commands::Flash { action, progress_step } => match action {
FlashCommands::Run {
soc,
binpkg,
port,
baud,
agent_baud,
at_baud,
port_mode,
script,
auto_reset,
dtr_boot,
Expand All @@ -659,7 +688,19 @@ fn main() {
} else {
None
};
cmd_flash::cmd_flash_run(&soc, &port, baud, script_opt, progress_step, &cli.format, reset_config)
cmd_flash::cmd_flash_run(
soc.as_deref(),
binpkg.as_deref(),
&port,
baud,
agent_baud,
at_baud,
port_mode,
script_opt,
progress_step,
&cli.format,
reset_config,
)
}
FlashCommands::Script {
soc,
Expand Down
Loading