Trouble adding STM32G031Fxxx to pio

I tried to add the STM32G031F to platform IO.
inspired by the discussion here Custom Board definition for STM32G031G8.
I added the .platformio\packages\framework-arduinoststm32\variants\STM32G0xx\G031F(4-6-8)P_G031Y8Y_G041F(6-8)P_G041Y8Y – copied from github/stm32duino.
Created a genericSTM32G031F8.json file in the project boards folder and a plotformio.ini configuration.

[env:custom_g031f8p6]
platform = ststm32
board = genericSTM32G031F8
board_build.mcu = stm32g031f8p6
framework = arduino
upload_protocol = stlink
build_flags = -D ARDUINO_GENERIC_G031F8PX
lib_deps = adafruit/Adafruit NeoPixel@^1.12.4

but I always get a this error:
: fatal error: variant_generic.h: No such file or directory
but in the variants\STM32G0xx\G031F(4-6-8)P_G031Y8Y_G041F(6-8)P_G041Y8Y folder there is the variant_generic.h file.

It seems like the new variant folder is ignored - maybe not in the search path …

How can I fix this?
Any suggestions are highly appreciated - Thanks

Why? The latest version already has that folder.

In any case, there’s a bit of magic involved with the naming of the board definition file and its fields. E.g. the mcu field should have the “full” name with the package type and temperature.

You will find if you create any arbitrary project (e.g. Arduino Uno + framework Arduino), then create the file boards/genericSTM32G031F8.json with content

{
    "build": {
      "core": "stm32",
      "cpu": "cortex-m0plus",
      "extra_flags": "-DSTM32G0 -DSTM32G0xx -DSTM32G031xx",
      "f_cpu": "64000000L",
      "framework_extra_flags": {
        "arduino": "-D__CORTEX_SC=0"
      },
      "mcu": "stm32g031f8p6",
      "product_line": "STM32G031xx",
      "variant": "STM32G0xx/G031F(4-6-8)P_G031Y8Y_G041F(6-8)P_G041Y8Y"
    },
    "debug": {
      "default_tools": [
        "stlink"
      ],
      "jlink_device": "STM32G031F8",
      "onboard_tools": [
        "stlink"
      ],
      "openocd_target": "stm32g0x",
      "svd_path": "STM32G031.svd"
    },
    "frameworks": [
      "arduino",
      "cmsis",
      "stm32cube"
    ],
    "name": "Generic STM32G031F8",
    "upload": {
      "maximum_ram_size": 8192,
      "maximum_size": 65536,
      "protocol": "stlink",
      "protocols": [
        "blackmagic",
        "cmsis-dap",
        "dfu",
        "jlink",
        "serial",
        "stlink",
        "mbed"
      ]
    },
    "url": "https://www.st.com/en/microcontrollers-microprocessors/stm32g031f8.html",
    "vendor": "FB"
  }

then overwrite the platformio.ini as

[env:genericSTM32G031F8]
platform = ststm32@19.0.0
board = genericSTM32G031F8
framework = arduino

then eveything will compile out of the box, e.g. src/main.cpp

#include <Arduino.h>
#define LED PA3
void setup() {
    pinMode(LED, OUTPUT);
}

void loop() {
    digitalWrite(LED, HIGH);
    delay(1000);
    digitalWrite(LED, LOW);
    delay(1000);
}

Thanks @maxgerhardt → great help and so fast.

I might have been on an older ststm32 version could be the reason that the ST32G0xx folder was not there.

The issue that led to the error was the “ST32G0xx/” was missing in the variant text in my .json file. :frowning:

"variant": "STM32G0xx/G031F(4-6-8)P_G031Y8Y_G041F(6-8)P_G041Y8Y"

Next issue would have been the mcu: string with “6” missing at the end.

So it compiles and downloads now - Thanks a lot for the fast help.
Felix

In the meantime I have added more code to my program for the STM32GO31F8P6.
The code compiles fine but during programming I get warnings and errors (see below). Note: some smaller test code gets programmed fine without warnings about flash banks…

Any hint what the cause could be? Where to search?

  • Program too large for the G031x8?
  • IO configuration bad?
  • some other configuration screwed?

**** build output ****

Building in release mode
Checking size .pio\build\G031_serial\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [===       ]  29.5% (used 2416 bytes from 8192 bytes)
Flash: [=====     ]  51.9% (used 34028 bytes from 65536 bytes)
Configuring upload protocol...
AVAILABLE: blackmagic, cmsis-dap, dfu, jlink, mbed, serial, stlink
CURRENT: upload_protocol = stlink
Uploading .pio\build\G031_serial\firmware.elf
xPack Open On-Chip Debugger 0.12.0-01004-g9ea7f3d64-dirty (2023-01-30-15:04)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
debug_level: 1

hla_swd
[stm32g0x.cpu] halted due to breakpoint, current mode: Thread 
xPSR: 0x61000000 pc: 0x2000002e msp: 0x20002000
[stm32g0x.cpu] halted due to debug-request, current mode: Thread 
xPSR: 0xf1000000 pc: 0x080062a0 msp: 0x20002000
** Programming Started **
Warn : no flash bank found for address 0x08008000
Warn : no flash bank found for address 0x08008460
** Programming Finished **
** Verify Started **
Error: checksum mismatch - attempting binary compare
embedded:startup.tcl:1516: Error: ** Verify Failed **
in procedure 'program'
in procedure 'program_error' called at file "embedded:startup.tcl", line 1577
at file "embedded:startup.tcl", line 1516
*** [upload] Error 1

So OpenOCD tried to flash the firmware, but programming the first address above 32K failed. (0x8000 = 32 kilobytes)

Are you sure you have STM32G031F8P6? It should have 64KB and the program should be flashable. If you have a G032F6, then it only has 32K.

Thank you @maxgerhardt.

Yes I was sure … but it is in fact a G031F6p6 :frowning:

Rewriting the firmware in other frameworks (e.g., framework = stm32cube) can reduce used flash size significantly, but at the cost of being more difficult to program.

Yes, thank you.

I have also noticed the arduino framework is eating quite some space on these STM32s.

But the hassle of mastering multiple framworks is not very appealing to me.
I will rather try to improve my arduino framework skills and spend a few extra cents for the 64kB version of the WeAcct G032Fx boards. (The formfactor of those boards is very small and fits good for small RC model tinkering.)