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