cancel
Showing results for 
Search instead for 
Did you mean: 

[Bug] UART_HandleTypeDef->gState not handled correctly when working in DMA mode

carmine
Associate II
Posted on May 11, 2016 at 12:22

Hi,

the latest CubeF4 HAL (but I suspect that this nasty bug affects ALL HALs), doesn't properly handle the global UART state when working in DMA mode. For example, if the HAL_UART_TransmitDMA() is used, this sets theUART_HandleTypeDef->gState to the HAL_UART_STATE_BUSY_TX value, but it is not cleared in theUART_DMATransmitCplt() callback, which should be recoded in the following way:

static
void
UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
{
UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
/* DMA Normal mode*/
if
((hdma->Instance->CR & DMA_SxCR_CIRC) == 0U)
{
huart->TxXferCount = 0U;
/* Disable the DMA transfer for transmit request by setting the DMAT bit
in the UART CR3 register */
huart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAT);
if
(huart->gState == HAL_UART_STATE_BUSY_TX_RX)
{
huart->gState = HAL_UART_STATE_BUSY_RX;
}
else
{
huart->gState = HAL_UART_STATE_READY;
}
/* Enable the UART Transmit Complete Interrupt */
__HAL_UART_ENABLE_IT(huart, UART_IT_TC);
}
/* DMA Circular mode */
else
{
HAL_UART_TxCpltCallback(huart);
}
}

The same problem affects theUART_DMAReceiveCplt().
2 REPLIES 2
Nesrine M_O
Lead II
Posted on May 25, 2016 at 13:25

Hi cnoviello,

1. The enable of the USART handler in this case is mandatory as it is described in the USART specification: so you have to add the USARTx_ IRQHandler inside ''stm32f4xx_it.c'', in addition to USARTx-DMA_TX and USARTx-DMA_RX IRQHandlers.

2. The main purpose to add the handler is to be sure that the USART communication is complete by checking the TC flag: The set of the ready flag is managed inside HAL_UART_IRQHandler() by the UART_EndTransmit_IT().

3. For more details refer to the Continuous communication using DMA paragraph in the reference manual.

4. There should be no issue with the UART_DMAReceiveCplt()

-Syrine-

carmine
Associate II
Posted on May 25, 2016 at 15:38

Hi Syrine, 

Thanks for clarifying me this point.