Author Topic: STM32 HAL SPI Interrupts  (Read 13500 times)

0 Members and 1 Guest are viewing this topic.

Offline DubbieTopic starter

  • Supporter
  • ****
  • Posts: 1114
  • Country: nz
STM32 HAL SPI Interrupts
« on: April 30, 2017, 06:54:27 am »
Hi,

I am a bit new to the HAL drivers and this one has got my stumped.
I can't figure out how to use the HAL_SPI_TxCpltCallback.

all I am doing is defining the handler:

Code: [Select]
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi2) {
HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_SET);
}

as far as I understand, this is all I need to do.
However the handler is never being called. Any ideas why not?

Thanks for any hints anyone can offer.

R
 

Offline newbrain

  • Super Contributor
  • ***
  • Posts: 1714
  • Country: se
Re: STM32 HAL SPI Interrupts
« Reply #1 on: April 30, 2017, 11:09:03 am »
Yes, several ideas...
But, without your code, especially the initialization one and the SPI send part, it's quite difficult to give a meaningful hint. :horse:

Make sure that:
  • Interrupts are enabled for the SPI peripheral
  • The SPI ISR is calling HAL_SPI_IRQHandler()
  • HAL_SPI_Transmit_IT() is used (not HAL_SPI_Transmit().

If you've used CubeMX to generate the initialization code, and correctly set up the SPI to use interrupts, the only point to be taken care of is 3.
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline DubbieTopic starter

  • Supporter
  • ****
  • Posts: 1114
  • Country: nz
Re: STM32 HAL SPI Interrupts
« Reply #2 on: April 30, 2017, 11:28:54 am »
Thanks newbrain.
I wasn't using Transmit_IT, so right there is something to try.
Regarding #1, which interrupt should I enable? There are 3 options (not at a computer now so can't remember them)
Lastly, regarding #2, I'm not 100% sure what this means. Something to dig into!

Yes, I did use cubeMX to generate the setup.

Thanks for the valuable hints. I'll try again tomorrow.
 

Offline newbrain

  • Super Contributor
  • ***
  • Posts: 1714
  • Country: se
Re: STM32 HAL SPI Interrupts
« Reply #3 on: April 30, 2017, 12:30:20 pm »
Thanks newbrain.
I wasn't using Transmit_IT, so right there is something to try.
When the regular HAL_PPP_Transmit() calls are used, the "PPP" peripheral interrupt enable flags are not set.
So, one will never get an interrupt with the regular call, even though the global IRQ for the peripheral is enabled.
The flags are set inside the HAL_PPP_Transmit_IT() calls and reset by the HAL IRQ handler.

It's possible to call again the transmit IT API inside the transmit complete callback.

Quote
Regarding #1, which interrupt should I enable? There are 3 options (not at a computer now so can't remember them)
As said above, the three interrupt enable flags (SPI_IT_RXNE,  SPI_IT_TXE, SPI_IT_ERR) are taken care of by the HAL.
You only have to make sure the global interrupt is enabled, e.g. for SPI1 on an STM32F0xx:
Code: [Select]
    HAL_NVIC_SetPriority(SPI1_IRQn, 1, 0);
    HAL_NVIC_EnableIRQ(SPI1_IRQn);
Similar lines are placed usually at the end of the HAL_PPP_MspInit(PPP_HandleTypeDef*) function.
CubeMX takes care of this, if instructed to use interrupts with the peripheral.

Quote
Lastly, regarding #2, I'm not 100% sure what this means. Something to dig into!
If one wants the HAL to handle interrupts, a call to HAL_PPP_IRQHandler() needs to be placed inside the global ISR for the peripheral.
The specific ISR name used depends on the startup file, for the one provided by the HAL/CubeMX, it's PPP_IRQHandler(), but depending on the specific family of STM32 there might be variations (e.g. shared interrupts as CEC_CAN_IRQHandler() in an STMF0x2).
CubeMX will take care of generating the relevant code for this also, if interrupts are enabled for the peripheral.

Quote
Yes, I did use cubeMX to generate the setup.
So, if in CubeMX you've specified to use interrupts for the SPI, the only thing would be using the correct transmit routine.

Quote
Thanks for the valuable hints. I'll try again tomorrow.
You're welcome.
Nandemo wa shiranai wa yo, shitteru koto dake.
 

Offline DubbieTopic starter

  • Supporter
  • ****
  • Posts: 1114
  • Country: nz
Re: STM32 HAL SPI Interrupts
« Reply #4 on: April 30, 2017, 09:40:34 pm »
Ah! I see now the checkbox that I neglected to check in the CubeMX.
It's frustrating in hindsight how close I got to having it work :D

Thanks again newbrain. I wish the HAL manual was more than just a header file with a fancier font.
 

Offline DubbieTopic starter

  • Supporter
  • ****
  • Posts: 1114
  • Country: nz
Re: STM32 HAL SPI Interrupts
« Reply #5 on: May 01, 2017, 09:25:58 am »
Just for completeness and any future searchers, I'd just like to report that everything is working perfectly as expected now thanks to your help Newbrain.

I turned on interrupts in the SPI2 peripheral config as shown in the image attached.

This added the handlers into the right places.

then I setup a dummy test transmission as follows:
Code: [Select]
  while (1) {
HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_RESET);
(HAL_SPI_TransmitReceive_IT(&hspi2, (uint8_t*)aTxBuffer, (uint8_t*)aRxBuffer, (BUFFERSIZE + 1)));
HAL_Delay(500);
  }

and my callback was called as expected to de-assert the CS line in a non blocking way.
Code: [Select]
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi2) {
HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_SET);
}

Thanks again!

R
 

Offline newbrain

  • Super Contributor
  • ***
  • Posts: 1714
  • Country: se
Re: STM32 HAL SPI Interrupts
« Reply #6 on: May 01, 2017, 05:43:22 pm »
 :-+ Glad to hear it worked!

The HAL is considered "bloated" by many (I'd go for a more politically correct "curvy"), but it usually work as expected.
The problem is, as you also pointed out, to know what to expect, and some parts are really moronic (UART...).

I have run into relatively few bugs, compared e.g. with TI driverlib.

The manual is pointless (I can check anything much faster in the actual code with a good IDE!), I find the example projects much more helpful.
Nandemo wa shiranai wa yo, shitteru koto dake.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf