STM32 build errors (relating to SerialUSB) seemingly out of no where

I seem to have ran into an issue yesterday.

Everything was building fine, I changed a Serial.printf statement, went to build again but then started receiving the following errors. I reverted the change, but still got the errors. Tried doing full clean, etc, redownloading deps, but the same issue remains. The only thing that changed between the working build and the non-working build was a change to a string, so I am thinking it might have just been coincidence and unrelated and that something else is wrong, but I can’t figure out what. I had not changed anything in my platformio.conf or anything on my system that I can recall at that time. I tried to restart just in case, as nothing else worked, but the issue remains.

I have a few devices profile/env’s in my config (current device is MakerBase xDrive using genericSTM32F405RG board profile), and noticed that the other stm32 based board (b-g431b-esc1) seems to have the same build issue/errors as well, and I had not made any changes to that profile or the code associated with that board in over a week. My esp32 based profile makes use of SerialUSB, etc, and still builds just fine.

Indexing .pio/build/xdrive_foc_0/libFrameworkArduino.a
Linking .pio/build/xdrive_foc_0/firmware.elf
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: warning: .pio/build/xdrive_foc_0/firmware.elf has a LOAD segment with RWX permissions
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/xdrive_foc_0/SrcWrapper/src/stm32/hw_config.c.o: in function `hw_config_init':
/home/mosthated/.platformio/packages/framework-arduinoststm32/libraries/SrcWrapper/src/stm32/hw_config.c:79: undefined reference to `USBD_CDC_init'
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/xdrive_foc_0/libbc3/SimpleCAN/BaseCAN.cpp.o: in function `BaseCAN::BaseCAN()':
/home/mosthated/_dev/languages/esp32/xdrive_mini/foc/.pio/libdeps/xdrive_foc_0/SimpleCAN/src/BaseCAN.cpp:7: undefined reference to `SerialUSB'
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/xdrive_foc_0/src/_xdrive/main.cpp.o: in function `CANHandler::HandleCanMessage(CanMsg const&, unsigned char const*)':
/home/mosthated/_dev/languages/esp32/canbus_lib/CANProfile/V1/CANProfile_V1.h:669: undefined reference to `SerialUSB'
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/xdrive_foc_0/src/_xdrive/main.cpp.o: in function `_GLOBAL__sub_I_message_':
/home/mosthated/_dev/languages/esp32/xdrive_mini/foc/src/_xdrive/main.cpp:71: undefined reference to `SerialUSB'
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/xdrive_foc_0/src/_xdrive/main.cpp.o: in function `CANBusSetup()':
/home/mosthated/_dev/languages/esp32/xdrive_mini/foc/src/_xdrive/main.cpp:121: undefined reference to `SerialUSB'
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/xdrive_foc_0/src/_xdrive/main.cpp.o: in function `setup':
/home/mosthated/_dev/languages/esp32/xdrive_mini/foc/src/_xdrive/main.cpp:130: undefined reference to `USBSerial::begin(unsigned long)'
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: /home/mosthated/_dev/languages/esp32/xdrive_mini/foc/src/_xdrive/main.cpp:133: undefined reference to `USBSerial::operator bool()'
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: /home/mosthated/_dev/languages/esp32/xdrive_mini/foc/src/_xdrive/main.cpp:170: undefined reference to `SerialUSB'
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/xdrive_foc_0/src/_xdrive/main.cpp.o: in function `CANBusLoop()':
/home/mosthated/_dev/languages/esp32/xdrive_mini/foc/src/_xdrive/main.cpp:486: undefined reference to `SerialUSB'
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/xdrive_foc_0/src/_xdrive/main.cpp.o: in function `loop':
/home/mosthated/_dev/languages/esp32/xdrive_mini/foc/src/_xdrive/main.cpp:614: undefined reference to `SerialUSB'
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/xdrive_foc_0/libFrameworkArduino.a(WSerial.cpp.o): in function `serialEventRun()':
/home/mosthated/.platformio/packages/framework-arduinoststm32/cores/arduino/WSerial.cpp:66: undefined reference to `USBSerial::available()'
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: /home/mosthated/.platformio/packages/framework-arduinoststm32/cores/arduino/WSerial.cpp:75: undefined reference to `SerialUSB'
collect2: error: ld returned 1 exit status
*** [.pio/build/xdrive_foc_0/firmware.elf] Error 1

It seems like it’s not finding SerialUSB related things at all anymore, but I didn’t add/remove any dependencies or build flags between the build that worked and the next build that didn’t work.

It feels like something with platformio packages or my system. Does anyone happen to have any ideas of what I might be able to check or fix?

Sounds like your project got messed up somehow between the different envs?

I’m guessing if you re-create a new project with just the env you need it will work again.

I crossed my fingers and gave it a go, but unfortunately, I still ended up with the same error messages.

Hmmm… this is very strange.

You’re using platformIO, so normally things don’t just change by themselves… in ArduinoIDE it is easy to change some menu options and cause these kind of errors, but in PlatformIO you have edit the platformio.ini to make changes.

The error is related to the USB support - if you enable USB serial in the build flags, this error should normally go away because then SerialUSB is defined.
Are you sure you didn’t change the platformio.ini? And are you sure its using the env you intend it to use? Is it the case that some of your envs have USB defined and some do not?

Did you maybe git-pull some new code?

Is the same library maybe present in different locations?

To test, I emptied the $HOME/.platformio packages and platform folders to redownload them.

Then in a newly created project I took my config I whittled things down to this below, yet still am getting the errors at the bottom.

[system]
main = 1

#custom common options
[common]
lib_archive = false
monitor_speed = 230400
lib_deps =
    askuric/Simple FOC@^2.3.3

build_flags =
    -D BAUDRATE=115200
    -D MONITOR_SPEED=230400
    -D RUN_MAIN=${system.main}

# --| xdrive --------------------------
# --|----------------------------------
[xdrive]
framework = arduino
platform = ststm32
board = genericSTM32F405RG
board_build.mcu = stm32f405rgt6
debug_tool = stlink
upload_protocol = stlink
lib_archive = ${common.lib_archive}
monitor_speed = ${common.monitor_speed}
monitor_eol = LF
lib_deps =
    ${common.lib_deps}

# --| Xdrive Mini ----------------
[env:xdrive_foc_0]
extends = xdrive
build_flags =
    ${common.build_flags}
    -D FOC_CONTROLLER=22
    -D HAL_CAN_MODULE_ENABLED
    -D PIO_FRAMEWORK_ARDUINO_NANOLIB_FLOAT_PRINTF
    -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
    -D ARDUINO_USB_CDC_ON_BOOT=1
    -D USBCON
    -D USBD_USE_CDC
#if defined(RUN_MAIN) && RUN_MAIN == 1 && defined(FOC_CONTROLLER) && FOC_CONTROLLER == 22

#include <SimpleFOC.h>

void setup(){}
void loop(){
    // put your main code here, to run repeatedly:
    Serial.println("Hello World!");
    delay(1000);
}

#endif
Processing xdrive_foc_0 (framework: arduino; platform: ststm32; board: genericSTM32F405RG)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/genericSTM32F405RG.html
PLATFORM: ST STM32 (17.5.0) > STM32F405RG (128k RAM. 1024k Flash)
HARDWARE: STM32F405RGT6 168MHz, 128KB RAM, 1MB Flash
DEBUG: Current (stlink) External (blackmagic, jlink, stlink)
PACKAGES: 
 - framework-arduinoststm32 @ 4.20801.0 (2.8.1) 
 - framework-cmsis @ 2.50900.0 (5.9.0) 
 - toolchain-gccarmnoneeabi @ 1.120301.0 (12.3.1)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 15 compatible libraries
Scanning dependencies...
Dependency Graph
|-- Simple FOC @ 2.3.4
Building in release mode
Linking .pio/build/xdrive_foc_0/firmware.elf
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: warning: .pio/build/xdrive_foc_0/firmware.elf has a LOAD segment with RWX permissions
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/xdrive_foc_0/SrcWrapper/src/stm32/hw_config.c.o: in function `hw_config_init':
/home/mosthated/.platformio/packages/framework-arduinoststm32/libraries/SrcWrapper/src/stm32/hw_config.c:79: undefined reference to `USBD_CDC_init'
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/xdrive_foc_0/src/main.two.cpp.o: in function `loop':
/home/mosthated/_dev/languages/esp32/xdrive_mini/xdrive/src/main.two.cpp:9: undefined reference to `SerialUSB'
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: .pio/build/xdrive_foc_0/libFrameworkArduino.a(WSerial.cpp.o): in function `serialEventRun()':
/home/mosthated/.platformio/packages/framework-arduinoststm32/cores/arduino/WSerial.cpp:66: undefined reference to `USBSerial::available()'
/home/mosthated/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld: /home/mosthated/.platformio/packages/framework-arduinoststm32/cores/arduino/WSerial.cpp:75: undefined reference to `SerialUSB'
collect2: error: ld returned 1 exit status
*** [.pio/build/xdrive_foc_0/firmware.elf] Error 1

