Interrupt of SPI

Go To Last Post
33 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone,

I am using attiny88 and want to use spi in interrupt mode. I made my attiny88 as the slave and let the other device, master, control the chip select line. 

Now I am having a bit confusion. I am not getting enough info how and when the spi interrupt is generated. I have to send data from attiny88, slave, to master, another device. 

 

Thanks in advance.

Akhilesh Gangwar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

With spi, the master runs the show. If the slave wants to send something, then the master must initiate it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, master will initiate but when the interrupt will be generated?

Akhilesh Gangwar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The slave interrupt will be raised when the master has sent a byte.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sending is done by slave. Now I can relate. The master interrupt will be raised when the slave has sent 1 byte. Am I right?

Akhilesh Gangwar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

john.ab wrote:
I am not getting enough info how and when the spi interrupt is generated

ATTiny88 Datasheet wrote:

When configured as a Slave, the SPI interface will remain sleeping with MISO tri-stated as long as the SS pin is driven high.

In this state, software may update the contents of the SPI Data Register, SPDR, but the data will not be shifted out by incoming clock pulses on the SCK pin until the SS pin is driven low.

As one byte has been completely shifted, the end of Transmission Flag, SPIF is set. If the SPI Interrupt Enable bit, SPIE, in the SPCR Register is set, an interrupt is requested.

The Slave may continue to place new data to be sent into SPDR before reading the incoming data.

The last incoming byte will be kept in the Buffer Register for later use.

 

http://ww1.microchip.com/downloads/en/DeviceDoc/doc8008.pdf

What is unclear there?

What more information do you require?

 

Have you checked-out the Application Notes available on the 'Documents' tab of the Product Page:

 

http://www.microchip.com/wwwproducts/en/ATtiny88

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looks like you need to review SPI basics - not specific to AVR:

 

SPI is always full-duplex - as each bit is shifted out from the master, a bit is shifted in from the slave.

 

Effectively, The master "pushes" its bits out to the slave while at the same time "pulling" bits from the slave.

 

https://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus

 

 

EDIT

 

typo

 

EDIT 2

 

See the diagram in the datasheet:

 

 

 

EDIT 3

 

So I was just thinking, "what we need here is an animation to illustrate this" - and google provides:

 

SPI Working - Data Transfer

 

from https://electrosome.com/spi/

 

 

EDIT 4

 

I separated-out the steps of the animation:

 

https://www.avrfreaks.net/commen...

 

and gave a summary of what SPI is - and isn't:

 

https://www.avrfreaks.net/commen...

 

 

#HowSpiWorks

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Jan 30, 2020 - 10:22 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can we read the slave select pin to check whether it is low or high? My code is too big and I have to send multiple bytes at a time but only when ss pin is done low by master. Because at the point where I have to send the bytes, it will take a bit time to reach the execution at that point. So I want to make spi sending just after reading the ss pin. 

Akhilesh Gangwar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But the whole point of the hardware SPI peripheral is that it does all that for you!!

 

Read the quote from the datasheet again - the SPI peripheral itself is monitoring the SS pin, and its state controls the peripheral's operation.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK thanks a lot. 

Akhilesh Gangwar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can we read the slave select pin to check whether it is low or high?

Of course.  However, once the master begins its data clocking, it is too late to get the byte ready on the slave side...whatever was set in the slave is already being read out.  So the byte needs to be ready before the master attempts to grab it.  Of course you probably have some handshaking protocol (master requests A (while reading previous request),  set up A for next round.  Next round master requests D, (while reading A) ...set up D, mater requests B (while reading D), set up  B...)

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Thu. May 3, 2018 - 07:22 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Note that the datasheet does include code examples.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
the datasheet does include

Someone should have mentioned "the datasheet" earlier.

 

Now, what does the datasheet say about SPI interrupts?  When is the SPIF flag set?

• 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)
 

So after a careful reading of the datasheet why should knowing whether you are selected or not be a concern, as you are given an event to indicate that?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Thu. May 3, 2018 - 02:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
Someone should have mentioned "the datasheet" earlier.

in #6, perhaps ... ?

 

cheeky

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

One of the ways to do what the OP wants is to add another line from the slave to the master. Typically, an "INT" signal. Slave yanks the line when it wants to send something. It (can) put the first data byte into the SPI buffer at that point so that the master will read it on the very first transfer. More common is to have the master send a "register number" or "transfer code number" or such as the first byte for which the slave sends nothing. It has a little time to put the first byte into its buffer, then the master starts clocking the data.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
for which the slave sends nothing.

Just to make it crystal clear for the OP:

 

