cancel
Showing results for 
Search instead for 
Did you mean: 

STM32f4 Timer interrupt

Vaclav Chrascina
Associate II
Posted on June 28, 2018 at 13:18

I got program which is requesting measurement results from external device and store them in array. My goal is to generate timer interrupt every 0.5s and process collected results, but it seems like timer interrupt somehow disrupt my UART communication with external device.

Here is my code:

case 2: 
{ 
HAL_TIM_Base_Start_IT(&htim3); 
while(Instruction == 2) 
{
 Send_request(&huart3, &Ocular_1_TxBuffer[0], &Ocular_1_RxBuffer[0]);
 GetMeasurementValue(&Ocular_1_RxBuffer[0]); 
 CollectedData[Measurement] = MeasurementValue; 
 Measurement++;
 } 
 HAL_TIM_Base_Stop_IT(&htim3);
 break; 
}
Timer interrupt handler:
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) 
{ 
if(htim == &htim3)
 { 
BubbleSort(CollectedData,Measurement);
 Measuerement = 0;
 }
 }

Timer has priority number 1, UART has priority number 0. Error is generated becouse UART does not receive 4 bytes as expected. It seems like UART communication is not finished before timer interrupt occurs. When I comment line: HAL_TIM_Base_Start_IT(&htim3) UART communication works fine. Timer is configurated fine I already checked it's init function.

Do you have any idea what is wrong?

#stm32f4-discovery #stm32f4-timer #stm32f4-uart
7 REPLIES 7
henry.dick
Senior II
Posted on June 28, 2018 at 13:27

'

Do you have any idea what is wrong?'

1) hal;

2) uart got interrupted by the timer interrupt;

solution is simple:

1) obvious;

2) use the uart interrupt for transmission; or to shrink the uart transmission to be within half a second;

you may also think about the wisedom of sorting in the timer isr -> if the data to be sorted is numerous, that can be a problem - especially when atomicity of the sorted data is not assured in your code.

Posted on June 28, 2018 at 14:55

Make sure they aren't at the same preemption level.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 28, 2018 at 16:55

And PRIGROUP (in SCB_AIRCR) is set how?

JW

Posted on June 28, 2018 at 16:50

Timer has number 1 preemption priority, UART has number 0 preemption priority.

Posted on June 28, 2018 at 17:02

1) Not sure I do understand

2) I am using uart interrup to send and receive. I mean I am using these functions: HAL_UART_Transmit_IT and HAL_UART_Receive_IT which should use an interrupt. Also I am using their callback functions to managing driver enable signal for RS-485.

How is it possible to UART be interrupted by timer while has lower preemption priority? I thought processes with lower preemtion priority are complete before interrupts

Posted on June 29, 2018 at 11:33

Priority group is set to : 4 bits for pre-emption priority 0 bits for subpriority

Posted on June 29, 2018 at 14:24

Make a test to verify if the problem is indeed that one interrupt can't interrupt the other.

The best way to do that is to set a pin at the beginning of the ISR and clear it at the end; doing this on two pins ('debug pins') for the two interrupts and then observe the result on oscilloscope or logic analyzer.

You can even observe the incoming bytes in USART and compare to the USART ISR's 'debug pin', which should come immediately after the received bytes' stopbit.

By ISR I mean the real interrupt service routine, not just the callbacks.

JW