Repository navigation
Debug not working when using USB CDC serial on Nucleo-L412 #2921
Copy link
Copy link
Closed
Labels
question ❓Usually converted as a discussionUsually converted as a discussion
Description
When using CDC USB serial as main Serial object, it is impossible to start a debug session.
I tested this several times back and forth, and also chose OpenOCD SWD as upload method, which fails as well when USB CDC is activated.
The sketch works fine in both configurations, there is serial output on the corresponding port (STLink serial or CDC serial) but debug is only possible without CDC serial.
I only tested with Nucleo-L412 and a custom STM32L412KBUx board.
This is a showstopper, because the application of my custom board relies on CDC Serial...
this is the GDB console output when debug fails
Waiting for gdb server to start...[2026-03-04T10:25:34.141Z] SERVER CONSOLE DEBUG: onBackendConnect: gdb-server session connected. You can switch to "DEBUG CONSOLE" to see GDB interactions.
"C:\\Users\\USERNAME\\AppData\\Local\\Arduino15\\packages\\STMicroelectronics\\tools\\xpack-openocd\\0.12.0-6/bin/openocd" -c "gdb_port 50000" -c "tcl_port 50001" -c "telnet_port 50002" -s "c:\\Users\\USERNAME\\AppData\\Local\\Temp\\.arduinoIDE-unsaved202624-2744-p2uc6v.br0gq\\sketch_mar4b" -f "C:/Program Files/Arduino IDE/resources/app/plugins/cortex-debug/extension/support/openocd-helpers.tcl" -f interface/stlink.cfg -f "C:\\Users\\USERNAME\\AppData\\Local\\Arduino15\\packages\\STMicroelectronics\\hardware\\stm32\\2.12.0/debugger/select_dapdirect.cfg" -f target/stm32l4x.cfg
xPack Open On-Chip Debugger 0.12.0+dev-01850-geb6f2745b-dirty (2025-02-07-10:08)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
DEPRECATED! use 'gdb port', not 'gdb_port'
DEPRECATED! use 'tcl port' not 'tcl_port'
DEPRECATED! use 'telnet port', not 'telnet_port'
CDRTOSConfigure
Info : Listening on port 50001 for tcl connections
Info : Listening on port 50002 for telnet connections
Info : STLINK V2J46M33 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.243713
Error: init mode failed (unable to connect to the target)
[2026-03-04T10:25:34.838Z] SERVER CONSOLE DEBUG: onBackendConnect: gdb-server session closed
GDB server session ended. This terminal will be reused, waiting for next session to start...
This is the output of the OpenOCD-STLink upload trial:
"C:\Users\USERNAME\AppData\Local\Arduino15\packages\STMicroelectronics\tools\xpack-openocd\0.12.0-6/bin/openocd.exe" -d2 -f interface/stlink.cfg -f target/stm32l4x.cfg -c "program {C:\Users\USERNAME\AppData\Local\arduino\sketches\6F0FFCCA1D6814E997BDF4F54B33F0BE/sketch_mar4b.ino.elf} verify reset exit"
xPack Open On-Chip Debugger 0.12.0+dev-01850-geb6f2745b-dirty (2025-02-07-10:08)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
debug_level: 2
Info : auto-selecting first available session transport "dapdirect_swd". To override use 'transport select <transport>'.
Info : STLINK V2J46M33 (API v2) VID:PID 0483:374B
Info : Target voltage: 3.243713
Error: init mode failed (unable to connect to the target)
** OpenOCD init failed **
shutdown command invoked
Failed uploading: uploading error: exit status 1
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
question ❓Usually converted as a discussionUsually converted as a discussion
Activity
PerennialNovice commentedon Mar 4, 2026
I have the same behaviour for a STM32L4R5ZITxP custom board
fpistm commentedon Mar 9, 2026
Hi @PerennialNovice
When you select USB, this array is parsed and all pin are initialised; ex for the Nucleo L412KB
Arduino_Core_STM32/variants/STM32L4xx/L412K(8-B)(T-U)_L422KB(T-U)/PeripheralPins.c
Lines 243 to 248 in 739acb7
But PA13 is used by the debug. So when it is initialized the debug connection is lost.
You can redefined this array on top of your sketch without PA13