Passing Parameters on chVTSet()

Discussions and support about ChibiOS/RT, the free embedded RTOS.
Post Reply
samtatasurya
Posts: 22
Joined: Sun Nov 29, 2015 8:13 pm

Passing Parameters on chVTSet()

Post by samtatasurya »

Hi everyone,

I need to pass a parameter to a callback function that is going to be called by chVTSet(). However, I couldn't find an example that performs this task.
Basically, I need to pass a boolean pointer to the callback, and for my application global variable can't be used.
Can somebody show me how to do this the right way?

Below is my sample code. I got a warning that states that "passing argument 3 of 'chVTSet' from incompatible pointer type"

Code: Select all

void raiseTimeout(bool *p) {
    (*p) = true;
}

void testFunc(void) {
    bool is_timeout = false;
    virtual_timer_t vt;

    chVTReset(&vt);
    chVTSet(&vt, MS2ST(200), raiseTimeout, &is_timeout);
}


I'm using ChibiOS 3.0.4, and my application is for STM32F4-Discovery kit.

Thanks! :)

- Sam
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: Passing Parameters on chVTSet()

Post by barthess »

samtatasurya wrote:void raiseTimeout(bool *p) {
(*p) = true;
}

Your function accepts (bool *p). You need to change it to (void *p).
User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Has thanked: 5 times
Been thanked: 6 times

Re: Passing Parameters on chVTSet()

Post by Korken »

Plus your testfunc will create the bool and the timer object on the stack that cannot be guaranteed to exist when the function exits, as it might be over written. You must make them static or global.
samtatasurya
Posts: 22
Joined: Sun Nov 29, 2015 8:13 pm

Re: Passing Parameters on chVTSet()

Post by samtatasurya »

barthess wrote:
samtatasurya wrote:void raiseTimeout(bool *p) {
(*p) = true;
}

Your function accepts (bool *p). You need to change it to (void *p).


Hi barthess,

I've just tried changing the parameter of raiseTimeout to (void *p), but instead of warning it is now giving an error and a warning instead.
Error: conflicting types for 'raiseTimeout'
Warning: dereferencing 'void *' pointer

I know the cause of the error above (since the parameter that I passed from testFunc() to raiseTimeout() is of type bool *) and the warning (since I dereference p and want to change the value of p from inside raiseTimeout()). But I still don't know how I should pass one or more parameters to a callback called using chVTSet().

Do you have any other suggestion that I might be able to try?

Thanks!

- Sam
samtatasurya
Posts: 22
Joined: Sun Nov 29, 2015 8:13 pm

Re: Passing Parameters on chVTSet()

Post by samtatasurya »

Korken wrote:Plus your testfunc will create the bool and the timer object on the stack that cannot be guaranteed to exist when the function exits, as it might be over written. You must make them static or global.


Hi Korken,

Thank you for your reminder regarding the lifetime of the variable that's passed to the callback. I'm pretty sure your suggestion of using static variable is suitable for my application. :D

- Sam
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Passing Parameters on chVTSet()

Post by Giovanni »

You need to cast it again to (bool *) in the callback.

Giovanni
samtatasurya
Posts: 22
Joined: Sun Nov 29, 2015 8:13 pm

Re: Passing Parameters on chVTSet()

Post by samtatasurya »

Giovanni wrote:You need to cast it again to (bool *) in the callback.

Giovanni


Hi Giovanni,

Thank you, the code compiled successfully without any error or warning! :D
I'll verify if it works as intended once I actually test it on the board.

Again, thank you everyone for the quick reply!

- Sam
samtatasurya
Posts: 22
Joined: Sun Nov 29, 2015 8:13 pm

Re: Passing Parameters on chVTSet()

Post by samtatasurya »

Hi everyone,

Just an update after my test:

Code: Select all

void raiseTimeout(bool *p) {
    (*((bool*)p)) = true;
}

void testFunc(void) {
    static bool is_timeout;
    static virtual_timer_t vt;

    is_timeout = false;

    chVTReset(&vt);
    chVTSet(&vt, MS2ST(200), raiseTimeout, &is_timeout);
}


This particular prototypical implementation works.
Again, thank you everyone!

- Sam
Post Reply