• AVR Freaks

Saving register before ISR

Author
smam
Junior Member
  • Total Posts : 91
  • Reward points : 0
  • Joined: 2008/04/09 14:32:14
  • Location: 0
  • Status: offline
2008/09/04 10:46:08 (permalink)
0

Saving register before ISR

Hi,
    I want to save the values of the registers before going into the ISR and restore them when I come out. I have gone through C30 userguide and found these below.

/* Interrupt Service Routine 2 */
/* Fast context save (using push.s and pop.s) */
void __attribute__((__interrupt__, __shadow__)) _T1Interrupt(void)
{
/* Interrupt Service Routine code goes here */
}
/* Interrupt Service Routine 3: INT0Interrupt */
/* Save and restore variables var1, var2, etc. */
void __attribute__((__interrupt__(__save__(variable1,variable2))))
_INT0Interrupt(void)
{
/* Interrupt Service Routine code goes here */
}

Can anyone please explain the right method  to save the registers (for example W0..W4 etc.)

                                           Thanks very much,
                                           Syed
#1

12 Replies Related Threads

    guymc
    Administrator
    • Total Posts : 769
    • Reward points : 0
    • Joined: 2004/06/08 07:14:37
    • Status: offline
    RE: Saving register before ISR 2008/09/04 11:12:14 (permalink)
    0
    Syed,

    The compiler automatically saves and restores any registers that it uses in an interrupt function.

    If it sees that the interrupt function calls another function, it will save and restore all of the working registers that the called function might use.

    It is not necessary to be concerned with individual registers when programming in C, since the W register array is a compiler-managed resource.

    Cheers..
    #2
    smam
    Junior Member
    • Total Posts : 91
    • Reward points : 0
    • Joined: 2008/04/09 14:32:14
    • Location: 0
    • Status: offline
    RE: Saving register before ISR 2008/09/05 01:14:42 (permalink)
    0
    Hi,
        Thanks for your reply. But how can we save the values of register of we are calling a library function. For example the Vectoradd function uses the following registers which are not restord. Can anyone please provide some exmaples.

    Example: (VectorAdd)
    Function Profile: System resources usage:
    W0..W4 used, not restored
    ACCA used, not restored
    CORCON saved, used, restored
    DO and REPEAT instruction usage:
    1 level DO instructions
    no REPEAT instructions

                                                 Thanks very much,
                                                  Syed
    #3
    zardoz1
    Super Member
    • Total Posts : 1852
    • Reward points : 0
    • Joined: 2005/07/09 08:03:28
    • Location: 's-Hertogenbosch, The Netherlands
    • Status: offline
    RE: Saving register before ISR 2008/09/05 05:46:19 (permalink)
    0
    The behaviour mentioned by Guymc works for the working registers according the C calling convention. So when calling a function from an ISR, the compiler will take care of saving and restoring w0-w7 since these may be changed by the called function.
     
    So the fact that VectorAdd uses w0-w4 and does not restore them is taken care of by the C compiler.
     
    The DSP specific registers that are used and not restored you have to take care of yourself inside the ISR by using inside assembly.
     
    Personally I don't like calling DSP functions from within an ISR. First since these functions can use all kinds of registers not compatible with the standard C30 register save/restore procedure, as is illustrated by your example, second the duration of these functions and the required bookkeeping. DSP functions often work on arrays so the data has to be accumulated first during a number of activations of the ISR. So you might just as well fill some buffer in the ISR and once the buffer is complete, set some flag to trigger the main code for the required processing. In the meantime the ISR can be using a second buffer for new data coming in.


    AVIX
    the PIC32 & dsPIC/PIC24 RTOS with:
    - Zero Latency Interrupts
    - The best performance!
    - Integrated Power Management
    Download here: http://www.avix-rt.com/
    #4
    smam
    Junior Member
    • Total Posts : 91
    • Reward points : 0
    • Joined: 2008/04/09 14:32:14
    • Location: 0
    • Status: offline
    RE: Saving register before ISR 2008/09/05 06:00:05 (permalink)
    0
    Thanks for your reply.

    Sorry for the confusion. I want to know the method for restoring the DSP specific registers that are used and not restored (not only in ISR but even inside main code). I want to know some example codes for how to do this in assembly. Please, can anyone provide me some example codes if possible.


                                                    Regards,
                                                    Syed
    #5
    zardoz1
    Super Member
    • Total Posts : 1852
    • Reward points : 0
    • Joined: 2005/07/09 08:03:28
    • Location: 's-Hertogenbosch, The Netherlands
    • Status: offline
    RE: Saving register before ISR 2008/09/05 11:06:37 (permalink)
    0
    In C program:
     
    asm volatile ("push CORCON");
     
    to save and:
     
    asm volatile ("pop CORCON");
     
    to restore


    AVIX
    the PIC32 & dsPIC/PIC24 RTOS with:
    - Zero Latency Interrupts
    - The best performance!
    - Integrated Power Management
    Download here: http://www.avix-rt.com/
    #6
    MBedder
    Circuit breaker
    • Total Posts : 6809
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    RE: Saving register before ISR 2008/09/05 12:11:38 (permalink)
    0
    Note that the DSP engine status flags found in the SRH register can not be saved/restored by PUSH/POP operations because the OA/OB and SA/SB DSP status bits are read/clear only. The background task must make its DSP operations atomic (disable interrupts), or the ISR should handle these flags in a special way and signal their pre-ISR states to a background task
    #7
    zardoz1
    Super Member
    • Total Posts : 1852
    • Reward points : 0
    • Joined: 2005/07/09 08:03:28
    • Location: 's-Hertogenbosch, The Netherlands
    • Status: offline
    RE: Saving register before ISR 2008/09/05 13:22:12 (permalink)
    0
    The easiest and most understandable way of using DSP operations is from a single 'thread', not using DSP operations from an ISR or in case of an RTOS from multiple threads.
     
    Many discussions have already been held on this topic of the DSP status flags and the above was more or less the common conclusion.


    AVIX
    the PIC32 & dsPIC/PIC24 RTOS with:
    - Zero Latency Interrupts
    - The best performance!
    - Integrated Power Management
    Download here: http://www.avix-rt.com/
    #8
    smam
    Junior Member
    • Total Posts : 91
    • Reward points : 0
    • Joined: 2008/04/09 14:32:14
    • Location: 0
    • Status: offline
    RE: Saving register before ISR 2008/09/06 17:24:16 (permalink)
    0
    Hi,
        Thanks for your replies. I am using all the DSP fucntion from the main program and not from within the ISR.  So is this the right way to save registers w1, w2, w3 ?
     
    asm volatile ("push W1");
    asm volatile ("push W2");
    asm volatile ("push W3");

    to save and:

    asm volatile ("pop W1");
    asm volatile ("pop W2");
    asm volatile ("pop W3");

                                         Regards,
                                         Syed

    #9
    Oznog
    Super Member
    • Total Posts : 1011
    • Reward points : 0
    • Joined: 2004/10/14 21:54:10
    • Status: offline
    RE: Saving register before ISR 2008/09/06 17:40:55 (permalink)
    0
    Correct me if I'm wrong but the C compiler assumes w0-w4 and ACCA/ACCB will not be preserved during any ASM call, right?  It already saves these registers off and/or has code where it won't matter.  For example if the next piece of code loads something into w0 then there was no reason to push/pop w0 to preserve it over the function call.  And the C compiler doesn't use or manage the ACCA/B regs at all.

    If the ISR has only an ASM call in it then the compiler should be saving off w0-w4 automatically to preserve them.

    You must POP in the opposite order from PUSH, LIFO:
    asm volatile ("push W1");
    asm volatile ("push W2");
    asm volatile ("push W3");

    to save and:

    asm volatile ("pop W3");
    asm volatile ("pop W2");
    asm volatile ("pop W1");
    #10
    MBedder
    Circuit breaker
    • Total Posts : 6809
    • Reward points : 0
    • Joined: 2008/05/30 11:24:01
    • Location: Zelenograd, Russia
    • Status: offline
    RE: Saving register before ISR 2008/09/06 23:02:44 (permalink)
    0
    And the C compiler doesn't use or manage the ACCA/B regs at all.
    The ACCx registers are used by the C30 compiler whenever any DSP relevant builtin function is used
    #11
    zardoz1
    Super Member
    • Total Posts : 1852
    • Reward points : 0
    • Joined: 2005/07/09 08:03:28
    • Location: 's-Hertogenbosch, The Netherlands
    • Status: offline
    RE: Saving register before ISR 2008/09/07 00:47:54 (permalink)
    0
    If the ISR has only an ASM call in it then the compiler should be saving off w0-w4 automatically to preserve them

     
    The C compiler does not care whether a function called from an ISR is a C or an ASM function, it just does not know.
     
    So a C ISR will always save w0-w7 when a function is called from the ISR. The called function is responsible for saving w8-w14 might it use those registers.
     
    If the called function is a C function the C compiler takes care of this. If the called function is an ASM function, the programmer must take care of this.


    AVIX
    the PIC32 & dsPIC/PIC24 RTOS with:
    - Zero Latency Interrupts
    - The best performance!
    - Integrated Power Management
    Download here: http://www.avix-rt.com/
    #12
    smam
    Junior Member
    • Total Posts : 91
    • Reward points : 0
    • Joined: 2008/04/09 14:32:14
    • Location: 0
    • Status: offline
    RE: Saving register before ISR 2008/09/10 02:41:00 (permalink)
    0
     Hi,
         Sorry for this but I need some help when using the DSP library function .  I have come across this in the DSP library documentation. I AM CALLING THESE LIBRARY FUNCTION FROM THE MAIN PROGRAM AND NOT FROM WITHIN THE ISR.
     
    “If a library function will be interrupted, it is your responsibility to save and restore the contents of all registers used by the function, including the state of the DO, REPEAT and special addressing hardware. Naturally this also includes saving and restoring the contents of the CORCON and Status registers.”
     
    For example the Vectoradd function uses the following registers which are not restord.

    Example: (VectorAdd)
    Function Profile: System resources usage:
    W0..W4 used, not restored
    ACCA used, not restored
    CORCON saved, used, restored
    DO and REPEAT instruction usage:
    1 level DO instructions
    no REPEAT instructions
     
    So, what should I need to do in order to save and restore these functions, if suppose the Vector add function is interrupted by the ISR (see below). It would be helpful if someone could provide some examples.
     
    main(){
    ………………
    ………………..
    (void) VectorAdd(TOTAL_SAMPLES, &ip_sig[0], ip_sig[0],&sig_2[0]);
    ………………
    ………………
    ………………
    ………………
    }

     
    ISR(){
    ………………
    ………………
    ………………
    ………………
     
    }
                                                                      Thanks very much,
                                                                       Syed

    #13
    Jump to:
    © 2019 APG vNext Commercial Version 4.5