STM32G0 USB-PD Source stack periodically sends hard resets when compiler optimizations are enabled.
I'm using a Cube IDE generated project (latest version) with the latest version of the G0 SDK. This problem originally showed up on a custom board but I have reproduced the problem using a Nucleo-G071RB dev board wired up to a USB-C breakout jack with voltage divider to sample VBus and a mosfet to switch VBus.
The system runs fine when compiled at -O0. The Source port establishes a contract with a Sink device and stays connected indefinitely. However, as soon as any level of optimizations are turned on (even just -Og) the stack will start sending hard resets to a connected Sink roughly every 15 seconds. Sometimes this only happens 1 or 2 times after the initial contract is established, sometimes it repeats for several minutes and then stops, sometimes it repeats forever.
I can set a breakpoint to stop when the reset message is sent but the hard reset signal call chain originates inside the ST stack library code. I can't see what is causing the problem because there is no source code.
I have attached the cubeIDE project that reproduces the problem. There is 1 sink and 1 source port configured so that the project matches our custom board configuration. Only the source port is actually connected to a physical USB connector. The tracer is redirected to a custom serial print log system because that is what we use on the custom board.
How can I fix this? I can't just turn off optimizations because the code for the custom boards gets too big to fit in the flash.
