Skip to content

Commit 0c830f1

Browse files
authored
feat: 添加粒度更细的before/after判断 (#48)
1 parent 8b0c203 commit 0c830f1

File tree

10 files changed

+68
-40
lines changed

10 files changed

+68
-40
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ sftool [选项] 命令 [命令选项]
5353
- `-m, --memory <MEMORY>`: 存储类型 [nor, nand, sd] (默认: nor)
5454
- `-p, --port <PORT>`: 串行端口设备路径
5555
- `-b, --baud <BAUD>`: 闪存/读取时使用的串口波特率 (默认: 1000000)
56-
- `--before <OPERATION>`: 连接芯片前的操作 [no_reset, soft_reset] (默认: no_reset)
57-
- `--after <OPERATION>`: 工具完成后的操作 [no_reset, soft_reset] (默认: soft_reset)
56+
- `--before <OPERATION>`: 连接芯片前的操作 [default_reset, no_reset, no_reset_no_sync] (默认: default_reset)
57+
- `--after <OPERATION>`: 工具完成后的操作 [soft_reset, no_reset] (默认: soft_reset)
5858
- `--connect-attempts <ATTEMPTS>`: 连接尝试次数,负数或0表示无限次 (默认: 7)
5959
- `--compat` : 兼容模式,如果经常出现超时错误或下载后校验失败,则应打开此选项。
6060

README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ sftool [OPTIONS] COMMAND [COMMAND OPTIONS]
5555
- `-m, --memory <MEMORY>`: Storage type [nor, nand, sd] (default: nor)
5656
- `-p, --port <PORT>`: Serial port device path
5757
- `-b, --baud <BAUD>`: Baud rate used for flashing/reading (default: 1000000)
58-
- `--before <OPERATION>`: Operation before connecting to the chip [no_reset, soft_reset] (default: no_reset)
59-
- `--after <OPERATION>`: Operation after the tool completes [no_reset, soft_reset] (default: soft_reset)
58+
- `--before <OPERATION>`: Operation before connecting to the chip [default_reset, no_reset, no_reset_no_sync] (default: default_reset)
59+
- `--after <OPERATION>`: Operation after the tool completes [soft_reset, no_reset] (default: soft_reset)
6060
- `--connect-attempts <ATTEMPTS>`: Number of connection attempts, negative or 0 means infinite (default: 7)
6161
- `--compat` : Compatibility mode, should be turned on if timeout errors or verification failures occur frequently after downloading.
6262

sftool-lib/src/lib.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,38 @@ use std::sync::Arc;
3131

3232
#[derive(Debug, Clone, PartialEq, Eq)]
3333
#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
34-
pub enum Operation {
34+
pub enum BeforeOperation {
35+
#[cfg_attr(feature = "cli", clap(name = "default_reset"))]
36+
DefaultReset,
3537
#[cfg_attr(feature = "cli", clap(name = "no_reset"))]
36-
None,
38+
NoReset,
39+
#[cfg_attr(feature = "cli", clap(name = "no_reset_no_sync"))]
40+
NoResetNoSync,
41+
}
42+
43+
impl BeforeOperation {
44+
pub fn requires_reset(&self) -> bool {
45+
matches!(self, Self::DefaultReset)
46+
}
47+
48+
pub fn should_download_stub(&self) -> bool {
49+
!matches!(self, Self::NoResetNoSync)
50+
}
51+
}
52+
53+
#[derive(Debug, Clone, PartialEq, Eq)]
54+
#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
55+
pub enum AfterOperation {
56+
#[cfg_attr(feature = "cli", clap(name = "no_reset"))]
57+
NoReset,
3758
#[cfg_attr(feature = "cli", clap(name = "soft_reset"))]
3859
SoftReset,
39-
#[cfg_attr(feature = "cli", clap(name = "default_reset"))]
40-
DefaultReset,
60+
}
61+
62+
impl AfterOperation {
63+
pub fn requires_soft_reset(&self) -> bool {
64+
matches!(self, Self::SoftReset)
65+
}
4166
}
4267

4368
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -54,7 +79,7 @@ pub enum ChipType {
5479
#[derive(Clone)]
5580
pub struct SifliToolBase {
5681
pub port_name: String,
57-
pub before: Operation,
82+
pub before: BeforeOperation,
5883
pub memory_type: String,
5984
pub baud: u32,
6085
pub connect_attempts: i8,
@@ -67,7 +92,7 @@ impl SifliToolBase {
6792
/// 创建一个使用默认空进度回调的 SifliToolBase
6893
pub fn new_with_no_progress(
6994
port_name: String,
70-
before: Operation,
95+
before: BeforeOperation,
7196
memory_type: String,
7297
baud: u32,
7398
connect_attempts: i8,
@@ -90,7 +115,7 @@ impl SifliToolBase {
90115
/// 创建一个使用自定义进度回调的 SifliToolBase
91116
pub fn new_with_progress(
92117
port_name: String,
93-
before: Operation,
118+
before: BeforeOperation,
94119
memory_type: String,
95120
baud: u32,
96121
connect_attempts: i8,

sftool-lib/src/sf32lb52/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ impl SF32LB52Tool {
123123
}
124124

125125
fn attempt_connect(&mut self) -> Result<()> {
126-
use crate::Operation;
127126
use crate::common::sifli_debug::{SifliUartCommand, SifliUartResponse};
128127

129128
let infinite_attempts = self.base.connect_attempts <= 0;
@@ -133,7 +132,7 @@ impl SF32LB52Tool {
133132
Some(self.base.connect_attempts)
134133
};
135134
loop {
136-
if self.base.before == Operation::DefaultReset {
135+
if self.base.before.requires_reset() {
137136
// 使用RTS引脚复位
138137
self.port.write_request_to_send(true)?;
139138
std::thread::sleep(Duration::from_millis(100));
@@ -280,7 +279,9 @@ impl SifliTool for SF32LB52Tool {
280279
std::thread::sleep(Duration::from_millis(100));
281280

282281
let mut tool = Box::new(Self { base, port });
283-
tool.download_stub().expect("Failed to download stub");
282+
if tool.base.before.should_download_stub() {
283+
tool.download_stub().expect("Failed to download stub");
284+
}
284285
tool
285286
}
286287
}

sftool-lib/src/sf32lb56/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ impl SF32LB56Tool {
276276
}
277277

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

282281
let infinite_attempts = self.base.connect_attempts <= 0;
@@ -286,7 +285,7 @@ impl SF32LB56Tool {
286285
Some(self.base.connect_attempts)
287286
};
288287
loop {
289-
if self.base.before == Operation::DefaultReset {
288+
if self.base.before.requires_reset() {
290289
// 使用RTS引脚复位
291290
self.port.write_request_to_send(true)?;
292291
std::thread::sleep(Duration::from_millis(100));
@@ -430,7 +429,9 @@ impl SifliTool for SF32LB56Tool {
430429
std::thread::sleep(Duration::from_millis(100));
431430

432431
let mut tool = Box::new(Self { base, port });
433-
tool.download_stub().expect("Failed to download stub");
432+
if tool.base.before.should_download_stub() {
433+
tool.download_stub().expect("Failed to download stub");
434+
}
434435
tool
435436
}
436437
}

sftool-lib/src/sf32lb58/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ impl SifliTool for SF32LB58Tool {
340340
std::thread::sleep(Duration::from_millis(100));
341341

342342
let mut tool = Box::new(Self { base, port });
343-
tool.download_stub().expect("Failed to download stub");
343+
if tool.base.before.should_download_stub() {
344+
tool.download_stub().expect("Failed to download stub");
345+
}
344346
tool
345347
}
346348
}

sftool/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ sftool [OPTIONS] COMMAND [COMMAND_OPTIONS]
6363
- `-m, --memory <MEMORY>`: Memory type [nor, nand, sd] (default: nor)
6464
- `-p, --port <PORT>`: Serial port device path
6565
- `-b, --baud <BAUD>`: Baud rate for flash/read operations (default: 1000000)
66-
- `--before <OPERATION>`: Operation before connecting to chip [no_reset, soft_reset] (default: no_reset)
67-
- `--after <OPERATION>`: Operation after tool completion [no_reset, soft_reset] (default: soft_reset)
66+
- `--before <OPERATION>`: Operation before connecting to the chip [default_reset, no_reset, no_reset_no_sync] (default: default_reset)
67+
- `--after <OPERATION>`: Operation after tool completion [soft_reset, no_reset] (default: soft_reset)
6868
- `--connect-attempts <ATTEMPTS>`: Number of connection attempts, negative or 0 for infinite (default: 7)
6969
- `--compat`: Compatibility mode, enable if you frequently encounter timeout errors or checksum failures
7070

sftool/sftool_param_schema.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
},
2727
"before": {
2828
"type": "string",
29-
"enum": [ "no_reset", "soft_reset", "default_reset" ],
29+
"enum": [ "default_reset", "no_reset", "no_reset_no_sync" ],
3030
"default": "default_reset",
3131
"description": "Action before connecting to the chip"
3232
},
3333
"after": {
3434
"type": "string",
35-
"enum": [ "no_reset", "soft_reset", "default_reset" ],
35+
"enum": [ "no_reset", "soft_reset" ],
3636
"default": "soft_reset",
3737
"description": "Action after sftool finishes"
3838
},
@@ -193,4 +193,4 @@
193193
"additionalProperties": false
194194
}
195195
}
196-
}
196+
}

sftool/src/config.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use serde::{Deserialize, Serialize};
2-
use sftool_lib::{ChipType, Operation};
2+
use sftool_lib::{AfterOperation, BeforeOperation, ChipType};
33

44
/// 应用程序的默认配置值
55
pub struct Defaults;
@@ -162,22 +162,21 @@ impl SfToolConfig {
162162
}
163163
}
164164

165-
/// 将字符串转换为 Operation 枚举
166-
pub fn parse_before(&self) -> Result<Operation, String> {
165+
/// 将字符串转换为 before 操作枚举
166+
pub fn parse_before(&self) -> Result<BeforeOperation, String> {
167167
match self.before.as_str() {
168-
"no_reset" => Ok(Operation::None),
169-
"soft_reset" => Ok(Operation::SoftReset),
170-
"default_reset" => Ok(Operation::DefaultReset),
168+
"default_reset" => Ok(BeforeOperation::DefaultReset),
169+
"no_reset" => Ok(BeforeOperation::NoReset),
170+
"no_reset_no_sync" => Ok(BeforeOperation::NoResetNoSync),
171171
_ => Err(format!("Invalid before operation: {}", self.before)),
172172
}
173173
}
174174

175-
/// 将字符串转换为 Operation 枚举
176-
pub fn parse_after(&self) -> Result<Operation, String> {
175+
/// 将字符串转换为 after 操作枚举
176+
pub fn parse_after(&self) -> Result<AfterOperation, String> {
177177
match self.after.as_str() {
178-
"no_reset" => Ok(Operation::None),
179-
"soft_reset" => Ok(Operation::SoftReset),
180-
"default_reset" => Ok(Operation::DefaultReset),
178+
"no_reset" => Ok(AfterOperation::NoReset),
179+
"soft_reset" => Ok(AfterOperation::SoftReset),
181180
_ => Err(format!("Invalid after operation: {}", self.after)),
182181
}
183182
}

sftool/src/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::{Context, Result, anyhow, bail};
22
use clap::{Parser, Subcommand, ValueEnum};
3-
use sftool_lib::{ChipType, Operation, SifliToolBase, create_sifli_tool};
3+
use sftool_lib::{AfterOperation, BeforeOperation, ChipType, SifliToolBase, create_sifli_tool};
44
use strum::{Display, EnumString};
55

66
mod config;
@@ -14,8 +14,8 @@ type MergedConfig = (
1414
String,
1515
String,
1616
u32,
17-
Operation,
18-
Operation,
17+
BeforeOperation,
18+
AfterOperation,
1919
i8,
2020
bool,
2121
bool,
@@ -162,11 +162,11 @@ struct Cli {
162162

163163
/// What to do before connecting to the chip (default: default_reset)
164164
#[arg(long = "before", value_enum)]
165-
before: Option<Operation>,
165+
before: Option<BeforeOperation>,
166166

167167
/// What to do after siflitool is finished (default: soft_reset)
168168
#[arg(long = "after", value_enum)]
169-
after: Option<Operation>,
169+
after: Option<AfterOperation>,
170170

171171
/// Number of attempts to connect, negative or 0 for infinite (default: 3)
172172
#[arg(long = "connect-attempts")]
@@ -520,7 +520,7 @@ fn main() -> Result<()> {
520520
}
521521
}
522522

523-
if after != Operation::None {
523+
if after.requires_soft_reset() {
524524
siflitool
525525
.soft_reset()
526526
.context("Failed to perform post-operation soft reset")?;

0 commit comments

Comments
 (0)