Skip to main content

sftool_lib/sf32lb57/
ram_command.rs

1use 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::sf32lb57::SF32LB57Tool;
6
7pub use crate::common::ram_command::{Command, DownloadStub, RamCommand, Response};
8
9impl RamCommand for SF32LB57Tool {
10    fn command(&mut self, cmd: Command) -> Result<Response> {
11        let cmd_string = self.format_command(&cmd);
12        let memory_type = self.base.memory_type.clone();
13        let mut io = for_tool(self);
14        RamOps::send_command_and_wait_response(&mut io, cmd, &cmd_string, memory_type.as_str())
15    }
16
17    fn send_data(&mut self, data: &[u8]) -> Result<Response> {
18        let config = CommandConfig {
19            compat_mode: self.base.compat,
20            ..Default::default()
21        };
22        let mut io = for_tool(self);
23        RamOps::send_data_and_wait_response(&mut io, data, &config)
24    }
25
26    fn format_command(&self, cmd: &Command) -> String {
27        match cmd {
28            Command::EraseAll { address } => {
29                format!("burn_erase_all_factory 0x{address:08x}\r")
30            }
31            _ => cmd.to_string(),
32        }
33    }
34}
35
36impl DownloadStub for SF32LB57Tool {
37    fn download_stub(&mut self) -> Result<()> {
38        self.attempt_connect()?;
39        self.download_stub_impl()?;
40
41        sleep_with_cancel(
42            &self.base.cancel_token,
43            std::time::Duration::from_millis(100),
44        )?;
45        {
46            let mut io = for_tool(self);
47            io.clear(serialport::ClearBuffer::All)?;
48        }
49        self.debug_command(SifliUartCommand::Exit)?;
50
51        let is_sd = is_sd_memory(&self.base.memory_type);
52        let mut io = for_tool(self);
53        if is_sd {
54            RamOps::wait_for_shell_prompt(&mut io, b"sd0 OPEN success", 5000, 1)
55        } else {
56            RamOps::wait_for_shell_prompt(&mut io, b"msh >", 200, 5)
57        }
58    }
59}