RTAI fifo & shared memory examples from "RTAI Beginner's Guide":
http://www.aero.polimi.it/~rtai/documentation/articles/guide.html
Makefile for kernel 2.6; Makefile24 for kernel 2.4 (compile with # make -f Makefile24 )
Tested with kernel 2.6.7 and vesuvio (hal6c1-2.6.7.patch) on Fedora Core 2 and
kernel 2.4.25 and rtai-3.0r4 on debian sarge (testing release)
NOTE: I've tested these examples on RTAI 3.3 and kernel 2.6.15 with the ADEOS-IPIPE patch
2.6.15 v1.2 and they work. There are some warnings during compilation, but they work.
For other examples see the RTAI SHOWROOM accessible via CVS.
Download the examples (~2k)
Unpack with:
# mkdir rtai
# cd rtai
# tar xvzf rtai.tar.gz
Compile and run example 1:
# cd sine
# make
# ./run
Compile and run example 2:
# cd sine2
# make
# ./run
If you get some warning during compilation, don't worry. The unresolved symbols will
resolve as soon as the RTAI kernel modules are loaded.
# make
make -C /lib/modules/2.6.10/build SUBDIRS=/usr/src/rtai modules
make[1]: Entering directory `/usr/src/linux-2.6.10'
Building modules, stage 2.
MODPOST
*** Warning: "rt_task_delete" [/usr/src/rtai/myrt_process.ko] undefined!
*** Warning: "rtf_destroy" [/usr/src/rtai/myrt_process.ko] undefined!
*** Warning: "stop_rt_timer" [/usr/src/rtai/myrt_process.ko] undefined!
*** Warning: "rt_task_make_periodic" [/usr/src/rtai/myrt_process.ko] undefined!
*** Warning: "rt_get_time" [/usr/src/rtai/myrt_process.ko] undefined!
*** Warning: "start_rt_timer" [/usr/src/rtai/myrt_process.ko] undefined!
*** Warning: "nano2count" [/usr/src/rtai/myrt_process.ko] undefined!
*** Warning: "rtf_create" [/usr/src/rtai/myrt_process.ko] undefined!
*** Warning: "rt_task_init" [/usr/src/rtai/myrt_process.ko] undefined!
*** Warning: "rt_set_periodic_mode" [/usr/src/rtai/myrt_process.ko] undefined!
*** Warning: "rt_task_wait_period" [/usr/src/rtai/myrt_process.ko] undefined!
*** Warning: "rtf_put" [/usr/src/rtai/myrt_process.ko] undefined!
*** Warning: "rt_get_cpu_time_ns" [/usr/src/rtai/myrt_process.ko] undefined!
make[1]: Leaving directory `/usr/src/linux-2.6.10'
gcc -o scope scope.c
EXAMPLE 1 - FIFO
The kernel module myrt_process.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <asm/io.h>
#include <math.h>
#include <rtai.h>
#include <rtai_sched.h>
#include <rtai_fifos.h>
#define TICK_PERIOD 1000000
#define TASK_PRIORITY 1
#define STACK_SIZE 10000
#define FIFO 0
static RT_TASK rt_task;
static void fun(int t)
{
int counter = 0;
float sin_value;
while (1) {
sin_value = sin(2*M_PI*1*rt_get_cpu_time_ns()/1E9);
//sin_value = rt_get_cpu_time_ns()/1E9;
rtf_put(FIFO, &counter, sizeof(counter));
rtf_put(FIFO, &sin_value, sizeof(sin_value));
counter++;
rt_task_wait_period();
}
}
int init_module(void)
{
RTIME tick_period;
rt_set_periodic_mode();
rt_task_init(&rt_task, fun, 1, STACK_SIZE, TASK_PRIORITY, 1, 0);
rtf_create(FIFO, 8000);
tick_period = start_rt_timer(nano2count(TICK_PERIOD));
rt_task_make_periodic(&rt_task, rt_get_time() + tick_period, tick_period);
return 0;
}
void cleanup_module(void)
{
stop_rt_timer();
rtf_destroy(FIFO);
rt_task_delete(&rt_task);
return;
}
MODULE_LICENSE("GPL");
The user-space program:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
static int end;
static void endme(int dummy)
{
end=1;
}
int main(int argc, char * argv[])
{
int fifo, counter;
float sin_value;
if ((fifo = open("/dev/rtf0", O_RDONLY)) < 0) {
fprintf(stderr, "Error opening /dev/rtf0\n");
exit(1);
}
signal(SIGINT, endme);
while (!end) {
read(fifo, &counter, sizeof(counter));
read(fifo, &sin_value, sizeof(sin_value));
printf(" Counter : %d Seno : %f \n", counter, sin_value);
}
return 0;
}
The Makefile for kernel 2.6:
obj-m := myrt_process.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
EXTRA_CFLAGS := -I/usr/realtime/include -I/usr/include/ -ffast-math -mhard-float
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
gcc -o scope scope.c
The Makefile for kernel 2.4:
TARGET := myrt_process
INCLUDE := -I/lib/modules/`uname -r`/build/include -I/usr/realtime/include
CFLAGS := -O2 -Wall -DMODULE -D__KERNEL__ -DLINUX -ffast-math
CC := gcc
${TARGET}.o: ${TARGET}.c
$(CC) $(CFLAGS) ${INCLUDE} -c ${TARGET}.c
gcc -o scope scope.c
The "run" script (as in the testsuite):
/usr/realtime/bin/rtai-load
The .runinfo file for rtai-load:
latency:ksched+fifos:push myrt_process;./scope;popall:control_c
EXAMPLE 2 - Shared Memory
The kernel module myrt_process.c
#include <linux/module.h>
#include <asm/io.h>
#include <math.h>
#include <rtai.h>
#include <rtai_shm.h>
#include <rtai_sched.h>
#include <rtai_nam2num.h>
#include "parameters.h"
static RT_TASK rt_task;
static struct data_str *data;
static void fun(int t)
{
unsigned int count = 0;
float seno,coseno;
while (1) {
data->indx_counter = count;
seno = sin(2*M_PI*1*rt_get_cpu_time_ns()/1E9);
coseno = cos(2*M_PI*1*rt_get_cpu_time_ns()/1E9);
data->sin_value = seno;
data->cos_value = coseno;
count++;
rt_task_wait_period();
}
}
int init_module(void)
{
RTIME tick_period;
rt_set_periodic_mode();
rt_task_init(&rt_task, fun, 1, STACK_SIZE, TASK_PRIORITY, 1, 0);
data = rtai_kmalloc(nam2num(SHMNAM), sizeof(struct data_str));
tick_period = start_rt_timer(nano2count(TICK_PERIOD));
rt_task_make_periodic(&rt_task, rt_get_time() + tick_period, tick_period);
return 0;
}
void cleanup_module(void)
{
stop_rt_timer();
rt_task_delete(&rt_task);
rtai_kfree(nam2num(SHMNAM));
return;
}
MODULE_LICENSE("GPL");
The user-space program:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <rtai_shm.h>
#include "parameters.h"
static int end;
static void endme(int dummy) { end=1; }
int main (void)
{
struct data_str *data;
signal(SIGINT, endme);
data = rtai_malloc (nam2num(SHMNAM),1);
while (!end) {
printf("Counter: %d Sine: %f Cosine: %f \n", data->indx_counter,
data->sin_value, data->cos_value);
}
rtai_free (nam2num(SHMNAM), &data);
return 0;
}
The Makefile for kernel 2.6:
obj-m := myrt_process.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
EXTRA_CFLAGS := -I/usr/realtime/include -I/usr/include/ -D__IN_RTAI__ -ffast-math -mhard-float
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
gcc -I/usr/realtime/include -I/usr/include/ -o scope scope.c
The Makefile for kernel 2.4:
TARGET := myrt_process
INCLUDE := -I/lib/modules/`uname -r`/build/include -I/usr/realtime/include
CFLAGS := -O2 -Wall -DMODULE -D__KERNEL__ -DLINUX -ffast-math
CC := gcc
${TARGET}.o: ${TARGET}.c
$(CC) $(CFLAGS) ${INCLUDE} -c ${TARGET}.c
gcc -I/usr/realtime/include -o scope scope.c
The include file parameters.h
#define TICK_PERIOD 1000000
#define TASK_PRIORITY 1
#define STACK_SIZE 10000
#define SHMNAM "MIRSHM"
struct data_str
{
int indx_counter;
float sin_value;
float cos_value;
};
The "run" script (as in the testsuite):
/usr/realtime/bin/rtai-load
The .runinfo file for rtai-load:
sine2:ksched+shm+fifos:push myrt_process;./scope;popall:control_c
Last-Modified: Fri, 03 Mar 2006 17:24:51 GMT