Mario Charest postmaster@127.0.0.1 wrote:
MC > “John Zou” <yjzou@sina.com> wrote in message
MC > news:bnm5ro$gh0$1@inn.qnx.com…
Hi everyone,
I want to create a task that runs once every 2.5ms. Can anyone tell me by
what calling can I make this?
I’ve read the documentation on the timer_create() and timer_settime(), Is
these functions the best solution to my question? If so, please paste an
example, thanks!
MC > Paste and example, you want it easy don’t you, lol!
MC > Yeah setup timer to fire a pulse ever 2.5 (and don’t forget to set tick size
MC > to 500us via ClockPeriod() )
MC > Your program sit in the receive loop and will get waken up every 2.5 ms,
MC > there should be enough information/example in the online-help to get to
MC > where you want to be.
This example is cut from the helpviewer page for TimerCreate().
Syudy it and make the necessary changes to suit your needs.
/*
- Demonstrate how to set up a timer that, on expiry,
- sends us a pulse. This example sets the first
- expiry to 1.5 seconds and the repetition interval
- to 1.5 seconds.
*/
#include <stdio.h>
#include <time.h>
#include <sys/netmgr.h>
#include <sys/neutrino.h>
#define MY_PULSE_CODE _PULSE_CODE_MINAVAIL
typedef union {
struct _pulse pulse;
/* your other message structures would go
here too */
} my_message_t;
main()
{
struct sigevent event;
struct itimerspec itime;
timer_t timer_id;
int chid;
int rcvid;
my_message_t msg;
chid = ChannelCreate(0);
event.sigev_notify = SIGEV_PULSE;
event.sigev_coid = ConnectAttach(ND_LOCAL_NODE, 0,
chid,
_NTO_SIDE_CHANNEL, 0);
event.sigev_priority = getprio(0);
event.sigev_code = MY_PULSE_CODE;
timer_create(CLOCK_REALTIME, &event, &timer_id);
itime.it_value.tv_sec = 1;
/* 500 million nsecs = .5 secs /
itime.it_value.tv_nsec = 500000000;
itime.it_interval.tv_sec = 1;
/ 500 million nsecs = .5 secs */
itime.it_interval.tv_nsec = 500000000;
timer_settime(timer_id, 0, &itime, NULL);
/*
- As of the timer_settime(), we will receive our pulse
- in 1.5 seconds (the itime.it_value) and every 1.5
- seconds thereafter (the itime.it_interval)
*/
for (; {
rcvid = MsgReceive(chid, &msg, sizeof(msg), NULL);
if (rcvid == 0) { /* we got a pulse /
if (msg.pulse.code == MY_PULSE_CODE) {
printf(“we got a pulse from our timer\n”);
} / else other pulses … /
} / else other messages … */
}
}