sftool_lib/sf32lb52/
ram_command.rs

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