cancel
Showing results for 
Search instead for 
Did you mean: 

Resetting CNDTR for UART DMA method

d4
Associate III

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

1 REPLY 1
FGraz.1
Associate

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.