Skip to main content

sftool_lib/sf32lb56/
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::sf32lb56::SF32LB56Tool;
6
7// 重新导出公共类型
8pub use crate::common::ram_command::{Command, DownloadStub, RamCommand, Response};
9
10impl RamCommand for SF32LB56Tool {
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
28impl DownloadStub for SF32LB56Tool {
29    fn download_stub(&mut self) -> Result<()> {
30        // Use SifliTool trait methods
31        self.attempt_connect()?;
32        self.download_stub_impl()?;
33
34        sleep_with_cancel(
35            &self.base.cancel_token,
36            std::time::Duration::from_millis(100),
37        )?;
38        {
39            let mut io = for_tool(self);
40            io.clear(serialport::ClearBuffer::All)?;
41        }
42        self.debug_command(SifliUartCommand::Exit)?;
43
44        // 根据memory_type选择不同的等待条件
45        let is_sd = is_sd_memory(&self.base.memory_type);
46        let mut io = for_tool(self);
47        if is_sd {
48            // SD卡模式:等待 "sd0 OPEN success",超时5秒
49            RamOps::wait_for_shell_prompt(
50                &mut io,
51                b"sd0 OPEN success",
52                1000, // 1秒间隔
53                5,    // 最多重试5次 (总计5秒)
54            )
55        } else {
56            // 非SD模式:等待shell提示符 "msh >"
57            RamOps::wait_for_shell_prompt(
58                &mut io, b"msh >", 200, // 200ms间隔
59                5,   // 最多重试5次
60            )
61        }
62    }
63}