I wish to read a SPI slave device. Here's what I thought the procedure would be like :
CS_LOW
- SPI_Write(slave_address)
- SPI_Write(register)
- SPI_Write(0x00) //dummy byte
- data = SPI_Read()
CS_HIGH
However, this doesn't work. To make it work, here's what I have to follow:
CS_LOW
- SPI_Write(slave_address)
- temp = SPI_Read()
- SPI_Write(register)
- temp = SPI_Read()
- SPI_Write(0x00) //dummy byte
- data = SPI_Read()
CS_HIGH
Why do I have to read the data on MISO line even when I know there will be nothing desirable?
PS: The SPI Read() and Write() functions are just my own implementations, nothing particular to a single MCU.
Edit : As requested, I am providing some details on the MCUs.
I am using two MCUs - STM32F303VCT and TI TIVA123GH6PM. Both the MCUs require me to follow the said procedure.
Here's my implementation of SPI transmit & receive function on STM32
uint8_t SPI_rdwr(uint8_t data)
{
*(__IO uint8_t *)&SPI1->DR = data; //send data
while(!(SPI1->SR & SPI_SR_TXE)); // wait till TX buffer is empty
while(SPI1->SR & SPI_SR_BSY); // wait if SPI line is busy
while(!(SPI1->SR & SPI_SR_RXNE)); //wait till RX buffe is NOT empty
return *(__IO uint8_t *)&SPI1->DR; //return data if data received on MISO line
}