Analogread cannot be used together with low side current sensing on STM32 because it initializes/configures/calibrates/starts/stops/deinitializes the ADC each time, which breaks the low side current sensing and makes Analogread very slow.
I have a promising fix here that could potentially be accepted by STM32DUINO, but this code is used by many STM32 chips, so I would need your feedback on different boards/chips with different use cases
with platformio, it’s easy to test it by editing the platformio.ini file.
Adding this will pick the modified STSTM32 platform:
platform_packages = framework-arduinoststm32@https://github.com/Candas1/Arduino_Core_STM32.git#analogread
Adding this will use a fork of SimpleFOC with small fixes to make analogread work:
https://github.com/Candas1/Arduino-FOC.git#analogread
Don’t use this in foc_current mode if you are not confident that the current sense is not broken.
In this example I use low side current sense and a potentiometer:
One aspect I didn’t check yet is the possibility of changing the resolution of analogRead with the analogReadResolution function, I need to test it to make sure it works for STM32DUINO, but in the case of SimpleFOC it might break the low current sensing.
[EDIT] it won’t work on the b-g431b-esc1 implementation
[EDIT2] Now changing the resolution should also work, but you need to set the resolution to 12 bits for the current sense to work properly analogReadResolution(12);