STM32 communication with BMP280 using SPI - How many bits?
Hey all. I am trying to write an SPI driver for the Adafruit BMP280 sensor, for the STM32F4 discovery board.
I am confused about the number of data bits for transmission and reception.
For SPI write sequence, it is shown as burst of 8 bits (indicated by bit7, bit6 ... bit0).
However, for SPI read sequence, it is shown as 16 bits of data received.
Do I configure the SPI in 8 bit mode? Or do i use 16-bit mode and send a dummy byte for upper 8 bits in write sequence?
My code currently uses the second one. I don't have the hardware to test it yet, but I wanted to see if I was accurate in reading the datasheet, as I am a beginner.
uint16_t SPI_TxRx(uint8_t data){
SPI2 ->DR = data | data<<8; //INITIATE TRANSMISSION BY WRITING DATA INTO DATA REGISTER
while(!(SPI2->SR & SPI_SR_TXE)); //WAIT FOR TX BUFFER TO GET EMPTY (load into shift reg)
while(!(SPI2->SR & SPI_SR_BSY)); // WAIT TILL SPI IS NOT BUSY
uint16_t temp = SPI2 ->DR;
return temp;
}
I was thinking of using this for both sequences. I don't know how accurate it is, though.