This is the code I use to check the state of the SPI_SR_RXNE bit:
This is the code for the
LIS3DSH accelerometer, when I send data to the accelerometer in response the accelerometer sends data to the Microcontroller.
This code is working, with this code I am getting data from accelerometer.
You can use other code to check the SPI_SR_RXNE status, it doesn't matter.
int main()
{
uint8_t Read_Out_X_Y_Z_REG_Address=0xA8;
uint8_t Out_X_Y_Z_Data_Separate[6]={0};
int16_t Out_X_Y_Z_Data_RESULT[3]={0};
SPI1->CR1 |= SPI_CR1_SPE;
uint8_t address=0x8F; //Register address+read
uint8_t result;
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET); //Chip Select
while(!(SPI1->SR & SPI_SR_TXE));
SPI1->DR = address;
while(!(SPI1->SR & SPI_SR_TXE));
while((SPI1->SR & SPI_SR_BSY) != RESET);
while((SPI1->SR & SPI_SR_RXNE));
result=SPI1->DR; //STARTING HERE I COMMENT THE CODE FOR THE EXPERIMENT.
while((SPI1->SR & SPI_SR_RXNE));
while((SPI1->SR & SPI_SR_BSY) != RESET);
while(!(SPI1->SR & SPI_SR_TXE));
SPI1->DR = 0;
while(!(SPI1->SR & SPI_SR_TXE));
while((SPI1->SR & SPI_SR_BSY) != RESET);
while((SPI1->SR & SPI_SR_RXNE));
result=SPI1->DR;
while((SPI1->SR & SPI_SR_RXNE));
while((SPI1->SR & SPI_SR_BSY) != RESET);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET); //Chip Select
}
The Reference Manual says: Clearing of the RXNE bit is performed by reading the SPI_DR register.
So after this code the RXNE bit will be cleared?
uint8_t result;
result=SPI1->DR;
It's hard for me to imagine that the SPI_DR register keeps track of when SPI_DR will be read.
When I say "reading SPI_DR " I mean this:
result=SPI1->DR;
I cut the code above to check the state of the SPI_SR_RXNE bit.
SPI1->CR1 |= SPI_CR1_SPE;
uint8_t address=0x8F; //Register address+read
uint8_t result;
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET); //Chip Select
while(!(SPI1->SR & SPI_SR_TXE));
SPI1->DR = address;
while(!(SPI1->SR & SPI_SR_TXE));
while((SPI1->SR & SPI_SR_BSY) != RESET);
while((SPI1->SR & SPI_SR_RXNE));
//result=SPI1->DR; //DEACTIVATED
Maybe under "reading the DR register" in Reference Manual there is meaning something else?
For example, transferring data to the SPI1->DR register?
I don't clear the SPI_SR_RXNE bit myself, but SPI_SR_RXNE clears itself without reading the SPI1->DR register.
Maybe I'm wrong, but in my opinion clearing SPI_SR_RXNE signals the transfer of data to the SPI1->DR register, and not reading SPI1->DR.
Do the same experiment for yourself, send something to the RX buffer, DO NOT READ SPI_DR REGISTER and look at the state of the SPI_SR_RXNE bit.
It’s very important for me to know, the code algorithm depends on it.
If you follow my logic that SPI_SR_RXNE is cleared after the data is transferred to SPI1->DR, then the data should be received like this:
while((SPI1->SR & SPI_SR_RXNE));
result=SPI1->DR;
while((SPI1->SR & SPI_SR_BSY) != RESET);
But if this is not the case, then my code for receiving data will be incorrect.