Skip to main content

sftool_lib/sf32lb52/
ram_command.rs

1use crate::Result;
2use crate::common::ram_command::{CommandConfig, RamOps};
3use crate::common::sifli_debug::{SifliDebug, SifliUartCommand};
4use crate::sf32lb52::SF32LB52Tool;
5
6// 重新导出公共类型,保持向后兼容
7pub use crate::common::ram_command::{Command, DownloadStub, RamCommand, Response};
8
9impl RamCommand for SF32LB52Tool {
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    fn format_command(&self, cmd: &Command) -> String {
29        match cmd {
30            Command::EraseAll { address } => {
31                format!("burn_erase_all_factory 0x{address:08x}\r")
32            }
33            _ => cmd.to_string(),
34        }
35    }
36}
37
38impl DownloadStub for SF32LB52Tool {
39    fn download_stub(&mut self) -> Result<()> {
40        // Use SifliTool trait methods
41        self.attempt_connect()?;
42        self.download_stub_impl()?;
43
44        std::thread::sleep(std::time::Duration::from_millis(100));
45        self.port.clear(serialport::ClearBuffer::All)?;
46        self.debug_command(SifliUartCommand::Exit)?;
47
48        // 根据memory_type选择不同的等待条件
49        if self.base.memory_type == "sd" {
50            // SD卡模式:等待 "sd0 OPEN success",超时5秒
51            RamOps::wait_for_shell_prompt(
52                &mut self.port,
53                b"sd0 OPEN success",
54                5000, // 5秒间隔
55                1,    // 最多重试1次 (总计5秒)
56            )
57        } else {
58            // 非SD模式:等待shell提示符 "msh >"
59            RamOps::wait_for_shell_prompt(
60                &mut self.port,
61                b"msh >",
62                200, // 200ms间隔
63                5,   // 最多重试5次
64            )
65        }
66    }
67}