Page 1 of 1

Event flag use

PostPosted: Mon Sep 07, 2015 3:10 am
by mattwhillock

After reading the manual and posts on this forum I am a little unsure whether the way I want to use events is feasible or not. For testing purposes I want a cyclic timer to generate an event every second lets say. I have a task waiting for this event. When it gets the event it goes off, does some processing and then waits for the event again. The way I see it the task would clear the event before going back to wait for the event from the timer; would this work as expected? In the case of multiple events being signalled then the receiving task would have to determine which event has been generated by looking at the event bits in turn and then clearing each event after dealing with it?
The example in the manual (page 258) has each task clearing the event it generated after waiting for a "return" event from the main task (as far as I can tell). (Aside: the OSCreateEFlag call on page 259 has only 2 parameters - this is incorrect, it should be 3?).


Re: Event flag use

PostPosted: Wed Sep 09, 2015 7:04 am
by aek
Event flags are a little different from other Salvo events, in that the piece(s) of information that cause(s) a task to successfully wait the event (i.e., proceed after an OS_WaitEFlag() call) are not automatically cleared by passing through OS_WaitEFlag() ... the user has to "manually clear" those flags before the next call to OS_WaitEFlag().

That said, what you are describing is possible and will work ...

Event flags essentially give you the ability to wait on multiple events (the bits in an EFlag) in a single place / with a single call to OS_WaitEFlag() ... in Salvo's other events (binSems, Sems, Msgs and MsgQs) it's only possible to wait on a single event at a time. With EFlags, you can wait on an exact match, any any match, or an all match, on as many bits as you require. The tradeoff is that -- in order to maintain Salvo's true event-driven performance -- because of these pattern matches, it's up to the user to "undo the settedness of the bit(s) that signaled that an event had happened". Other event types in Salvo can automatically reset the event because there's only one event involved (i.e., there's no ambiguity).

Note that if all you need is a periodic task servicing a (clearly-defined single) event every second, it's much simpler to just use OS_Delay(ONE_SEC) and be done with it ....

Re: Event flag use

PostPosted: Thu Sep 10, 2015 1:31 am
by mattwhillock
Hi aek,

Thanks for clearing that up. Looks like I can do want I want to do.

I guess that a task/timer can set an event multiple times before the waiting task processes the event such that it appears only one event has happened to the waiting task, i.e., an event can be missed, but that probably means I've got my design wrong.

So if multiple (different) events are signalled I do a OSReadEFlag after waiting with the OSANY_BITS mask set with all my event types or'ed together the second parameter to OS_WaitEFlag to find out which event happened then clear only that event with OSClrEFlag, like so:

#define evt1 0x01 // sent by isr
#define evt2 0x02 // sent by timer
#define evt3 0x04 // sent by task
#define evts (evt1 | evt2 | evt3)
OS_WaitEFlag(eventq, evts, OSANY_BITS, OSNO_TIMEOUT);
event = OSReadEFlag(eventq);
if(event & evt1) { OSClrEFlag(eventq, evt1); ... }

Sounds good. My example program with a timer sending events is only to test the event handling - in real life the different event sources will be at indeterminate times from an interrupt handler, a cyclic timer and a another task, all clamouring for attention.

Just one thing has occurred to me - I declare the event queue like this:

OSgltypeMsgQP eventq[SIZEOF_EVENTQ];

with SIZEOF_EVENTQ set to 3. Does this mean I have an event queue with room for 3 events? If the same event is set 3 times before the waiting task wakes up will it see 3 events waiting in the queue?

Thanks for your help.