RTAI/fusion Timer Task Example
Also see Porting software from RTAI/fusion to Xenomai
This is a simple RTAI/fusion hard real time user-space timer example.
Compile the program "timer.c" with "make" (Makefile below).
Run "timer" (press CTRL-C to end it) - you'll see an output like this:
# ./timer
start
message from testtask: count=1
message from testtask: count=2
message from testtask: count=3
message from testtask: count=4
message from testtask: count=5
cleanup
end
If you get a compilation error like this
timer.c: In function `main':
*timer.c:45: warning: implicit declaration of function `rt_task_spawn'
/tmp/ccIQ6xk6.o(.text+0x129): In function `main':
: undefined reference to `rt_task_spawn'
collect2: ld returned 1 exit status*
make: *** [timer] Error 1
you need to upgrade to a recent version of RTAI/fusion (0.8.2 as of writing this).
rt_task_spawn was added in version 0.7.3.
rt_task_spawn() is actually a combination of rt_task_create() and rt_task_start().
timer.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/io.h>
#include <sys/mman.h>
#include <rtai/task.h>
#include <rtai/queue.h>
#include <rtai/intr.h>
#define STACK_SIZE 8192
#define STD_PRIO 1
RT_TASK test_task_ptr;
int int_count = 0;
int end = 0;
void testtask(void *cookie){
int count = 0;
while(!end){
rt_task_sleep(1000); // sleep 1 sec (1000 ticks, 1ms each)
count++;
printf("message from testtask: count=%d\n", count);
fflush(NULL);
}
}
// signal-handler, to ensure clean exit on Ctrl-C
void clean_exit(int dummy) {
printf("cleanup\n");
end = 1;
rt_task_delete(&test_task_ptr);
printf("end\n");
}
int main(int argc, char *argv[]) {
int err;
printf("start\n");
// install signal handler
signal(SIGTERM, clean_exit);
signal(SIGINT, clean_exit);
// start timer
rt_timer_start(1000000); // 1ms ticks
err = rt_task_spawn(&test_task_ptr, "Timer", STACK_SIZE, STD_PRIO, 0, &testtask, NULL);
if (err) {
printf("error rt_task_spawn\n");
return 0;
}
// wait for signal & return of signal handler
pause();
fflush(NULL);
return 0;
}
loadmods.sh
#!/bin/sh
TMP=$PWD
cd /usr/realtime/modules/
insmod ./rtai_hal.ko
insmod ./rtai_nucleus.ko
insmod ./rtai_native.ko
cd $TMP
Makefile
prefix := $(shell rtai-config --prefix)
ifeq ($(prefix),)
$(error Please add <rtai-install>/bin to your PATH variable)
endif
CC = $(shell rtai-config --cc)
LXRT_CFLAGS = $(shell rtai-config --fusion-cflags)
LXRT_LDFLAGS = $(shell rtai-config --fusion-ldflags)
all: timer
timer: timer.c
$(CC) $(LXRT_CFLAGS) $(LXRT_LDFLAGS) -lrtai -o $@ $<
clean:
rm -f *.o timer
.PHONY: clean
Last-Modified: Sat, 04 Feb 2006 16:06:01 GMT