Programming the STspinG4 (G431VBT)


Since The Field Stack is powered up, its time to talk STM_Arduino_Core and how to make a Variant file for the project. @runger, you mentioned something about you have experience with this. If you can spare some time, and give some pointers that would be awesome.

I see there is generic_clock.c file in the folder. I suppose that is where all the appropriate clock configs generated by the STM_CUBE_MX should reside? Using a 24Mhz clock, I suppose we need to set that up in there. Maybe STM released some clock confiqs for the STspin32G4 Eval Board.

How can we specify the CAN FD pins and enable that feature?

Note: There is a small difference between MCU internal LDO voltage (3.3V exact) and the Analog Domain (3.29V), which is the one the ST-Link will read. I do not think it will pose a issue.

Are there any fuse settings we should be mindful of. Its my first time flashing a STM MCU, so I am not that familiar with the process.

Here is the STM project pinout, for refence

Note: The SWCLK needs to have a dual purpose, so it should be released for TIM8_CH2.

Also, the BOOT0 pin should be released for CAN FD RX and the nBOOT0/nBOOT1 option bit set up for bootloader space.

Arduino_Core_STM32/variants/STM32G4xx/G431V(6-8-B)T_G441VBT at main · stm32duino/Arduino_Core_STM32 · GitHub

Do you work in Platformio? It’s so much easier to make custom boards in that environment…

Tonight when I get home I plan to continue work on the Lepton…

But I could make you a Platformio project for the STSPIN as well, it should not take too long… if it’s useful to you?

1 Like

Yes, I have Atom installed with PlatformIO. I usually use that to flash my 3D printer.

I will try to go over the install guide. It throws some weird error at me. Do you use Development version of PlatformIO IDE and CORE?

Whats this Sunsetting about : Sunsetting Atom | The GitHub Blog

Some kind of security breach in the code…

Perhaps its time to upgrade to VSCode? I think its kind of the “successor” to Atom?

Some questions, since I’m passing time waiting for my flight?

  • do you use any external oscillators?
  • which pins are you using for the default serial output?
  • JTAG or SWD programming?

I know I could check your schematic but I’m a bit lazy… maybe if you could repost it or the link to it?

Oh Ok, I hope you have a good journey!

Yes, just as the EV-STspin32G4 I did put a 24Mhz oscillator/crystal on there.

SWD programming.

The default serial I guess should be virtual USB (emulated)?

Right. The schematic probably need a update, will do that and link it.

I have PlatformIO enabled in VScode globally

1 Like

GitHub - Juanduino/STSPIN32G4-FunQi-Stack: SimpleFoc Format Dev. for potential 8pin PWM stepper use w. 4th half-bridge (also 6pin PWM BLDC use w. extra FET driver for eg. brake-resistor)

Note: I see the STM project image above is not correct. The Schematic is correct w. NEO-LED on PA10 and STBY-CAN-FD on PB6.

Configure the project…

  1. Generate the code by clicking on _ Generate Code_ button and open the folder

Generate the project…

  1. Copy the STM32YYxxxxxx_FLASH.ld generated by STM32CubeMX in the variant folder and rename it: ldscript.ld

Example for the Nucleo-G0B1RE: STM32G0B1RETX_FLASH.ld

Is this how the pins are assigned ? Or whats up with this ?

I’m sorry I got stuck on the Lepton yesterday, but I will give it a shot later today…

1 Like

Ok thx :smiley:

I’m just getting to know PlatformIO. Making a ESC-G431 project now, and snooping around.

I see it created a STSTM32 directory with the boards folder. Supposedly that´s where the custom entry goes. Untill platformIO integrates the FOC SPING4 variant/board, I think it should reside in the repo with the schematics.

This was just randomly modified from the G431-ESC .json file in boards dir.

The Datasheet does say “The integrated MCU (STM32G431VBx3)”, so I am actually not sure “VBTx” is correct? Although it is the closest we get with STM32duino…

  "build": {
    "core": "stm32",
    "cpu": "cortex-m4",
    "extra_flags": "-DSTM32G4xx -DSTM32G431xx",
    "f_cpu": "170000000L",
    "framework_extra_flags": {
      "arduino": "-DCUSTOM_PERIPHERAL_PINS"
    "mcu": "stm32g431vbtx",
    "product_line": "STM32G431xx",
    "variant": "STM32G4xx/G431V(6-8-B)T_G441VBT"
  "connectivity": [
  "debug": {
    "default_tools": [
    "jlink_device": "STM32G431VBTx",
    "onboard_tools": [
    "openocd_target": "stm32g4x",
    "svd_path": "STM32G431xx.svd"
  "frameworks": [
  "name": "FOC_SPING4",
  "upload": {
    "maximum_ram_size": 32768,
    "maximum_size": 131072,
    "protocol": "stlink",
    "protocols": [
  "url": "",
  "vendor": "Venus Orbit Inc. (VOI) - By Juan-Antonio Søren E. Pedersen"

Ok, im pretty sure its the correct definition:

<command-line>: fatal error: variant_FOC_SPING4.h: No such file or directory

Hmm… Suppose its looking for a variant file…

"defines": [

This compiles for me.

I think you could put some code in the main.cpp file (it’s an empty program at the moment) that blinks a LED or something, and test it out.

I didn’t do any pins or peripherals yet, will add those.

1 Like

The clock is at 168MHz based on the external oscillator. Come to think of it, I didn’t check whether you’re using a crystal oscillator or clock source. One moment before you try anything :slight_smile:

Not entirely sure how to configure the clocks yet. I think it should use the internal clock for sys_clock and the external 24Mhz for PWM generation ? Ill need to look into how ST does it with their Eval-boards.

[SUCCESS] Took 17.20 seconds

Ok, fixed that.

Anyway, with a 24MHz external oscillator I don’t see any way to get 170MHz (max) out of the chip, best I could do was 168MHz. Don’t think the difference will be too painful, but still.

Seems 17MHz would be a good oscillator value to use. There also doesn’t seem to be a way that I can find to derive the USB clock (48MHz) from the external oscillator (24MHz) without significantly reducing the system core speed, which seems weird. But it has a dedicated 48MHz internal clock, seemingly for USB, so I’m using that.

1 Like

Yes it does have few internal clocks. I just went with the same 24Mhz as the STspin32G4-EV board.

Thank you verry kindly for taken the time ! Youll be the first to get a demo-board :smiley: I ordered 30 pcs

I just pushed some more build flags which should enable USB.

So Serial.begin(115200); should start a serial connection on USB. Wish I could try it out, but we’ll just have to do it like this for now :slight_smile:

Right, USB. I guess the only reason for flashing via ST-link the first time, is to set the nBOOT0 and nBOOT1 bit, to disable the BOOT0 pin and enable USB bootloader in the right mem-area? As I understand it, it comes with the bootloader already “installed”?

Edit: Or maybe that should be defined in the ldscript.ld?

Yes, you don’t have to do anything special for the boot loader, its built-in to the chip.
Via SWD and STLink, you can in normal circumstances just flash whenever you need to, no need to press any buttons. For the other methods, it can be necessary to hold down boot0 to get it into boot loader mode.
I’d recommend using SWD in the beginning, to eliminate a source of errors and problems.

Normally you don’t need to set any fuses, but it can be worth connecting with STM32CubeProgrammer (a free tool from ST) via the STLink. It will show which fuses are set and you can change them if needed.

Memory protection should obviously be disabled if you want to flash the chip.

You can set the bits to disable the boot pins, but that would only really be needed if you’re pulling it high I think, which you don’t seem to be.