Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not crash when the STLink chip returns a voltage factor of zero. #1343

Merged
merged 1 commit into from
Oct 15, 2023

Conversation

shysaur
Copy link
Contributor

@shysaur shysaur commented Oct 14, 2023

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 the GET_TARGET_VOLTAGE command returns an all-zero response, and as a result 2400 * reading \ factor is a division by zero.

This pull request adds a check for factor==0 or reading==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.

@Nightwalker-87 Nightwalker-87 added this to the v1.8.0 milestone Oct 15, 2023
@Nightwalker-87 Nightwalker-87 merged commit 31c7fc6 into stlink-org:develop Oct 15, 2023
10 checks passed
@stlink-org stlink-org locked as resolved and limited conversation to collaborators Oct 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants