cancel
Showing results for 
Search instead for 
Did you mean: 

Simple Interrupt Driven SPI Receive

za kia
Associate II

Can someone please write me a few lines to enable my code to receive an interrupt as a byte is coming?

For the life of me I cannot find any code examples. All I can find is application notes that go on and on about the capabilities of the STMicro SPI. Some code examples show implementations that are too advanced for my below average brain.

All I need is for the SPI to fire an interrupt as a byte is coming. That's all. My code polls Okay, but I need interrupts. I wrote the interrupt code before but that wasn't using HAL, which seems to have changed everything.

I did go through the HAL source code without understanding much.

Thanks.

I am using:

STM32L4RVITx

IAR Embedded Workbench for ARM 8.30.1.17148

6 REPLIES 6

From polling RXNE to interrupt should be:

  • enable interrupt in SPI by setting SPIx_CR2.RXNEIE
  • enable interrupt in NVIC using NVIC_EnableIRQ(SPIx_IRQn);
  • write the interrupt handler void SPIx_IRQHandler(void) { }
  • check in disassembly, that the compiler inserted correctly the handler's address into the vector table (there's a gotcha with name mangling if compiling as C++)

and a bonus hint, don't forget to check OVF flag and clear it if as that causes an interrupt too when RXNEIE is set, not only the RXNE flag.

You're not compelled to use HAL/Cube

The mechanics of the old SPL and register level interaction are still reasonably viable

STM32Cube_FW_L4_V1.12.0\Projects\NUCLEO-L4R5ZI\Examples\SPI\SPI_FullDuplex_ComIT

Will note that doing SPI via interrupts seems a bit of a circus, if you can blast blocks of data look at using DMA

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
za kia
Associate II

Thank you very much for your help.

I will be sure to post code once I get back to this and use your valuable input.

Regards.

za kia
Associate II

The problem is, and has been all along, with pausing the code.

After I pause the code SPI port closes and nothing is read afterwards either in polling or in interrupt modes.

Let's say SPI works fine, and I can send discrete commands from a C# master and receive responses from my SPI driver with no problem.

Then I place a breakpoint:

  • somewhere in the code, completely unrelated to SPI
  • while there is no SPI activity

After I resume execution no more data will be read by SPI.

This happens even after I create a fresh project from CubeMX with SPI as the only peripheral.

The custom board I am using was functioning fine with an STM32F072 installed. Then we replaced that processor with an STM32L4R7, leaving everything else intact.

I am lost!!!

> After I pause the code SPI port closes

What? You mean SPIx_CR1.SPE=0?

AFAIK there's no inherent timeout in the SPI module, so such behaviour is most probably imposed by the "libraries" used.

JW

za kia
Associate II

I should have said, "it appears as if it were closed".

In fact SPIx_CR1.SPE is still a one.

Sorry for the misinformation!

It's the behavior that resembles like a closed port. It won't detect or receive any more bytes.

Thanks