Post Go back to editing

ADuC7026 SPI Slave MISO

Everything works great except MISO.  I'm doing a 4 byte frame and load SPITX after each SPIRx byte is received but only the SPITX byte loaded before the master starts is returned.  It is returned 4 times even though I put a new value in SPITX after each byte is received.  Here's my code.  Any suggestions?

Also where can I find SPI example code?  I've been searching.


/* Buffers */
UINT_8 spiRxBuff[SPI_MSG_NUM_RX_BYTES];
UINT_8 spiTxBuff[SPI_MSG_NUM_TX_BYTES];

/* Counters/Pointers */
UINT_8 spiRxCount;
enum SPIState spiState;

/* SPI busy semifore */
bool Spi_Busy = FALSE;

/* Local Prototypes */
__arm void spiRxTimeout(void);

void SPIInit(void)
{
  /* Configure Port */
  SPICON = 0;   // Clear SPI Con to start
  SPICON = (SPICON_XFER_AND_INT_MODE        |
            SPICON_SPI_ENABLE               |   // Enable
            SPICON_SPIRX_OVERFLOW_OVERWRITE |   // Overwrite Rx data
            SPICON_SLAVE_OUTPUT_ENABLE);         // Enable MISO
 
  SPICON &= ~SPICON_XFER_AND_INT_MODE;                  // Rx ISR

  SPIDIV = 0x00;        // Max speed of 3.482MHz (TODO: Valid for slave?)
 
  /* Init Queue */
  spiRxCount = 0;
  spiState = SPI_STATE_IDLE;
 
  FIQEN = SPI_SLAVE_BIT;
}
 
UINT_8 SPI_temp[4];

void SPIINT(void)
{  
  UINT_32 intFlags;
  UINT_8 rxByte;
 
  intFlags = SPISTA;    //  Local copy of spiStatus reg

  /* Check to see if the Rx buffer is full (1 byte depth) */
  if (intFlags & SPISTA_SPIRX_IRQ)
  {
      rxByte = SPIRX; // Read Rx Byte  
      
      if(spiState != SPI_STATE_PROCESSING)      // Ignore new command if still processing the last command
      {
        spiRxBuff[spiRxCount++] = rxByte; // Save byte then bump index

        if (spiRxCount < SPI_MSG_NUM_RX_BYTES)
        {
          SPITX = spiTxBuff[spiRxCount];   // Get next byte ready to send
          SPI_temp[spiRxCount] = spiTxBuff[spiRxCount];   // Get next byte ready to send
          spiState = SPI_STATE_COLLECTING;
         }
        else
        {
          FIQCLR = SPI_SLAVE_BIT;        // Disenable SPI until data processed

          /* Change msg state */
          spiState = SPI_STATE_PROCESSING;
          
          /* Process Message */
          taskEnqueue(&CommandResponseExecuteCommand);
          FIQCLR = SPI_SLAVE_BIT;        // Disenable SPI until data processed
         }
      }
  }
}