/* SPI Clean Disable. 
Disable the SPI peripheral according to the procedure in section 23.3.8 of the reference manual. This prevents corruption of any ongoing transfers and prevents the BSY flag from becoming unreliable. */
uint16_t spi_clean_disable(uint32_t spi)
/* SPI Clean Disable. 
Disable the SPI peripheral according to the procedure in section 23.3.8 of the reference manual. This prevents corruption of any ongoing transfers and prevents the BSY flag from becoming unreliable. */
uint16_t spi_clean_disable(uint32_t spi)
{
	while (!(SPI_SR(spi) & SPI_SR_RXNE));
	uint16_t data = SPI_DR(spi);
	while (!(SPI_SR(spi) & SPI_SR_TXE));
	while (SPI_SR(spi) & SPI_SR_BSY);
	SPI_CR1(spi) &= ~SPI_CR1_SPE;
	return data;
}