Skip to main content
d4_it
Associate II
October 28, 2019
Question

Resetting CNDTR for UART DMA method

  • October 28, 2019
  • 1 reply
  • 2642 views

I'd like to set the counter of huart2.hdmarx->Instance->CNDTR;

To reset the buffer index.

How is this done?

nevermind.

The peripheral required disabling first it seems.

huart2.hdmarx->Instance->CCR &= ~DMA_CCR_EN; // disable

huart2.hdmarx->Instance->CNDTR = sizeof(rx_buff); // reset counter

huart2.hdmarx->Instance->CCR |= DMA_CCR_EN; // re-enable

This topic has been closed for replies.

1 reply

FGraz.1
Visitor II
September 25, 2022

That was the answer that was doing exactly what I was searching for. Another way to write it using HAL :

/*
* Reset your SPI RX DMA Buffer
*/
void reset_spi_dma_rx_buffer(SPI_HandleTypeDef hspi, int rx_buffer_size) {
 __HAL_DMA_DISABLE(hspi.hdmarx);
 hspi.hdmarx->Instance->CNDTR = rx_buffer_size; // reset counter
 __HAL_DMA_ENABLE(hspi.hdmarx);
}

I attach this function to SPI CS(aka SS) on interrupt of rising edge (end of SPI transaction):

void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin) {
 if (GPIO_Pin == GPIO_PIN_5) {
 reset_spi_dma_rx_buffer(hspi2, sizeof(buffer_rx_spi2));
 }
}

This way, if the count data buffer is too small (or bigger), the buffer is init and ready for a new Message. I use DMA circular.