Do not crash when the STLink chip returns a voltage factor of zero. #1343
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I happen to have a stm32f4discovery board with an integrated STLink-V2 which cannot read the target voltage (should be around 3V). The cause for this issue appears to be a broken ADC in the STLink STM32F103 chip. Otherwise the STLink USB interface works, and the STM32CubeProgrammer software provided by ST manages to perform flash writing operations just fine even though it shows a target voltage of zero.
However, when attempting to flash the board with st-flash, as soon as flash erase completes, the tool crashes with a floating point exception in
_stlink_usb_target_voltage()
, as theGET_TARGET_VOLTAGE
command returns an all-zero response, and as a result2400 * reading \ factor
is a division by zero.This pull request adds a check for
factor==0
orreading==0
, and in that case always returns zero as the target voltage like STM32CubeProgrammer does. This allows programming to succeed in 8 bit mode with this board. Additional logging statements were added to the code to simplify future debugging of similar issues.An alternative fix would be to return a voltage of -1 rather than zero, but that would cause the entire flashing process to fail with no reason. Indeed, if the target voltage was zero volts the flashing process would have failed much earlier. Therefore I think it is reasonable to use "0V" as the appropriate return value for this specific case in which the
GET_TARGET_VOLTAGE
command succeeds but returns invalid data.