Skip to main content

sftool_lib/sf32lb52/
ram_command.rs

1use crate::common::debug_sequence::DebugSequenceReplacementInfo;
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;
6use crate::{Error, Result};
7
8// 重新导出公共类型,保持向后兼容
9pub use crate::common::ram_command::{Command, DownloadStub, RamCommand, Response};
10
11impl RamCommand for SF32LB52Tool {
12    fn command(&mut self, cmd: Command) -> Result<Response> {
13        let cmd_string = self.format_command(&cmd);
14        let memory_type = self.base.memory_type.clone();
15        let mut io = for_tool(self);
16        RamOps::send_command_and_wait_response(&mut io, cmd, &cmd_string, memory_type.as_str())
17    }
18
19    fn send_data(&mut self, data: &[u8]) -> Result<Response> {
20        let config = CommandConfig {
21            compat_mode: self.base.compat,
22            ..Default::default()
23        };
24        let mut io = for_tool(self);
25        RamOps::send_data_and_wait_response(&mut io, data, &config)
26    }
27
28    fn supports_debug_sequence_replacement(&mut self) -> Result<bool> {
29        if let Some(supported) = self.debug_sequence_replacement_supported {
30            return Ok(supported);
31        }
32
33        let supported = match self
34            .send_debug_sequence_replacement_info(&DebugSequenceReplacementInfo::empty())
35        {
36            Ok(Response::Ok) => true,
37            Ok(response) => {
38                tracing::debug!(?response, "RAM patch does not support burn_info");
39                false
40            }
41            Err(Error::Cancelled) => return Err(Error::Cancelled),
42            Err(error) => {
43                tracing::debug!(%error, "RAM patch does not support burn_info");
44                false
45            }
46        };
47        self.debug_sequence_replacement_supported = Some(supported);
48        Ok(supported)
49    }
50
51    fn send_debug_sequence_replacement_info(
52        &mut self,
53        info: &DebugSequenceReplacementInfo,
54    ) -> Result<Response> {
55        let data = info.encode()?;
56        let memory_type = self.base.memory_type.clone();
57        let mut io = for_tool(self);
58        RamOps::send_debug_sequence_replacement_info(&mut io, &data, memory_type.as_str())
59    }
60
61    fn format_command(&self, cmd: &Command) -> String {
62        match cmd {
63            Command::EraseAll { address } => {
64                format!("burn_erase_all_factory 0x{address:08x}\r")
65            }
66            _ => cmd.to_string(),
67        }
68    }
69}
70
71impl DownloadStub for SF32LB52Tool {
72    fn download_stub(&mut self) -> Result<()> {
73        self.debug_sequence_replacement_supported = None;
74        // Use SifliTool trait methods
75        self.attempt_connect()?;
76        self.download_stub_impl()?;
77
78        sleep_with_cancel(
79            &self.base.cancel_token,
80            std::time::Duration::from_millis(100),
81        )?;
82        {
83            let mut io = for_tool(self);
84            io.clear(serialport::ClearBuffer::All)?;
85        }
86        self.debug_command(SifliUartCommand::Exit)?;
87
88        // 根据memory_type选择不同的等待条件
89        let is_sd = is_sd_memory(&self.base.memory_type);
90        let mut io = for_tool(self);
91        if is_sd {
92            // SD卡模式:等待 "sd0 OPEN success",超时5秒
93            RamOps::wait_for_shell_prompt(
94                &mut io,
95                b"sd0 OPEN success",
96                5000, // 5秒间隔
97                1,    // 最多重试1次 (总计5秒)
98            )
99        } else {
100            // 非SD模式:等待shell提示符 "msh >"
101            RamOps::wait_for_shell_prompt(
102                &mut io, b"msh >", 200, // 200ms间隔
103                5,   // 最多重试5次
104            )
105        }
106    }
107}