• AVR Freaks

Hot!Compiler interrupt flag

Author
visenri
Junior Member
  • Total Posts : 25
  • Reward points : 0
  • Joined: 2017/10/25 15:30:29
  • Location: 0
  • Status: offline
2020/11/25 18:29:29 (permalink)
0

Compiler interrupt flag

Some time ago I noticed an extra couple of instructions that are added automatically by XC8 compiler for the interrupt function:

void interrupt ISR (void)
{
...
}

Translates into:

bsf 126,0 ;set compiler interrupt flag (level 1)
....
bcf 126,0 ;clear compiler interrupt flag (level 1)
retfie

There is no mention to this behavior in the manual.
It sets a flag at address 0x7E (common memory in this example using PIC16F18346)
 
Does anyone know what is the purpose of this flag?
I've never seen it being used, it's just set at the ISR beginning and cleared before "retfie".
 
I've been working in an application that needed the minimal interrupt latency, so this extra instruction was a candidate for optimization (Yes, I needed every little optimization, I had to write to one port in reaction to the ISR in less than 1us, 6 instructions was my target: 4 lattency, movf and movwf using common memory @32Mhz).
 
I had to do this to avoid the extra instructions:

void ISR (void) __at(0x0004)

And add a call after the main loop to stop the compiler from removing this "unused" function.
 
The problem with this is that:
Compiler thinks no interrupt is being used.
Compiler thinks it's free to reuse any temporary variable used in the interrupt function for any other function.
 
In my case, these were not big problems, because the code used no temporary variables and was mainly inline asm.
 
Does anyone know a way to avoid the use of this special compiler interrupt flag (using C)?
#1

7 Replies Related Threads

    ric
    Super Member
    • Total Posts : 29435
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Compiler interrupt flag 2020/11/25 18:33:46 (permalink)
    0
    visenri
    ...
    Does anyone know a way to avoid the use of this special compiler interrupt flag (using C)?

    Only by ignoring the last requirement.
    i.e. write the whole thing in assembler, which is a must if you require minimum latency.
     

    Compiler thinks it's free to reuse any temporary variable used in the interrupt function for any other function.

    Just make them "static" or "global", which is a given if they are used by an ISR.
     
     

    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!
    #2
    visenri
    Junior Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2017/10/25 15:30:29
    • Location: 0
    • Status: offline
    Re: Compiler interrupt flag 2020/11/25 18:38:04 (permalink)
    +1 (1)
    ric
    visenri
    Compiler thinks it's free to reuse any temporary variable used in the interrupt function for any other function.

    Just make them "static" or "global", which is a given if they are used by an ISR.

    I was not talking about the variables defined by me, this is easy as you pointed. I was talking about compiler temporary storage for operations.
     
    #3
    ric
    Super Member
    • Total Posts : 29435
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Compiler interrupt flag 2020/11/25 18:44:07 (permalink)
    +1 (1)
    Indeed, if you go out of your way to convince the compiler it is not an interupt service, then it won't treat it as an interrupt service.
    As stated, the only real solution is to write it in assembler yourself.

    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!
    #4
    ric
    Super Member
    • Total Posts : 29435
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Compiler interrupt flag 2020/11/25 18:51:09 (permalink)
    +1 (1)
    n.b. user "mad_c" is best placed to answer what the flag is used for,
    He alludes to it in this response: https://www.microchip.com/forums/FindPost/1016064
     

    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!
    #5
    mad_c
    Super Member
    • Total Posts : 1271
    • Reward points : 0
    • Joined: 2010/12/12 17:48:27
    • Location: Brisbane, Australia
    • Status: offline
    Re: Compiler interrupt flag 2020/11/26 17:18:46 (permalink)
    +2 (2)
    visenri
    Some time ago I noticed an extra couple of instructions that are added automatically by XC8 compiler for the interrupt function:
    Does anyone know what is the purpose of this flag?

    Hi,
     
    It indicates that code is in an interrupt, as opposed to main-line code, and is used to work out which instance of a routine should be called in some situations, for example where a routine was called via a pointer. If your interrupt code has no need for it, then the instructions to set and clear it could be omitted, although it may still be used by code generated from C source. It's only defined when a compiled stack is being used.
     
    Jeff.
    #6
    visenri
    Junior Member
    • Total Posts : 25
    • Reward points : 0
    • Joined: 2017/10/25 15:30:29
    • Location: 0
    • Status: offline
    Re: Compiler interrupt flag 2020/11/26 18:03:15 (permalink)
    +1 (1)
    mad_c
    visenri
    Some time ago I noticed an extra couple of instructions that are added automatically by XC8 compiler for the interrupt function:
    Does anyone know what is the purpose of this flag?

    Hi,
    It indicates that code is in an interrupt, as opposed to main-line code, and is used to work out which instance of a routine should be called in some situations, for example where a routine was called via a pointer. If your interrupt code has no need for it, then the instructions to set and clear it could be omitted, although it may still be used by code generated from C source. It's only defined when a compiled stack is being used.

     
    This flag could be omitted by the optimizer if it is not used by any code generated by the compiler, but it's not.
    Other optimizations like this exist for the interrupt, IIRC with non-enhanced cores, FSR register is only saved if used by the compiler.
     
    So, the only way to disable these instructions is using reentrant stack for the whole project.
    Am I right?
     
    post edited by visenri - 2020/11/26 18:04:28
    #7
    mad_c
    Super Member
    • Total Posts : 1271
    • Reward points : 0
    • Joined: 2010/12/12 17:48:27
    • Location: Brisbane, Australia
    • Status: offline
    Re: Compiler interrupt flag 2020/11/26 20:17:00 (permalink)
    +2 (2)
    visenri
    This flag could be omitted by the optimizer if it is not used by any code generated by the compiler, but it's not.

    In theory, yes, but there could be complications to such a scheme.
    So, the only way to disable these instructions is using reentrant stack for the whole project.

    Assuming you don't want to use a device that has a similar feature in hardware or write in assembly code, then yes.
     
    Jeff.
    #8
    Jump to:
    © 2021 APG vNext Commercial Version 4.5