Real Time Application Interface for Linux

25
> < B 1 THE REAL TIME APPLICATION INTERFACE FOR LINUX S. Hussein & A. Taleb | RTAI For Linux Lebanese University Faculty of Engineering Branch 1 Submitted to dr. Wassim EL FALOU – TC02 January 9 th , 2016

Transcript of Real Time Application Interface for Linux

Page 1: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS

B�LOGO

1

THE REAL TIME APPLICATION INTERFACE FOR LINUX

S. Hussein & A. Taleb | RTAI For Linux

Lebanese University Faculty of Engineering Branch 1

Submitted to dr. Wassim EL FALOU – TC02 January 9th, 2016

Page 2: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS

01�02�03�

04�05�06�

2

Outline

Introduction!.

S. Hussein & A. Taleb | RTAI For Linux

System Description!

System Functions!

.

Integrated Development Environment!

Processor Target!

Example!

Page 3: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS

> <

01�Perspiciatis unde.!

3

Context■  Linux is a time-sharing OS providing good

performance and highly sophisticated services

■  Linux suffers from a lack of real time support

■  To obtain a timing correctness behavior, some changes in the kernel sources are must

■  These changes ensures real time platform, with low latency and high predictability requirements, within full non real time Linux environment

■  Need for an RTAI system extension

Introduction

Page 4: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS

> <

02�Perspiciatis unde.!

4

System Description

Definition■  RTAI stands for Real Time Application Interface

■  It is NOT a real time operating system

■  It is based on the Linux kernel, providing the ability to make it fully pre-emptable

■  RTAI offers the same services of the Linux kernel core & adds the features of an industrial real time OS

■  RTAI uses the concept of HAL (hardware abstraction layer) to get information from Linux and to trap some fundamental functions

■  This HAL provides few dependencies to Linux Kernel

■  RTAI considers Linux as a background task running when no real time activity occurs

Page 5: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS

01� 02� 03� 04�

5

�Task Definition & TypesIn real time systems, time-critical & non time-critical activities coexist and both are called tasks. Tasks with timeliness requirement are real time tasks and have the following types of requirements and/or constraints :

Timing Constraint!

A p e r i o d i c t a s k w i t h deadline by which it finish or start, or with constraint on start and finish times.

Periodic task has to be repeated once per period.

Resource Requirements!

A real time task may require access to certain resources such as I/O devices, data structures, files and databases.

Communication Requirement!

T a s k s s h o u l d b e a l l o w e d t o communicate wi th messages.

Concurrency Constraint!

T a s k s s h o u l d b e allowed concurrent access to common resources providing the consistency of the r e s o u r c e i s n o t violated.

S. Hussein & A. Taleb | RTAI For Linux

Page 6: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS 6

◼  Correct timing and interrupt management represent the really challenge of a real time system, and hence of RTAI

◼  RTAI consists basically of an  interrupt dispatcher: RTAI mainly traps the peripherals interrupts and if necessary re-routes them to Linux

◼  RTAI provides both a periodic and a one shot timer, using the counter0 to load the initial count

◼  In the one shot mode the clock is reprogrammed every interrupt, while in the periodic one it is programmed only at beginning and then generates interrupts periodically

Timers & Interrupts

S. Hussein & A. Taleb | RTAI For Linux

Page 7: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS

> <

03�Perspiciatis unde.!

7

System Functions

RTAI Modules■  RTAI is module oriented

■  The use of dynamic kernel modules allows writing portions of the kernel as separate objects that can be loaded and unloaded on a running system

■  A kernel module is simply an object file containing routines and/or data to load into a running kernel

■  The module code resides in the kernel's address space and executes in the context of the kernel

■  A module can be any set of routines, with the one restriction that two functions, init_module() and cleanup_module(), must be provided

■  The first is executed once the module is loaded, and the other, before the module is unloaded from kernel 

Page 8: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS 8

■  To use RTAI, you have to load the modules that implement whatever RTAI capabilities you need. According to 1.3 release, available are the following core modules:

¨  rtai – The really core module

¨  rtai_sched – The real time scheduling module

¨  rtai_fifos – Implements FIFOs services

¨  rtai_shm – Allows sharing of memory among different real time tasks and Linux processes

¨  Lxrt – Allows a symmetric implementation of real time services

¨  rtai_pqueue – Provides hard real time threads

¨  rtai_pthread – Provides kernel-safe message queues

RTAI Modules (Cont.)

S. Hussein & A. Taleb | RTAI For Linux

Page 9: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS 9

◼  The main functions to use to load /unload and inspect kernel modules are contained in the modutils package. They are:

¨  /sbin/insmod - insert a module into the running kernel

¨  /sbin/rmmod - remove a module from the running kernel

¨  /sbin/lsmod - inspect modules in the running codes

