sftool_lib/sf32lb52/
sifli_debug.rs

1use super::SF32LB52Tool;
2use crate::Result;
3use crate::common::sifli_debug::{
4    ChipFrameFormat, RecvError, START_WORD, SifliUartCommand, SifliUartResponse, common_debug,
5};
6use std::io::{BufReader, Read};
7
8// Re-export for the module
9pub use crate::common::sifli_debug::SifliDebug;
10
11// SF32LB52 specific frame format implementation
12pub struct SF32LB52FrameFormat;
13
14impl ChipFrameFormat for SF32LB52FrameFormat {
15    fn create_header(len: u16) -> Vec<u8> {
16        let mut header = vec![];
17        header.extend_from_slice(&START_WORD);
18        // SF32LB52 uses little-endian for data length
19        header.extend_from_slice(&len.to_le_bytes());
20        header.push(0x10);
21        header.push(0x00);
22        header
23    }
24
25    fn parse_frame_header(
26        reader: &mut BufReader<Box<dyn Read + Send>>,
27    ) -> std::result::Result<usize, RecvError> {
28        // 读取长度 (2字节) - SF32LB52 uses little-endian
29        let mut length_bytes = [0; 2];
30        if let Err(e) = reader.read_exact(&mut length_bytes) {
31            tracing::error!("Failed to read length bytes: {}", e);
32            return Err(RecvError::InvalidHeaderLength);
33        }
34
35        let payload_size = u16::from_le_bytes(length_bytes) as usize;
36
37        // 读取通道和CRC (2字节)
38        let mut channel_crc = [0; 2];
39        if let Err(e) = reader.read_exact(&mut channel_crc) {
40            tracing::error!("Failed to read channel and CRC bytes: {}", e);
41            return Err(RecvError::InvalidHeaderChannel);
42        }
43
44        Ok(payload_size)
45    }
46
47    fn encode_command_data(command: &SifliUartCommand) -> Vec<u8> {
48        let mut send_data = vec![];
49        match command {
50            SifliUartCommand::Enter => {
51                let temp = [0x41, 0x54, 0x53, 0x46, 0x33, 0x32, 0x05, 0x21];
52                send_data.extend_from_slice(&temp);
53            }
54            SifliUartCommand::Exit => {
55                let temp = [0x41, 0x54, 0x53, 0x46, 0x33, 0x32, 0x18, 0x21];
56                send_data.extend_from_slice(&temp);
57            }
58            SifliUartCommand::MEMRead { addr, len } => {
59                send_data.push(0x40);
60                send_data.push(0x72);
61                // SF32LB52 uses little-endian for address and length
62                send_data.extend_from_slice(&addr.to_le_bytes());
63                send_data.extend_from_slice(&len.to_le_bytes());
64            }
65            SifliUartCommand::MEMWrite { addr, data } => {
66                send_data.push(0x40);
67                send_data.push(0x77);
68                // SF32LB52 uses little-endian for address and data length
69                send_data.extend_from_slice(&addr.to_le_bytes());
70                send_data.extend_from_slice(&(data.len() as u16).to_le_bytes());
71                for d in data.iter() {
72                    send_data.extend_from_slice(&d.to_le_bytes());
73                }
74            }
75        }
76        send_data
77    }
78
79    fn decode_response_data(data: &[u8]) -> u32 {
80        // SF32LB52 uses little-endian
81        u32::from_le_bytes([data[0], data[1], data[2], data[3]])
82    }
83
84    // SF32LB52 uses no address mapping
85    fn map_address(addr: u32) -> u32 {
86        addr
87    }
88}
89
90impl crate::common::sifli_debug::SifliDebug for SF32LB52Tool {
91    fn debug_command(&mut self, command: SifliUartCommand) -> Result<SifliUartResponse> {
92        common_debug::debug_command_impl::<SF32LB52Tool, SF32LB52FrameFormat>(self, command)
93    }
94
95    fn debug_read_word32(&mut self, addr: u32) -> Result<u32> {
96        common_debug::debug_read_word32_impl::<SF32LB52Tool, SF32LB52FrameFormat>(self, addr)
97    }
98
99    fn debug_write_word32(&mut self, addr: u32, data: u32) -> Result<()> {
100        common_debug::debug_write_word32_impl::<SF32LB52Tool, SF32LB52FrameFormat>(self, addr, data)
101    }
102
103    fn debug_write_memory(&mut self, addr: u32, data: &[u8]) -> Result<()> {
104        common_debug::debug_write_memory_impl::<SF32LB52Tool, SF32LB52FrameFormat>(self, addr, data)
105    }
106
107    fn debug_write_core_reg(&mut self, reg: u16, data: u32) -> Result<()> {
108        common_debug::debug_write_core_reg_impl::<SF32LB52Tool, SF32LB52FrameFormat>(
109            self, reg, data,
110        )
111    }
112
113    fn debug_step(&mut self) -> Result<()> {
114        common_debug::debug_step_impl::<SF32LB52Tool, SF32LB52FrameFormat>(self)
115    }
116
117    fn debug_run(&mut self) -> Result<()> {
118        common_debug::debug_run_impl::<SF32LB52Tool, SF32LB52FrameFormat>(self)
119    }
120
121    fn debug_halt(&mut self) -> Result<()> {
122        common_debug::debug_halt_impl::<SF32LB52Tool, SF32LB52FrameFormat>(self)
123    }
124}