## SPI Peripheral State Determination

5 posts / 0 new
Author
Message

I have been studying the datasheets describing AVRs that include SPI peripherals (e.g. mega48, mega164p) and I have not been able to identify a way to distinguish the following two operating states of the peripheral:

1. Transmission of a byte has started but not finished 2. All bytes received have been read (and no transmission is in progress)
In both cases SPIF = 0 and WCOL = 0, but the profound difference is that in one case SPI transmission is taking place and in the other it is not.
I would like to be able to tell the difference. Am I stuck keeping track of this on my own, or is there some way to distinguish these two operating states by examining the I/O registers?

Quote:
Transmission of a byte has started but not finished
SPIF clear if the program was written correctly.
Quote:
All bytes received have been read (and no transmission is in progress)
There is no ALL bytes, just 1 byte at the time. If SPDR has sent and received (like love and marriage they got together like a horse and carriage...) a byte then SPIF wiil be set.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

Quote:
If SPDR has sent and received (like love and marriage they got together like a horse and carriage...) a byte then SPIF wiil be set.
... but once SPDR has been read by the CPU, then SPIF will not be set anymore.
Maybe I should clarify what I believe are all of the significant SPI master operating states:
Idle. No transmission is taking place, no new data to be read is in SPDR [SPIF = 0]. Active. Transmission is taking place, but is not yet complete and no new data to be read is in SPDR [SPIF = 0].
Complete. Transmission is complete, but the received data has not yet been read from SPDR [SPIF = 1].
I have no problem distinguishing the Complete state from the others. But how can I distinguish the Idle and Active states?

Perhaps a way to approach this problem is to understand under what circumstances you need to distinguish between the idle and active states. While there is no hardware flag that is of much help in almost all cases the program will at least implicitly code the difference. For instance, if the program is in an SPI send routine, it is active, not idle. For buffered transmissions, if the buffer is non-empty it is active and otherwise idle or complete ( which can, of course, be discriminated in hardware ).

Martin Jay McKee

As with most things in engineering, the answer is an unabashed, "It depends."

Quote:
But how can I distinguish the Idle and Active states?...Transmission is taking place, but is not yet complete and no new data to be read is in SPDR [SPIF = 0].
You answered you own question. The active state can ONLY be started by you by writing data into SPDR (with SPIF clear) so you wait for SPIF to be set and then the transmission is complete, both going out and coming in.

edit of course you can also wait for an interrupt to occur if you have it set up that way.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly