Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training




Loading

Compiler Seems to Generate Incorrect Interrupt Handler Code

Posted by bmenkveld on June 4, 2014

I finally got to the bottom of a very nasty issue that was causing random crashes of my software. With this post I'm hoping to maybe save somebody else a lot of time and aggravation, but I would also appreciate any insights from those with more knowledge of the compiler, FreeRTOS, and just what they are supposed to do with interrupt handlers.

The program I'm working on is a new application for an AT32UC3C1512 CPU, using FreeRTOS V7.0.0. I'm using Atmel Studio V6.2.

The problem code looked like this:

if GNUC

attribute((naked))

elif ICCAVR32
pragma shadow_registers = full // Naked.
endif

static void can0inttxhandler(void) { /* This ISR can cause a context switch, so the first statement must be a call to the portENTERSWITCHINGISR() macro. This must be BEFORE any variable declarations. */ portENTERSWITCHING_ISR();

U8 handle; handle = CANIFmobgetmobtxok(0); ...

/* Exit the ISR. Supposed to pass indication of task switch, but no in the FreeRTOS example code. */ portEXITSWITCHINGISR(); }

Here's the listing file showing what the compiler produced for this code segment:

8000ffcc : static void can0inttxhandler(void) { /* This ISR can cause a context switch, so the first statement must be a call to the portENTERSWITCHINGISR() macro. This must be BEFORE any variable declarations. */ portENTERSWITCHINGISR(); 8000ffcc: eb cd 00 ff pushm r0-r7 8000ffd0: e0 68 00 08 mov r8,8 8000ffd4: ea 18 00 00 orh r8,0x0 8000ffd8: 70 00 ld.w r0,r8[0x0] 8000ffda: 1a d0 st.w --sp,r0 8000ffdc: 7a 90 ld.w r0,sp[0x24] 8000ffde: e1 d0 c2 c3 bfextu r0,r0,0x16,0x3 8000ffe2: 58 10 cp.w r0,1 8000ffe4: e0 8b 00 08 brhi 8000fff4 <LABELISRSKIPSAVECONTEXT102> 8000ffe8: e0 68 12 c0 mov r8,4800 8000ffec: ea 18 00 00 orh r8,0x0 8000fff0: 70 00 ld.w r0,r8[0x0] 8000fff2: 81 0d st.w r0[0x0],sp

8000fff4 : U8 handle; handle = CANIFmobgetmobtxok(0); 8000fff4: fc 78 1c 00 mov r8,-189440 8000fff8: 70 c8 ld.w r8,r8[0x30] 8000fffa: e6 18 00 3f andh r8,0x3f,COH 8000fffe: b1 88 lsr r8,0x10 80010000: ef 68 ff ff st.b r7[-1],r8

Note that the very last line of assembler here uses register R7, but R7 has not been initialized. So this code ends up over-writing one byte on some task's stack, which eventually leads to bad things happening.

I fixed the problem by declaring "handle" as "static".

But why did the compiler generate what appears to be wrong code? And is there a different way to declare an interrupt handler so the compiler will generate correct code even for automatic variables?

Any insights would be appreciated.

And in a related matter, the portEXITSWITCHINGISR() macro generates code that seems to use R12 to decide whether or not to invoke a context switch. But how is the C code supposed to get the correct value into R12?

Bert Menkveld bert@greentronics.com


Compiler Seems to Generate Incorrect Interrupt Handler Code

Posted by heinbali01 on June 4, 2014

Hi Bert,

... must have been hard to find out where the problem comes from

This is what I usually do and which is safe:

~~~~~~ long irqHandlerPA1623 () { /* This is a normal function which saves and restores * registers used */ long needSwitch = 0;

/* Do your work here, be careful not to use too much stack
 * but local variables are OK to use */

xSemaphoreGiveFromISR( xGpioSemaphore, &needSwitch);

/* The return value will set r12 */
return needSwitch;

}

attribute((naked)) void irqPA1623 () { portENTERSWITCHINGISR (); { /* This function has the attribute naked, meaning * that registers are not saved or restored. * Just call your handler which returns a flag for * task-switching in r12 */ irqHandlerPA1623 (); } portEXITSWITCHINGISR (); } ~~~~~~

PS: if you put literal code in a SF post, it gets a nice syntax formatting if you put it between two lines which only contain 6 tildes each (~~~~~~), like I did here above.

Regards, Hein


Compiler Seems to Generate Incorrect Interrupt Handler Code

Posted by bmenkveld on June 4, 2014

Thanks for the response, Hein.

Yes, it was very hard to find the cause of this problem -- I will sleep better tonight!

Thanks for the example of a safe solution. I have seen and used the pair of functions as you show them. But in this case the interrupt handler was supplied by the Atmel Software Framework, and I did not think to re-work it. I will know better in the future.

And thank you also for explaining just how R12 is supposed to end up with the "needSwitch" value. That also was not at all clear to me from the FreeRTOS docs.

And I will try your code formatting magic next time I need to post some code here -- my apologies for the ugly post.

Regards,

Bert


[ Back to the top ]    [ About FreeRTOS ]    [ Sitemap ]    [ ]




Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists