Page 1 of 2
The Event Question
Posted: Thu Jan 15, 2015 5:46 pm
by Tabulous
Hi is this correct way to use events ?
Code: Select all
EventSource es1;
int main(void)
{
EventListener el1;
chEvtInit(&es1);
chEvtRegister(&es1, &el1, EVENT_MASK(0));
/*
* Normal main() thread activity
*/
while( TRUE )
{
chEvtWaitOne( EVENT_MASK(0) );
palTogglePad(GPIOG, GPIOG_LED_IND_BLUE);
}
}
static msg_t Thread(void *arg)
{
(void)arg;
while( TRUE )
{
chThdSleepMilliseconds(2500);
chEvtBroadcastFlags(&es1, EVENT_MASK(0));
}
}
Re: The Event Question
Posted: Thu Jan 15, 2015 7:02 pm
by Giovanni
It works but there is a basic misunderstanding of this:
chEvtBroadcastFlags(&es1, EVENT_MASK(0));
The second parameter is not the mask of the event, it is a mask of event-flags that is accumulated (ORed) into the listener. Note that there are two distinct types. If you don't need flags just pass zero as second parameter.
The difference between a mask-of-events and event-flags:
1) The mask of events is "local" to the thread, different threads can have a different meaning for each bit. Each event can listen to up 32 different sources but there can be more than 32 sources in the system.
2) The event flags are meaningful in the context of the event source, the flags represent additional info sent by the source to the listeners. For example, in the serial drivers are used for the line conditions: parity error, framing error etc.
I hope this makes it more clear, it is easy in reality but surprisingly hard to explain for some reason, I have yet to find the right formula for explanation

Giovanni
Re: The Event Question
Posted: Fri Jan 16, 2015 7:53 am
by Tabulous
Still not any clearer on this to be fair.
what i want to achieve is thread monitoring i.e. all other threads in my system set a flag.
The loop in main(void) looks to see if all these flags are set, and if they are it will update a watchdog timer, if they are not, ie. one or more thread has stopped then it will not update and thus the watchdog will reset the MCU.
Re: The Event Question
Posted: Fri Jan 16, 2015 8:41 am
by Giovanni
Then you don't need event flags, just do:
chEvtBroadcast(&es1);
Which is equivalent to:
chEvtBroadcastFlags(esp, 0);
Then you need to use:
chEvtWaitAll(<your_sources_mask>);
This way you only exit from wait if ALL threads broadcasted at least once.
Giovanni
Re: The Event Question
Posted: Fri Jan 16, 2015 9:30 am
by Tabulous
still nothing

this is my code
Code: Select all
EventSource es1;
int main(void)
{
EventListener el1;
chEvtInit(&es1);
chEvtRegister(&es1, &el1, EVENT_MASK(0));
/*
* Normal main() thread activity
*/
while( TRUE )
{
chEvtWaitAll( EVENT_MASK(0) );
palTogglePad(GPIOG, GPIOG_LED_IND_BLUE);
}
}
static msg_t Thread(void *arg)
{
(void)arg;
while( TRUE )
{
chThdSleepMilliseconds(2500);
chEvtBroadcast(&es1);
}
}
Re: The Event Question
Posted: Fri Jan 16, 2015 9:39 am
by Giovanni
Is the thread started somewhere? in the code I don't see it, nor I see the system initialization.
Giovanni
Re: The Event Question
Posted: Fri Jan 16, 2015 9:52 am
by Tabulous
Giovanni wrote:Is the thread started somewhere? in the code I don't see it, nor I see the system initialization.
Giovanni
The thread is running, i just did not show the starting of it.
Re: The Event Question
Posted: Fri Jan 16, 2015 10:12 am
by Giovanni
Probably found the problem:
chEvtRegister(&es1, &el1, EVENT_MASK(0));
it should be either:
chEvtRegister(&es1, &el1, 0);
or
chEvtRegisterMask(&es1, &el1, EVENT_MASK(0));
Giovanni
Re: The Event Question
Posted: Fri Jan 16, 2015 10:19 am
by Tabulous
Giovanni wrote:Probably found the problem:
chEvtRegister(&es1, &el1, EVENT_MASK(0));
it should be either:
chEvtRegister(&es1, &el1, 0);
or
chEvtRegisterMask(&es1, &el1, EVENT_MASK(0));
Giovanni
Ive just found it too lol
I have to say out of all the os services available, these events are tricky to understand........
Thanks for the help much appreciated

Re: The Event Question
Posted: Fri Jan 16, 2015 10:34 am
by steved
Nice to see I'm not the only one that has to think hard about events!
I've been using a slightly different arrangement, which works well if only one thread needs to know about the event (and if you don't have too many threads). It doesn't need a specific listener.
Code: Select all
void watchdogNotify(uint8_t threadNumber)
{
chEvtSignal(mainThread, EVENT_MASK(threadNumber));
}
Each monitored thread calls watchdogNotify() periodically; threadNumber is an arbitrary value (within limits) which uniquely identifies the thread.
mainThread is a pointer to the main thread! (or the thread which is triggering the watchdog).
Then use chEvtWaitAll(<your_sources_mask>); as previously mentioned.