Skip to main content

sftool_lib/sf32lb52/
ram_command.rs

1use crate::Result;
2use crate::common::ram_command::{CommandConfig, RamOps, is_sd_memory};
3use crate::common::serial_io::{for_tool, sleep_with_cancel};
4use crate::common::sifli_debug::{SifliDebug, SifliUartCommand};
5use crate::sf32lb52::SF32LB52Tool;
6
7// 重新导出公共类型,保持向后兼容
8pub use crate::common::ram_command::{Command, DownloadStub, RamCommand, Response};
9
10impl RamCommand for SF32LB52Tool {
11    fn command(&mut self, cmd: Command) -> Result<Response> {
12        let cmd_string = self.format_command(&cmd);
13        let memory_type = self.base.memory_type.clone();
14        let mut io = for_tool(self);
15        RamOps::send_command_and_wait_response(&mut io, cmd, &cmd_string, memory_type.as_str())
16    }
17
18    fn send_data(&mut self, data: &[u8]) -> Result<Response> {
19        let config = CommandConfig {
20            compat_mode: self.base.compat,
21            ..Default::default()
22        };
23        let mut io = for_tool(self);
24        RamOps::send_data_and_wait_response(&mut io, data, &config)
25    }
26
27    fn format_command(&self, cmd: &Command) -> String {
28        match cmd {
29            Command::EraseAll { address } => {
30                format!("burn_erase_all_factory 0x{address:08x}\r")
31            }
32            _ => cmd.to_string(),
33        }
34    }
35}
36
37impl DownloadStub for SF32LB52Tool {
38    fn download_stub(&mut self) -> Result<()> {
39        // Use SifliTool trait methods
40        self.attempt_connect()?;
41        self.download_stub_impl()?;
42
43        sleep_with_cancel(
44            &self.base.cancel_token,
45            std::time::Duration::from_millis(100),
46        )?;
47        {
48            let mut io = for_tool(self);
49            io.clear(serialport::ClearBuffer::All)?;
50        }
51        self.debug_command(SifliUartCommand::Exit)?;
52
53        // 根据memory_type选择不同的等待条件
54        let is_sd = is_sd_memory(&self.base.memory_type);
55        let mut io = for_tool(self);
56        if is_sd {
57            // SD卡模式:等待 "sd0 OPEN success",超时5秒
58            RamOps::wait_for_shell_prompt(
59                &mut io,
60                b"sd0 OPEN success",
61                5000, // 5秒间隔
62                1,    // 最多重试1次 (总计5秒)
63            )
64        } else {
65            // 非SD模式:等待shell提示符 "msh >"
66            RamOps::wait_for_shell_prompt(
67                &mut io, b"msh >", 200, // 200ms间隔
68                5,   // 最多重试5次
69            )
70        }
71    }
72}