// x >> y, mean x is shifted by y bit-positions to the right
unsigned char Byte_0 = data;
unsigned char Byte_1 = address;
unsigned char Byte_2 = address>>8 | command;
// SPIx BUS_TYPE -------------------------------------------------------------
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==0) {}
SPI_I2S_SendData(SPI1, Byte_2); // send byte 2
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==0) {}
SPI_I2S_SendData(SPI1, Byte_1); // send byte 1
if (command==0x90) // write command
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==0) {}
SPI_I2S_SendData(SPI1, Byte_0); // send byte 0
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==0) {}
Simple_delay_43ns((void*)10); // important delay (16) at SPI freq.=750kHz
}
if (command==0x10) // read command
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)==RESET) {};
Simple_delay_43ns((void*)10); // important delay (16) at SPI freq.=750kHz
//Compulsory reads to DR and SR to clear OVR,
//so that next incoming data is saved
SPI_I2S_ReceiveData(SPI1); // To clear OVR
SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE); // To clear OVR
//Reading byte1
SPI_I2S_SendData(SPI1, 0xFF); // DUMMY WRITE
// Wait until RX buffer is not empty, then read the received data
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==0) {}