I need to read variable length strings from a usart. The strings may be the result of local programming or not. I'm using the HAL DMA receive driver under FreeRTOS. I've found that the following approach works:
HAL_UART_Receive_DMA(&huart3, (unsigned char*)&WIFI.RXbuffer.data, _RX_BUFFER_SIZE);
while (WIFI.RXbuffer.data == 0)
comm_index = 0;
while (comm_index != huart3.hdmarx->Instance->NDTR)
comm_index = huart3.hdmarx->Instance->NDTR;
// if was the result of a remote command, then command = AT_IP otherwise
// it's from the send routine and is a valid command
This works as follows:
1) clear WIFI buffer if needed
2) start DMA reception before programmed transmit
3) loop, delaying, until reception starts (allows remote to respond)
4) read NDTR in chip for this transaction. NDTR starts high and decrements with each character transferred. Set transfer limit to max buffer size.
5) loop waiting for NDTR to stabilize. Note that this assumes variable length data transmitted in a burst (ESP8266 does).
6) abort reception to reset DMA flags and stabilize buffer
7) process data