AnsweredAssumed Answered

SPI Reading problem

Question asked by Evgeny Erenburg on Jun 20, 2018
Latest reply on Jun 24, 2018 by Guo Li

I read from SPI slave

volatile struct    SPI_MemMap *SPIx[NUM_DSPI] = {SPI0_BASE_PTR, SPI1_BASE_PTR, SPI2_BASE_PTR};

 

 

uint32_t SPI_TransferByte(uint32_t spi_num, uint8_t tx_data, uint8_t *rx_data)
{
     uint32_t timeout;
     uint32_t value;


   SPIx[spi_num]->PUSHR = /*SPI_PUSHR_PCS(0) |*/ SPI_PUSHR_CTAS(0) |       SPI_PUSHR_TXDATA((uint32_t)tx_data);

 

    timeout = 0;
    // while transfer complete
    while (!(SPIx[spi_num]->SR & SPI_SR_TCF_MASK))
    {
       timeout++;
       if (timeout > SPI_TIMEOUT)
       {
         // clear flag
         SPIx[spi_num]->SR = SPI_SR_TCF_MASK;
         return SPI_TIMEOUT_ERROR;
       }
    };
    // clear flag
    SPIx[spi_num]->SR = SPI_SR_TCF_MASK;

 

    *rx_data = (uint8_t)SPIx[spi_num]->POPR;
    *rx_data = (uint8_t)SPIx[spi_num]->POPR;

 

    return 0;
}

Then I use it

uint8_t SPI_rw_flash(uint8_t tx, uint8_t* rx)
{
    uint8_t err = 0 ;
    uint8_t dummy;

 

    err = SPI_TransferByte(SPI2, tx, rx);

 

     return err;
}

 

First of all - I have to read it twice.

 

*rx_data = (uint8_t)SPIx[spi_num]->POPR;
 *rx_data = (uint8_t)SPIx[spi_num]->POPR;

 

Otherwise I don't get a value.

 

The second problem - I read a value only if I set a break point on the second *rx_data = (uint8_t)SPIx[spi_num]->POPR;

Otherwise if it a free run - I read zeros.

 

What do I miss?

Outcomes