RTAI maximum task frequency test - RTAI Timer Task Example
This is a quick test for determination of the max. frequency of a RTAI real time task.
Compile "freqtest.c" with the "Makefile". Load the RTAI modules with "loadmods.sh" and execute
"freqtest". This program produces a square wave on _all_ parallel port pins (D0-7 - pin2-9).
Adjust PERIOD for other frequencies. Be careful: if the period is too low, there will
be a square wave on the parallel port, but the linux domain won't get any time to execute
it's tasks, hence the system locks up. Tested 19/NOV/2005 on magma (which will be RTAI 3.3)
and kernel 2.6.14.
freqtest.c
/* RTAI LXRT Frequency Test - www.captain.at
This example is for RTAI3.2 and magma (which will be 3.3)
Produces a square wave on the parallel port
*/
#include <stdio.h>
#include <errno.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <rtai_lxrt.h>
#include <rtai_sem.h>
#include <rtai_usi.h>
#include <sys/io.h>
static volatile int end = 0;
static volatile int endsquare = 1;
#define BASEPORT 0x378
// s m μ n
#define PERIOD 100000
static void *square_handler(void *args) {
RT_TASK *handler;
RTIME period;
if (!(handler = rt_task_init_schmod(nam2num("SQHDLR"), 0, 0, 0, SCHED_FIFO, 0xF))) {
printf("CANNOT INIT HANDLER TASK > SQHDLR <\n");
exit(1);
}
rt_allow_nonroot_hrt();
mlockall(MCL_CURRENT | MCL_FUTURE);
rt_set_oneshot_mode();
start_rt_timer(0);
period = nano2count(PERIOD);
rt_make_hard_real_time();
endsquare = 0;
rt_task_make_periodic(handler, rt_get_time() + period, period);
while ( !endsquare ) {
outb_p(0, BASEPORT);
rt_task_wait_period();
outb_p(255, BASEPORT);
rt_task_wait_period();
}
stop_rt_timer();
rt_make_soft_real_time();
rt_task_delete(handler);
return 0;
}
// signal-handler, to ensure clean exit on Ctrl-C
void clean_exit(int dummy) { end = 1; }
int main(void) {
RT_TASK *maint; //, *squaretask;
int squarethread;
// install signal handler
signal(SIGTERM, clean_exit);
signal(SIGINT, clean_exit);
if (!(maint = rt_task_init(nam2num("MAIN"), 1, 0, 0))) {
printf("CANNOT INIT MAIN TASK > MAIN <\n");
exit(1);
}
// ask for permission to access the parallel port from user-space
if (iopl(3)) {
printf("iopl err\n");
rt_task_delete(maint);
exit(1);
}
squarethread = rt_thread_create(square_handler, NULL, 10000); // create thread
while (endsquare) { // wait until thread went to hard real time
usleep(100000);
}
while (!end) { usleep(100000); }
endsquare = 1;
printf("TEST ENDS\n");
rt_thread_join(squarethread);
rt_task_delete(maint);
return 0;
}
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 --lxrt-cflags)
LXRT_LDFLAGS = $(shell rtai-config --lxrt-ldflags)
all: freqtest
freqtest: freqtest.c
$(CC) $(LXRT_CFLAGS) -o $@ $< $(LXRT_LDFLAGS)
clean:
rm -f *.o freqtest
.PHONY: clean
loadmods.sh
#!/bin/sh
TMP=$PWD
cd /usr/realtime/modules/
insmod ./rtai_hal.ko
insmod ./rtai_lxrt.ko
insmod ./rtai_sem.ko
insmod ./rtai_tasklets.ko
insmod ./rtai_usi.ko
#insmod ./rtai_hal.o
#insmod ./rtai_lxrt.o
#insmod ./rtai_sem.o
#insmod ./rtai_tasklets.o
#insmod ./rtai_usi.o
cd $TMP
Last-Modified: Sat, 04 Feb 2006 15:43:18 GMT