Skip to content

Commit

Permalink
Merge pull request #1343 from shysaur/zero-voltage-factor-fix
Browse files Browse the repository at this point in the history
Do not crash when the STLink chip returns a voltage factor of zero.
  • Loading branch information
Nightwalker-87 committed Oct 15, 2023
2 parents 40fbdb4 + 3387ca5 commit 31c7fc6
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/stlink-lib/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,13 @@ int32_t _stlink_usb_target_voltage(stlink_t *sl) {

factor = (rdata[3] << 24) | (rdata[2] << 16) | (rdata[1] << 8) | (rdata[0] << 0);
reading = (rdata[7] << 24) | (rdata[6] << 16) | (rdata[5] << 8) | (rdata[4] << 0);
voltage = 2400 * reading / factor;
DLOG("target voltage factor=%08x reading=%08x\n", factor, reading);
if (factor != 0 && reading != 0) {
voltage = 2400 * reading / factor;
} else {
DLOG("voltage reading failed at device side, bad STLink chip?\n");
voltage = 0;
}

return (voltage);
}
Expand Down

0 comments on commit 31c7fc6

Please sign in to comment.