2019-10-28 04:23 AM
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
2022-09-25 01:24 PM
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.