Function Pointer and passing arguments inside an interrrupt

This is a discussion on Function Pointer and passing arguments inside an interrrupt within the C Programming forums, part of the General Programming Boards category; Hi, I am struggling with the following and would like to know if the following would be possible. The Scenario ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    Function Pointer and passing arguments inside an interrrupt

    Hi,

    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:
    Code:
    //The function have triggered by the Interrupt Timer
    char Timer_Expired(char *Flag){
    
             *Flag = True;
    
    return *Flag;
     }
    Now I will use it as follows:
    Code:
     char Do_Something( char *Data, char Size){
    
     char flagset = True;
    
                SendData(Data, Size);
                Hook_Function_to_Timer(&Timer_Expired(&flagset )); //This does not work
    
     }
    Code:
     //Type Defs
    
     typedef char (*ISR_FUNC_PTR)();//Something needs to change here I think.
    
     /****************/
     //Variables
    
     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.
         }
         __low_power_mode_off_on_exit();
     }
    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,

    Gary

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    By its nature, the ISR doesn't receive any parameters. You have no choice but a global variable.
    Code:
    //try
    //{
    	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
    Location
    The edge of the known universe
    Posts
    32,851
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 07-28-2011, 07:45 AM
  2. Passing a pointer to a function inside a class
    By shiroaisu in forum C++ Programming
    Replies: 1
    Last Post: 11-02-2010, 12:22 AM
  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, 03:45 AM
  5. Replies: 4
    Last Post: 06-15-2005, 09:30 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21