cancel
Showing results for 
Search instead for 
Did you mean: 

A question about UART interrupt

Taehyeong Gu
Associate II
Posted on April 18, 2017 at 14:45

In order to configure UART interrupt receive, I put HAL_UART_Receive_IT function in HAL_UART_RxCpltCallback.

but it doesnt work. below is my callback code.

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

uint8_t i;

if (huart->Instance == USART2) //current UART

{

if (Rx_indx==0) {for (i=0;i<RXBUFFERBYTE;i++) aRxBuffer[i]=0;} //clear Rx_Buffer before receiving new data

if (Rx_data[0]!=10) //if received data different from ascii 10

{

aRxBuffer[Rx_indx++]=Rx_data[0]; //add data to Rx_Buffer

}

else //if received data = 10

{

Rx_indx=0;

USART2_RX_STANBY=1;//transfer complete, data is ready to read

}

if(HAL_UART_Receive_IT(&huart2,Rx_data, 1) != HAL_OK)//activate UART receive interrupt every time

{

Error_Handler();

}

}

}

The main program has HAL_UART_Receive_IT in beginning part. so i put some character of ASCII and my program reach if(HAL_UART_Receive_IT(&huart2,Rx_data, 1) != HAL_OK) . I checked that HAL_UART_Receive_IT returned HAL_OK then it means preparation of UART receive interrupt. 

but the program doenst take interrupt when I send ASCII to MCU.

I have no clue.. anyone who have had same experience??

help me please.

Thank you.

11 REPLIES 11
Imen.D
ST Employee
Posted on April 18, 2017 at 15:56

Hi i, 

You may refer to the UART example of the STM32cube firmware package relevant to the device that you are using.

The example help you in your project to ensure UART transmission and reception with Interrupt.

You can use STM32CubeMx tool which help you to develop the basic configuration of your application and generate an initialization code.

 

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
Alex R
Senior
Posted on April 18, 2017 at 18:10

Hi,

1. Make sure the USART2 Global Interrupt is enabled (See NVIC settings in the USART2 Configuration in STM32CubeMx if you are using it).

2. I don't think you need to use HAL_UART_Receive_IT in the Interrupt handler. This call is only necessary one time at initialization time.

Regards,

Alex

Taehyeong Gu
Associate II
Posted on April 19, 2017 at 08:05

I executed the same program and it worked but has different problem.

my main while program is below

while (1)

{

if(TIM2_Loop_On==1)

{

if(USART2_RX_STANBY==1)

{

sprintf(aTxBuffer,'%s \n',aRxBuffer);

if(HAL_UART_Transmit(&huart2, aTxBuffer, TXBUFFERSIZE, 1000)!= HAL_OK)

{

Error_Handler();

}

USART2_RX_STANBY=0;

}

/*

sprintf(aTxBuffer,'welcome \n');

if(HAL_UART_Transmit(&huart2, (uint8_t*)aTxBuffer, TXBUFFERSIZE, 1000)!= HAL_OK)

{

Error_Handler();

}

*/

TIM2_Loop_On=0;

}

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

}

/* USER CODE END 3 */

}

When i executed this program, i can get same character as what i send.

I erased /* */(annotation) then i can get 'welcome' message at a regular interval.

but when i send some character, my program stops.

I guess the problem is when Rx Interrupt and Tx routine happen at the same time, program counter enter the Error_Handler().

so how can i use Rx interrupt and Tx routine together?

Thank you

Posted on April 19, 2017 at 07:44

Thank you. guy

1. I enabled USART2 global Interrupt by HAL_UART_Receive_IT  in beginning part of my program.

2. I am a little confused of the fact that HAL_UART_Receive_IT  or  __HAL_USART_ENABLE_IT should be executed to wait for next UART Receive Interrupt whenever UART Receive ends.  where can i get a manual related with it?? 

I will update this post after making sure that HAL_UART_Receive_IT should be executed only one time by some experiment.

Posted on October 04, 2017 at 06:06

Could you solve your problem?

What was the version of your HAl library ?

Posted on October 04, 2017 at 06:38

Evidence suggests an STM32L0 version of HAL..

I will observe that it's not unduly hard to code very efficient register based IRQ Handlers for (LP)U(S)ART on the L0 parts. The Rx and Tx can definitely occur concurrently.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 07, 2017 at 10:11

what about STM32f1?

My microcontroller is stm32f103zet6 and sensor continuously sends me data and I receive them via uart. when I try to send data to this sensor without interrupt(using HAL_UART_Transmit()) at some times seems uart peripheral hangs and can not receive data any more. but when I use 

HAL_UART_Transmit_IT() which uses interrupt for sending data over uart, it seems that uart doesn't hang any more. why? is it because of that concorrent working with data register?

Isn't any way to send data without interrupt in these situation?

Posted on October 07, 2017 at 17:38

>>what about STM32f1?

The register names and status bits are slightly different, but functionally very similar.

>>when I try to send data to this sensor without interrupt(using HAL_UART_Transmit()) at some times seems uart peripheral hangs and can not receive data any more. 

Well the function likely blocks for multiple byte times, and the Rx overruns and flags error status, which must be cleared.

>>

Isn't any way to send data without interrupt in these situation?

Yes, but you have to move beyond the Wait for buffer TX, Wait for buffer RX, Repeat mentality. When things are occurring concurrently you can't serialize the operations like this.

What you need is mechanism that doesn't block, for example

while(1) // Polling loop pseudo code

{

 if SR.RXNE then InputFifoWrite(DR);

if SR.TXE and OutputFifoNotEmpty() then DR = OutputFifoRead();

PumpProcess();

}

Where you monitor the status, but don't get hung up waiting for a buffer to be completely full, or completely empty. Think of it as juggling with three balls, and two hands, at least one is always in the air, and you drop things if you stop.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 07, 2017 at 21:00

Turvey.Clive

This reply is to specify that you gave almost the exact same response to me a few days ago.

https://community.st.com/0D50X00009XkgKMSAZ