Page 1 of 1

Ask about Event on ver 2.4.x

Posted: Fri Apr 05, 2013 9:34 am
by namhieu
Hi all,
Chibi is the first OS I use so that I have many misunderstanding when using it. Now I try to use Event and BroadcastFlag, however I see it doesn't work as I wish, or I have something misunderstand. I have 2 threads need to be call by 1 interrupt, like that:
It's main thread:

Code: Select all

EventSource evtS1;
void main(void)
{
   EventListener evtL1;

   chEvtInit(&evtS1);
   chEvtRegister(&evtS1, &evtL1, 1);
   chThdCreateStatic(waMeasurementThread, sizeof(waMeasurementThread), NORMALPRIO, MeasureProcess, NULL);
   while (TRUE)
   {
      chEvtWaitOne(EVENT_MASK(1));
      CP_SENSOR();
   }
}


It's MeasurementThread

Code: Select all

extern EventSource evtS1;

msg_t MeasureProcess(void *p)
{
       EventListener evtL2;
       chEvtRegister(&evtS1, &evtL2, 2);
       while(1)
       {
           chEvtWaitOne(EVENT_MASK(2));
           MeasureTemperature();
       }
}

And in RTC interrupt, base on Alarm or Wakeup interrupt I will broadcast with EventMask(1) or EventMask(2)

Code: Select all

CH_IRQ_HANDLER(4) 
{
CH_IRQ_PROLOGUE();
   if(RTC_GetITStatus(RTC_IT_WUT)==SET)
   {
      RTC_ClearITPendingBit(RTC_IT_WUT);
      chEvtBroadcastFlagsI(&evtS1, EVENT_MASK(2));
   }

   if(RTC_GetITStatus(RTC_IT_ALRA)==SET)   
   {
      RTC_ClearITPendingBit(RTC_IT_ALRA);
      chEvtBroadcastFlagsI(&evtS1, EVENT_MASK(1));
   }
   
CH_IRQ_EPILOGUE();
}



Absolutely that I make Alarm and Wakeup interrupt never occur at the same time. And I see that when chEvtBroadcastFlagsI() function is called, both threads (with different ListenEvent and Mask) will wake up, and this is not what I expect.
Follow what I understand, Broadcast will call exactly a thread that has specific SourceEvent, ListenEvent and EventMask. Is that right? Can you explain for me?
Thanks!

Re: Ask about Event on ver 2.4.x

Posted: Fri Apr 05, 2013 10:28 am
by Giovanni
Hi,

That code wakes up both threads, it is correct. The event flags passes to chEvtBroadcast() are just ORed to the flags specified when the listener is initialized and do not define two distinct events.

One event source is ONE event, you need to use two distinct event sources if you need two... event sources. From the ISR call chEvtBroadcast(..., 0) and specify the flags in the listener (you can use 1 for both since they are independent).

Note that the behavior of events changed in 2.5.x to make it more rational but you still need one event source object for each event you want to monitor.

Giovanni

Re: Ask about Event on ver 2.4.x

Posted: Mon Apr 08, 2013 4:13 am
by namhieu
Hi Giovanni,
Thanks for your explanation, I also thought that I had misunderstood EventSource and chEvtBroadcast(). I have changed into using chEvtSignalFlagsI() function to wakeup threads I want.
Thanks!

Re: Ask about Event on ver 2.4.x

Posted: Mon Apr 08, 2013 8:38 am
by Giovanni
Hi,

It is OK, if you "know" which is the thread you want to wakeup then chEvtSignalFlagsI() is the most efficient approach.

Giovanni