AnsweredAssumed Answered

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

Question asked by cnoviello on May 11, 2016
Latest reply on May 25, 2016 by cnoviello

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 the UART_HandleTypeDef->gState to the HAL_UART_STATE_BUSY_TX value, but it is not cleared in the UART_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 the UART_DMAReceiveCplt().

Outcomes