Thread: Function Pointer and passing arguments inside an interrrupt

  1. #1
    Registered User
    Join Date
    Oct 2011

    Function Pointer and passing arguments inside an interrrupt


    I am struggling with the following and would like to know if the following would be possible.

    The Scenario - I have a timer running and I have the ability to connect a function to the timer if it triggers. This works great but what if I want to pass to the pointer that triggered an argument as well.

    For Example:
    //The function have triggered by the Interrupt Timer
    char Timer_Expired(char *Flag){
             *Flag = True;
    return *Flag;
    Now I will use it as follows:
     char Do_Something( char *Data, char Size){
     char flagset = True;
                SendData(Data, Size);
                Hook_Function_to_Timer(&Timer_Expired(&flagset )); //This does not work
     //Type Defs
     typedef char (*ISR_FUNC_PTR)();//Something needs to change here I think.
     static ISR_FUNC_PTR fptr;
     The Interupt looks something like this:
     #pragma vector=TIMERB0_VECTOR
     __interrupt void timerISR(void)
         if (fptr != NULL)
             (*fptr)();//Something needs to change here I think.
    Please can someone point me into the right direction. I basically want to pass the address of the function and the address of the variable to the interrupt that fires so to change the variable to False if the timer has trigger. I would like to stay away from global variables if possible.

    Best Regards,


  2. #2
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    This is actually one of the few cases where globals are acceptable, and even more incredulous is that they are required here. Parameters and return values aren't used for interrupt callbacks (at least none of the ones I've ever see). Interrupts can be called at any time from anywhere. They interrupt your normal programming, so you never know what state your program is in when the interrupt fires, and it makes it difficult to pass in data or return values.

    Actually, it's not that difficult. It's used by lots of "higher-level" callbacks, like those used for thread handlers, XML parsers or ADTs. Having to dig up the user data and return value stuff from a global structure just doesn't make sense for lower-level, time-sensitive stuff like interrupts.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    By its nature, the ISR doesn't receive any parameters. You have no choice but a global variable.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    All you need to know about function pointers -> The Function Pointer Tutorials - Index
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 07-28-2011, 06:45 AM
  2. Passing a pointer to a function inside a class
    By shiroaisu in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2010, 11:22 PM
  3. Help passing arguments to function
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 11-26-2007, 02:15 PM
  4. need function help, passing arguments
    By infernosnow in forum Game Programming
    Replies: 18
    Last Post: 07-18-2006, 02:45 AM
  5. Replies: 4
    Last Post: 06-15-2005, 08:30 PM

Tags for this Thread