Ok, looks like I got it figured out now after removing more and more. My original config had:

     -D PIO_FRAMEWORK_ARDUINO_ENABLE_CDC
     -D ARDUINO_USB_CDC_ON_BOOT=1
     -D USBD_USE_CDC

Which… I believe have been there for 2+ weeks, and had been working just fine. I know for sure the USBD_USE_CDC had been there as well as the ENABLE_CDC, as I pieced the config together originally from my b-g431b-esc1 config and parts of the config from:

https://community.simplefoc.com/t/read-a-potentiometer-continuously-on-stm32-and-analogread/4214/148?u=mosthated

For some reason, now I had to remove all 3 of them for it to build. While I am glad to see it build, I don’t understand why or what in the world changed to cause this behavior.

These are exactly the options that control the USB Serial - so I guess you’ve found it :slight_smile:

Sometimes it’s hard to keep track of all the changes one does - this is certainly something that’s happened to me before too :slight_smile:

I was under the impression that those were needed for Serial access over USB, ex. when you use Serial.print (except for the on_boot, which makes it wait for a serial connection to be opened)? I am not sure why they would suddenly make it not build when I am still using Serial.print statements.

Can it be linked to the latest stm32duino release 2 weeks ago?
Have you updated anything?

When you say “Serial.print” the “Serial” is a class instance, and the type can be either “HardwareSerial” or “USBSerial” depending on whether your default “Serial” object is configured as USB or as USART.

You had some of these options in there, so Serial was being defined as an object of type USBSerial, but not all of the required options to make it work, so the type USBSerial was not being built and the linker could therefore not find any of its methods…

Something along these lines.

The real mystery is what got changed/deleted to suddenly cause the problem, and why.

Something I do frequently is hit some keys while the wrong window has focus - then you overtype something unintentionally, for example.

Edit: Looks like this finally showed up in the package issues board. Just updating here in case anyone else runs into it.


phew

Ok, so I am not losing my mind. Setting the ststm32 platform option manually to the previous release fixed the issue fully back to how it was, with USB serial working again.

The newest release came out two days ago (Release 17.5.0 · platformio/platform-ststm32 · GitHub), which certainly tracks.

1 Like