3
\$\begingroup\$

I have been trying to understand SPI Interrupt i.e. what does it do and why is it used ? Reading about it on the web has not helped me. Can some one please explain it in a simple terms ?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ you list both stm32 and arduino. These are fairly different platforms, and to some extent, the behavior you're asking about will be platform specific. \$\endgroup\$ Aug 12, 2014 at 14:02

3 Answers 3

6
\$\begingroup\$

SPIE or "SPI Interrupt Enable" is used to enable the interrupt.

(Assuming you are in Master mode) There are two ways of transferring data using SPI:

1.Polling: Here you write data in SPI's data register (SPDR) and then poll on the flag for SPI Transmission complete (SPIF).

2.Interrupt based: In this case, when you enable Interrupt for SPI, you write data in SPDR. Controller will transmit the data and will raise an interrupt when transmission is completed.

A question may arise that why there is interrupt for transmission but not for the reception, I am tempted to answer but It would be better if you figure that out! :-)

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for your response, few followup questions: 1. Does every micro controller allow for 2 ways of transferring data ? 2. What happens if interrupt is not enabled (does it lead to errors) ? 3. Why is interrupt for transmission mode but not for reception ? \$\endgroup\$
    – Pravi
    Aug 12, 2014 at 11:50
  • \$\begingroup\$ @user44776 I would suggest you to read about SPI on Wikipedia and some other websites. \$\endgroup\$
    – Swanand
    Aug 13, 2014 at 2:43
3
\$\begingroup\$

Assuming you are talking about AVR, the SPIE (SPI Interrupt Enable) bit is to enable SPI related interrupts.

The number of interrupts an SPI peripheral can have will vary depending on the device, but if we take the Atmega8L as an example, it only has one interrupt.

Looking at the datasheet for what SPIE does, it says the following :

• Bit 7 – SPIE: SPI Interrupt Enable This bit causes the SPI interrupt to be executed if SPIF bit in the SPSR Register is set and the if the global interrupt enable bit in SREG is set.

Basically when SPIE AND SPIF AND the global interrupt enable bit is set, you will get get an interrupt.

What's the interrupt ? Well lets see what SPIF does. In the datashet, it says the following:

• Bit 7 – SPIF: SPI Interrupt Flag When a serial transfer is complete, the SPIF Flag is set. An interrupt is generated if SPIE in SPCR is set and global interrupts are enabled. If SS is an input and is driven low when the SPI is in Master mode, this will also set the SPIF Flag. SPIF is cleared by hardware when executing the corresponding interrupt Handling Vector. Alternatively, the SPIF bit is cleared by first reading the SPI Status Register with SPIF set, then accessing the SPI Data Register (SPDR).

The text here (I think) is pretty self explanatory, so I won't repeat it.

If you don't understand what a particular register or bit does, flip through the datasheet. Sometimes you might have to jump from bit to bit in order to get the full picture of how it works or what its supposed to be doing.

\$\endgroup\$
2
\$\begingroup\$

In a typical application that uses SPI communications, the microcontroller needs to receive data at some point. To receive data, the microcontroller has to read and write the SPI signals in a specific way as dictated by the protocol.

Now since you can't have the microcontroller constantly busy doing this, microcontrollers sometimes are equipped with a hardware module solely responsible for the SPI communications. This allows the microcontroller to work on other stuff instead of handling SPI.

The interrupt is raised by the SPI module and it is used to notify the microcontroller that a byte (or more) was received and it is ready for processing.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you for your response, is an interrupt raised when a byte is transmitted or received ? What happens if an interrupt is not enabled ? \$\endgroup\$
    – Pravi
    Aug 12, 2014 at 11:57
  • \$\begingroup\$ Depending on the microcontroller you could have a receive interrupt or transmit interrupt or both. If it is not enabled then there is no way to get a notification (alternatively you can poll the received flag) that a byte has arrived. If you fail to read the data out of the buffer, it could get overwritten or cause overflow. \$\endgroup\$
    – Demetris
    Aug 12, 2014 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.