Skip to main content

sftool_lib/common/
speed.rs

1use crate::common::ram_command::{Command, RamCommand, RamOps};
2use crate::common::serial_io::{for_tool, sleep_with_cancel};
3use crate::{Result, SifliToolTrait};
4use std::time::Duration;
5
6/// 通用的速度设置操作实现
7pub struct SpeedOps;
8
9impl SpeedOps {
10    /// 设置串口速度的通用实现
11    pub fn set_speed<T>(tool: &mut T, speed: u32) -> Result<()>
12    where
13        T: SifliToolTrait + RamCommand,
14    {
15        // 发送设置波特率命令
16        tool.command(Command::SetBaud {
17            baud: speed,
18            delay: 10,
19        })?;
20
21        // 等待一段时间让设置生效
22        sleep_with_cancel(&tool.base().cancel_token, Duration::from_millis(50))?;
23
24        let mut io = for_tool(tool);
25        io.set_baud_rate(speed)?;
26        io.clear(serialport::ClearBuffer::All)?;
27        RamOps::wait_for_shell_prompt(&mut io, b"msh >", 200, 5)?;
28
29        Ok(())
30    }
31}