wtorek, 1 czerwca 2010

First success with RTAI

Recently I've been trying to grok Real Time Application Interface (more on www.rtai.org). It certainly wasn't a walk in the park, but still far far easier than RTLinux. This time at least I had a clue what was going on. Following this tutorial worked for me: http://qrtailab.sourceforge.net/rtai_installation.html.

Finally when everything is set up you can do: sudo start_rtai on command line. This starts RTAI.

Now for some programming. Here's a snippet:

/* led1.c, an LXRT LED blinker */

#include <rtai_lxrt.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/io.h>

// delay in nanoseconds
#define TICKS 500000000 

main()
{
        RT_TASK *task;
        int priority = 0, i;
        int stack_size = 4096;
        int msg_size = 0; // use default

        // get enough privilege to 
        // access the I/O ports.

        iopl(3);

        task = rt_task_init(nam2num("main"), priority, stack_size, msg_size);
        if(task == 0)
        {
         printf("Task could not be initialized. Are you root?");
         exit(1);
        }
        rt_set_oneshot_mode();
        start_rt_timer(0);
        mlockall(MCL_CURRENT|MCL_FUTURE);
        rt_make_hard_real_time();

        for(i = 0; i < 10; i++) {
                outb(0xff, 0x378);
                rt_sleep(nano2count(TICKS));
                outb(0x0, 0x378);
                rt_sleep(nano2count(TICKS));
        }
        // back to non-rt land!
        rt_make_soft_real_time();
        stop_rt_timer();
        rt_task_delete(task);
        return 0;
}
It's a program that comes from this article with added troubleshooting comment.

To compile this program it's necessary to run:

CFLAGS=$(rtai-config --lxrt-cflags
LDFLAGS=$(rtai-config --lxrt-ldflags
gcc $(CFLAGS) $(LDFLAGS) main.c -o blinker
Where main.c is the name of the source file and blinker is the name of output executable.
To run it you should write sudo ./blinker. Sudo is needed because of the direct hardware access. You should see that the leds hooked up to lpt port are blinking.