Page 1 of 1

EventHandlers not responding

Posted: Thu Sep 11, 2014 1:55 am
by Thiapalm
Hello,

I am trying to use an Event to activate a thread. The event is fired after some timeout from Thread1 and Thread2 must receive it and act accordingly.

I have implemented both threads as below, however Thread2 is not being activated. Could someone helpe and point me on the code below where have I wrongly implemented?

Also, is there any way to pass information to Thread2 inside the event? I'm using a Mutex for that purpose...

Thanks

Code: Select all

#include.....

#define OPERATE_LED 1
//------------------------------------------------------------------------------
// Shared data, use volatile to insure correct access.

// Mutex for atomic access to data.
MUTEX_DECL(dataMutex);

EventSource controllerEvt;
EventListener ledListener;

//------------------------------------------------------------------------------

// 64 byte stack beyond task switch and interrupt needs.
static WORKING_AREA(waThread2, 64);

static msg_t LedControl(void *arg) {

    eventmask_t event;
    chEvtRegister(&controllerEvt, &ledListener, OPERATE_LED);

  while (1) {

     event = chEvtWaitOne(OPERATE_LED);

     if (event ==OPERATE_LED){

        chMtxLock(&dataMutex);  // Lock access to data.
                  code();
        chMtxUnlock();  // Unlock data access.

   #ifdef DEBUG
         nss.println(F("Received LED evt1"));
   #endif


          chThdYield();
     }
  }
}

//------------------------------------------------------------------------------

// 64 byte stack beyond task switch and interrupt needs.
static WORKING_AREA(waThread1, 64);

static msg_t Controller(void *arg) {

   systime_t Time = chTimeNow();
   chEvtInit(&controllerEvt);

  while (1) {

     if (chTimeNow() - Time > MS2ST(2000)) {

       Time = chTimeNow();
       chMtxLock(&dataMutex);  // Lock access to data.
                 code();
       chMtxUnlock();  // Unlock data access.

      chEvtBroadcast(&controllerEvt);
   #ifdef DEBUG
      nss.println(F("Broadcast Sent"));
   #endif
                chThdYield();
     }
  return 0;
}
//------------------------------------------------------------------------------
// main thread runs at NORMALPRIO
void mainThread() {


     // start LED thread
     chThdCreateStatic(waThread2, sizeof(waThread4), NORMALPRIO+1, LedControl, NULL);

     // start CONTROLLER thread
     chThdCreateStatic(waThread1, sizeof(waThread2), NORMALPRIO+1, Controller, NULL);


while(1){

}

}
//------------------------------------------------------------------------------
void setup() {

#ifdef DEBUG
  nss.begin(57600); //DEBUG ONLY
  delay(2000);
  nss.println(F("=SETUP="));
#endif

  chBegin(mainThread);
  // chBegin never returns, main thread continues with mainThread()
  while(1) {}
}

//------------------------------------------------------------------------------
void loop() {
 // not used
}

Re: EventHandlers not responding

Posted: Fri Sep 12, 2014 2:24 pm
by utzig
You are creating the LedControl thread before the Controller thread and initializing the EventSource in the Controller thread. Any reason for not initializing/registering the EventSource/Listener in the mainThread() function before creating the other threads?

Re: EventHandlers not responding

Posted: Fri Sep 12, 2014 5:56 pm
by Thiapalm
No reason at all, I simply did not know where to start them.

So If I put the

Code: Select all

chEvtInit(&controllerEvt);
and

Code: Select all

chEvtRegister(&controllerEvt, &ledListener, OPERATE_LED);
on the mainthread, that code will work?

Re: EventHandlers not responding

Posted: Fri Sep 12, 2014 7:08 pm
by utzig
Thiapalm,

This section of the forum is for AVR development and by that what is meant is low-level work on AVR port of ChibiOS not support for Arduino port. Although the ChibiOS Arduino technically runs on an AVR, it's not the same thing. That means I can't test your code. The most I can do is to look at it and try to find something which is not right. But even most important, the problem with this code is not even AVR related at all. It is a ChibiOS general development problem. The AVR section is probably only read by a handful of people so you would have much better luck posting to the General Development section which is read by a LOT of people.

Below I'm pasting some code which has I would suggest as a better and simpler approach to solve your problem (based on what I believe you want to do). I copied only relevant code so fill in missing parts (and error checking)!

Code: Select all

static Thread *g_led_thd = NULL;

static WORKING_AREA(waThread2, 64);
static msg_t LedControl(void *arg) {

  while (1) {
    eventmask_t event = chEvtWaitOne(OPERATE_LED);
    if (event == OPERATE_LED) {
      chMtxLock(&dataMutex);
      code();
      chMtxUnlock();
    }
  }
}

static WORKING_AREA(waThread1, 64);
static msg_t Controller(void *arg) {

  while (1) {
    chMtxLock(&dataMutex);
    code();
    chMtxUnlock();
    chEvtSignal(g_led_thd, OPERATE_LED);
    chThdSleepMilliseconds(2000);
  }
  return 0;
}

void mainThread() {
  g_led_thd = chThdCreateStatic(waThread2, sizeof(waThread4), NORMALPRIO+1, LedControl, NULL);
  chThdCreateStatic(waThread1, sizeof(waThread2), NORMALPRIO+1, Controller, NULL);
}


Regards,
Fabio Utzig

Re: EventHandlers not responding

Posted: Fri Sep 12, 2014 7:41 pm
by Giovanni
Events cannot pass information, you could use:
1) Synchronous messages (very efficient), there is a message and then a response in the other direction but works only between threads.
2) Mailboxes, circular queues of messages, messages can be posted also from IRQs.

BTW, in you code I don't see the call to chSysInit(), is that implicit in that library? also, I don't see the mutex initialization.

Giovanni

Re: EventHandlers not responding

Posted: Fri Sep 12, 2014 7:44 pm
by utzig
BTW, in you code I don't see the call to chSysInit()


Yes, the library calls it already.

Re: EventHandlers not responding

Posted: Fri Sep 12, 2014 7:49 pm
by utzig
BTW, in you code I don't see the call to chSysInit(), is that implicit in that library?


Yes.

also, I don't see the mutex initialization.


Yes, it seems to be missing!

Code: Select all

chMtxInit(&dataMutex);


should be added to mainThread() before creating the other threads.

Thanks Giovanni