sftool-lib 0.2.3

SiFli SoC serial utility library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! SF32LB56 芯片特定实现模块

pub mod erase_flash;
pub mod ram_command;
pub mod read_flash;
pub mod reset;
pub mod sifli_debug;
pub mod speed;
pub mod write_flash;

use crate::common::serial_io::is_cancelled_io_error;
use crate::common::serial_io::{for_tool, sleep_with_cancel};
use crate::common::sifli_debug::{
    ChipFrameFormat, RecvError, START_WORD, SifliDebug, SifliUartCommand, SifliUartResponse,
    common_debug,
};
use crate::progress::{
    EraseFlashStyle, EraseRegionStyle, ProgressOperation, ProgressStatus, StubStage,
};
use crate::sf32lb56::ram_command::DownloadStub;
use crate::{Result, SifliTool, SifliToolBase, SifliToolTrait};
use serialport::SerialPort;
use std::io::{BufReader, Read};
use std::time::Duration;

// Define SF32LB56FrameFormat here to avoid import issues
pub struct SF32LB56FrameFormat;

impl ChipFrameFormat for SF32LB56FrameFormat {
    fn create_header(len: u16) -> Vec<u8> {
        let mut header = vec![];
        header.extend_from_slice(&START_WORD);
        // SF32LB56 uses big-endian for data length
        header.extend_from_slice(&len.to_be_bytes());
        // SF32LB56 adds 4-byte timestamp (fixed to 0)
        header.extend_from_slice(&[0x00, 0x00, 0x00, 0x00]);
        // Channel number fixed to 0x10
        header.push(0x10);
        // CRC fixed to 0x00
        header.push(0x00);
        // reserved field (2 bytes, fixed to 0x00)
        header.extend_from_slice(&[0x00, 0x00]);
        header
    }

    fn parse_frame_header<R: Read>(
        reader: &mut BufReader<R>,
    ) -> std::result::Result<usize, RecvError> {
        // 读取长度 (2字节) - SF32LB56 uses big-endian
        let mut length_bytes = [0; 2];
        if let Err(e) = reader.read_exact(&mut length_bytes) {
            if is_cancelled_io_error(&e) {
                return Err(RecvError::Cancelled);
            }
            tracing::error!("Failed to read length bytes: {}", e);
            return Err(RecvError::InvalidHeaderLength);
        }

        let payload_size = u16::from_be_bytes(length_bytes) as usize;

        // 读取时间戳 (4字节) - SF32LB56 specific
        let mut timestamp_bytes = [0; 4];
        if let Err(e) = reader.read_exact(&mut timestamp_bytes) {
            if is_cancelled_io_error(&e) {
                return Err(RecvError::Cancelled);
            }
            tracing::error!("Failed to read timestamp bytes: {}", e);
            return Err(RecvError::InvalidHeaderChannel);
        }

        // 读取通道和CRC (2字节)
        let mut channel_crc = [0; 2];
        if let Err(e) = reader.read_exact(&mut channel_crc) {
            if is_cancelled_io_error(&e) {
                return Err(RecvError::Cancelled);
            }
            tracing::error!("Failed to read channel and CRC bytes: {}", e);
            return Err(RecvError::InvalidHeaderChannel);
        }

        // SF32LB56: Read 2-byte reserved field
        let mut reserved_bytes = [0; 2];
        if let Err(e) = reader.read_exact(&mut reserved_bytes) {
            if is_cancelled_io_error(&e) {
                return Err(RecvError::Cancelled);
            }
            tracing::error!("Failed to read reserved bytes: {}", e);
            return Err(RecvError::ReadError(e));
        }

        Ok(payload_size)
    }

