The Event Question

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

The Event Question

Post 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));
    }
}

User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: The Event Question

Post 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
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: The Event Question

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: The Event Question

Post 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
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: The Event Question

Post 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);
    }
}
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: The Event Question

Post by Giovanni »

Is the thread started somewhere? in the code I don't see it, nor I see the system initialization.

Giovanni
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: The Event Question

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: The Event Question

Post 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
Tabulous
Posts: 518
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 18 times

Re: The Event Question

Post 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 :-)
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: The Event Question

Post 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.
Post Reply