stm32: Wait for transmission to complete before returning from spi_transfer()
It's possible for the SCLK pin to still be updating even after the last byte of data has been read from the receive pin. (In particular in spi mode 0 and 1.) Exiting early from spi_transfer() in this case could result in the CS pin being raised before the final updates to SCLK pin. Add an additional wait at the end of spi_transfer() to avoid this issue. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
parent
1c594ef27a
commit
99d55185a2
|
@ -109,12 +109,15 @@ spi_transfer(struct spi_config config, uint8_t receive_data,
|
||||||
{
|
{
|
||||||
SPI_TypeDef *spi = config.spi;
|
SPI_TypeDef *spi = config.spi;
|
||||||
while (len--) {
|
while (len--) {
|
||||||
writeb((void *)&spi->DR, *data);
|
writeb((void*)&spi->DR, *data);
|
||||||
while (!(spi->SR & SPI_SR_RXNE))
|
while (!(spi->SR & SPI_SR_RXNE))
|
||||||
;
|
;
|
||||||
uint8_t rdata = readb((void *)&spi->DR);
|
uint8_t rdata = readb((void*)&spi->DR);
|
||||||
if (receive_data)
|
if (receive_data)
|
||||||
*data = rdata;
|
*data = rdata;
|
||||||
data++;
|
data++;
|
||||||
}
|
}
|
||||||
|
// Wait for any remaining SCLK updates before returning
|
||||||
|
while ((spi->SR & (SPI_SR_TXE|SPI_SR_BSY)) != SPI_SR_TXE)
|
||||||
|
;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue