Page 1 of 1

How to use periodic event timer?

Posted: Fri Aug 07, 2015 5:21 pm
by kiv
I need to interrogate a set of sensors with a certain frequency. I want to run this separate thread that will be waiting for the timer and do polling. Here is a fragment of my code:

Code: Select all

static event_timer_t sensorsPollTimer;
#define POLL_TIMER_EVENT_MASK EVENT_MASK(1)

static THD_WORKING_AREA(waSensorsReaderThread, 128);
static THD_FUNCTION(sensorsReaderThread, arg) {
   chRegSetThreadName("Sensors reader");
   event_listener_t eventListener;
   evtObjectInit(&sensorsPollTimer, CH_CFG_ST_FREQUENCY / SENSORS_POLL_FREQUENCY);
   chEvtRegisterMask((event_source_t*)&sensorsPollTimer, &eventListener, POLL_TIMER_EVENT_MASK);
   evtStart(&sensorsPollTimer);
   while (TRUE) {
      chEvtWaitAny(POLL_TIMER_EVENT_MASK);
      ... do polling sensors ...
   }
}

This code freezes in the waiting for the event from the timer. If I replace the delay in waiting for an event using chThdSleepMilliseconds, the code works.
I do not quite understand how to use the periodic event timer. What am i doing wrong?

Re: How to use periodic event timer?

Posted: Fri Aug 07, 2015 6:18 pm
by Giovanni
Hi,

It looks right except the event source is &sensorsPollTimer.et_es.

That cast ((event_source_t*)&sensorsPollTimer) should tell you something is not exactly right.

Giovanni

Re: How to use periodic event timer?

Posted: Fri Aug 07, 2015 6:53 pm
by kiv
Yes, it was the problem. I corrected the error and it worked. Thank you very much.