    fn encode_command_data(command: &SifliUartCommand) -> Vec<u8> {
        let mut send_data = vec![];
        match command {
            SifliUartCommand::Enter => {
                let temp = [0x41, 0x54, 0x53, 0x46, 0x33, 0x32, 0x05, 0x21];
                send_data.extend_from_slice(&temp);
            }
            SifliUartCommand::Exit => {
                let temp = [0x41, 0x54, 0x53, 0x46, 0x33, 0x32, 0x18, 0x21];
                send_data.extend_from_slice(&temp);
            }
            SifliUartCommand::MEMRead { addr, len } => {
                send_data.push(0x40);
                send_data.push(0x72);
                // SF32LB56 uses big-endian for address and length
                send_data.extend_from_slice(&addr.to_be_bytes());
                send_data.extend_from_slice(&len.to_be_bytes());
            }
            SifliUartCommand::MEMWrite { addr, data } => {
                send_data.push(0x40);
                send_data.push(0x77);
                // SF32LB56 uses big-endian for address and data length
                send_data.extend_from_slice(&addr.to_be_bytes());
                send_data.extend_from_slice(&(data.len() as u16).to_be_bytes());
                for d in data.iter() {
                    send_data.extend_from_slice(&d.to_be_bytes());
                }
            }
        }
        send_data
    }

    fn decode_response_data(data: &[u8]) -> u32 {
        // SF32LB56 uses big-endian
        u32::from_be_bytes([data[0], data[1], data[2], data[3]])
    }

    // SF32LB56 specific address mapping function
    fn map_address(addr: u32) -> u32 {
        let mut l_addr = addr;
        if (0xE0000000..0xF0000000).contains(&addr) {
            l_addr = (addr & 0x0fffffff) | 0xF0000000;
            // l_addr = addr
        } else if (0x00400000..=0x0041FFFF).contains(&addr) {
            // L_RAM
            l_addr += 0x20000000;
        } else if (0x20C00000..=0x20C1FFFF).contains(&addr) {
            // L_RAM
            l_addr -= 0x00800000;
        } else if (0x20000000..=0x200C7FFF).contains(&addr) {
            // H_RAM
            l_addr += 0x0A000000;
        } else if (0x20800000..=0x20BFFFFF).contains(&addr) {
            // H_RAM
            l_addr -= 0x20800000;
        } else if (0x10000000..=0x1FFFFFFF).contains(&addr) {
            // EXT_MEM
            l_addr += 0x50000000;
        }
        l_addr
    }
}

pub struct SF32LB56Tool {
    pub base: SifliToolBase,
    pub port: Box<dyn SerialPort>,
}

// 为 SF32LB56Tool 实现 Send 和 Sync
unsafe impl Send for SF32LB56Tool {}
unsafe impl Sync for SF32LB56Tool {}

// SifliDebug trait implementation for SF32LB56Tool
impl SifliDebug for SF32LB56Tool {
    fn debug_command(&mut self, command: SifliUartCommand) -> Result<SifliUartResponse> {
        common_debug::debug_command_impl::<SF32LB56Tool, SF32LB56FrameFormat>(self, command)
    }

    fn debug_read_word32(&mut self, addr: u32) -> Result<u32> {
        common_debug::debug_read_word32_impl::<SF32LB56Tool, SF32LB56FrameFormat>(self, addr)
    }

    fn debug_write_word32(&mut self, addr: u32, data: u32) -> Result<()> {
        common_debug::debug_write_word32_impl::<SF32LB56Tool, SF32LB56FrameFormat>(self, addr, data)
    }

    fn debug_write_memory(&mut self, addr: u32, data: &[u8]) -> Result<()> {
        common_debug::debug_write_memory_impl::<SF32LB56Tool, SF32LB56FrameFormat>(self, addr, data)
    }

    fn debug_write_core_reg(&mut self, reg: u16, data: u32) -> Result<()> {
        common_debug::debug_write_core_reg_impl::<SF32LB56Tool, SF32LB56FrameFormat>(
            self, reg, data,
        )
    }

    fn debug_step(&mut self) -> Result<()> {
        common_debug::debug_step_impl::<SF32LB56Tool, SF32LB56FrameFormat>(self)
    }

    fn debug_run(&mut self) -> Result<()> {
        common_debug::debug_run_impl::<SF32LB56Tool, SF32LB56FrameFormat>(self)
    }

