cancel
Showing results for 
Search instead for 
Did you mean: 

UART IRQ ECHO program using the STM32 Discovery using HAL libraries

rakeshpatil1983
Associate II
Posted on October 10, 2014 at 08:24

I'm upgrading to the new HAL libraries and got struck with UART interrupt when trying to implement ECHO,

I generated code using STMcubeMX for UART3 added in the msp init of the uart

HAL_NVIC_SetPriority(USART3_IRQn, 0, 1);
HAL_NVIC_EnableIRQ(USART3_IRQn);

Added in the stm32xx_it.c

void USART3_IRQHandler(void)
{
HAL_UART_IRQHandler(&huart3);
}

Created callbacks in the main.c

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{ 
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_13); 
if (huart->Instance == USART3);
{
buffsize = huart->RxXferSize; 
HAL_UART_Receive_IT(huart, Datarx, buffsize);
for (int i = 0; i<buffsize;i++)
{
Datatx[i] = Datarx[i];
}
HAL_UART_Transmit_IT(huart, Datatx, buffsize);
}
}

And adding the below in the USART init

__HAL_UART_ENABLE_IT(&huart3,UART_IT_RXNE);

after running there is nothing on the UART. can anyone guide me in this please?
4 REPLIES 4
Amel NASRI
ST Employee
Posted on October 10, 2014 at 13:00

Hi Rakesh,

The HAL_UART_Receive_IT has to be called in your main function not in the reception complete callback.

Please check some examples in the STM32Cube package. If you are using the STM32F4 discovery board, you should look for USART example under STM32Cube_FW_F4_V1.3.0\Projects\STM32F4-Discovery\Examples\UART\UART_TwoBoards_ComIT.

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

rakeshpatil1983
Associate II
Posted on October 10, 2014 at 14:38

Mayla ,

Thanks for the quick reply.

I saw that example and it works. But you need to wait till the USART reception is complete . In the example there is    while (UartReady != SET){} which waits for all the usart bytes are received. if in case if the no of bytes needed are not known and it has to happen as an interrupt this is not clear.

Usually when the interrupt comes the below operation should happen

Interrupt -> check the IT flag -> read the byte -> write the byte -> clear the IT flag.

I believe adding it in the main(), the purpose gets defeated as the main has to wait till the

HAL_UART_Receive_IT. 

I might be wrong. Could you please clarify if there is another method with which this works ? Thanks for the prompt reply.

rakeshpatil1983
Associate II
Posted on October 11, 2014 at 20:17

Mayla,

Thanks, As you said receive has to be called periodically in the main and not in callback. transmit has to be called in the receive callback . final code looks like this in main.

while (1)
{
HAL_UART_Receive_IT(&huart3, Data, 1);
}

In the receive callback add the tx_IT like this.

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_13);
if (huart->Instance == USART3)
{
HAL_UART_Transmit_IT(huart, Data, 1);
}
}

And it works !!
Posted on December 12, 2016 at 15:27

Hello Rakesh,

I am also trying to do Echo program on STM32F207 Nucleo 144 board. Unfortunately i can just send data while receiving part is not operating. Any Suggestion or idea?? I have configured UART2 over PD5,PD6 Pins. I have attached my program file below. No error while Debugging but i am not receiving data in Receiver buffer.

________________

Attachments :

UART2_Nucleo144.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hz04&d=%2Fa%2F0X0000000bFl%2F85OjDIpEYiUNdG5Yj8CVFt2xzMsQ55oWKBzRoj4EU0c&asPdf=false