◼  Once a module is loaded, it passes to form part of the operating system, hence it can use all the functions and access all variables and structures of the kernel

◼  Similarly the global symbols created are made available or exported to other modules

◼  If you don't want to share some symbol (variable or function), you have to declare it as static

RTAI Functions

S. Hussein & A. Taleb | RTAI For Linux

Page 10: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS 10

◼  A module is built from a "C" source

◼  The kernel offers a different version of printf() called printk();

◼  It works as the first but it sends the output to a kernel ring buffer

◼  At any instant you can examine the contents of the buffer using the command dmesg;

◼  If you want to set the value of the variable var at installation, include the following macro: MODULE_PARM(var,"i"); after its declaration

◼  If you want to add a math function, say sin(), to our simple module you have to use the linker to get the sin() function into the kernel space

 

RTAI Functions (Cont.)

S. Hussein & A. Taleb | RTAI For Linux

Page 11: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS

> <

05�Perspiciatis unde.!

11

�RTAI-Lab tool Chain

The RTAI-Lab tool-chain is based on:

■  Scilab/Scicos: an open source CACSD software for numerical computation, including Scicos, a block diagram editor used to create simulations and automatically generate and compile code

■  Comedi: provides the drivers, library functions, and an API to interact with signal acquisition hardware

■  RTAI: distributed as a package with a patch to apply to the Linux kernel. It inserts a sub-kernel where prioritized, hard real-time tasks can run. FIFOs and shared memory can be used to transfer data between real-time and user space processes

Integrated Development Environment

4�

Page 12: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS 12

■  RTAI-Lib: a palette of Scicos blocks that allow designing block diagrams with sensors and actuators. It provides an interface to RTAI and signal acquisition hardware. Block diagrams that use RTAI-Lib can be compiled into RTAI execuatble software

■  Xrtailab: an oscilloscope-like software that can conour real-time executables, allow visual monitoring of signals and real-time events using gauge, scope, and LED mock-ups. It also allow adjustment the real-time executable parameters while running

RTAI-Lab tool Chain (Cont.)

Page 13: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS

> <

04�Perspiciatis unde.!

Processor Target

13

■  An RTAI supports the hardware architectures: x86 and x86_64, PowerPC and ARM

■  RCP suite for Rapid Control Prototyping is used on a destination embedded hardware such as TS-7300

■  The embedded platform consists of the TS-7300 Single Board Computer with the RTAI real-time OS

■  The TS-7300 embedded system is multipurpose board with Cirrus Logic EP9302 200MHz processor

■  EP9302 features an advanced ARM920T processor designed with a memory management unit (MMU) that allows the support of high level operating systems such as Linux with a real-time extension

Processor Target

5�

Page 14: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS 14 S. Hussein & A. Taleb | RTAI For Linux

TS-7300 Single Board Computer

Page 15: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS 15 S. Hussein & A. Taleb | RTAI For Linux

■  The sequence of actions taken up when tool-chain will be used is

Page 16: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS

> <

05�Perspiciatis unde.!

�Example

16

Example

6�■  The following is a simple program to generates a

sine signal and displays the instant values on the screen

-------------- RT_PROCESS.C -------------------

%The module.h file is needed to create a module

\#include <linux/module.h>

\#include <asm/io.h>

\#include <math.h>

%The header files to make use of RTAI giving access to the functions and data structures within RTAI

\#include <rtai.h>

\#include <rtai_sched.h>

\#include <rtai_fifos.h>

Page 17: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS 17

% TICK_PERIOD defines the timer’s tick rate in nanoseconds, in this case equating to 1 ms, which means that the timer will tick once per millisecond.

\#define TICK_PERIOD 1000000

\#define TASK_PRIORITY 1 % define the task priority

\#define STACK_SIZE 10000 % define the stack size

\#define FIFO 0

% Task data structures are declared for the real-time task to be created.

static RT_TASK rt_task;

static void fun(int t)

{

int counter = 0; % reset the counter

float sin_value;   

S. Hussein & A. Taleb | RTAI For Linux

Page 18: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS 18

 while (1) {

sin_value = sin(2*M_PI*1*rt_get_cpu_time_ns()/1E9); %time in ns to sec (*10^-9)

rtf_put(FIFO, &counter, sizeof(counter)); % put the counter onto the FIFO

rtf_put(FIFO, &sin_value, sizeof(sin_value)); % put the sin_value onto the FIFO

counter++;

rt_task_wait_period(); } } % suspend the task, yielding the processor this time, until

it is next scheduled to run

% init_module, is where the module starts. It declares one ‘time’ variable: tick_period is used to hold the base timer tick period, used when the tasks are enabled.

int init_module(void)

