cancel
Showing results for 
Search instead for 
Did you mean: 

Reading SPI data register after RXNE has asserted

ontares
Associate

Hello, I found a problem trying to read the Data Register of SPI over STM32L4:

If I read the register after wait that RXNE is went to 0, like the follow code:

// Wait until RXNE flag is reset
while (!UTILITY_READ_REGISTER_BIT(SPI1->SR,SPI_SR_RXNE));
// Read Data from register (doing the right conversion)
*((uint8_t *)&data_rx[counterRx]) = *((volatile uint8_t *)&SPI1->DR);

...all works right!

but if I introduce a check, for detecting the size of data, like the follow code:

// Wait until RXNE flag is reset
while (!UTILITY_READ_REGISTER_BIT(SPI1->SR,SPI_SR_RXNE));
////ie. at this point of code, the register SPI1->DR is equal to 0x0022
// Read Data from register (doing the right conversion)
if (dev->datasize > SPI_DATASIZE_8BIT) {
//// at this point of code, the register SPI1->DR is equal to 0x2200
	*((uint16_t *)&data_rx[counterRx]) = *((volatile uint16_t *)&SPI1->DR);
} else {
//// at this point of code, the register SPI1->DR is equal to 0x2200
	*((uint8_t *)&data_rx[counterRx]) = *((volatile uint8_t *)&SPI1->DR);
}

the register is shifted before the reading, and data is not readble.

Someone know why this happen?

Best Regards

1 REPLY 1

Do you observe the SPI registers using debugger? Can't this be a side effect of the debugger?

JW