Greetings freaks -
This is really a mixed question about optimization and AVR SPI operation.
Environment: AS7 in Windows 10, ATmega328P
I use the SPIF bit in the SPSR (SPI Status Register) to detect when a byte transfer is complete. No interrupts are used. The spec sheet does not indicate any method for clearing the SPIF other than reading the SPDR (SPI Data Register) or responding to an interrupt.
Now, I have come to the point where i need to read a bunch of bytes to empty them in the peripheral, discarding the results. I set up a local variable to do the read, but it is optimized away (presumably since the variable is never used), leading to no SPDR read and no reset of SPIF. I know that I could set up a global volatile variable, but that seems to be a waste for something that is really a local variable with a temporary life.
So, how can I insure that this read takes place?