Motor not working correctly under STM32CubeIDE

I’ve ported STM32duino to STM32CubeIDE, so that I can use the SimpleFOC library (or any other Arduino library, for that matter) in a STM32CubeIDE project.

It works seamlessly with a few examples, including magnetic_sensor_spi_example.

However, open_loop_velocity_example and single_full_control_example don’t work correctly: the motor has the correct movement and speed, but there is a loud whistling, an impressive magnetic disruption of my LCD monitor, and the motor overheats.

So it seems I did something wrong in my porting of STM32duino. Based on the above symptoms, where do you think I need to look at? Which functional area is likely to be faulty?

Did you tested your hardware with a “regular” version of SimpleFOC?

I wold try to test the hardware using a standard implementation of SimpleFOC in order to see if the problem comes form your hardware or any bug introduced while porting SimpleFOC to work in a STM32CubeIDE environment

All is working fine both in Arduino IDE and PlatformIO. My port is clearly involved here.

One thing I did in my port is to disable the DAC module (because it’s hard to enable). Could this be the problem?

Many Arduino boards doesn’t have an integrated DAC … I don’t think that disabling DAC module could cause any problem. As far as I know SimpleFOC uses ADC for current sensing but not DAC.

@quentin - I ported the SimpleFOC library to work under STM32CubeIDE. It is modified to work with my specific hardware configuration, however it may be helpful as a starting point.

  • code for SimpleFOC voltage control only here.
  • code for SimpleFOC implementing inline current sense code (using position encoder) here.
1 Like

Wow, thanks a lot @ngalin, I will have a look.
It seems you initialize a TIM on each of the 3 PWM. Why is it necessary? Under Arduino IDE it seems to work by default…

@quentin - In the code I setup 3 channels of TIM1 in PWM mode. Are you asking why I do this? The reason is that the processor needs to be directed to output PWM signals on those three channels - so that we can then change the duty cycle of the these three PWM channels to get the motor to spin. This doesn’t really happen by default within the Arduino IDE, all that heavy lifting is abstracted away from the end user, and setup in the background depending on what motor pins are selected.

There is SimpleFOC and STM32duino library code that goes ahead and setups the relevant timer channels. Here’s the SimpleFOC code that does some of this:

The beauty of the SimpleFOC code is that it’s general in nature, you are given the choice to select a number of different digital pins and the relevant STM32 timers+channels will be correctly setup in the background for you. I ported the code into the STM32CubeIDE because for my application I need lower level control of various parameters, as well as the advanced debugging features within the STM32CubeIDE. Hope you find it useful for your application too.

1 Like

Thanks a million, @ngalin. Your detailed explanation unlocked my understanding and I quickly found the problem. The origin was in SimpleFOC code, not in my STM32duino port.

For reference, I filed an issue here: