huart2.hdmarx->Instance->CNDTR(or NDTR)
is the number of bytes left to be received. In your case this is set to 100 when you call HAL_UART_Receive_DMA(..., 100);
. When 10 bytes are received, this number is decremented by 10; so the number of bytes received is 100 - CNDTR
.
However you have to check this register manually probably in the main while loop or in a timer isr. If you want to know when UART stops receiving data, check this. If you need an interrupt every time one byte is received then you should probably receive only one byte using dma and in the callback call HAL_UART_Receive_DMA(..., 1)
again.
Update:
You can also use HAL_UARTEx_ReceiveToIdle_DMA()
which calls HAL_UARTEx_RxEventCallback(UART_HandleTypeDef * huart, uint16_t Size)
which gives you the number of bytes received (Size
). However it gets called on idle, transfer complete and transfer half complete and you can't know which one has happened so you have to manually disable half transfer interrupt.
Here's an example:
uint8_t buffer[100];
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef* huart, uint16_t Size)
{
printf("UART received: %hu byte(s)\n", Size);
printf("%.*s\n\n", Size, buffer);
HAL_UARTEx_ReceiveToIdle_DMA(huart, buffer, 100);
//disable half transfer interrupt; it is enabled in HAL_UARTEx_ReceiveToIdle_DMA()
__HAL_DMA_DISABLE_IT(huart1.hdmarx, DMA_IT_HT);
}
int main()
{
HAL_UARTEx_ReceiveToIdle_DMA(&huart1, buffer, 100);
//disable half transfer interrupt; it is enabled in HAL_UARTEx_ReceiveToIdle_DMA()
__HAL_DMA_DISABLE_IT(huart1.hdmarx, DMA_IT_HT);
}
Needless to say, you should add a DMA request for RX in STM32CubeMX and enable its interrupt.
HAL_UART_Receive_DMA
for the moment. Instead describe what condition should trigger the processing of the received data. Is it a message delimiter? Is it a timeout? Is it a length field in the data? Or can you process any chunk of data? \$\endgroup\$