STM32 Dev Board (MBED) - AnalogIn voltage errors

Oh actually there is. A quick search shows

There is the env variable PIODEBUGFLAGS which you can set but the code strips away all -O[0,1,2] flags afterwards… You might try your luck if -Og works (described as “Optimize debugging experience”) but if not you should modify the python script I pointed to in your local installation to prevent the unflags.append from happening, then setting that env var.

I believe I have found a decent workaround for this.
So the issue seems to be with the compiler optimisation causing an issue with the HAL library, specifically one of the ADC configuration functions that sets the value of a register. I have tested this by setting the build flag optimisation level to -O0 as noted previously. Unfortunately for my project I need to be able to use sleep states of the device, so this is not a long term solution.

A fix I have found is to use a previous version of the GCC toolchain provided by ARM, specifically

gcc-arm-none-eabi-6-2017-q2-update

When I overwrite the files within .platformio/packages/toolchain-gccarmnoneeabi folder with those from the older version of the toolchain I do not get the issue, even with optimisation set to -0s. I hope this helps others on the STM32L4x5 and x6 platforms.