STSPIN32G4 problems

Hello Fellas

I’m trying to use stspin32g4 in my project, i’m almost there, but I encountered a problem

#include <Arduino.h>
#include "SimpleFOC.h"
#include "SimpleFOCDrivers.h"
#include "drivers/stspin32g4/STSPIN32G4.h"


STSPIN32G4 driver = STSPIN32G4();

BLDCMotor motor = BLDCMotor(6,0.069,200,0.000062);
// BLDCMotor motor = BLDCMotor(7);

void setup() {
    driver.voltage_power_supply = 24.0f;
    driver.init();
    motor.voltage_limit = driver.voltage_limit / 2.0f;
    motor.controller = MotionControlType::velocity_openloop;
    motor.linkDriver(&driver);
    motor.init();


    pinMode(PC2, OUTPUT);
    pinMode(PB10,INPUT);
    // Serial.begin(115200);
    // Serial.println("aaa");
    
}

void loop(){

    // STSPIN32G4NFault xxx;
    // xxx=driver.getNFaultRegister();

    // digitalWrite(PC2, 1);
    // delay(100);
    // digitalWrite(PC2, 0);
    // delay(100);

    // if(driver.isFault()) digitalWrite(PC2, 1);
    // else digitalWrite(PC2, 0);
    motor.move(5.0f); // 5 rad/s open loop
    delayMicroseconds(100); // STM32G4 is very fast, add a delay in open loop if we do nothing else
}

There is a signal on GLS1 (~50% PWM) but there is no on GLS2 and GLS3, obviously motor doesn’t move :wink:

Is it possible that there is something else with software or i have broken IC? (i’m sure it is solded well, because i can measure it)

Hey, is this in ArduinoIDE or in PlatformIO?

Could you add some debug output:

