I've moved the logic to the run loop and disabled interrupts. The goal is to immediately respond with the word that was sent in. I'm still getting a single overflow after having successfully pushed the response onto SPI0_PUSHR_SLAVE (the push completed, the fifo depth is 1, and the next pointer is pointing at the value pushed on in the stack), before the next word exchange is clocked by the master. Any thoughts on how to make this echo protocol work?
Note: I am pushing a word into the queue at initialization so there will be no underflow when the first word is exchanged.
void loop()
{
if(SPI0_SR & SPI_SR_TFUF)
{
SPI0_SR &= SPI_SR_TFUF;
}
if(SPI0_SR & SPI_SR_RFDF)
{
if( false == writing )
{
uint8_t response = SPI0_POPR;
SPI0_PUSHR_SLAVE = response;
SPI0_SR |= SPI_SR_RFDF;
writing = true;
}
else
{
uint8_t throw_away;
throw_away = SPI0_POPR;
SPI0_SR |= SPI_SR_RFDF;
SPI0_PUSHR_SLAVE = 0;
writing = false;
command_phase = true;
}
}
}