Skip to main content
Visitor II
February 21, 2017
Question

uint8_t TI_TDC1000_SPIByteReadReg(uint8_t addr) { uint8_t read_byte,inst; uint8_t dummy = 0x00; inst = TDC1000_READ_BIT

  • February 21, 2017
  • 1 reply
  • 1242 views
Posted on February 21, 2017 at 09:33

Dear all,

here is my code to read spi data:

uint8_t TI_TDC1000_SPIByteReadReg(uint8_t addr)

{

uint8_t read_byte,inst;

uint8_t dummy = 0x00;

inst = TDC1000_READ_BIT & addr; // for read make sure bit6 is 0

//GPIO_ResetBits(TI_TDC1000_CSn_PORT ,TI_TDC1000_CSn_PIN); // CS enable

while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE)== RESET);

SPI_SendData(SPI1, inst);

while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE)== RESET);

SPI_SendData(SPI1, dummy);

/* Wait to receive a byte */

while (SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);

/* Return the byte read from the SPI bus */

read_byte = SPI_ReceiveData(SPI1);

return read_byte;

}

I using oscilloscope and see that the data is read = 0x45, but in my code 

read_byte alway = 0xFF, I think somewhere wrong when spi recieve data.

    This topic has been closed for replies.

    1 reply

    Visitor II
    February 21, 2017
    Posted on February 21, 2017 at 11:51

    Change the wait events for waiting for RXNE.

    When TXE is set, the SPI is just starting to spit the byte out (and a new one can be pre-loaded)

    When RXNE is set the whole byte has been shipped and one has arrived for reading.

    You can toggle a GPIO to view when each wait happens respective to the SPI Clocks to debug.

    np1Author
    Visitor II
    February 22, 2017
    Posted on February 22, 2017 at 04:00

    Hi,

    I toogle GPIO and see SPI_FLAG_RXNE = SET after recieved all data, I think that is correct?

    Could you suggest me what i must fix with this bug?

    '

    Change the wait events for waiting for RXNE.

    '  what should I change?

    Thanks

    Visitor II
    February 22, 2017
    Posted on February 22, 2017 at 09:26

    Try replacing all the TXE flag test by RXNE flag test and see what happens.