{

RTIME tick_period;

rt_set_periodic_mode(); % start timer

 

S. Hussein & A. Taleb | RTAI For Linux

Page 19: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS 19

  % Now we create the application task. It has a pointer to a pre-declared task structure e.g. rt_Task), a pointer to the function to be used as the runtime portion of the task, e.g. fun, an integer data value to be passed to the task as it starts, a task stack size, a task priority, a flag to say whether or not the tasks uses floating point calculations (does it need the FPU or not?) and a pointer to a signal handler function.

rt_task_init(&rt_task, fun, 1, STACK_SIZE, TASK_PRIORITY, 1, 0);

 

% Next, a Real-Time FIFO is created of size 8000 bytes.

rtf_create(FIFO, 8000);

% Now we start the timer. In periodic mode the TICK_PERIOD values declared above is converted from nanoseconds to a period in internal count units. The return value, tick_period, is the internal period value that the timer is actually set to.

tick_period = start_rt_timer(nano2count(TICK_PERIOD));

 

 

S. Hussein & A. Taleb | RTAI For Linux

Page 20: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS 20

% Start application task running,current time is read and the it start with tick-period

rt_task_make_periodic(&rt_task, rt_get_time() + tick_period, tick_period);

return 0;#} 

% The cleanup_module function is responsible for tidying-up when the module is removed. Essentially, all system resources allocated by the module need to be de-allocated, all tasks and FIFOs destroyed, the timer stopped.

void cleanup_module(void) {

% Stop the timer. Then destroy the FIFO created in init_module and used by the application tasks and finally, delete the application task.

stop_rt_timer();

rtf_destroy(FIFO);

rt_task_delete(&rt_task);

return; }

 

 

S. Hussein & A. Taleb | RTAI For Linux

Page 21: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS 21

■  Now it’s time to look at the User Space program that reads from the FIFO and writes reports to the console, allowing you to visualize what’s going on. 

-------------------- SCOPE.C ----------------------

% Include the relevant Linux headers, declare a static variable ‘end’ (initialized to 0) used for loop control, and a function ‘endme’ used to set end to ‘1’ and thus force loop exit

\#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; } }

 

 

S. Hussein & A. Taleb | RTAI For Linux

Page 22: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS 22

% The ‘main’ User-Space program. Note that it declares a number of local variables: sin_value and counter identical to the both declared by the real-time application task.

int main (void)

{

int fifo, counter;

float sin_value;

% The process opens the real-time FIFO ‘/dev/rtf0’. The minor number used ‘0’ is the same number as the #define FIFO 0 in the real-time module. This is essential if the real-time applications are to communicate with program. (the test hasn’t terminate yet)

if ((fifo = open("/dev/rtf0", O_RDONLY)) < 0)

{

fprintf(stderr, "Error opening /dev/rtf0\n");

exit(1); % Exit the while loop

}

 

 

S. Hussein & A. Taleb | RTAI For Linux

Page 23: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS 23

% This line, attaches the function ‘endme’ to the signal SIGINT (which will be generated by Ctrl-C). When the example is run, the user is prompted to hit Ctrl-C in order to terminate the test.

signal(SIGINT, endme);

% Finally, we reach the main loop of the program. Until ‘end’ is set to ‘1’ this loop will continue. The user is prompted by the ‘run’ script accompanying this example in the RTAI distribution, to hit ‘Ctrl-C’ to terminate the program and as we’ve already seen, the signal that generates (SIGINT) has been linked to the ‘endme’ function that will set ‘end’ to ‘1’. The program reads the counter and the sin_value at the head of the FIFO and prints out the counter and sin_value values onto the console screen, containing all the information passed to it by the real-time application tasks. 

while (!end) {

        read(fifo, &counter, sizeof(counter));

        read(fifo, &sin_value, sizeof(sin_value));

        printf(" Counter : %d Seno : %f \n", counter, sin_value);

}}

 

 

S. Hussein & A. Taleb | RTAI For Linux

Page 24: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS 24

-------------------- RUN ----------------------

% rtai, rtai_shm, rtai_sched and rtai_fifos are loaded (insmod loads), then scope is running to manipulate data in the shared memory blocks created.

sync

insmod /home/rtai-1.4/modules/rtai.o

insmod /home/rtai-1.4/modules/rtai_sched.o

insmod /home/rtai-1.4/modules/rtai_shm.o

insmod /home/rtai-1.4/modules/rtai_fifos.o

insmod rt_process.o

./scope

% Finally, rtai, rtai_shm, rtai_sched and rtai_fifos are unloaded.

rmmod rt_process

rmmod rtai_shm

rmmod rtai_fifos

rmmod rtai_sched

 

 

S. Hussein & A. Taleb | RTAI For Linux

Page 25: Real Time Application Interface for Linux

> < NEMO ENIM IPSAM VOLUPTATEM QUIA VOLUPTAS

next 25 THANK YOU