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

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

Re: [avr-gcc-list] Interrupt prologue and epilogue (TEMP register)


From: Bruce D. Lightner
Subject: Re: [avr-gcc-list] Interrupt prologue and epilogue (TEMP register)
Date: Wed, 11 Oct 2006 14:45:45 -0700
User-agent: Thunderbird 1.5.0.7 (Windows/20060909)

Joerg Wunsch wrote:
Simon Han <address@hidden> wrote:

  
	Is there any documentation about how interrupt prologue and
epilogue are implemented?  I am mainly interested in what registers
need to be save and why.
    

In an interrupt context: everything you change in your ISR needs to be
saved.  If your instructions modify SREG, that needs to be saved as
well.  You are not even allowed to make the assumption that r1
contained a literal 0 at interrupt time.
  

What Joerg says is correct, "everything you change in your ISR needs to be saved", but sometimes your interrupt service routine (ISR) cannot preserve and then restore *everything* that you may need to change inside the ISR, namely the AVR's "hidden" 8-bit temporary register, sometimes called "TEMP".

See for example the "avr-libc" FAQ "Why do some 16-bit timer registers sometimes get trashed?"...

  http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_16bitio

...which points out that if you have an ISR that accesses a 16-bit AVR I/O register that make use of TEMP for "atomic" access, you will need to disable interrupts around any and all of your *application code* that uses TEMP in any way.

For example, when accessing the 16-bit timer/counter register TCNT1 from your "non-interrupt level" code, you need to do something like this if any of your ISR routines might alter TEMP:

        unsigned char sreg;
        unsigned short val;
        .
	.
	.
	sreg = SREG;  // save interrupt flag
        cli();        // turn interrupts off
        val = TCNT1;  // read counter/timer (alters TEMP!)
        SREG = sreg;  // restore interrupt flag

Otherwise, you probably are in for some nasty surprises!

Why Atmel never provided a way to directly access TEMP (e.g., via an 8-bit I/O register) is a mystery to me!  "Hidden state" in a microprocessor is like "write-only" registers...it's "bad form", and gets negative "style points" from computer architects and programmers alike!

For "extra credit": Who knows how to save and restore TEMP inside an ISR?  [Hint: It is possible, but a "general solution" that works under *all* circumstances eludes me.]

For more "extra credit": How would you confirm that there is only a single TEMP register in a given AVR microcontroller family?  If there is more than one TEMP register, can one in fact write code to save/restore TEMP inside an ISR?  [Hint: Unlikely!]

Best regards,

Bruce


-- 
 Bruce D. Lightner
 Lightner Engineering
 La Jolla, California
 Voice: +1-858-551-4011
 FAX: +1-858-551-0777
 Email: address@hidden
 URL: http://www.lightner.net/lightner/bruce/

reply via email to

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