need to clear the SPIF flag in SPSR?

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

Is it bad practice not to clear the SPIF after sending a byte? Many times I don't bother to read the byte that is transfered in as the byte I am sending goes out. DataSheet says that to clear the flag read the SPSR then the SPDR. Only thing that happens if SPIF is not cleared is that the WCOL flag gets set if SPDR is written while SPIF is still set.

I would rather keep it clean and clear the SPIF, but I don't know how to read it and throw away data without using many instructions.

Code used to send two bytes, one is address & controll, second is data.

void SPI_WriteAt(uint8_t addrs, uint8_t data)
{
   SEL_SLV;
   SPDR = addrs;		
   while(SPI_BSY);
      // should read SPDR right here...		
   SPDR = data;		
   while(SPI_BSY);	
      // and here...	
   DSEL_SLV;
   return;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

so I stopped assuming (assuming is not a good thing to do) that putting a

uint8_t junk;

at top of function and reading the SPDR to it would not work to clear the flag. The compiler never bothers to clear memory for the junk variable and only adds two instructions to the function, one for each read; this is at optimization -Os. At -O0 it pushes a bunch of the registers... I need to read up on what exactly the different optimization levels do; and stop assuming.

Sorry to bother all.

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

Most people have a generic SPI send/receive routine for sending/receiving one byte along the lines of:

uint8_t SPI_Tx_Rx(uint8_t to_send){
  SPDR = to_send;
  while(SPI_BSY);
  return SPDR;
}

This means that the received SPDR is always read. Whether the function is then used in the form:

SPI_Tx_Rx(byte_to_send); // just send
byte_received = SPI_Tx_Rx(byte_to_send); // send and receive
byte_received = SPI_Tx_Rx(dummy_byte); // just receive

doesn't really matter.

Clif