void setup() {
  Serial.begin(115200);
  SimpleFOCDebug::enable();
  delay(5000); // give some time to connect
  ...

and see what comes on the serial console?

platformIO, which one uart does it use? stm32g431 has 4 ones :smiley:

You can use any Serial object you want:

Serial3.begin(115200);
SimpleFOCDebug::enable(&Serial3);

I use these build_flags in my platformio.ini:

	-DSIMPLEFOC_STM32_DEBUG
	-DPIO_FRAMEWORK_ARDUINO_ENABLE_CDC
    -DUSBD_USE_CDC
    -DUSBCON

Then “Serial” is the USB serial. I don’t know if you have USB connected?

I use nucleo as stlink, so I can open serial port and connect RX TX, that’s what you mean?

Are you using the ST-Link’s VCP serial feature?

Either way, you will have to choose some serial port pins on your driver board, and connect them. Either using the ST-Link or by using another TTL Serial to USB adapter.

Remember to cross the RX and TX lines :slight_smile:

I’m using nucleo, should work (when i short RX to gnd it shows “something”) so my stspin32 don’t send anything on Serial (PA9 PA10), when i try to build with Serial1 (included “HardwareSerial.h”) i get

Processing genericSTM32G431VB (platform: ststm32; board: genericSTM32G431VB; framework: arduino)
-------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/ststm32/genericSTM32G431VB.html
PLATFORM: ST STM32 (17.0.0) > STM32G431VB
HARDWARE: STM32G431VBT6 170MHz, 32KB RAM, 126KB Flash
DEBUG: Current (stlink) On-board (stlink) External (blackmagic, cmsis-dap, jlink)
PACKAGES:
 - framework-arduinoststm32 @ 4.20600.231001 (2.6.0)
 - framework-cmsis @ 2.50700.210515 (5.7.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
|-- Adafruit NeoPixel @ 1.12.0
|-- SimpleFOCDrivers @ 1.0.6
|-- Simple FOC @ 2.3.2
Building in release mode
Compiling .pio\build\genericSTM32G431VB\src\main.cpp.o
Linking .pio\build\genericSTM32G431VB\firmware.elf
c:/users/tomek/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld.exe: warning: .pio/build/genericSTM32G431VB/firmware.elf has a LOAD segment 
with RWX permissions
c:/users/tomek/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld.exe: .pio/build/genericSTM32G431VB/src/main.cpp.o: in function `loop':      
main.cpp:(.text.loop+0x38): undefined reference to `Serial1'
c:/users/tomek/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/12.3.1/../../../../arm-none-eabi/bin/ld.exe: .pio/build/genericSTM32G431VB/src/main.cpp.o: in function `setup':     
main.cpp:(.text.setup+0x60): undefined reference to `Serial1'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\genericSTM32G431VB\firmware.elf] Error 1
===================================== [FAILED] Took 27.18 seconds =====================================
 *  The terminal process "C:\Users\tomek\.platformio\penv\Scripts\platformio.exe 'run', '--environment', 'genericSTM32G431VB'" terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it. 

I still have a problem with uart/serial port

i connected RX TX with my debugger and run code with stmcubeide which sends sth on serial monitor, works perfect

when im trying do it with platformio i get nothing

i tried Serial.begin(hardware uart), mySerial.begin(software uart “SoftwareSerial.h”)

even copy pasted hal init, clock init and usart init from stmcubeide (generated by cubemx, same which works in stmcubemx) and then tried to
HAL_UART_Transmit(&huart1, (uint8_t*)message, strlen(message), HAL_MAX_DELAY);
still nothing

Serial1.begin doesn’t compile, like i said earlier

pls help :frowning:

Are you comparing stmcubeide/hal to platformio/arduino framework ?

Have you checked which pins are used by default on your board variant ?

yes, i redefined them

  #define PIN_SERIAL_RX         PA10
  #define PIN_SERIAL_TX         PA9

Is the Serial1 port enabled? You can try to add a

-DSERIAL_UART_INSTANCE=1

to your platformio.ini, and then the normal “Serial” object should refer to USART1.

Make sure the options for USB serial are not set!

Additionally you need to set the correct pins, if they’re not the default ones:

-DPIN_SERIAL1_RX         PA_10
-DPIN_SERIAL1_TX         PA_9

Here you have to check the pinmap for the serial functions. Sometimes the pin you’re using needs to be specified using an alternate function, like: PA_10_ALT1

You can’t set any of this using #define - you have to put it into build flags so it acts on the entire build, including the framework.

Are you sure that platformio is looking for the right serial port? Annoyingly, for me, it will always default to the wrong port, either you can manuall set it in platformio.ini with monitor_port = or by pressing Ctrl T then P.

1 Like

I’ve recently got serial working on my custom stspin32g4 board (coincidence!!)

Had a problem too, I was using a stlink3-minie which has a virtual com port. Annoyingly their idea of rx / tx is opposite to what I was expecting (t_vcp_rx seems to means target rx not stlink rx). Grrr.

Anyway back to your problem. As a debugging step:

Try this:

import SoftwareSerial;

SoftwareSerial mySerial(PA10, PA9);
// SoftwareSerial mySerial(PA9, PA10);   <- Try this too

void setup() {
   mySerial.begin(115200); //<- or what ever you have setup as monitor_speed in platfromio.ini
   delay(1000);
   mySerial.printn("hello world");
}

Once you get SoftwareSerial working, you can try to sort out HardwareSerial e.g. following steps recommended by others.

In my case I can only get SoftwareSerial on this first revision of my board as my pcb layout is wrong :face_with_head_bandage:

1 Like

adding this solved problem

1 Like

Hey, I’m glad!

STM32duino is perhaps a little too flexible in its serial configuration options. Its really super-confusing, but usually a combination of setting the UART instance and the right pins leads to success…

Let us know how it goes with your board!

#include <Arduino.h>
#include "SimpleFOC.h"
#include "SimpleFOCDrivers.h"
#include "drivers/stspin32g4/STSPIN32G4.h"
#include "stm32g4xx_hal.h"
#include "HardwareSerial.h"


STSPIN32G4 driver = STSPIN32G4();
BLDCMotor motor = BLDCMotor(6,0.069,200,0.000062);


void setup() {
    Serial1.begin(115200);
    SimpleFOCDebug::enable(&Serial1);

    driver.voltage_power_supply = 24.0f;
    driver.init();
    motor.voltage_limit = driver.voltage_limit / 2.0f;
    motor.controller = MotionControlType::velocity_openloop;
    motor.linkDriver(&driver);
    motor.init();   

    pinMode(PC2, OUTPUT);
    
}

void loop(){
    motor.move(2.0f); // 5 rad/s open loop
    delayMicroseconds(100); // STM32G4 is very fast, add a delay in open loop if we do nothing else
}

On every single phase i have 50% PWM

that’s what debugger gives me :stuck_out_tongue: :
image

btw

 driver.setPwm(5,10,15);

works

Hmmmm… not sure what is happening there. Perhaps you still need to add a motor.enable() if you’re not calling the initFOC() method due to open loop mode?

@DarkMajster
Can you set

driver.voltage_limit = 4; // or what ever is appropriate

Before calling driver.init()

@runger - just got my stspin32g4 board running using your driver. Big :heart: for writing it! Are you going to merge from dev to main soon?

1 Like
#include <Arduino.h>
#include "SimpleFOC.h"
#include "SimpleFOCDrivers.h"
#include "drivers/stspin32g4/STSPIN32G4.h"
#include "stm32g4xx_hal.h"
#include "HardwareSerial.h"


STSPIN32G4 driver = STSPIN32G4();
BLDCMotor motor = BLDCMotor(6,0.069,200,0.000062);


void setup() {
    Serial1.begin(115200);
    SimpleFOCDebug::enable(&Serial1);

    driver.voltage_power_supply = 24.0f;
    driver.init();
    motor.voltage_limit = 4;
    motor.controller = MotionControlType::velocity_openloop;
    motor.linkDriver(&driver);
    motor.enable();
    motor.init();   

    pinMode(PC2, OUTPUT);

    
}

void loop(){
    motor.move(2.0f); // 5 rad/s open loop
    delayMicroseconds(100); // STM32G4 is very fast, add a delay in open loop if we do nothing else
}

still the same, tried motor.enable() after init also

image