cancel
Showing results for 
Search instead for 
Did you mean: 

Why I need to add delay after HAL_UART_Transmit_DMA and before memset at IRQ call back function

JChew.1
Associate II
void vcom_IRQHandler(void)                    // external interrupt for USART2_IRQHandler
{
	HAL_UART_IRQHandler(&UartHandle);		
	USER_UART_IRQHandler(&UartHandle);         
}
 
void USER_UART_IRQHandler(UART_HandleTypeDef *huart)
{
    if(USART2 == UartHandle.Instance)       //Determine whether it is serial port 
    {
        if(RESET != __HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_IDLE))   
        {
            __HAL_UART_CLEAR_IDLEFLAG(&UartHandle);                   
            USAR_UART_IDLECallback(huart);                        
        }
    }
}
 
void USAR_UART_IDLECallback(UART_HandleTypeDef *huart)
{
	
    HAL_UART_DMAStop(&UartHandle);  
                                                       
  
    uint8_t data_length  = BUFFER_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart2_rx);   
    
 
     HAL_UART_Transmit_DMA(&UartHandle,(uint8_t *)&aRx2Buffer, data_length); 	 
     DelayMs(100);          // why need this delay
 
    memset(aRx2Buffer,0,data_length);                                            
    data_length = 0;
    
 
    HAL_UART_Receive_DMA(&UartHandle, (uint8_t*)aRx2Buffer, 25);                    
}

Scenario : After I call HAL_UART_Transmit_DMA, if I didn't put a delay at there, I monitor at serial terminal like docklight/putty, I won't see a full data.

And then I use JTAG to debug, step one by one, found out that I can receive actual data. (I think because of stepping is not run time anymore, and it indirectly perform an delay(s) at there)

So I add delay after that function, I can solve the problem. But I want to know the reason and also the drawback if I add the delay. To be honest I think a delay like that will halt the CPU for so long.

3 REPLIES 3
TDK
Guru

The buffer you pass to HAL_UART_Transmit_DMA (aRx2Buffer) needs to be valid until the DMA is done transferring. This takes some time. Don't erase the buffer until the transmission is over.

Use one buffer for transmitting and a different one for reception.

If you feel a post has answered your question, please click "Accept as Solution".
berendi
Principal

Yes, the drawback of adding a delay is just that, halting program execution. Instead of doing something necessary or useful, it just sits there.

Keep in mind that each time your program is just sitting somewhere in a tight loop waiting for some event to happen, it could miss some other event. In your program, it would miss incoming data if it comes at the wrong time. In a more complex project, it would miss lots of different events.

This answer is very helpful for me. Thanks.