1

As the title said, I am trying to see if an Interrupt Vector can be embedded inside a function? I am using IAR, but gcc or ccs would work too. I don't see it done in any code online. Example:

void function_funtime (int test){
    int lm4970_state = 0;
    int abc = test + 3;

    // Port 1 interrupt service routine
    #pragma vector = PORT1_VECTOR
    __interrupt void Port_1_ISR (void)
    {
         _BIC_SR_IRQ(LPM3_bits + GIE);     // Clear LPM/Disable Interrupts
         lm4970_state++;     // Increase LM4970 state by 1
         P1IFG &= ~PB;       // P1.3 IFG cleared
    }
}

Function "function_funtime" is called from the main code before the interrupt is ever seen. Can this be done?

Passerby
  • 72,580
  • 7
  • 90
  • 202
  • 2
    Is nesting function definitions not prohibited in C? – user28910 Nov 26 '13 at 20:45
  • 1
    And even if you could, why would you want to? – user28910 Nov 26 '13 at 20:53
  • 2
    XY problem: You are asking about your attempted solution rather than your actual problem. – Rev Nov 26 '13 at 21:11
  • 1
    @Rev1.0 making tidy code that I can move between projects without having to pick through which ISR goes with which functions, without needing ifdefined checks or anything like that. – Passerby Nov 26 '13 at 21:57
  • 1
    Hmm, if you want to "share" code between projects, wouldn't it be cleaner to create an independent module (c/h file combo)? That way you can encapsulate the ISR and just expose function_funtime() from the header. – Rev Nov 27 '13 at 07:55
  • 1
    @Rev1.0, The ISR has to be "exposed" too, otherwise the linker won't be able to find it. – user28910 Nov 27 '13 at 14:25
  • 1
    @user28910: Doesn't the ISR has global scope by default like all functions that are not declared static? And since it doesn't need to be referenced from code it can stay out of the header file as well. – Rev Nov 27 '13 at 15:27
  • True; I was maybe confused about what you meant by "encapsulate". – user28910 Nov 27 '13 at 19:16

1 Answers1

1

No.

You can't define a function inside of another function in C. I'm not sure what you're trying to do, but maybe you can set flags for the ISR in the function or simply enable the interrupt in the function. Either of those may have the same effect.

Samuel
  • 11,811
  • 31
  • 51