4 Replies Latest reply on Feb 28, 2019 12:56 AM by ShanmathiN_06

    Getting incorrect SPI data received

    DaHu_285096

      I am a bit confused regarding use of the flags when sending SPI data and seem to get back wrong data from my device

       

      I have a SPIM component set up and started and can see activity on CS, Miso, Mosi and Sck but get wrong data back.

       

      Is my code below correct to send a command byte (read command and register ID) and then read back the expected bytes?

       

      Header length is number of command bytes and header buffer is buffer passed to routine containing command bytes.

       

      readlenth is number of bytes to read and they are passed back in readBuffer.

       

      int readfromspi(uint16 headerLength, const uint8 *headerBuffer, uint32 readlength, uint8 *readBuffer)

      {

      CS_Write(0);

       

          for(i=0; i<headerLength; i++)

          {

          SPIM_WriteTxData(headerBuffer[i]);

              while(!(SPIM_ReadTxStatus() & SPIM_STS_SPI_DONE)){};

            readBuffer[0] = SPIM_ReadRxData() ; // Dummy read as we write the header

          }

       

       

          for(i=0; i<(int)readlength; i++)

          {

              SPIM_WriteTxData(0x00);  //dummy write

              while(!(SPIM_ReadTxStatus() & SPIM_STS_SPI_DONE)){};

            readBuffer[i] = SPIM_ReadRxData() ; // SPI_receive_data

          }

       

          CS_Write(1);

        

          return 0;

      }