STM32 HAL SPI Interrupts - Page 1
Forum section: electronics / general

author: Dubbie
author_rank: Unknown
timestamp: April 30, 2017, 06:54:27 am
content: 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: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

author: newbrain
author_rank: Super Contributor
timestamp: April 30, 2017, 11:09:03 am
content: 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.Make sure that:Interrupts are enabled for the SPI peripheralThe SPI ISR is callingHAL_SPI_IRQHandler()HAL_SPI_Transmit_IT()is used (notHAL_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.

author: Dubbie
author_rank: Unknown
timestamp: April 30, 2017, 11:28:54 am
content: 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.

author: newbrain
author_rank: Super Contributor
timestamp: April 30, 2017, 12:30:20 pm
content: Quote from: Dubbie on April 30, 2017, 11:28:54 amWhen the regularHAL_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 theHAL_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.QuoteAs 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:HAL_NVIC_SetPriority(SPI1_IRQn, 1, 0);HAL_NVIC_EnableIRQ(SPI1_IRQn);Similar lines are placed usually at the end of theHAL_PPP_MspInit(PPP_HandleTypeDef*)function.CubeMX takes care of this, if instructed to use interrupts with the peripheral.QuoteIf one wants the HAL to handle interrupts, a call toHAL_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'sPPP_IRQHandler(), but depending on the specific family of STM32 there might be variations (e.g. shared interrupts asCEC_CAN_IRQHandler()in an STMF0x2).CubeMX will take care of generating the relevant code for this also, if interrupts are enabled for the peripheral.QuoteSo, ifin CubeMXyou've specified to use interrupts for the SPI, the only thing would be using the correct transmit routine.QuoteYou're welcome.

author: Dubbie
author_rank: Unknown
timestamp: April 30, 2017, 09:40:34 pm
content: 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 workThanks again newbrain. I wish the HAL manual was more than just a header file with a fancier font.

author: Dubbie
author_rank: Unknown
timestamp: May 01, 2017, 09:25:58 am
content: 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: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:void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi2) {HAL_GPIO_WritePin(SPI2_CS_GPIO_Port, SPI2_CS_Pin, GPIO_PIN_SET);}Thanks again!R

author: newbrain
author_rank: Super Contributor
timestamp: May 01, 2017, 05:43:22 pm
content: 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.