Page 1 of 1
Passing Parameters on chVTSet()
Posted: Sat Feb 27, 2016 12:41 am
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
Re: Passing Parameters on chVTSet()
Posted: Sat Feb 27, 2016 7:27 am
by barthess
samtatasurya wrote:void raiseTimeout(bool *p) {
(*p) = true;
}
Your function accepts (bool *p). You need to change it to (void *p).
Re: Passing Parameters on chVTSet()
Posted: Sat Feb 27, 2016 8:29 am
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.
Re: Passing Parameters on chVTSet()
Posted: Sat Feb 27, 2016 9:34 am
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 *' pointerI 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
Re: Passing Parameters on chVTSet()
Posted: Sat Feb 27, 2016 9:36 am
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.
- Sam
Re: Passing Parameters on chVTSet()
Posted: Sat Feb 27, 2016 9:36 am
by Giovanni
You need to cast it again to (bool *) in the callback.
Giovanni
Re: Passing Parameters on chVTSet()
Posted: Sat Feb 27, 2016 9:47 am
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!
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
Re: Passing Parameters on chVTSet()
Posted: Wed Mar 09, 2016 1:43 am
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