sftool_lib/sf32lb57/
ram_command.rs1use 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::sf32lb57::SF32LB57Tool;
6use crate::{Error, Result};
7
8pub use crate::common::ram_command::{Command, DownloadStub, RamCommand, Response};
9
10impl RamCommand for SF32LB57Tool {
11 fn command(&mut self, cmd: Command) -> Result<Response> {
12 let cmd_string = self.format_command(&cmd);
13 let memory_type = self.base.memory_type.clone();
14 let mut io = for_tool(self);
15 RamOps::send_command_and_wait_response(&mut io, cmd, &cmd_string, memory_type.as_str())
16 }
17
18 fn send_data(&mut self, data: &[u8]) -> Result<Response> {
19 let config = CommandConfig {
20 compat_mode: self.base.compat,
21 ..Default::default()
22 };
23 let mut io = for_tool(self);
24 RamOps::send_data_and_wait_response(&mut io, data, &config)
25 }
26
27 fn supports_debug_sequence_replacement(&mut self) -> Result<bool> {
28 if let Some(supported) = self.debug_sequence_replacement_supported {
29 return Ok(supported);
30 }
31
32 let supported = match self
33 .send_debug_sequence_replacement_info(&DebugSequenceReplacementInfo::empty())
34 {
35 Ok(Response::Ok) => true,
36 Ok(response) => {
37 tracing::debug!(?response, "RAM patch does not support burn_info");
38 false
39 }
40 Err(Error::Cancelled) => return Err(Error::Cancelled),
41 Err(error) => {
42 tracing::debug!(%error, "RAM patch does not support burn_info");
43 false
44 }
45 };
46 self.debug_sequence_replacement_supported = Some(supported);
47 Ok(supported)
48 }
49
50 fn send_debug_sequence_replacement_info(
51 &mut self,
52 info: &DebugSequenceReplacementInfo,
53 ) -> Result<Response> {
54 let data = info.encode()?;
55 let memory_type = self.base.memory_type.clone();
56 let mut io = for_tool(self);
57 RamOps::send_debug_sequence_replacement_info(&mut io, &data, memory_type.as_str())
58 }
59
60 fn format_command(&self, cmd: &Command) -> String {
61 match cmd {
62 Command::EraseAll { address } => {
63 format!("burn_erase_all_factory 0x{address:08x}\r")
64 }
65 _ => cmd.to_string(),
66 }
67 }
68}
69
70impl DownloadStub for SF32LB57Tool {
71 fn download_stub(&mut self) -> Result<()> {
72 self.debug_sequence_replacement_supported = None;
73 self.attempt_connect()?;
74 self.download_stub_impl()?;
75
76 sleep_with_cancel(
77 &self.base.cancel_token,
78 std::time::Duration::from_millis(100),
79 )?;
80 {
81 let mut io = for_tool(self);
82 io.clear(serialport::ClearBuffer::All)?;
83 }
84 self.debug_command(SifliUartCommand::Exit)?;
85
86 let is_sd = is_sd_memory(&self.base.memory_type);
87 let mut io = for_tool(self);
88 if is_sd {
89 RamOps::wait_for_shell_prompt(&mut io, b"sd0 OPEN success", 5000, 1)
90 } else {
91 RamOps::wait_for_shell_prompt(&mut io, b"msh >", 200, 5)
92 }
93 }
94}