sftool_lib/sf32lb52/
ram_command.rs1use crate::Result;
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;
6
7pub use crate::common::ram_command::{Command, DownloadStub, RamCommand, Response};
9
10impl RamCommand for SF32LB52Tool {
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 format_command(&self, cmd: &Command) -> String {
28 match cmd {
29 Command::EraseAll { address } => {
30 format!("burn_erase_all_factory 0x{address:08x}\r")
31 }
32 _ => cmd.to_string(),
33 }
34 }
35}
36
37impl DownloadStub for SF32LB52Tool {
38 fn download_stub(&mut self) -> Result<()> {
39 self.attempt_connect()?;
41 self.download_stub_impl()?;
42
43 sleep_with_cancel(
44 &self.base.cancel_token,
45 std::time::Duration::from_millis(100),
46 )?;
47 {
48 let mut io = for_tool(self);
49 io.clear(serialport::ClearBuffer::All)?;
50 }
51 self.debug_command(SifliUartCommand::Exit)?;
52
53 let is_sd = is_sd_memory(&self.base.memory_type);
55 let mut io = for_tool(self);
56 if is_sd {
57 RamOps::wait_for_shell_prompt(
59 &mut io,
60 b"sd0 OPEN success",
61 5000, 1, )
64 } else {
65 RamOps::wait_for_shell_prompt(
67 &mut io, b"msh >", 200, 5, )
70 }
71 }
72}