avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Help: Weirdness with interrupts, ending up in unreac


From: Theodore A. Roth
Subject: Re: [avr-gcc-list] Help: Weirdness with interrupts, ending up in unreachable code
Date: Tue, 25 Nov 2003 20:30:51 -0800 (PST)

On Wed, 26 Nov 2003, Christian Ludlam wrote:

> On 26 Nov Dan Lyke wrote:
> 
> [snip]
> > Because I can't find "timer_enable_int" in any of my libraries, and I have:
> > 
> > __vector_7() // SIGNAL(_SIG_OVERFLOW0)
> > {
> >     if (slowdown)
> >     {
> >             slowdown--;
> >     }
> >     TCNT0 = 128; // will be variable once I get this working
> >     sei();
> > }
> > 
> 
> Don't declare your function as __vector_7, use SIGNAL(SIG_OVERFLOW0). I
> assume you tried this but you've got an extra _ in your signal name which
> will cause the function to be ignored - it would be nice if the compiler
> could warn you about this, and that has been suggested many times before.
> 
> The reason this will help is that the SIGNAL() macro tells the compiler that
> the functon is an interrupt handler, which will make it preserve more
> registers than it would normally have to. It will also preserve the status
> register (SREG) which will remove the need for the extra sei().

The sei() is never needed in a properly functioning interrupt handler. The 
compiler will generate code that uses the 'RETI' instruction to return from 
the interrupt which automatically sets the global interrupt flag in the 
status register (i.e. the equivalent of an 'SEI' instruction).

I just compiled this:

  SIGNAL (SIG_INTERRUPT0)
  {
  }

and the compiler spit out this:

   00000088 <__vector_1>:

  SIGNAL(SIG_INTERRUPT0)
  {
    88:   1f 92           push    r1
    8a:   0f 92           push    r0
    8c:   0f b6           in      r0, 0x3f        ; 63
    8e:   0f 92           push    r0
    90:   11 24           eor     r1, r1
    92:   0f 90           pop     r0
    94:   0f be           out     0x3f, r0        ; 63
    96:   0f 90           pop     r0
    98:   1f 90           pop     r1
    9a:   18 95           reti

Another gotcha, is if you forget to #include <avr/signal.h>. In that case, 
gcc doesn't know about the SIGNAL macro and will interpret it as a function 
returning int. Gcc will think the function looks like this:

  int SIGNAL (int SIG_OVERFLOW0) { ... }

You can catch this if you use the -Wall gcc option. Gcc will then spit out a 
warning like this:

  test.c:40: warning: return type defaults to `int'
  test.c: In function `SIGNAL':
  test.c:40: warning: control reaches end of non-void function

The -Wall will still not catch the case where you misspell the signal name.

Ted Roth



reply via email to

[Prev in Thread] Current Thread [Next in Thread]