Skip to main content
Pilous Droip
Senior
March 20, 2018
Question

Callback function with parameter?

  • March 20, 2018
  • 1 reply
  • 1074 views
Posted on March 20, 2018 at 13:03

Hello friends.

I create callback function for uart:

typedef void (*InterruptHandler) (uint32_t uart_interrupt);

   InterruptHandler UART_HandlerTable[MaxInterrupt];

   extern void UART_callback(uint32_t InterruptID, InterruptHandler Handler);

And callback function:

void UART_callback(uint32_t InterruptID, InterruptHandler Handler)

{

   UART_HandlerTable[InterruptID] = Handler;

}

And when I receive data from uart I generate callback function:

void UART_generate_call_back(uint8_t *input_buffer)

{

   UART_HandlerTable[input_buffer[0]](input_buffer[0]);

}

And in main I use this:

void callback_HELLO();

int main(void)

{

UART_callback( 0x01, (InterruptHandler)

callback_HELLO 

);

   while (1)

   {

   }

}

void

callback_HELLO

()

{

   usart_puts('\r\nHello world\r\n');

}

Now, it is working very good. 

But how to add parameter to callback function?

For example. I receive message, this message I analyze and I need hand over some data.

So I need modificate this function to return *data to callback.

void UART_generate_call_back(uint8_t *input_buffer, uint8_t *data)

{

   UART_HandlerTable[input_buffer[0]](input_buffer[0]);

}

So how to get data to callback function? And in main I would like get data.

void

callback_HELLO

(uint8_t *data)

{

       /* do something with data */

   usart_puts('\r\nHello world\r\n');

}

Is it posible?

#callback
    This topic has been closed for replies.

    1 reply

    Tesla DeLorean
    Guru
    March 20, 2018
    Posted on March 20, 2018 at 14:34

    >>Is it possible?

    Sure, the way you have the call defined at the moment it expects a 32-bit parameter.

    If you want the data in the array with the callback, you'd likely want to use a structure, and array that.

    Think you want

    void

    callback_HELLO

    (uint8_t data)

    rather than the pointer.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    Pilous Droip
    Senior
    March 20, 2018
    Posted on March 20, 2018 at 15:26

    I call this function, when I receive data on uart.

    void UART_generate_call_back(uint8_t *input_buffer)

    {

       UART_HandlerTable[input_buffer[0]](input_buffer[0]);

    }

    I have uart data in input_buffer. And this datas I would like to send by parameter to callback function.

    So how to modificate this function, to send array data as parameter?

       UART_HandlerTable[input_buffer[0]](input_buffer[0]);

    And here I would like to work with send datas.

    void 

    callback_HELLO

    (uint8_t *data)

    {

       /* do something with data */

       usart_puts('\r\nHello world\r\n');

    }

    Tesla DeLorean
    Guru
    March 20, 2018
    Posted on March 20, 2018 at 16:00

    It is currently sending the first character in the buffer, if you want it to send the pointer, then do that instead

    typedef void (*InterruptHandler)(uint8_t *data);

    ..

    UART_HandlerTable[input_buffer[0]](input_buffer); // Send the pointer

    ..

    void 

    callback_HELLO

    (uint8_t *data)

    typedef void (*InterruptHandler) (uint8_t data);

    ..

    UART_HandlerTable[input_buffer[0]](input_buffer[0]); // Send the char

    ..

    void 

    callback_HELLO

    (uint8_t data)
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..