cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 USART interrupt problem

ondrej2
Associate II
Posted on December 13, 2013 at 00:55

My application use EXTI1 interrupt, SPI1 interrupt and USART1(RXNE) interrupt. USART1 interrupt has highest priority. USART1_IRQHandler is very short, only storing incomming bytes to the circular buffer. When I disable EXTI1 and SPI1 interrupts and do some tests, communication over USART1 is OK (bytes is reading from circular buffer in the main loop). But when other interrupts are enabled (SPI1 and EXTI1, both with lower priority than USART1 interrupt), I lost incomming byte sometime. I am sure, that USART buffer did not overflow, but probably USART RXNE interrupt is missed somehow. uP runs on 72MHz and USART baud rate is 0.5Mbit. I think, that highest priority USART1 interrupt can break lower priority interrupt and do it's job ''immediately'', right? How long can take breaking of lower priority handler execution? Any other idea?

Thanks 
5 REPLIES 5
Posted on December 13, 2013 at 01:44

Preemption should take a dozen cycles. You'd want to double check the NVIC grouping and preemption settings. Lower numbers have higher priority.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ondrej2
Associate II
Posted on December 13, 2013 at 08:06

Here is my NVIC setting. Do you think it is correct?

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);

/* Enable the USARTy Interrupt */

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

 

/* Enable and set EXTI1 Interrupt to the highest priority */

NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);    

 

/* Configure and enable SPI interrupt --------------------------------*/

NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_Init(&NVIC_InitStructure);

Posted on December 13, 2013 at 08:54

> I lost incomming byte sometime.

Do you have an oscilloscope or logic analyzer? Try toggle a pin in the USART ISR and watch with oscilloscope together with the UART Rx. You might also try to transmit the received byte, possibly on a different UART with slightly higher baudrate to avoid overruns, capture on PC and compare to received.

JW

frankmeyer9
Associate II
Posted on December 13, 2013 at 09:39

I would not rule out the possibility of transmission errors, especially at such high bit rates. I always check for ORE,  NE, FE and PE in my uart interrupt handlers. Maybe you lose your characters there ...

ondrej2
Associate II
Posted on December 13, 2013 at 21:53

I got it. At first I checked timing of problem by the oscilloscope. Than I checked mentioned error flags. Error flags was ok, and every incomming byte was serviced by the USART ISR correctly! But sometimes when USART byte comes during execution of EXTI and SPI ISR routines execution, I lost communication (main loop didn't response to the PC). After some time I found it - problem was forgotten line from previous version of my code (reading USART DR manually in the main loop). Forgotten line in the main loop corrupt my code only when main loop was delayed by the EXTI or SPI interrupt handlers with specific timing. Sorry for this stupid mistake. Thanks to all for your time!