How to use periodic event timer?

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
kiv
Posts: 10
Joined: Fri Aug 07, 2015 5:14 pm

How to use periodic event timer?

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

Re: How to use periodic event timer?

Post 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
kiv
Posts: 10
Joined: Fri Aug 07, 2015 5:14 pm

Re: How to use periodic event timer?

Post by kiv »

Yes, it was the problem. I corrected the error and it worked. Thank you very much.
Post Reply