STM32CUBEU5: USB conflicts with user ADC1 use
When I use STM32Uxx boards with USB device and STM32CubeU5 drivers/libraries - I cannot make use of ADC1.
I want to use ADC1 also (not just USB), to measure VCORE and Temperature.
It is a SW issue (and it can be fixed), not a HW issue.
Details:
I use STM32U5xx (e.g. NUCLEO-U5A5ZJ-Q, with user USB-C as USB VCP UART. The related demo for it is in STM32CubeU5 USBX/Ux_Device_CDC_ACM (starting from it for my project).
It is a USB device VCP UART demo.
The ADC1 is used as USB VSENSE!
So, even you configure ADC1 for your own purposes, e.g. you provide your own MX_ADC1_Init() and void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) - it is not really acting: your config is overwritten by the USB driver:
See in file ".../Drivers/BSP/STM32U5xx_Nucleo/stem32u5xx_nucleo_usbpd_pwr.c" that the same ADC1 is initialized again. It overwrites your settings for ADC1. And the measurement done for USB VSENSE seems to change your ADC1 config (e.g. back to one channel only).
In function "int32_t BSP_USBPD_PWR_VBUSInit(uint32_t PortNum)" the ADC is initialized again.
And when the USB driver is activated, e.g. you plug in the USB (anytime later), the function int32_t BSP_USBPD_PWR_VBUSGetVoltage(uint32_t PortNum, uint32_t *pVoltage) is called (to measure the USB VBUS voltage). This might be not aligned with your ADC1 use.
My ADC1 stuff worked fine until I have plugged on USB (USB modifies ADC1).
Solution:
- modify the USB drivers so that their functions are aligned with your purpose, e.g. for me also to measure VBAT, VCORE and esp. Temperature sensor (not just external pin PC2 with a voltage divider for VBUS)
- or disable all USB related ADC1 stuff - fake the result of function int32_t BSP_USBPD_PWR_VBUSGetVoltage(uint32_t PortNum, uint32_t *pVoltage) without to touch the ADC1 (return the right fix value from this function, w/o to measure really, so that driver is happy)
My project, which uses USB and ADC1 to measure the VREF, VBAT and Temperature (USB not using anymore ADC1 at all) is here:
GitHub - tjaekel/STM32U5A5-VCP_UART: VCP UART on U5A5 NUCLEO and my board
You can find a better solution for a shared use of ADC1 (for USB and for your code), potentially a need to modify the USB driver a bit (at least to be aware what the USB driver will do on ADC1).
