cancel
Showing results for 
Search instead for 
Did you mean: 

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

JBakk.1
Associate

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

1 ACCEPTED SOLUTION

Accepted Solutions
berendi
Principal

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;

View solution in original post

3 REPLIES 3
berendi
Principal

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;

It works! much faster now! thanks a lot!

Tlai.1
Associate

Hello,

Do you have 16 bits version of code?

BR,

Terry