Portenta H7 STM32 just stopped loading, Think it is the USB

Hi All
As of a few hours ago my STM32H7 (Arduino Portenta H7) has stopped loading. When i upload using ST-Link it keeps halting on line 37 of arduino>Main.cpp

initVariant();

if i pull out USB on COM4 (Monitor Port) i can some times get my code to move into my main.cpp but this is hit and miss. If i do some times the Portenta H7 fails with LED blinking red 4 times. Which i belive is a USB fault.

BTW at time of this all starting i was working on GPIO interupts and call back routines, but i cant see what went wrong.

So i went back to run a basics and decided to run a basic blinking code as a test. And it wont work either.

Please help or explain what is going wrong, i fear my device has permantly failed.

Here is the basic code. (NOTE i have updated this to add serial.print()"

// My Baseline

#include <Arduino.h>
#include <Arduino_PortentaBreakout.h>

// Declare constants and vars
  const int ledRed = LEDR; // Pin to use for the LED
  const int ledBlue = LEDB; // Pin to use for the LED
  const int ledGreen = LEDG; // Pin to use for the LED

void setup() {
  
  // Set LED pin to output mode
    pinMode(ledRed, OUTPUT);
    digitalWrite(ledRed, HIGH);

    pinMode(ledBlue, OUTPUT);
    digitalWrite(ledBlue, LOW);

    pinMode(ledGreen, OUTPUT);
    digitalWrite(ledGreen, HIGH); // Off
    
    Serial.begin(115200);
  //Serial1.begin(115200);
}

void loop() {
  Serial.print("Milli Seconds: " + String(millis()));

  digitalWrite(ledBlue, LOW);
  delay(500);
  digitalWrite(ledBlue, HIGH);
  delay(500);
  digitalWrite(ledBlue, LOW);
  delay(500);
  digitalWrite(ledBlue, HIGH);
  delay(500);

  digitalWrite(ledRed, LOW);
  delay(500);
  digitalWrite(ledRed, HIGH);
  delay(500);
  digitalWrite(ledRed, LOW);
  delay(500);
  digitalWrite(ledRed, HIGH);
  delay(500);

  digitalWrite(ledGreen, LOW);
  delay(500);
  digitalWrite(ledGreen, HIGH);
  delay(500);
  digitalWrite(ledGreen, LOW);
  delay(500);
  digitalWrite(ledGreen, HIGH);
  delay(500);
} 

platformio.ini

[env:portenta_h7_m7]

platform = ststm32

board = portenta_h7_m7

framework = arduino

debug_tool = stlink

upload_protocol = stlink

monitor_port = COM4

lib_deps =

https://github.com/maxgerhardt/Arduino_PortentaBreakout/archive/refs/heads/main.zip

build_flags =

-I include

-D TARGET_PORTENTA_H7_M7

here is the terminal

 *  Executing task: C:\Users\bucko\.platformio\penv\Scripts\platformio.exe run --target upload 

Processing portenta_h7_m7 (platform: ststm32; board: portenta_h7_m7; framework: arduino)
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/portenta_h7_m7.html
PLATFORM: ST STM32 (15.6.0) > Arduino Portenta H7 (M7 core)
HARDWARE: STM32H747XIH6 480MHz, 511.35KB RAM, 768KB Flash
DEBUG: Current (stlink) External (cmsis-dap, jlink, stlink)
PACKAGES:
 - framework-arduino-mbed @ 4.0.2
 - tool-dfuutil @ 1.11.0
 - tool-openocd @ 2.1100.211028 (11.0)
 - tool-stm32duino @ 1.0.2
 - toolchain-gccarmnoneeabi @ 1.70201.0 (7.2.1)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 45 compatible libraries
Scanning dependencies...
Dependency Graph
|-- Arduino_PortentaBreakout @ 1.0.1
Building in release mode
Checking size .pio\build\portenta_h7_m7\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]  13.5% (used 70800 bytes from 523624 bytes)
Flash: [===       ]  26.1% (used 205504 bytes from 786432 bytes)
Configuring upload protocol...
AVAILABLE: cmsis-dap, dfu, jlink, mbed, stlink
CURRENT: upload_protocol = stlink
Uploading .pio\build\portenta_h7_m7\firmware.bin
xPack OpenOCD x86_64 Open On-Chip Debugger 0.11.0+dev (2021-10-16-21:19)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 1

hla_swd
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08000480 msp: 0x24080000
** Programming Started **
Warn : Adding extra erase range, 0x08072620 .. 0x0807ffff
** Programming Finished **
** Verify Started **
** Verified OK **
** Resetting Target **
shutdown command invoked
==================================================================================================== [SUCCESS] Took 5.53 seconds ====================================================================================================
 *  Terminal will be reused by tasks, press any key to close it.

UPDate:
I have tried removing STLINK and programming via USB straight to STM32 (first double press into bootloader). It appears to upload with no errors but after loading there is no sign of life, nothing is flashing.

Going back to Arduino IDE with the above code it complies and runs. Good news my STM32 isnt bricked. Bad news is my problems are with PIO some where. So what is it? i have changed anything that i know of today.

Update: 17/4; 10am
Apon loading to the taget my code fails in

framework-arduino-mbed>variants>PORTENTA_H7_M7>variant.cpp

. the code fails in the this section and it appears that the millis() and micros() are not functioning as they are always = 0. Thus stuck in loop. If i comment out i can get past this point some times but it then fails when intialising USB. I assume its a timer in there as well.

BUT if i run the above code in Ardunio IDE it will work and print out the Serial.print("Milli Seconds: " + String(millis())); thus my STM32H7 is working fine. So my libaries are screwed somehow.

Here is the intial failure point

Update: 17/4 2:20pm
just in case anyone cares, I found an error in variant.cpp. I must have accidentally commented out a line. Stupid Stupid Stupid.
But my onboard USB still does not work when I am using the STLINK V3 in debug mode. But it will work when I upload (via STLINK) . i have my STLINK on COM3 via the arduino breakout board. And I monitor on COM4 from the STM32H7 onboard USB-C. In platfomio.ini i have set the monitor port. monitor_port = COM4

In device manager i can see COM3 and COM4, then i debug and COM4 disappears. Then when i stop debugging COM4 straight away reappears.
So now its a STLINK debug config issue I think.

I have fixed this… sort of anyway.
After 3 days of checking, verifying and bashing my head on the desk i have finally got my code all working again.

Basically I went back to my last working project version and copied the whole project folder, pasted and gave it the new version number. Then I copied all my .cpp and .h files from the corrupted project into my new project. And it worked. Only took 3 days.

Summary of the issue:

STM32H7 or Arduino Potenta H7 (so using arduino framework) STLINK V3 and monitor on COM4

1, Code would always work when i Platformio:Upload , but would fail when i used Run and Debug.

2, BUT only when i had code that called a hardware timer, in this case TIM14 (but i tried other timers), Still have no idea why this call caused the problem.

3, In Run and Debug it always breaks at init() within

framework-arduino-mbed>cores>audrino>main.cpp.

this is normal

4, Press F5 continue and code would crash at line 40 PluggableUSBD().begin();

5, Red led on PH7 board would flash 4 times indicating USB fault.

6, Rebuilt my project as per 1st paragraph

7, But i as i said before the code would work fine if i Platformio:Upload via my STLINK. Go figure.

Still no idea of the root cause, guessing i corrupted something but im not sure where.