Extra bits in SPI Slave DMA
I'm working on a SPI slave right now and found some weird behavior in the peripheral on a STM32g441. The following code is within a GPIO interrupt that's triggered by the CS pin rising. The peripheral clock is 160 MHz and sclk is 1.3 MHz. The spi mode is correct.
__HAL_DMA_DISABLE(hspi1.hdmarx);
__HAL_DMA_DISABLE(hspi1.hdmatx);
__HAL_SPI_DISABLE(&hspi1);
// Get the command by reading from head index to the DMA count
uint32_t uiRxSize = XSPI_RX_TX_BUFFER_SIZE - __HAL_DMA_GET_COUNTER(hspi1.hdmarx);
// Pull out the command information
if (uiRxSize <= 0)
{
// The command does not have any data. Reset and re-enable the DMA
// Move the rx/tx DMA buffer back to the beginning of the buffer to fully reset everything
hspi1.hdmarx->Instance->CNDTR = XSPI_RX_TX_BUFFER_SIZE;
hspi1.hdmatx->Instance->CNDTR = XSPI_RX_TX_BUFFER_SIZE;
__HAL_DMA_ENABLE(hspi1.hdmarx);
__HAL_DMA_ENABLE(hspi1.hdmatx);
__HAL_SPI_ENABLE(&hspi1);
return;
}
auto eAddress = static_cast<exSPIAddress_t>(pucRxBuffer[0] & XSPI_ADDRESS_Msk);
bool bRead = (pucRxBuffer[0] & XSPI_READ_WRITE_Msk);
//switch statement to handle read/write operation that reads from an array in RAM and is //populated by the DMA
// Move the rx/tx DMA buffer back to the beginning of the buffer to fully reset everything
hspi1.hdmarx->Instance->CNDTR = XSPI_RX_TX_BUFFER_SIZE;
hspi1.hdmatx->Instance->CNDTR = XSPI_RX_TX_BUFFER_SIZE;
__HAL_DMA_ENABLE(hspi1.hdmarx);
__HAL_DMA_ENABLE(hspi1.hdmatx);
__HAL_SPI_ENABLE(&hspi1);I've found that the spi works well most of the time. But when I send a command from master before SPI is re-enabled then the array in RAM will be shifted 2 to 4 bits to the right. In other words, there's 2 to 4 extra bits on the most significant side of DMA. An example is, I expect to have 0xD1 in the 0 index of my array but instead I have 0x1A and all of the following bytes in the array are corrupted in the same way.
I don't understand why anything would be read into the SPI shift register if SPI is disabled. I also don't fully understand what disabling the SPI->CR1->SPE bit does to the SPI. Any help understand would be appreciated.