    fn debug_halt(&mut self) -> Result<()> {
        common_debug::debug_halt_impl::<SF32LB56Tool, SF32LB56FrameFormat>(self)
    }
}

impl SF32LB56Tool {
    /// 执行全部flash擦除的内部方法
    pub fn internal_erase_all(&mut self, address: u32) -> Result<()> {
        use ram_command::{Command, RamCommand};

        let progress = self.progress();
        let spinner = progress.create_spinner(ProgressOperation::EraseFlash {
            address,
            style: EraseFlashStyle::Addressed,
        });

        // 发送擦除所有命令
        let _ = self.command(Command::EraseAll { address });

        let mut io = for_tool(self);
        io.wait_for_pattern(
            b"OK",
            Duration::from_millis(30_000),
            &format!("erasing flash at 0x{:08X}", address),
        )?;

        spinner.finish(ProgressStatus::Success);

        Ok(())
    }

    /// 执行区域擦除的内部方法
    pub fn internal_erase_region(&mut self, address: u32, len: u32) -> Result<()> {
        use ram_command::{Command, RamCommand};

        let progress = self.progress();
        let spinner = progress.create_spinner(ProgressOperation::EraseRegion {
            address,
            len,
            style: EraseRegionStyle::HexLength,
        });

        // 发送擦除区域命令
        let _ = self.command(Command::Erase { address, len });

        let mut io = for_tool(self);
        io.wait_for_pattern(
            b"OK",
            Duration::from_millis(30_000),
            &format!("erasing region at 0x{:08X}", address),
        )?;

        spinner.finish(ProgressStatus::Success);

        Ok(())
    }

    pub fn attempt_connect(&mut self) -> Result<()> {
        use crate::common::sifli_debug::{SifliUartCommand, SifliUartResponse};

        let infinite_attempts = self.base.connect_attempts <= 0;
        let mut remaining_attempts = if infinite_attempts {
            None
        } else {
            Some(self.base.connect_attempts)
        };
        loop {
            if self.base.before.requires_reset() {
                // 使用RTS引脚复位
                let mut io = for_tool(self);
                io.write_request_to_send(true)?;
                io.sleep(Duration::from_millis(100))?;
                io.write_request_to_send(false)?;
                io.sleep(Duration::from_millis(100))?;
            }
            let value: Result<()> = match self.debug_command(SifliUartCommand::Enter) {
                Ok(SifliUartResponse::Enter) => Ok(()),
                _ => Err(std::io::Error::other("Failed to enter debug mode").into()),
            };
            // 如果有限重试,检查是否还有机会
            if let Some(ref mut attempts) = remaining_attempts {
                if *attempts == 0 {
                    break; // 超过最大重试次数则退出循环
                }
                *attempts -= 1;
            }

            let progress = self.progress();
            let spinner = progress.create_spinner(ProgressOperation::Connect);

            // 尝试连接
            match value {
                Ok(_) => {
                    spinner.finish(ProgressStatus::Success);
                    return Ok(());
                }
                Err(_) => {
                    spinner.finish(ProgressStatus::Retry);
                    sleep_with_cancel(&self.base.cancel_token, Duration::from_millis(500))?;
                }
            }
        }
        Err(std::io::Error::other("Failed to connect to the chip").into())
    }

