Skip to content

Commit c9a7363

Browse files
committed
target/riscv: Fix some timeout check order
Reorder the timeout check and conditional judgment to ensure that timeout check is not wasted.
1 parent a68695b commit c9a7363

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/target/riscv/riscv-013.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,19 +1887,19 @@ static int reset_dm(struct target *target)
18871887

18881888
const time_t start = time(NULL);
18891889
LOG_TARGET_DEBUG(target, "Waiting for the DM to acknowledge reset.");
1890-
do {
1890+
while (1) {
18911891
result = dm_read(target, &dmcontrol, DM_DMCONTROL);
18921892
if (result != ERROR_OK)
18931893
return result;
1894-
1894+
if (!get_field32(dmcontrol, DM_DMCONTROL_DMACTIVE))
1895+
break;
18951896
if (time(NULL) - start > riscv_get_command_timeout_sec()) {
18961897
LOG_TARGET_ERROR(target, "DM didn't acknowledge reset in %d s. "
18971898
"Increase the timeout with 'riscv set_command_timeout_sec'.",
18981899
riscv_get_command_timeout_sec());
18991900
return ERROR_TIMEOUT_REACHED;
19001901
}
1901-
} while (get_field32(dmcontrol, DM_DMCONTROL_DMACTIVE));
1902-
LOG_TARGET_DEBUG(target, "DM reset initiated.");
1902+
}
19031903
}
19041904
/* TODO: Move the code above into `deactivate_dm()` function
19051905
* (a logical counterpart to activate_dm()). */
@@ -2999,10 +2999,20 @@ static int deassert_reset(struct target *target)
29992999
RISCV_DELAY_BASE);
30003000
time_t start = time(NULL);
30013001
LOG_TARGET_DEBUG(target, "Waiting for hart to come out of reset.");
3002-
do {
3002+
while (1) {
30033003
result = dmstatus_read(target, &dmstatus, true);
30043004
if (result != ERROR_OK)
30053005
return result;
3006+
/* Certain debug modules, like the one in GD32VF103
3007+
* MCUs, violate the specification's requirement that
3008+
* each hart is in "exactly one of four states" and,
3009+
* during reset, report harts as both unavailable and
3010+
* halted/running. To work around this, we check for
3011+
* the absence of the unavailable state rather than
3012+
* the presence of any other state. */
3013+
if (!get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL) ||
3014+
get_field(dmstatus, DM_DMSTATUS_ALLHAVERESET))
3015+
break;
30063016

30073017
if (time(NULL) - start > riscv_get_command_timeout_sec()) {
30083018
LOG_TARGET_ERROR(target, "Hart didn't leave reset in %ds; "
@@ -3013,15 +3023,7 @@ static int deassert_reset(struct target *target)
30133023
get_field(dmstatus, DM_DMSTATUS_ALLHAVERESET) ? "true" : "false");
30143024
return ERROR_TIMEOUT_REACHED;
30153025
}
3016-
/* Certain debug modules, like the one in GD32VF103
3017-
* MCUs, violate the specification's requirement that
3018-
* each hart is in "exactly one of four states" and,
3019-
* during reset, report harts as both unavailable and
3020-
* halted/running. To work around this, we check for
3021-
* the absence of the unavailable state rather than
3022-
* the presence of any other state. */
3023-
} while (get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL) &&
3024-
!get_field(dmstatus, DM_DMSTATUS_ALLHAVERESET));
3026+
}
30253027

30263028
riscv_scan_set_delay(&info->learned_delays, RISCV_DELAY_BASE,
30273029
orig_base_delay);

0 commit comments

Comments
 (0)