sftool_lib/sf32lb57/
sifli_debug.rs1use super::SF32LB57Tool;
2use crate::Result;
3use crate::common::serial_io::is_cancelled_io_error;
4use crate::common::sifli_debug::{
5 ChipFrameFormat, RecvError, START_WORD, SifliUartCommand, SifliUartResponse, common_debug,
6};
7use std::io::{BufReader, Read};
8
9pub use crate::common::sifli_debug::SifliDebug;
10
11pub struct SF32LB57FrameFormat;
12
13impl ChipFrameFormat for SF32LB57FrameFormat {
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());
18 header.push(0x10);
19 header.push(0x00);
20 header
21 }
22
23 fn parse_frame_header<R: Read>(
24 reader: &mut BufReader<R>,
25 ) -> std::result::Result<usize, RecvError> {
26 let mut length_bytes = [0; 2];
27 if let Err(e) = reader.read_exact(&mut length_bytes) {
28 if is_cancelled_io_error(&e) {
29 return Err(RecvError::Cancelled);
30 }
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 let mut channel_crc = [0; 2];
38 if let Err(e) = reader.read_exact(&mut channel_crc) {
39 if is_cancelled_io_error(&e) {
40 return Err(RecvError::Cancelled);
41 }
42 tracing::error!("Failed to read channel and CRC bytes: {}", e);
43 return Err(RecvError::InvalidHeaderChannel);
44 }
45
46 Ok(payload_size)
47 }
48
49 fn encode_command_data(command: &SifliUartCommand) -> Vec<u8> {
50 let mut send_data = vec![];
51 match command {
52 SifliUartCommand::Enter => {
53 let temp = [0x41, 0x54, 0x53, 0x46, 0x33, 0x32, 0x05, 0x21];
54 send_data.extend_from_slice(&temp);
55 }
56 SifliUartCommand::Exit => {
57 let temp = [0x41, 0x54, 0x53, 0x46, 0x33, 0x32, 0x18, 0x21];
58 send_data.extend_from_slice(&temp);
59 }
60 SifliUartCommand::MEMRead { addr, len } => {
61 send_data.push(0x40);
62 send_data.push(0x72);
63 send_data.extend_from_slice(&addr.to_le_bytes());
64 send_data.extend_from_slice(&len.to_le_bytes());
65 }
66 SifliUartCommand::MEMWrite { addr, data } => {
67 send_data.push(0x40);
68 send_data.push(0x77);
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 u32::from_le_bytes([data[0], data[1], data[2], data[3]])
81 }
82
83 fn map_address(addr: u32) -> u32 {
84 addr
85 }
86}
87
88impl crate::common::sifli_debug::SifliDebug for SF32LB57Tool {
89 fn debug_command(&mut self, command: SifliUartCommand) -> Result<SifliUartResponse> {
90 common_debug::debug_command_impl::<SF32LB57Tool, SF32LB57FrameFormat>(self, command)
91 }
92
93 fn debug_read_word32(&mut self, addr: u32) -> Result<u32> {
94 common_debug::debug_read_word32_impl::<SF32LB57Tool, SF32LB57FrameFormat>(self, addr)
95 }
96
97 fn debug_write_word32(&mut self, addr: u32, data: u32) -> Result<()> {
98 common_debug::debug_write_word32_impl::<SF32LB57Tool, SF32LB57FrameFormat>(self, addr, data)
99 }
100
101 fn debug_write_memory(&mut self, addr: u32, data: &[u8]) -> Result<()> {
102 common_debug::debug_write_memory_impl::<SF32LB57Tool, SF32LB57FrameFormat>(self, addr, data)
103 }
104
105 fn debug_write_core_reg(&mut self, reg: u16, data: u32) -> Result<()> {
106 common_debug::debug_write_core_reg_impl::<SF32LB57Tool, SF32LB57FrameFormat>(
107 self, reg, data,
108 )
109 }
110
111 fn debug_step(&mut self) -> Result<()> {
112 common_debug::debug_step_impl::<SF32LB57Tool, SF32LB57FrameFormat>(self)
113 }
114
115 fn debug_run(&mut self) -> Result<()> {
116 common_debug::debug_run_impl::<SF32LB57Tool, SF32LB57FrameFormat>(self)
117 }
118
119 fn debug_halt(&mut self) -> Result<()> {
120 common_debug::debug_halt_impl::<SF32LB57Tool, SF32LB57FrameFormat>(self)
121 }
122}