Page 1 of 1

Events fail with exception

Posted: Fri Sep 22, 2017 9:32 am
by elagil
Hello!

I am trying to use events. I have a thread that checks buttons and an event should be broadcast in case of a button press to multiple other threads that implement listeners.

The listening threads are like this:

Code: Select all

THD_WORKING_AREA(wa_asic_thread, ASIC_THREAD_STACK_SIZE);
THD_FUNCTION(Thread_asic, p) {
  (void)p;

  event_listener_t switch_listener;

  /*
   * Registering on the switch thread as event 0
   */
  chEvtRegister(&switch_event_source, &switch_listener, 0);
 
    while (true) {
    /*
     * Switch position changed? Reconfigure if detected.
     * Wait for switch event to start.
     */
     
    eventmask_t evt = chEvtWaitAny(ALL_EVENTS);

    if (evt & EVENT_MASK(0)) {
      /* Event from the switches, do something */
        chThdSleepMilliseconds(1000);
    }
 


The switch checker is like this:

Code: Select all

event_source_t switch_event_source;

THD_WORKING_AREA(wa_Switch, 128);
THD_FUNCTION(Thread_Switch, arg) {
 
    while (true) {
    /*
     * Do detection stuff... Switch position changed?
     */
     if (new_switch_settings) chEvtBroadcast(&switch_event_source);

     chThdSleepMilliseconds(100);
    }
}


The functions are inside individual .c files and the "switch_event_source" variable is defined as external in a common header.

Code: Select all

extern event_source_t switch_event_source;


I always jump into _unhandled_exception(void) when calling the chEvtBroadcast(). Do you have any idea why?

Thanks in advance,
Adrian

Re: Events fail with exception

Posted: Fri Sep 22, 2017 9:39 am
by Giovanni
Hi,

Is the event source object initialized?

Giovanni

Re: Events fail with exception

Posted: Fri Sep 22, 2017 10:05 am
by elagil
No, I forgot this! Now the exception is gone. However, the event listener does never trigger. I am sure that the event broadcast is called, however.

Re: Events fail with exception

Posted: Fri Sep 22, 2017 10:24 am
by elagil
Sorry, it works. I should have initialized the event source first and then register it in the listener.

Thank you for your help!

So, fixing step:
Add initialization of the event source before registering a listener or broadcasting an event!