Skip to content

Commit

Permalink
cli/serial: 增加 --timeout 参数用于覆盖所有操作的超时设置
Browse files Browse the repository at this point in the history
  • Loading branch information
xychen committed Mar 29, 2024
1 parent e0bdb85 commit 3be8100
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ Serial burning options:
verify all partitions after burning
-n, --nand
burn to NAND flash (CSK6 only)
--timeout <ms>
override timeout for each operation, acceptable values:
-1: no timeout
0: use default strategy
>0: timeout in milliseconds
Advanced operations (serial only):
--erase <addr:size>
Expand Down
14 changes: 13 additions & 1 deletion cskburn/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static struct option long_options[] = {
{"probe-timeout", required_argument, NULL, 0},
{"reset-attempts", required_argument, NULL, 0},
{"reset-delay", required_argument, NULL, 0},
{"timeout", required_argument, NULL, 0},
{"pass-delay", required_argument, NULL, 0},
{"fail-delay", required_argument, NULL, 0},
{"burner", required_argument, NULL, 0},
Expand All @@ -81,6 +82,7 @@ static const char option_string[] = {
"w"
"C:"
"n"
"t:"
"r"
#ifndef WITHOUT_USB
"u:"
Expand Down Expand Up @@ -148,6 +150,7 @@ static struct {
uint32_t probe_timeout;
uint32_t reset_attempts;
uint32_t reset_delay;
int32_t timeout;
char *burner;
uint8_t *burner_buf;
uint32_t burner_len;
Expand Down Expand Up @@ -178,6 +181,7 @@ static struct {
.probe_timeout = DEFAULT_PROBE_TIMEOUT,
.reset_attempts = DEFAULT_RESET_ATTEMPTS,
.reset_delay = DEFAULT_RESET_DELAY,
.timeout = 0,
.burner = NULL,
.burner_len = 0,
.update_high = false,
Expand Down Expand Up @@ -248,6 +252,11 @@ print_help(const char *progname)
LOGI(" verify all partitions after burning");
LOGI(" -n, --nand");
LOGI(" burn to NAND flash (CSK6 only)");
LOGI(" --timeout <ms>");
LOGI(" override timeout for each operation, acceptable values:");
LOGI(" -1: no timeout");
LOGI(" 0: use default strategy");
LOGI(" >0: timeout in milliseconds");
LOGI("");

LOGI("Advanced operations (serial only):");
Expand Down Expand Up @@ -324,6 +333,9 @@ main(int argc, char **argv)
case 'r':
options.target = TARGET_RAM;
break;
case 't':
sscanf(optarg, "%d", &options.timeout);
break;
case 0: { /* long-only options */
const char *name = long_options[long_index].name;
if (strcmp(name, "chip-id") == 0) {
Expand Down Expand Up @@ -802,7 +814,7 @@ serial_burn(cskburn_partition_t *parts, int parts_cnt)
}

cskburn_serial_device_t *dev = NULL;
if ((ret = cskburn_serial_open(&dev, options.serial, options.chip)) != 0) {
if ((ret = cskburn_serial_open(&dev, options.serial, options.chip, options.timeout)) != 0) {
LOGE_RET(ret, "ERROR: Failed opening device");
goto err_open;
}
Expand Down
3 changes: 2 additions & 1 deletion libcskburn_serial/include/cskburn_serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ typedef enum {
* @retval 0 if successful
* @retval -errno on other errors from serial device
*/
int cskburn_serial_open(cskburn_serial_device_t **dev, const char *path, uint32_t chip);
int cskburn_serial_open(
cskburn_serial_device_t **dev, const char *path, uint32_t chip, int32_t timeout);

/**
* @brief Close CSK device
Expand Down
6 changes: 5 additions & 1 deletion libcskburn_serial/src/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ command_send(cskburn_serial_device_t *dev, uint8_t op, uint8_t *req_buf, uint32_
static ssize_t
command_recv(cskburn_serial_device_t *dev, uint8_t op, uint8_t **res_buf, uint32_t timeout)
{
if (dev->timeout > 0) {
timeout = dev->timeout;
}

uint64_t start = time_monotonic();
do {
ssize_t r = slip_read(dev->slip, dev->res_buf, MAX_RES_SLIP_LEN, timeout);
Expand All @@ -142,7 +146,7 @@ command_recv(cskburn_serial_device_t *dev, uint8_t op, uint8_t **res_buf, uint32
*res_buf = dev->res_buf;
return r;
}
} while (TIME_SINCE_MS(start) < timeout);
} while (dev->timeout == -1 || TIME_SINCE_MS(start) < timeout);

return -ETIMEDOUT;
}
Expand Down
3 changes: 2 additions & 1 deletion libcskburn_serial/src/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ cskburn_serial_init(int flags)
}

int
cskburn_serial_open(cskburn_serial_device_t **dev, const char *path, uint32_t chip)
cskburn_serial_open(cskburn_serial_device_t **dev, const char *path, uint32_t chip, int32_t timeout)
{
int ret;

Expand All @@ -57,6 +57,7 @@ cskburn_serial_open(cskburn_serial_device_t **dev, const char *path, uint32_t ch
(*dev)->req_hdr = (*dev)->req_buf;
(*dev)->req_cmd = (*dev)->req_buf + sizeof(csk_command_t);
(*dev)->chip = chip;
(*dev)->timeout = timeout;

return 0;

Expand Down
1 change: 1 addition & 0 deletions libcskburn_serial/src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct _cskburn_serial_device_t {
uint8_t *req_buf;
uint8_t *res_buf;
uint32_t chip;
int32_t timeout;
};

#endif // __LIB_CSKBURN_SERIAL_CORE__

0 comments on commit 3be8100

Please sign in to comment.