Note that the slave can never send nothing; it will always send something - even if it is only whatever junk happened to be left in the data register.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Correct. Poor wording, there. Should have written something like "dummy byte" or "don't care byte" or such.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 Typically, an "INT" signal. Slave yanks the line when it wants to send something. It (can) put the first data byte into the SPI buffer at that point so that the master will read it on the very first transfer

It's probably better controlled if the slave yanks the line, causing the master to ask "what do you want", then the slave puts the data, then the master gets it.  Otherwise there might be contention if the master was already on the edge of sending it's own new request at the same instant the slave yanked the line--thereby the master bringing in the byte that it thought it requested previously, but it would mistakenly be the byte the slave jumped in with).  In other words, to avoid contention the slave only places certain data after the master requests it, though the slave can signal the master to ask by yanking a line (basically jumping into the master request's queue, or tickling the queue to resume NOW)

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Thu. May 3, 2018 - 05:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

With a 5-wire SPI connection (MISO, MOSI, SCK plus both SS pins being driven from the other AVR) you can implement a bi-directional bus with auto master/slave switching. 

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

On the subject of yanking ones line.......

 

The OP needs to know that should they choose to implement such a process, you should use a diode in series on the pin of each AVR that will pull this line low.  And at the master you will need a pull up resistor on the input that will serve as this interrupt.

 

Tying output pins together is not going to work, and will also cause erratic behavior of the system.

 

 

Separate note:

Are these avr's all on one board, or are you looking to have multiple boards and connect them together with SPI?  If the latter, keep in mind that SPI is not meant for long distance communications and you might want to look at an AVR with a USART in it since you can implement RS485 very easily which will possibly make your life a lot easier.

 

Just my two pence.

 

East Coast Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey, thanks a lot. Actually, I was having a different problem which I could not explain. But though, your answers helped me in getting spi in more detail. Thanks again.

Akhilesh Gangwar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can anyone check this code?

attiny88 is slave and Arduino is master.

attiny88 code--

 

unsigned char spi_tranceiver (unsigned char data)

{

SPDR = data;                                  //Load data into buffer

while(!(SPSR & (1<<SPIF) ));                  //Wait until transmission complete

return(SPDR);                                 //Return received data

}

int main(void)

{

DDRB=(1<<6);                                  //MISO as OUTPUT

SPCR=(1<<SPE);                                //Enable SPI

while(1)

{

data = spi_tranceiver(ACK);               //Receive data, send ACK

itoa(data, buffer, 10);                   //Convert integer into string

_delay_ms(1000);                            //Wait

}

}

 

I am sending 0x01 from Arduino and getting 0x03 on attiny88. Similarly if sending 0x03, getting 0x07, sending 0x04, getting 0x09. 

And, sending any byte from attiny88 to Arduino, I am getting the only 0xff on Arduino.

 

Can anyone explain this? Power supply for both the boards is 5volts.

 

 

Akhilesh Gangwar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do  you have char buffer[15];  or such in your code to reserve the buffer?

Are you positive the Arduino is sending what you think it is?

Best bet is to scope it out to see what is being sent & verify your signals.

Also, spi can be used in 4 modes (data polarity, clock phase)...are both systems set to match?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am using spimode0. I have removed buffer. And, also, now I have enabled interrupt of spi, whenever I am putting any data in SPDR on attiny88 side, the arduino is able to read that data but my code at attiny88 side is not getting into the ISR of spi. 

Akhilesh Gangwar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Does the spi mode on both the sides need to be same? Because, on making spimode 1 on master side and spi mode 0 on slave side, I am now able to read and send correct data on both the sides.

Akhilesh Gangwar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Of course the SPI mode has to be identical. That's true whatever the AVR is connecting to. In fact the only tricky bit in SPI is making sure you've picked the right one of the 4 to match.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm too lazy to look up the modes, but if the diff is due to polarity, it could be due to any adapters, logic, driver chips etc you use---some of them invert the signal.

As always, a schematic is required.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Schematic is so simple-

Arduino uno, master, is connected to attiny88, slave, with direct connecting-

miso-miso

mosi-mosi

sck-sck

ss-ss

 

Both are working on 5v.

Akhilesh Gangwar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

0v to 0v?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I did not get you. What 0to0v? Sorry..

Akhilesh Gangwar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need a common wire as well : 0V or gnd between the two devices.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh, yeah, I have a common ground between both.

Akhilesh Gangwar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

See how much easier it would have been if you had just posted an actual schematic diagram!

 

"A picture is worth a thousand words" - and a few dozen forum posts!

 

For instructions, see Tip #1

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...