Best way to use DMA without knowing size of SPI transfer ?
Hi,
device : STM32G473VBT6
What is the best way to process SPI data when we don't know how many bytes will come from the SPI master ?I use the SPI in full duplex slave.
The problem with DMA is that the DMA buffer is circular, where my processing functions are not. I don't have the time to copy bytes to another buffer, and code to access the circular buffer slows down significantly when processing 320 bytes .
i need to access the buffer as fast as i can.
I have used an easy solution ,see under "// reset spi to flush FIFO" , this reset the SPI every interrupt and set the DMA buffer back to element 0 of spi_buf_in.
The disadvantage is that this reset also takes 40µs at 170 Mhz.
This works mostly perfectly, but sometimes after 1000's SPI transfers my spi_buf_in data is corrupt (compared to the data i see trough the logic analyzer)
My guess is that this corruption comes because of the unconventional reset of the SPI.
How do i read incoming SPI data most elegantly at the highest speed ?
// SPI enable line will have positive flank once the data is transferred from PI to STM, this will be used as interrupt to
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
uint16_t SPISize;
uint32_t head;
if(GPIO_Pin == GPIO_PIN_12){ // spi enable line interrupt at positive edge
head = __HAL_DMA_GET_COUNTER(phMainSPI->hdmarx); // get number of bytes received by the SPI rx DMA
SPISize = (uint16_t) (SPI_SIZE - head);
// processing spi_buf_in data ........
// reset spi to flush FIFO
HAL_SPI_DeInit(phMainSPI);
__HAL_RCC_SPI2_FORCE_RESET();
__HAL_RCC_SPI2_RELEASE_RESET();
HAL_SPI_Init(phMainSPI);
HAL_SPI_TransmitReceive_DMA(phMainSPI,spi_buf_out,spi_buf_in,320);
}
}