What does the SPI SPE bit do?
I am new to the STM32 microcontrollers and I am experimenting with the SPI peripheral (interrupt mode, non DMA, slave) of the STM32F410. I noticed that the reference manual mentions the SPE bit (bit 6 of the SPIx CR1 register) but does not provide much explanation to it.
I was wondering what disabling the SPE bit does after each SPI transaction. Does disabling the SPI SPE bit (at each SPI CS rising interrupt) clears the Rx buffer and the Tx buffer? Is the code snippet below for operating the SPI at the GPIO interrupt side correct?
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == SPI2_CS_Pin)
{
if(HAL_GPIO_ReadPin(SPI2_CS_GPIO_Port, SPI2_CS_Pin) == GPIO_PIN_RESET)
{
/* SPI CS Falling Edge Interrupt Handler */
/* Enable SPI2 SPE bit to listen to master device for SPI byte */
SPI2->CR1 |= (0x1 << 6);
/* Exchange byte with master device and do further processing in HAL_SPI_TxRxCpltCallback and main loop */
HAL_SPI_TransmitReceive_IT(&hspi2, (uint8_t*)dummybyte, (uint8_t*)pSpi2RxBuff, cSpi2RxLen);
/* Disable SPI2 SPE bit inside main while loop */
}
else
{
/* SPI CS Rising Edge Interrupt Handler */
/* Force disable SPI2 peripheral by disabling SPE bit */
SPI2->CR1 &= ~(0x1 << 6);
/* Clear Rx buffer if not empty by reading SPI2 Data Register and Status Register */
if(SPI2->SR & SPI_FLAG_OVR || SPI2->SR & SPI_FLAG_RXNE)
{
uint8_t temp = 0;
while(SPI2->SR & SPI_FLAG_RXNE)
{
temp = SPI2->DR;
}
temp = SPI2->SR;
}
}
}
}Thank you!