    pub fn download_stub_impl(&mut self) -> Result<()> {
        use crate::common::sifli_debug::SifliUartCommand;
        use crate::ram_stub::load_stub_file;
        use probe_rs::MemoryMappedRegister;
        use probe_rs::architecture::arm::core::armv7m::{Aircr, Demcr};
        use probe_rs::architecture::arm::core::registers::cortex_m::{PC, SP};

        let progress = self.progress();
        let spinner = progress.create_spinner(ProgressOperation::DownloadStub {
            stage: StubStage::Start,
        });

        // 0.0 HCPU Unconditional halt
        self.debug_halt()?;
        //  0.1 HPSYS_AON->ISSR->LP_ACTIVE
        let mut data = self.debug_read_word32(0x4004_0028)?;
        if data & 0x20 != 0 {
            data = 0xa05f0003;
            // LCPU Halt
            self.debug_write_word32(0x3000_EDF0, data)?;
        }

        // 1. reset and halt
        //    1.1. reset_catch_set
        let demcr = self.debug_read_word32(Demcr::get_mmio_address() as u32)?;
        let mut demcr = Demcr(demcr);
        demcr.set_vc_corereset(true);
        self.debug_write_word32(Demcr::get_mmio_address() as u32, demcr.into())?;

        // 1.2. reset_system
        let mut aircr = Aircr(0);
        aircr.vectkey();
        aircr.set_sysresetreq(true);
        let _ = self.debug_write_word32(Aircr::get_mmio_address() as u32, aircr.into()); // MCU已经重启,不一定能收到正确回复
        sleep_with_cancel(
            &self.base.cancel_token,
            std::time::Duration::from_millis(10),
        )?;

        // 1.3. Re-enter debug mode
        self.debug_command(SifliUartCommand::Enter)?;

        // 1.4. halt
        self.debug_halt()?;

        // 1.5. reset_catch_clear
        let demcr = self.debug_read_word32(Demcr::get_mmio_address() as u32)?;
        let mut demcr = Demcr(demcr);
        demcr.set_vc_corereset(false);
        self.debug_write_word32(Demcr::get_mmio_address() as u32, demcr.into())?;

        sleep_with_cancel(
            &self.base.cancel_token,
            std::time::Duration::from_millis(100),
        )?;
        // 2. Download stub - 支持外部 stub 文件
        let chip_memory_key = format!("sf32lb56_{}", self.base.memory_type);
        let stub = match load_stub_file(self.base.external_stub_path.as_deref(), &chip_memory_key) {
            Ok(s) => s,
            Err(e) => {
                spinner.finish(ProgressStatus::NotFound);
                return Err(e.into());
            }
        };

        let packet_size = if self.base.compat { 256 } else { 64 * 1024 };

        let mut addr = 0x2006_7000;
        let mut data = &stub.data[..];
        while !data.is_empty() {
            let chunk = &data[..std::cmp::min(data.len(), packet_size)];
            self.debug_write_memory(addr, chunk)?;
            addr += chunk.len() as u32;
            data = &data[chunk.len()..];
        }

        // 3. run ram stub
        // 3.1. set SP and PC
        let sp = u32::from_le_bytes(
            stub.data[0..4]
                .try_into()
                .expect("slice with exactly 4 bytes"),
        );
        let pc = u32::from_le_bytes(
            stub.data[4..8]
                .try_into()
                .expect("slice with exactly 4 bytes"),
        );
        self.debug_write_core_reg(PC.id.0, pc)?;
        self.debug_write_core_reg(SP.id.0, sp)?;

        // 3.2. run
        self.debug_run()?;

        spinner.finish(ProgressStatus::Success);

        Ok(())
    }
}

impl SifliTool for SF32LB56Tool {
    fn create_tool(base: SifliToolBase) -> Box<dyn SifliTool> {
        let mut port = serialport::new(&base.port_name, 1000000)
            .timeout(Duration::from_secs(5))
            .open()
            .unwrap();
        port.write_request_to_send(false).unwrap();
        std::thread::sleep(Duration::from_millis(100));

        let mut tool = Box::new(Self { base, port });
        if tool.base.before.should_download_stub() {
            tool.download_stub().expect("Failed to download stub");
        }
        tool
    }
}

impl SifliToolTrait for SF32LB56Tool {
    fn port(&mut self) -> &mut Box<dyn SerialPort> {
        &mut self.port
    }

    fn base(&self) -> &SifliToolBase {
        &self.base
    }

    fn set_speed(&mut self, baud: u32) -> Result<()> {
        use crate::speed::SpeedTrait;
        SpeedTrait::set_speed(self, baud)
    }

    fn soft_reset(&mut self) -> Result<()> {
        use crate::reset::Reset;
        Reset::soft_reset(self)
    }
}