Change Default Serial Port for STM32 Arduino

Then the first question I have to ask is, why not just enable the UART4 peripheral und use the according Serial4 object instead of Serial?

As you can see in the file Arduino_Core_STM32/cores/arduino/WSerial.h at main · stm32duino/Arduino_Core_STM32 · GitHub, the steps to enable this serial are to just define the ENABLE_HWSERIAL4 serial. Then just Serial4.println() stuff.

You can also select which serial the Serial object maps to by controlling the SERIAL_UART_INSTANCE, as you have shown. There you just need to modify this macro and the PIN_SERIAL_RX and PIN_SERIAL_TX macros, but not the definition of the pins in PeripheralPins.c.

But if it’s just for some debug code I’d just add

build_flags = -D ENABLE_HWSERIAL4

and

#define DebugSerial Serial4 

...
void func() { 
   DebugSerial.println("Debug strings");
}