Skip to main content

sftool_lib/sf32lb56/
ram_command.rs

1use crate::Result;
2use crate::common::ram_command::{CommandConfig, RamOps};
3use crate::common::sifli_debug::{SifliDebug, SifliUartCommand};
4use crate::sf32lb56::SF32LB56Tool;
5
6// 重新导出公共类型
7pub use crate::common::ram_command::{Command, DownloadStub, RamCommand, Response};
8
9impl RamCommand for SF32LB56Tool {
10    fn command(&mut self, cmd: Command) -> Result<Response> {
11        let cmd_string = self.format_command(&cmd);
12        RamOps::send_command_and_wait_response(
13            &mut self.port,
14            cmd,
15            &cmd_string,
16            self.base.memory_type.as_str(),
17        )
18    }
19
20    fn send_data(&mut self, data: &[u8]) -> Result<Response> {
21        let config = CommandConfig {
22            compat_mode: self.base.compat,
23            ..Default::default()
24        };
25        RamOps::send_data_and_wait_response(&mut self.port, data, &config)
26    }
27}
28
29impl DownloadStub for SF32LB56Tool {
30    fn download_stub(&mut self) -> Result<()> {
31        // Use SifliTool trait methods
32        self.attempt_connect()?;
33        self.download_stub_impl()?;
34
35        std::thread::sleep(std::time::Duration::from_millis(100));
36        self.port.clear(serialport::ClearBuffer::All)?;
37        self.debug_command(SifliUartCommand::Exit)?;
38
39        // 根据memory_type选择不同的等待条件
40        if self.base.memory_type == "sd" {
41            // SD卡模式:等待 "sd0 OPEN success",超时5秒
42            RamOps::wait_for_shell_prompt(
43                &mut self.port,
44                b"sd0 OPEN success",
45                1000, // 1秒间隔
46                5,    // 最多重试5次 (总计5秒)
47            )
48        } else {
49            // 非SD模式:等待shell提示符 "msh >"
50            RamOps::wait_for_shell_prompt(
51                &mut self.port,
52                b"msh >",
53                200, // 200ms间隔
54                5,   // 最多重试5次
55            )
56        }
57    }
58}