AnsweredAssumed Answered

SPI Mode on KEA128

Question asked by testbed on Nov 4, 2016
Latest reply on Nov 10, 2016 by Kerry Zhou

Hello Everyone,

 

I am using the KEA128 controller in one of my projects which requires communication over the SPI bus with CPOL=1 and CPHA=1. I've configured the SPI like shown below. However, i am not sure about the CPHA setting for SPI MODE3

 

   /* initialize SPI0 as master    */
    sSPIConfig.u32BitRate = SPI_BIT_RATE;  //1000000 - 1 MHz
    sSPIConfig.u32BusClkHz = BUS_CLK_HZ;  // 20000000 - Internal Clk Oscillator - 20.97 MHz
    sSPIConfig.sSettings.bModuleEn             = 1;
    sSPIConfig.sSettings.bMasterMode           = 1;
    sSPIConfig.sSettings.bClkPhase1            = 1;
    sSPIConfig.sSettings.bClkPolarityLow       = 1;
    sSPIConfig.sSettings.bMasterAutoDriveSS    = 0;

 

My SPI read write functions are as below. I am using the SPI library provided in the KEA quickstart package.

I would like to know if the above settings and below operations would work in SPI MODE3 or not, as i am observing

that even though there is data on MISO, the SPI read operation returns 0xff. The read operation is shown below. The MISO line is pulled up on the SPI device side since its a open drain line.

 

Read Operation

 

void spi_write_array(uint8_t len, // Option: Number of bytes to be written on the SPI port
                     uint8_t data[] )
{
    volatile uint8_t dummy_read = 0;
    
  for (uint8_t i = 0; i < len; i++)
  {
        /* spi_write((int8_t)data[i]); */
        while(!SPI_IsSPTEF(SPI0)){ dummy_read++; };  // empty while loop warning workaround
        SPI_WriteDataReg(SPI0, data[i]);        
        
        while(!SPI_IsSPRF(SPI0));
        dummy_read = SPI_ReadDataReg(SPI0);

 }

}

 

void spi_write_read(uint8_t tx_Data[],//array of data to be written on SPI port
                    uint8_t tx_len, //length of the tx data arry
                    uint8_t *rx_data,//Input: array that will store the data read by the SPI port
                    uint8_t rx_len //Option: number of bytes to be read from the SPI port
                   )
{
    volatile uint8_t dummy_read;
    
  for (uint8_t i = 0; i < tx_len; i++)
  {
    /* spi_write(tx_Data[i]); */
        
        while(!SPI_IsSPTEF(SPI0)){ dummy_read++; };
        SPI_WriteDataReg(SPI0, tx_Data[i]);
        
      while(!SPI_IsSPRF(SPI0));
        dummy_read = SPI_ReadDataReg(SPI0); // discard
    
  }
    
  for (uint8_t i = 0; i < rx_len; i++)
  {
    /* rx_data[i] = (uint8_t)spi_read(0xFF); */
        while(!SPI_IsSPTEF(SPI0)){ dummy_read++; };
        SPI_WriteDataReg(SPI0, 0xFF);
        
        while(!SPI_IsSPRF(SPI0));
        rx_data[i] = SPI_ReadDataReg(SPI0);
        
  }

 

}

 

Thanks.

Outcomes