EventHandlers not responding

ChibiOS public support forum for topics related to the Atmel AVR family of micro-controllers.

Moderator: tfAteba

Post Reply
Thiapalm
Posts: 8
Joined: Mon Sep 08, 2014 3:48 pm

EventHandlers not responding

Post 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
}
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: EventHandlers not responding

Post 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?
Thiapalm
Posts: 8
Joined: Mon Sep 08, 2014 3:48 pm

Re: EventHandlers not responding

Post 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?
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: EventHandlers not responding

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

Re: EventHandlers not responding

Post 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
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: EventHandlers not responding

Post by utzig »

BTW, in you code I don't see the call to chSysInit()


Yes, the library calls it already.
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: EventHandlers not responding

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