# Programming the STspinG4 (G431VBT)

**URL:** <https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081>\
**Category:** hardware support\
**Tags:** g431vbt, stm\_core\
**Created:** [March 5, 2023, 11:39am UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081 "2023-03-05T11:39:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juan-Antonio\_Soren\_E](https://community.simplefoc.com/user_avatar/community.simplefoc.com/juan-antonio_soren_e/32/9969_2.png) [@Juan-Antonio\_Soren\_E](https://community.simplefoc.com/u/Juan-Antonio_Soren_E)\
**Post date:** [March 5, 2023, 11:39am UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/1 "2023-03-05T11:39:28Z")

</div>

Hi,

Since [The Field Stack](https://community.simplefoc.com/t/the-field-stack-stspin32g4/3032) 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

 ![CUBE_MX_PROJECT_GO_GO_GO](https://community.simplefoc.com/uploads/default/original/2X/9/94190ed7b41b7a1d797195d57ec567ed6ace98e9.jpeg)

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](https://github.com/stm32duino/Arduino_Core_STM32/tree/main/variants/STM32G4xx/G431V(6-8-B)T_G441VBT)

---

<div class="post-metadata">

**Author:** ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)\
**Post date:** [March 5, 2023, 12:50pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/2 "2023-03-05T12:50:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Juan-Antonio\_Soren\_E](https://community.simplefoc.com/user_avatar/community.simplefoc.com/juan-antonio_soren_e/32/9969_2.png) [@Juan-Antonio\_Soren\_E](https://community.simplefoc.com/u/Juan-Antonio_Soren_E)\
**Post date:** [March 5, 2023, 1:09pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/3 "2023-03-05T13:09:39Z")

</div>

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](https://github.blog/2022-06-08-sunsetting-atom/)

---

<div class="post-metadata">

**Author:** ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)\
**Post date:** [March 5, 2023, 1:19pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/4 "2023-03-05T13:19:32Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Juan-Antonio\_Soren\_E](https://community.simplefoc.com/user_avatar/community.simplefoc.com/juan-antonio_soren_e/32/9969_2.png) [@Juan-Antonio\_Soren\_E](https://community.simplefoc.com/u/Juan-Antonio_Soren_E)\
**Post date:** [March 5, 2023, 1:24pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/5 "2023-03-05T13:24:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Juan-Antonio\_Soren\_E](https://community.simplefoc.com/user_avatar/community.simplefoc.com/juan-antonio_soren_e/32/9969_2.png) [@Juan-Antonio\_Soren\_E](https://community.simplefoc.com/u/Juan-Antonio_Soren_E)\
**Post date:** [March 5, 2023, 1:31pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/6 "2023-03-05T13:31:29Z")

</div>

[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)](https://github.com/Juanduino/STSPIN32G4-FunQi-Stack)

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.

---

<div class="post-metadata">

**Author:** ![Juan-Antonio\_Soren\_E](https://community.simplefoc.com/user_avatar/community.simplefoc.com/juan-antonio_soren_e/32/9969_2.png) [@Juan-Antonio\_Soren\_E](https://community.simplefoc.com/u/Juan-Antonio_Soren_E)\
**Post date:** [March 6, 2023, 7:01pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/7 "2023-03-06T19:01:48Z")

</div>

_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](http://www.st.com/en/development-tools/stm32cubemx.html) in the variant folder and rename it: `ldscript.ld`

**Example** for the [Nucleo-G0B1RE](https://www.st.com/en/evaluation-tools/nucleo-g0b1re.html): `STM32G0B1RETX_FLASH.ld`

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

---

<div class="post-metadata">

**Author:** ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)\
**Post date:** [March 6, 2023, 8:30pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/8 "2023-03-06T20:30:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Juan-Antonio\_Soren\_E](https://community.simplefoc.com/user_avatar/community.simplefoc.com/juan-antonio_soren_e/32/9969_2.png) [@Juan-Antonio\_Soren\_E](https://community.simplefoc.com/u/Juan-Antonio_Soren_E)\
**Post date:** [March 6, 2023, 8:32pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/9 "2023-03-06T20:32:12Z")

</div>

Ok thx 😃

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.

---

<div class="post-metadata">

**Author:** ![Juan-Antonio\_Soren\_E](https://community.simplefoc.com/user_avatar/community.simplefoc.com/juan-antonio_soren_e/32/9969_2.png) [@Juan-Antonio\_Soren\_E](https://community.simplefoc.com/u/Juan-Antonio_Soren_E)\
**Post date:** [March 6, 2023, 8:58pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/10 "2023-03-06T20:58:38Z")

</div>

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…

```auto
{
  "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": [
    "can"
  ],
  "debug": {
    "default_tools": [
      "stlink"
    ],
    "jlink_device": "STM32G431VBTx",
    "onboard_tools": [
      "stlink"
    ],
    "openocd_target": "stm32g4x",
    "svd_path": "STM32G431xx.svd"
  },
  "frameworks": [
    "arduino",
    "cmsis",
    "stm32cube"
  ],
  "name": "FOC_SPING4",
  "upload": {
    "maximum_ram_size": 32768,
    "maximum_size": 131072,
    "protocol": "stlink",
    "protocols": [
      "stlink",
      "jlink",
      "cmsis-dap",
      "blackmagic",
      "mbed"
    ]
  },
  "url": "https://github.com/Juanduino/STSPIN32G4-FunQi-Stack",
  "vendor": "Venus Orbit Inc. (VOI) - By Juan-Antonio Søren E. Pedersen"
}

```

Ok, im pretty sure its the correct definition:

 ![VBTx](https://community.simplefoc.com/uploads/default/original/2X/e/ead90ea11c610daeba685848742eb73f3c5ff7fe.jpeg)

---

<div class="post-metadata">

**Author:** ![Juan-Antonio\_Soren\_E](https://community.simplefoc.com/user_avatar/community.simplefoc.com/juan-antonio_soren_e/32/9969_2.png) [@Juan-Antonio\_Soren\_E](https://community.simplefoc.com/u/Juan-Antonio_Soren_E)\
**Post date:** [March 6, 2023, 10:35pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/11 "2023-03-06T22:35:46Z")

</div>

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

Hmm… Suppose its looking for a variant file…

```auto
"defines": [
                "PLATFORMIO=60106",
                "STM32G4xx",
                "STM32G431xx",
                "STM32G4xx",
                "ARDUINO=10808",
                "ARDUINO_ARCH_STM32",
                "ARDUINO_FOC_SPING4",
                "BOARD_NAME=\"FOC_SPING4\"",
                "HAL_UART_MODULE_ENABLED",
                "USE_FULL_LL_DRIVER",
                "VARIANT_H=\"variant_FOC_SPING4.h\"",
                "NDEBUG",
                "CUSTOM_PERIPHERAL_PINS",
                ""
            ],

```

---

<div class="post-metadata">

**Author:** ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)\
**Post date:** [March 6, 2023, 10:42pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/12 "2023-03-06T22:42:02Z")

</div>

[https://github.com/runger1101001/simplefoc\_funqi\_stack\_firmware](https://github.com/runger1101001/simplefoc_funqi_stack_firmware)

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.

---

<div class="post-metadata">

**Author:** ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)\
**Post date:** [March 6, 2023, 10:43pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/13 "2023-03-06T22:43:50Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Juan-Antonio\_Soren\_E](https://community.simplefoc.com/user_avatar/community.simplefoc.com/juan-antonio_soren_e/32/9969_2.png) [@Juan-Antonio\_Soren\_E](https://community.simplefoc.com/u/Juan-Antonio_Soren_E)\
**Post date:** [March 6, 2023, 10:46pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/14 "2023-03-06T22:46:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Juan-Antonio\_Soren\_E](https://community.simplefoc.com/user_avatar/community.simplefoc.com/juan-antonio_soren_e/32/9969_2.png) [@Juan-Antonio\_Soren\_E](https://community.simplefoc.com/u/Juan-Antonio_Soren_E)\
**Post date:** [March 6, 2023, 10:50pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/15 "2023-03-06T22:50:34Z")

</div>

`[SUCCESS] Took 17.20 seconds`

---

<div class="post-metadata">

**Author:** ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)\
**Post date:** [March 6, 2023, 10:53pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/16 "2023-03-06T22:53:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Juan-Antonio\_Soren\_E](https://community.simplefoc.com/user_avatar/community.simplefoc.com/juan-antonio_soren_e/32/9969_2.png) [@Juan-Antonio\_Soren\_E](https://community.simplefoc.com/u/Juan-Antonio_Soren_E)\
**Post date:** [March 6, 2023, 10:55pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/17 "2023-03-06T22:55:38Z")

</div>

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 😃 I ordered 30 pcs

---

<div class="post-metadata">

**Author:** ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)\
**Post date:** [March 6, 2023, 11:00pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/18 "2023-03-06T23:00:15Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Juan-Antonio\_Soren\_E](https://community.simplefoc.com/user_avatar/community.simplefoc.com/juan-antonio_soren_e/32/9969_2.png) [@Juan-Antonio\_Soren\_E](https://community.simplefoc.com/u/Juan-Antonio_Soren_E)\
**Post date:** [March 6, 2023, 11:04pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/19 "2023-03-06T23:04:42Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)\
**Post date:** [March 6, 2023, 11:16pm UTC](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081/20 "2023-03-06T23:16:39Z")

</div>

> [@Juan-Antonio\_Soren\_E](#):
>
> 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”?

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.

[Next page](https://community.simplefoc.com/t/programming-the-stsping4-g431vbt/3081.md?page=2)
