I'm trying to establish SPI communication between PIC18F2580 as master and STM32F4 Discovery as slave.
Using logic analyzer, I found that PIC and STM32F4 actually sending bytes (PIC's MOSI and STM32F4's MISO data are correct). However, PIC and STM32F4 are not receiving correct bytes (PIC's MISO and STM32F4's MOSI are always 0xFF).
Please note that everything works fine when STM32F4 is master and PIC is slave.
In my SPI_send() function, I send data byte, wait until transmission is completed, wait until reception is completed, and wait until SPI is not busy anymore. The code is as follows:
However, when I set breakpoint at "return SPI1->DR;" line, the value of SPI1->SR register is 0xC3 (0b11000011).
This means that even if I wait for transmission and reception to be finished before returning the received bytes, the following conditions still occur:
- receive buffer is not empty (yes, I wait until RXNE is cleared)
- SPI is busy (yes, I wait until BSY is cleared)
- buffer overrun occurs
How could these happen?
Also, in an attempt to clear the overrun flag, I refer to STM32F4 reference manual. The manual states that:
"Clearing the OVR bit is done by a read from the SPI_DR register followed by a read access to the SPI_SR register."
So I tried to read DR and SR registers at the beginning of function, to make sure that overrun flag is cleared. But I still get the same SR value (0xC3) which means the overrun flag is not cleared.
So Is there appropriate (and safe) way to implement SPI byte exchange function? I tried many implementation I found on the internet, but none of them work for my case.
Any help will be highly appreciated.