• AVR Freaks

Hot!static variables inside duplicated non-reentrant functions

Author
f3ynman
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2018/08/15 00:33:38
  • Location: 0
  • Status: offline
2020/01/24 08:58:27 (permalink)
0

static variables inside duplicated non-reentrant functions

hello everyone,
im using one of my isr functions outside isr too and i got that warning from compiler. I wonder if would static variables inside those functions still be same.
post edited by f3ynman - 2020/01/24 09:01:30
#1

7 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 3349
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: online
    Re: static variables inside duplicated non-reentrant functions 2020/01/24 09:08:40 (permalink)
    +2 (2)
    As the functions are duplicated (and the scope of the static variables is restricted to "within the function"), you should get duplicate variables as well. (Never tried such a construct, but I expect the outcome to be just like that.)

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    andersm
    Super Member
    • Total Posts : 2741
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: static variables inside duplicated non-reentrant functions 2020/01/24 09:10:06 (permalink)
    +1 (1)
    ISRs are not normal functions, and you can't call them from other functions. If you want to share code, extract the common parts into its own function that you can call from both the ISR and normal code. Accessing the same variables both from an ISR and user code without any synchronization is a bug.
    #3
    1and0
    Access is Denied
    • Total Posts : 10346
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: static variables inside duplicated non-reentrant functions 2020/01/24 09:10:18 (permalink)
    +1 (1)
    f3ynman
    hello everyone,
    im using one of my isr functions outside isr too and i got that warning from compiler. I wonder if would static variables inside those functions still be same.

    Check the Watch window or disassembly to see if they have same/different addresses.
    #4
    LdB_ECM
    Super Member
    • Total Posts : 236
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: static variables inside duplicated non-reentrant functions 2020/01/24 09:22:04 (permalink)
    0
    On most CPU's you cant call the ISR function outside an IRQ because it pushes registers down on the stack which wont happen when you try to call the function direct.
     
    However you can do it is simply make a normal function and call it from within the ISR
    So this is fully legal and however I also suggest you turn off interrupts when calling it from main unless your code is written to be re-enterant.
     
    static void MyFunction (int SomeData)
    {
        /* do something from both ISR and Outside */
    }
     
    void __attribute__((interrupt,no_auto_psv)) _SomeInterrupt(void)
    {
         MyFunction(10);
    }
     
    int main (void)
    {
        DisableInterrupts();
        MyFunction(7);
        EnableInterrupts();
    }

     
    #5
    1and0
    Access is Denied
    • Total Posts : 10346
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: static variables inside duplicated non-reentrant functions 2020/01/24 09:23:02 (permalink)
    +1 (1)
    From TFM (User's Guide):
    XC8 User's Guide "Function Duplication"
    The duplicated code and objects defined by the function use unique identifiers. A duplicate identifier is identical to that used by the original code, but is prefixed with i1. Duplicated PIC18 functions use the prefixes i1 and i2 for the low- and high-priority interrupts, respectively.

    #6
    NorthGuy
    Super Member
    • Total Posts : 5917
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: static variables inside duplicated non-reentrant functions 2020/01/24 12:27:07 (permalink)
    +3 (3)
    The easy way is to make the static variable global. Either way, you need to make sure that when the variable is used by two reincarnations of your function at the same time, nothing bad will happen.
    #7
    ric
    Super Member
    • Total Posts : 25592
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: static variables inside duplicated non-reentrant functions 2020/01/24 13:35:30 (permalink)
    +2 (2)
    Alternatively, if you really know what you are doing, you can tell the compiler to NOT duplicate the function.
    However, in general I would have to agree with:
    andersm
    ...
    Accessing the same variables both from an ISR and user code without any synchronization is a bug.




    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    Jump to:
    © 2020 APG vNext Commercial Version 4.5