Skip to main content
Explorer
November 8, 2023
Question

usart receive

  • November 8, 2023
  • 4 replies
  • 2853 views

USART receive is not providing the output please check and reply me asap

selected USART2 made it to asynchronous also enabled global interrupt
code lines that i have added

uint8_t txbuf[10]="hello ";

uint8_t rxbuf[10];

 

while (1)

{

/* USER CODE END WHILE */

HAL_UART_Receive(&huart2, rxbuf, sizeof(rxbuf), 100);

/* USER CODE BEGIN 3 */

}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

HAL_UART_Transmit(&huart2, rxbuf, sizeof(rxbuf), 100);

}

    This topic has been closed for replies.

    4 replies

    Technical Moderator
    November 8, 2023

    Hello @soundarya_h_s ,

    If you using the interrupt mode, the transmit and receive functions are HAL_UART_Transmit_IT() and HAL_UART_Receive_IT(). For that, I recommend you to take a look to this FAQ: How to use STM32 HAL UART driver API and Callbacks ?

    Also, I think Getting started with UART can help you.

    Thank you.

    Kaouthar

    Explorer
    November 9, 2023

    @KDJEM.1 i tried disabling the global interrupt but i am not getting output

     

    Graduate II
    November 9, 2023

    HAL_UART_RxCpltCallback only works when interrupt is enabled. HAL_UART_Receive is only polling and will not trigger an interrupt callback. If you want polling then you have to rewrite your code to transmit what you received.

    Graduate II
    November 8, 2023

    A better approach is to use the idle interrupt. That way you can receive any length string. You make your array size the longest string/bytes you'll expect. Then when you call HAL_UARTEx_ReceiveToIdle_IT, you pass an argument twice the size of your array. That way you'll either get an idle interrupt or if you're expecting like 5 bytes from "hello", it'll do a half callback interrupt. 

     

     

    #define RX_BUFFER_SIZE 10
    typedef struct
    {
    	int dataRdy;
    	uint32_t size;
    }RX_Status;
    uint8_t rxbuf[RX_BUFFER_SIZE];
    
    RX_Status rx_status = {0};
    
    
    int main(void)
    {
     HAL_UARTEx_ReceiveToIdle_IT(&huart2, rxbuf, RX_BUFFER_SIZE * 2);
     while (1)
     {
    	 if(rx_status.dataRdy)
    	 {
    		 rx_status.dataRdy = 0;
    		 HAL_UART_Transmit_IT(&huart2, rxbuf, rx_status.size);
    	 }
     }
    }
    
    void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
    {
    	if(huart == &huart2)
    	{
    		rx_status.dataRdy = 1;
    		rx_status.size = Size;
    		HAL_UARTEx_ReceiveToIdle_IT(&huart2, rxbuf, RX_BUFFER_SIZE * 2);
    	}
    }

     

     

    Make sure you enable UART interrupt

    UART Enable Interrupt.png

     

     

    Graduate II
    November 9, 2023

    Those zero timeouts for the HAL_UART_Transmit() calls are especially cool...

    Graduate II
    November 9, 2023

    Making my head spin, things that need to be global and volatile.

    Cramming data into a single byte, that might go out on an EXTI

    Pulling data from an active DMA buffer.

    Lot of disconnected code, ideas and concepts here.

    Explorer
    November 10, 2023

    @Tesla DeLorean can you please  tell it in simple words or modify the code which you think is not correct

    Graduate II
    November 10, 2023

    What is connected to your UART tx pin?