cancel
Showing results for 
Search instead for 
Did you mean: 

How to switch the DMA buffer in HAL_SPI_Transmit_DMA

Wechmarer
Associate II

While output data from DMA to SPI it should be able to use two buffers. One for the current transfer, one to write data.

But how do I switch between the buffers?

#define BUFFER_SIZE 10
uint8_t txBuffer[BUFFER_SIZE + BUFFER_SIZE];
HAL_SPI_Transmit_DMA(&hspi2, (uint8_t*)txBuffer, BUFFER_SIZE);
 
void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi)
{
  int value = 0;
  if (txBufferHalf == 0) {
    for (unsigned int i = 0; i < BUFFER_SIZE; ++i) {
        txBuffer[i] = ++value;
    }
    txBufferHalf = 1;
  }
  else
  {
    for (unsigned int i = 0; i < BUFFER_SIZE; ++i) {
        txBuffer[i + BUFFER_SIZE] = ++value + 100;
    }
    txBufferHalf = 0;
  }
}

1 ACCEPTED SOLUTION

Accepted Solutions
Wechmarer
Associate II

Solved. Has to use both HAL_SPI_TxHalfCpltCallback and HAL_SPI_TxCpltCallback

View solution in original post

1 REPLY 1
Wechmarer
Associate II

Solved. Has to use both HAL_SPI_TxHalfCpltCallback and HAL_SPI_TxCpltCallback