AnsweredAssumed Answered

STM32Cube_fw_f4_v1.19.0 HAL library UART DMA Idle interrupt doesn't call HAL_UART_RxCpltCallback.

Question asked by sh mm on Mar 2, 2018

Hello everyone! 

I have used the STM32Cube_fw_f4_v1.19.0 HAL library in my project. When i use the uart idle interrupt in DMA mode, i can receive data. but the HAL_UART_RxCpltCallback() function is not called. This is part of my code, and i select the main code, please help me!

/* USART2 initialization */
void SP3232_USART2_Init(void)
{
      UartHandle.Instance = SP3232_USART2;
      UartHandle.Init.BaudRate = 115200;
      UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
      UartHandle.Init.StopBits = UART_STOPBITS_1;
      UartHandle.Init.Parity = UART_PARITY_NONE;
      UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
      UartHandle.Init.Mode = UART_MODE_TX_RX;
      UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;

      HAL_UART_Init(&UartHandle);
}

 

/* UART MSP initialization */
void SP3232_HAL_USART2_MspInit(void)
{
      static DMA_HandleTypeDef hdma_tx;
      static DMA_HandleTypeDef hdma_rx;

      GPIO_InitTypeDef GPIO_InitStruct;

      /* Enable GPIO TX/RX clock */
      SP3232_USART2_GPIO_CLK_ENABLE();
      /* Enable USART clock */
      SP3232_USART2_CLK_ENABLE();
      /* Enable DMA clock */
      SP3232_USART2_DMA_CLK_ENABLE();

      / * UART TX GPIO pin configuration */
      GPIO_InitStruct.Pin = SP3232_USART2_TX_PIN;
      GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
      GPIO_InitStruct.Alternate = SP3232_USART2_GPIO_AF;
      HAL_GPIO_Init(SP3232_USART2_TX_GPIO_PORT, &GPIO_InitStruct);

      /* UART RX GPIO pin configuration */
      GPIO_InitStruct.Pin = SP3232_USART2_RX_PIN;
      HAL_GPIO_Init(SP3232_USART2_RX_GPIO_PORT, &GPIO_InitStruct);

      /* Configure the DMA streams */
      /* Configure the DMA handler for Transmission process */
      hdma_tx.Instance = SP3232_USART2_TX_DMA_STREAM;
      hdma_tx.Init.Channel = SP3232_USART2_TX_DMA_CHANNEL;
      hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
      hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
      hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
      hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
      hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
      hdma_tx.Init.Mode = DMA_NORMAL;
      hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
      hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
      hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
      hdma_tx.Init.MemBurst = DMA_MBURST_SINGLE;
      hdma_tx.Init.PeriphBurst = DMA_MBURST_SINGLE;
      HAL_DMA_Init(&hdma_tx);

      /* Associate the initialized DMA handle to the the UART handle */
      __HAL_LINKDMA(&UartHandle, hdmatx, hdma_tx);

      /* Configure the DMA handler for Transmission process */
      hdma_rx.Instance = SP3232_USART2_RX_DMA_STREAM;
      hdma_rx.Init.Channel = SP3232_USART2_RX_DMA_CHANNEL;
      hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
      hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
      hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
      hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
      hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
      hdma_rx.Init.Mode = DMA_NORMAL;
      hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
      hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
      hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
      hdma_rx.Init.MemBurst = DMA_MBURST_SINGLE;
      hdma_rx.Init.PeriphBurst = DMA_MBURST_SINGLE;
      HAL_DMA_Init(&hdma_rx);

      /* Associate the initialized DMA handle to the the UART handle */
      __HAL_LINKDMA(&UartHandle, hdmarx, hdma_rx);

      /* Configure the NVIC for DMA */
      /* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */
      HAL_NVIC_SetPriority(SP3232_USART2_DMA_TX_IRQn, 0, 1);
      HAL_NVIC_EnableIRQ(SP3232_USART2_DMA_TX_IRQn);

      /* NVIC configuration for DMA transfer complete interrupt (USARTx_RX) */
      HAL_NVIC_SetPriority(SP3232_USART2_DMA_RX_IRQn, 0, 0);
      HAL_NVIC_EnableIRQ(SP3232_USART2_DMA_RX_IRQn);

      /* NVIC configuration for USART TC interrupt */
      HAL_NVIC_SetPriority(SP3232_USART2_IRQn, 1, 0);
      HAL_NVIC_EnableIRQ(SP3232_USART2_IRQn);

      /* enable UART idle interrupt */
      __HAL_UART_ENABLE_IT(&UartHandle,UART_IT_IDLE);
}

 

/* This function handles DMA TX interrupt request. */
void SP3232_USART2_DMA_TX_IRQHandler(void)
{
      HAL_DMA_IRQHandler(UartHandle.hdmatx);
}

 

/* This function handles DMA RX interrupt request. */
void SP3232_USART2_DMA_RX_IRQHandler(void)
{
      HAL_DMA_IRQHandler(UartHandle.hdmarx);
}

 

/* This function handles USARTx interrupt request. receive dma data function is called here */
void SP3232_USART2_IRQHandler(void)
{
      uint32_t tmp1 = 0, tmp2 = 0;

      tmp1 = __HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_IDLE);
      tmp2 = __HAL_UART_GET_IT_SOURCE(&UartHandle, UART_IT_IDLE);

      /* UART idle interrupt occurred */
      if((tmp1 != RESET) && (tmp2 != RESET))
      {
               __HAL_UART_CLEAR_IDLEFLAG(&UartHandle);

               HAL_UART_DMAStop(&UartHandle);

               HAL_UART_Receive_DMA(&UartHandle, UartRxBuffer, SP3232_BUF_SIZE);
      }
      HAL_UART_IRQHandler(&UartHandle);
}

 

/* sp3232 start process */
void SP3232_Start(void)
{
         /* enable UART idle interrupt */
         __HAL_UART_ENABLE_IT(&UartHandle,UART_IT_IDLE);
         HAL_UART_Receive_DMA(&UartHandle, UartRxBuffer, SP3232_BUF_SIZE);
}

 

/* UART Tx Transfer completed callback */
void SP3232_HAL_USART2_TxCpltCallback(void)
{

}


/* UART Rx Transfer completed callback */
void SP3232_HAL_USART2_RxCpltCallback(void)
{
         LED_Toggle();
}

Attachments

Outcomes