Skip to main content

sftool_lib/sf32lb56/
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::sf32lb56::SF32LB56Tool;
6use crate::{Error, Result};
7
8// 重新导出公共类型
9pub use crate::common::ram_command::{Command, DownloadStub, RamCommand, Response};
10
11impl RamCommand for SF32LB56Tool {
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
62impl DownloadStub for SF32LB56Tool {
63    fn download_stub(&mut self) -> Result<()> {
64        self.debug_sequence_replacement_supported = None;
65        // Use SifliTool trait methods
66        self.attempt_connect()?;
67        self.download_stub_impl()?;
68
69        sleep_with_cancel(
70            &self.base.cancel_token,
71            std::time::Duration::from_millis(100),
72        )?;
73        {
74            let mut io = for_tool(self);
75            io.clear(serialport::ClearBuffer::All)?;
76        }
77        self.debug_command(SifliUartCommand::Exit)?;
78
79        // 根据memory_type选择不同的等待条件
80        let is_sd = is_sd_memory(&self.base.memory_type);
81        let mut io = for_tool(self);
82        if is_sd {
83            // SD卡模式:等待 "sd0 OPEN success",超时5秒
84            RamOps::wait_for_shell_prompt(
85                &mut io,
86                b"sd0 OPEN success",
87                1000, // 1秒间隔
88                5,    // 最多重试5次 (总计5秒)
89            )
90        } else {
91            // 非SD模式:等待shell提示符 "msh >"
92            RamOps::wait_for_shell_prompt(
93                &mut io, b"msh >", 200, // 200ms间隔
94                5,   // 最多重试5次
95            )
96        }
97    }
98}