sftool_lib/sf32lb52/
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::sf32lb52::SF32LB52Tool;
6use crate::{Error, Result};
7
8pub 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 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 let is_sd = is_sd_memory(&self.base.memory_type);
90 let mut io = for_tool(self);
91 if is_sd {
92 RamOps::wait_for_shell_prompt(
94 &mut io,
95 b"sd0 OPEN success",
96 5000, 1, )
99 } else {
100 RamOps::wait_for_shell_prompt(
102 &mut io, b"msh >", 200, 5, )
105 }
106 }
107}