I (possibly) had a problem similar to this.
- Works: When I completely remove power from STM + circuitry . Then re-power on - I’m able to upload and sensor ‘works’.
- Doesn’t work: However, my second and subsequent uploads fail (unless I power off/on).
What I tracked it down to was that the i2c sensor itself needed to be ‘powered off’ otherwise it was holding SDA low. Essentially if the sensor is in the middle of transmitting data when you upload sketch it won’t release SDA until it is sent a bunch of SCLKs or powered down. This only happened on some stm32, didn’t see it on esp32.
If you are seeing this behavioud then there are a few workarounds.