Weird behavior: when I include BLDCMotor::loopFOC()(motor_M1.loopFOC()) in my main loop the HAL_SPI_TransmitReceive() (STM32Cube HAL library) have weird behavior

I am using SimpleFOC to drive motors with an stm32F446RETx microprocessor.
When I use alone BLDCMotor::loopFOC() it works.
When I use alone HAL_SPI_TransmitReceive() from STM32Cube HAL library it works.
But when I use both HAL_SPI_TransmitReceive() behave weirdly.

HAL_SPI_TransmitReceive() before adding motor_M1.loopFOC() at right. And HAL_SPI_TransmitReceive() after adding motor_M1.loopFOC() at left.

The stm32F446RETx is on my own board an act as a slave using HAL_SPI_TransmitReceive().

I use I2C sensors (MagneticSensorI2C(AS5048_I2C)) so it shouldn’t interfere.

I tried to stop interrupt during HAL_SPI_TransmitReceive(), but I still have this bug.

Hi @Myrddhim ,

Welcome to SimpleFOC!

Are you controlling 2 motors from the same MCU? this can be difficult, and especially with I2C sensors your loop-time will be quite slow…

With regards to your question, there is no reason using loopFOC() should interfere with SPI. So if you are having this problem these can be some things to look for:

  • do you use the same pins for the motor or sensor as you are using for SPI?

  • are you in open-loop or closed loop mode? If you were using the sensor, you could try without the sensor (open loop) to see if the sensor reading is causing the problem

  • if there is nothing wrong with the setup and software configuration, then you could use an oscilloscope to check the SPI signal lines. With this you could check if there is an electrical problem that is causing interference between the PWM and the SPI, for example…