Skip to main content
JBakk.1
Associate
May 13, 2020
Solved

Is it possible to shorten the duration time of the HAL_SPI_Transmit() function?

  • May 13, 2020
  • 2 replies
  • 2137 views

Hello everybody!

I was wondering if the time of the HAL_SPI_Transmit function can be shorter. (see the picture for more info) ,

I use the STM32L031K6 dev. board which runs at 32 MHz with a ARM Cortex -M0+. I set de the baudrate to (/2), which is max, for shorten the SPI_message time. Sadly, nothing changed to the time duration before and after the message. I use __HAL_SPI_ENABLE and __HAL_SPI_DISABLE to control the SSEL (slave select) by hardware control for further speed optimalisation.

I understand it could be possible that it is on the limit now but I think the time it takes to send the message out takes too long. (see picture)

0693W000000XKujQAG.jpg

Red is the SEL line and blue is the SDA line.

As you can see the time of the SPI message takes approx. 2 µs to transmit. The time before and after the message takes around 9 µs. It is already quite fast but my goal is to put everything withing 15 µs.

Code of what it is doing here:

 __HAL_SPI_ENABLE(&hspi1); //SSEL low

 HAL_SPI_Transmit(&hspi1, spiData, 1, 1); //Transmit data

 __HAL_SPI_DISABLE(&hspi1); //SSEL high

I know it is quite in depth but hopefully somebody can help me with this optimalisation.

Cheers!

Jasper

This topic has been closed for replies.
Best answer by berendi

Yes. Don't use HAL, it is slow. Use the faster and better documented register interface instead.

Follow the step-by-step procedures in the SPI chapter of the reference manual.

Untested code, I don't have any STM32L0.

SPI1->CR1 |= SPI_CR1_SPE;
while((SPI1->SR & SPI_SR_TXE) == 0)
 ;
SPI1->DR = *spidata;
while((SPI1->SR & SPI_SR_TXE) == 0)
 ;
while(SPI1->SR & SPI_SR_BSY)
 ;
SPI1->CR1 &= ~SPI_CR1_SPE;

2 replies

berendi
berendiBest answer
Principal
May 13, 2020

Yes. Don't use HAL, it is slow. Use the faster and better documented register interface instead.

Follow the step-by-step procedures in the SPI chapter of the reference manual.

Untested code, I don't have any STM32L0.

SPI1->CR1 |= SPI_CR1_SPE;
while((SPI1->SR & SPI_SR_TXE) == 0)
 ;
SPI1->DR = *spidata;
while((SPI1->SR & SPI_SR_TXE) == 0)
 ;
while(SPI1->SR & SPI_SR_BSY)
 ;
SPI1->CR1 &= ~SPI_CR1_SPE;

JBakk.1
JBakk.1Author
Associate
May 13, 2020

It works! much faster now! thanks a lot!

Tlai.1
Visitor II
August 1, 2022

Hello,

Do you have 16 bits version of code?

BR,

Terry