+ All Categories
Home > Documents > Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1...

Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1...

Date post: 11-Aug-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
48
Lineo Education Services Lineo: Introduction to Real-time Linux ... Day 2 Table of Contents . . . . . . . . . . . . . . . . . 1 1 Real-Time FIFOs . . . . . . . . . . . . . . . . 1 1.1 Real-Time FIFOs . . . . . . . . . . . . . 1 1.2 FIFO Features and characteristics . . . . . . . . . . . . . 2 1.3 RTAI FIFOs Kernel Space API . . . . . . . . . 3 1.4 Exercise: Parameter Passing with Simple FIFOs . . . . . . . . . . . . . . . . 3 1.4.1 Instructions . . . . . . . . . . . . . . . . 3 1.4.2 Assignment . . . . . . . . . . . . . . . . . . 3 1.4.3 Tips . . . . . . . . . . . . . . . . 4 1.4.4 FIFO Example . . . . . . . . . . . . . . . 7 1.5 Lab: Basic Servo Driver . . . . . . . . . 8 1.6 Exercise : Parameter Passing with Named FIFOs . . . . . . . . . . . . . . . . 8 1.6.1 Information: . . . . . . . . . . . . . . . . 8 1.6.2 Assignment: . . . . . . . . . . . . . . . . . . 9 1.6.3 Tips: . . . . . . . . . . . . . . 9 1.6.4 Named FIFOs Example . . . . . . . . . . . . . . . 13 1.7 Lab: Servo Named FIFO . . . . . . . . . . . . . . . 14 1.8 Exercise : FIFO Handler . . . . . . . . . . . . . . . . 14 1.8.1 Information . . . . . . . . . . . . . . . . 14 1.8.2 Assignment . . . . . . . . . . . . . . . . . . 14 1.8.3 Tips . . . . . . . . . . . . . . 14 1.8.4 Fifo Handler example . . . . . . . . . . . . . . 18 1.9 Lab: Servo FIFO Handler . . . . . . . . . . . . . . . . . . 19 2 Shared Memory . . . . . . . . . . . 19 2.1 Memory Allocation and Shared Memory . . . . . . . . . . . . . . . . 20 2.2 Shared Memory . . . . . . . . . . . . . . . . 20 2.2.1 Instructions . . . . . . . . . . . . . . . . 20 2.2.2 Assignment . . . . . . . . . . . . . . . . . . 20 2.2.3 Tips . . . . . . . . . . . . . 20 2.2.4 Shared Memory Example . . . . . . . . . . . . . 25 2.3 Lab: Basic Servo Shared Memory . . . . . . . . . . . . . . . . 26 3 Real-Time Debugging . . . . . . . . . . . . . . . 26 3.1 Debugging in General . . . . . . . . . . . . . . 27 3.2 Debugging in User Space . . . . . . . . . . . . . 28 3.3 Debugging in User Space Lab . . . . . . . . . . . . . 29 3.4 RTAI Debugging in Kernel Space . . . . . . . . . . . . . . . 29 3.5 RTAI Debugging Data . . . . . . . . . . . . . 29 3.6 Remote Runtime Data Debugger . . . . . . . . . . . . . . . 32 3.7 RTAI Debugging Setup . . . . . . . . . . . . . . 33 3.8 Using R2D2 for User Space . . . . . . . . . 36 3.9 Lab: Experimenting with R2D2: Target Selection . . . . . . . . . . . . . . 37 3.10 Lab: R2D2 Module Debug . . . . . . . . . . . . . . . . . 38 4 Remote Debugging . . . . . . . . . . . . . . 38 4.1 Debugging in Kernel Space . . . . . . . . . . 38 4.2 Using R2D2 for Remote Module Debug, Part 1 . . . . . . . . . . . . . . 39 4.3 Lab: Remote R2D2, Part 2 . . . . . . . . . . . . . . . . 39 4.4 Serial Port Names - i -
Transcript
Page 1: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

Lineo: Introduction to Real-time Linux ... Day 2

Table of Contents................. 11 Real-Time FIFOs................ 11.1 Real-Time FIFOs............. 11.2 FIFO Features and characteristics ............. 21.3 RTAI FIFOs Kernel Space API......... 31.4 Exercise: Parameter Passing with Simple FIFOs ................ 31.4.1 Instructions ................ 31.4.2 Assignment .................. 31.4.3 Tips ................ 41.4.4 FIFO Example ............... 71.5 Lab: Basic Servo Driver......... 81.6 Exercise : Parameter Passing with Named FIFOs ................ 81.6.1 Information: ................ 81.6.2 Assignment: .................. 91.6.3 Tips: .............. 91.6.4 Named FIFOs Example ............... 131.7 Lab: Servo Named FIFO............... 141.8 Exercise : FIFO Handler ................ 141.8.1 Information ................ 141.8.2 Assignment .................. 141.8.3 Tips .............. 141.8.4 Fifo Handler example .............. 181.9 Lab: Servo FIFO Handler.................. 192 Shared Memory........... 192.1 Memory Allocation and Shared Memory................ 202.2 Shared Memory ................ 202.2.1 Instructions ................ 202.2.2 Assignment .................. 202.2.3 Tips ............. 202.2.4 Shared Memory Example ............. 252.3 Lab: Basic Servo Shared Memory................ 263 Real-Time Debugging............... 263.1 Debugging in General.............. 273.2 Debugging in User Space............. 283.3 Debugging in User Space Lab............. 293.4 RTAI Debugging in Kernel Space............... 293.5 RTAI Debugging Data............. 293.6 Remote Runtime Data Debugger............... 323.7 RTAI Debugging Setup.............. 333.8 Using R2D2 for User Space......... 363.9 Lab: Experimenting with R2D2: Target Selection.............. 373.10 Lab: R2D2 Module Debug................. 384 Remote Debugging.............. 384.1 Debugging in Kernel Space.......... 384.2 Using R2D2 for Remote Module Debug, Part 1.............. 394.3 Lab: Remote R2D2, Part 2................ 394.4 Serial Port Names

- i -

Page 2: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

................ 404.5 Serial Port Access

................. 404.6 Serial Port Use

................ 404.7 Installing Minicom

................. 404.8 Using Minicom

............. 414.9 Common Problems with minicom

................. 414.10 Serial Lab

............. 424.11 Lab: Host/Target Debugging

........... 424.12 Lab: Host/Target Debugging using DDD

........... 424.13 Lab: Host/Target Debugging using kGDB

.................. 445 Real-time LTT

............ 445.1 The Linux Trace Toolkit Introduction

............... 445.2 Linux Trace Toolkit Use

- ii -

Page 3: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

1 Real-Time FIFOs

1.1 Real-Time FIFOs

A FIFO (First In First Out) :

Is an unstructured, unidirectional read/write buffer

Is used to asynchronously transfer data between running tasks or processes

Is similar in principle to the UNIX pipe concept

Provides a useful communication mechanism between kernel spacereal-time tasks and user space processes

1.2 FIFO Features and characteristics

A FIFO is a Kernel memory structure that has read and write pointers to a dataarea. Data is written by one application and read by another.

FIFOs can be created from either user or kernel space.

FIFOs appear as devices /dev/rtf0..63 in the file system.

There is no limit to the number of FIFOs an application can use, or to thesize of data that can be written to an FIFO, other than practical memorylimits.

- 1 -

1 Real-Time FIFOsLineo: Introduction to Real-time Linux ... Day 2

Page 4: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

FIFOs can be "named" and referenced by that name.

FIFOs can be re-sized and reset after their creation. A resize will destroy data already in a fifo.

FIFOs queue data, and no protocol is required to prevent data overwrites.

Applicatons must deal with FIFO full/empty conditions.

Boundaries between successive data writes are not maintained.

Applications must detect boundaries between data, particulary if data is ofvarying size.

FIFOs support the /proc file system interface.

FIFOs support semaphores for synchronization.

FIFOs support multiple readers and writers and timed reads and writes.

FIFOs support asynchronous signals for "event" notification (such as thearrival of data on a previously empty FIFO).

1.3 RTAI FIFOs Kernel Space API

In user space FIFOs are inodes in /dev. Each of these inodes has a minor devicenumber. In kernel space individual FIFOs are identified using the minor devicenumber.

The following are examples of the kernel FIFO API:

int rtf_create (unsigned int fifo, int size) Create a FIFO of a given size.

rtf_destroy (unsigned int fifo) Delete a FIFO if no one else is using it.

rtf_reset (unsigned int fifo) Reset a FIFO; destroy contents.

rtf_resize (unsigned int fifo,int size) Resize a FIFO; destroy contents.

rtf_put (unsigned int fifo, void *buf, int count) Real-time: put data into the FIFO; return count.

- 2 -

1.3 RTAI FIFOs Kernel Space API

Page 5: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

rtf_get (unsigned int fifo, void *buf, int count) Real-time: get any data from the FIFO; return count.

rtf_create_handler (unsigned int fifo, int (*handler)(unsigned int fifo)) Real-time: create a handler to be called on user read/write to the FIFO.

1.4 Exercise: Parameter Passing with Simple FIFOs

1.4.1 Instructions The pulse width of the RC servo signal generator from the previous exercise can only be setwhen loading the module. Now the pulse width will be made adjustable using a FIFO. A usertask will send data to the FIFO.

1.4.2 Assignment Complete your (example) solution from the "basic" exercise to use a FIFO to set the pulsewidth. Use FIFO number zero (/dev/rtf0).

1.4.3 Tips

Assume that the user tasks sends a 32-bit integer value using the FIFO as four bytes, this is thepulse width value in nanoseconds.

You will use some simple user code to test the FIFO.

- 3 -

1.4 Exercise: Parameter Passing with Simple FIFOsLineo: Introduction to Real-time Linux ... Day 2

Page 6: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

// fifos user task; us_appl.c; compile with:// gcc -Wall -O2 -I/usr/src/rtai/include -o us_appl us_appl.c

#include <stdio.h>#include <stdlib.h>#include <rtai.h>#include <rtai_fifos.h>

/* we specify an array of fifos for use later */char* fifo_device[]={"/dev/rtf0","/dev/rtf1","/dev/rtf2","/dev/rtf3", "/dev/rtf4","/dev/rtf5","/dev/rtf6","/dev/rtf7"};

int main(int argc,char* argv[]) {

/* read the position in given as an argument to the program */ int position=(argc==2)? atoi(argv[1]) : 0; /* preset the fifo number to 0 */ int fifo_number=0;

int count; /* open the fifo */ int fd=open(fifo_device[fifo_number],O_WRONLY);

/* simple error check */ if (fd < 0 ) { printf(" error opening the fifo\n"); exit(1); } /* write the data */ count = write(fd,&position,sizeof(int));

printf(" wrote %d bytes to the fifo\n",count); /* close the fifo */ close(fd);

return 0;}

1.4.4 FIFO Example

// fifo; servofifo.c from scratch; compile with // gcc -O2 -I/usr/src/linux/include -I/usr/src/rtai/include // -D__KERNEL__ -DMODULE -c servofifo.c

#include <linux/module.h> #include <linux/kernel.h>

- 4 -

1.4.4 FIFO Example

Page 7: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

#include <linux/version.h> #include <asm/io.h>

#include <rtai.h> #include <rtai_sched.h> #include <rtai_fifos.h>

#define PORT 0x378 static RT_TASK task_data; static int position = 0; MODULE_PARM( position," i");

(A) 1#define TIME1ms (B) 1000 (C) 1000000

(A) 19#define TIME19ms (B) 19000 (C) 19000000

static int fifo = 0;

void task_code( int arg) {

RTIME nowns = rt_get_time_ns();

/* start an infinite loop */ for (;;) {

int data; /* get data from the fifo */

if (

(A)read (B)rtf_put (C)rtf_get

(fifo,&data,sizeof(int)) (A) == 0(B) < 0(C) > 0

) position=data;

/* set port output on */ outb(0xff,PORT);

/* wait a while (on time) */ nowns += TIME1ms + position; rt_sleep_until (nano2count(nowns));

/* set port output off */ outb(0x00,PORT);

- 5 -

1.4.4 FIFO ExampleLineo: Introduction to Real-time Linux ... Day 2

Page 8: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

/* wait a while (off time) */ nowns += TIME19ms - position; rt_sleep_until (nano2count(nowns));

/* end of loop */ }}

int init_module( void) {

RT_TASK * rttask_struct; void * rttask_code; int rttask_param; int rttask_stack; int rttask_priority; int rttask_use_fp; void * rttask_signal;

printk(" servofifo: started, position=% i\n", position); rt_set_oneshot_mode(); start_rt_timer(0);

/* set up the fifo */ (A) rtf_create(fifo,4000) ; (B) rtf_open(fifo,4000) ; (C) open("/dev/rtf0",O_RDONLY);

/* Set up the rt task */ rttask_struct = &task_data; rttask_code = task_code; rttask_param = 0; rttask_stack = 3000; rttask_priotity = 0; rttask_use_fp = 0; rttask_signal = 0;

rt_task_init( rttask_struct, rttask_code, rttask_param, rttask_stack, rttask_priority, rttask_use_fp, rttask_signal);

/* Start up the rt task */

- 6 -

1.4.4 FIFO Example

Page 9: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

rt_task_resume(rttask_struct);

return 0; }

void cleanup_module( void) {

RT_TASK * rttask_struct;

/* Set up the rt task */ rttask_struct = &task_data;

/* Stop the rt task */ rt_task_delete(rttask_struct);

/* close the fifo */ (A) close (fifo); (B) rtf_close(fifo); (C) rtf_destroy(fifo);

stop_rt_timer(); printk(" servofifo: stopped, position=% i\n", position); }

1.5 Lab: Basic Servo Driver

- 7 -

1.5 Lab: Basic Servo DriverLineo: Introduction to Real-time Linux ... Day 2

Page 10: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

Action Description

Get the code.

mkdir -p /$HOME/ClassLabs/Servo/basic/

Make a working directory.

cd /$HOME/ClassLabs/Servo/basic/ Go to the working directory.

cp -av/mnt/train/code/ClassLabs/Servo/basic/* ./

Copy the files.

Complete the code.

kedit (or vi) servofifo.c Edit the code to make the correct real-time calls.

Make the target.

make

(As root) sh servofifo_load 100000. Loads the driver and sets the servo position.

/sbin/lsmodThis lists all the kernel modules presently loadedand who is using them. You should see servofifo atthe top of the list, it was the last module loaded.

(As root) sh spos1;sleep 2; sh spos2 sets the position.

(As root) sh servofifo_unload Unloads the driver.

1.6 Exercise : Parameter Passing with Named FIFOs

1.6.1 Information:

If various real-time tasks and user space programs need to communicate with one another, thenusing hard coded FIFO numbers is open to error, since applications often use FIFOs with thesame numbers. For this reason so-called "named FIFOs" were introduced. FIFO numbers are determineddynamically at FIFO initialization. They can then be referenced by name from other RTAI tasksand user space applications.

1.6.2 Assignment:

Change your (example) solution from servofifo, so that a named FIFO with a dynamicallygenerated number is used.

started real-time task is delivered.

- 8 -

1.6 Exercise : Parameter Passing with Named FIFOs

Page 11: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

1.6.3 Tips:

The prototypes for named FIFOs are: module use int rtf_create_named(char* name); // return value = FIFO number user task use int rtf_getfifobyname(char* name); // return value = FIFO number You will need to include the compile flag -DRTAI_RTF_NAMED rtf_put cannot be used in user space.

1.6.4 Named FIFOs Example

// named fifo; servonfifo.c from scratch; compile with // gcc -O2 -I/usr/src/linux/include -I/usr/src/rtai/include //-I/usr/src/rtai/newfifos//-D__KERNEL__ -DMODULE -DRTAI_RTF_NAMED // -c servonfifo.c

#include <linux/module.h> #include <linux/kernel.h> #include <linux/version.h> #include <asm/io.h>

#include <rtai.h> #include <rtai_sched.h> #include <rtai_fifos.h>

#define PORT 0x378 RT_TASK task_data; static int position= 0; MODULE_PARM( position," i");

static int fifo=0;

(A) 1#define TIME1ms (B) 1000 (C) 1000000

(A) 19#define TIME19ms (B) 19000 (C) 19000000

- 9 -

1.6.3 Tips:Lineo: Introduction to Real-time Linux ... Day 2

Page 12: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

void task_code( int arg) { RTIME nowns = rt_get_time_ns();

/* start an infinite loop */ for (;;) { int data;

/* get data from the fifo */ (A)read (A) == 0 if ( (B)rtf_put (fifo,&data,sizeof(int)) (B) < 0 ) (C)rtf_get (C) > 0

position=data;

/* set port output on */ outb(0xff,PORT);

/* wait a while (on time) */ nowns += TIME1ms + position; rt_sleep_until (nano2count(nowns));

/* set port output off */ outb(0x00,PORT);

/* wait a while (off time) */ nowns += TIME19ms - position; rt_sleep_until (nano2count(nowns));

/* end of loop */ }}

int init_module(void) {

RT_TASK * rttask_struct; void * rttask_code; int rttask_param; int rttask_stack; int rttask_priority; int rttask_use_fp; void * rttask_signal;

printk(" servonfifo: started, position=% i\n", position); rt_set_oneshot_mode(); start_rt_timer( 0);

- 10 -

1.6.4 Named FIFOs Example

Page 13: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

/* set up the fifo */ (A) fifo=rtf_create_named("myfifo"); (B) fifo=rtf_create("myfifo",4000) (C) rtf_open("/dev/my_fifo",4000) ;

/* Set up the rt task */ rttask_struct = &task_data; rttask_code = task_code; rttask_param =0; rttask_stack = 3000; rttask_priotity =0; rttask_use_fp = 0; rttask_signal = 0;

rt_task_init( rttask_struct, rttask_code, rttask_param, rttask_stack, rttask_priority, rttask_use_fp, rttask_signal);

/* Start up the rt task */ rt_task_resume(rttask_struct);

return 0; }

void cleanup_module( void) {

RT_TASK * rttask_struct;

/* Set up the rt task */ rttask_struct = &task_data;

/* Stop the rt task */ rt_task_delete(rttask_struct);

/* close the fifo */ (A) rtf_close_named ("myfifo"); (B) rtf_close(fifo); (C) rtf_destroy(fifo);

- 11 -

1.6.4 Named FIFOs ExampleLineo: Introduction to Real-time Linux ... Day 2

Page 14: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

stop_rt_timer(); printk(" servonfifo: stopped, position=% i\n", position);

}

A user task is required for this example.

// lesson 2.1: named fifos; us_appl.c; compile with:// gcc -Wall -O2 -I/usr/src/rtai/include -o us_appl us_appl.c

#include <stdio.h>#include <stdlib.h>#include <rtai.h>#include <rtai_fifos.h>

char* fifo_device[]={"/dev/rtf0","/dev/rtf1","/dev/rtf2","/dev/rtf3", "/dev/rtf4","/dev/rtf5","/dev/rtf6","/dev/rtf7"};

int main(int argc,char* argv[]) { int position=(argc==2)? atoi(argv[1]) : 0; int fifo_number;

/* get the fifo */

(A) fifo_number=open("myfifo",O_RDONLY); (B) fifo_number=rtf_openfifo("myfifo"); (C) fifo_number=rtf_getfifobyname("myfifo");

int fd=open(fifo_device[fifo_number],O_WRONLY);

write(fd,&position,sizeof(int));

close(fd); return 0;}

- 12 -

1.6.4 Named FIFOs Example

Page 15: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

1.7 Lab: Servo Named FIFO

Action Description

Get the code.

mkdir -p /$HOME/ClassLabs/Servo/nfifo/ Make a working directory.

cd /$HOME/ClassLabs/Servo/nfifo/ Go to the working directory.

cp -av /mnt/train/code/ClassLabs/Servo/nfifo/* . Copy the files.

Complete the code.

kedit servonfifo.c Edit the code to make the correct FIFO calls.

Make the target.

cc -I/usr/src/rtai/include -O2-DRTAI_RTF_NAMED -DMODULE-D__KERNEL__ -c servonfifo.c

Make the User application.

cc -I/usr/src/rtai/include -O2-DRTAI_RTF_NAMED -o us_appl us_appl.c

As root sh servonfifo_load Load the driver.

/sbin/lsmod

This lists all the kernel modules presentlyloaded and who is using them. Youshould see servonfifo at the top of the list,it was the last module loaded.

As root sh spos1 Set the position via the FIFO.

As root sh spos2 Set the position via the FIFO.

As root sh servonfifo_unload Unload the driver

As root type dmesg Display "printk" messages.

- 13 -

1.7 Lab: Servo Named FIFOLineo: Introduction to Real-time Linux ... Day 2

Page 16: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

1.8 Exercise : FIFO Handler

1.8.1 Information

Rather than continually reading the FIFO to see if data is present, a handler can be used that istriggered by a task (in user space) writing data to the FIFO.

1.8.2 Assignment Take the (example) solution from the fifo Exercise and integrate a FIFO handler in servofifoh.c that reads four bytes at a time from the FIFO and writes them to a 32-bit integer value. Delivery of the time information to the signal pulse generator task uses the global variable"position."

1.8.3 Tips

The FIFO handler starts with "int fifo_handler( unsigned int fifo_num)" in which the fifo_num parameter is the FIFO number to be used.

If the data read was successful, the FIFO handler must provide a return value of zero; otherwisean error value (for example -EINVAL) should be returned. If a return of zero is received, thefifo driver will return the count of characters read or written to the fifo.

1.8.4 Fifo Handler example

// fifo handler task; servofifoh.c; compile with // gcc -O2 -I/usr/src/linux/include -I/usr/src/rtai/include //-I/usr/src/rtai/newfifos// -DRTAI_RTF_NAMED -D__KERNEL__ -DMODULE // -c servofifoh.c

#include <linux/module.h> #include <linux/kernel.h> #include <linux/version.h> #include <asm/io.h>

#include <rtai.h> #include <rtai_sched.h> #include <rtai_fifos.h>

#define PORT 0x378 RT_TASK task_data; static int position= 0; MODULE_PARM( position," i");

- 14 -

1.8 Exercise : FIFO Handler

Page 17: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

static int fifo=0;

#define TIME1ms 1000000#define TIME19ms 19000000

void task_code( int arg) {

RTIME nowns = rt_get_time_ns();

/* start an infinite loop */ for (;;) {

/* set port output on */ outb(0xff,PORT);

/* wait a while (on time) */ nowns += TIME1ms + position;

rt_sleep_until (nano2count(nowns));

/* set port output off */ outb(0x00,PORT);

/* wait a while (off time) */ nowns += TIME19ms - position; rt_sleep_until (nano2count(nowns));

/* end of loop */ }}

int fifo_handler(unsigned int fifo_num) { int data,got;

while((

/* get data from the fifo */ (A) got=rtf_read(fifo_num,&data,sizeof(int)))==sizeof(int)) (B) got=rtf_get(fifo_num,&data,sizeof(int)))==sizeof(int)) (C) got=read(fifo_num,&data,sizeof(int)))==sizeof(int))

position=data;

if(got !=0) return -EINVAL; return 0;}

- 15 -

1.8.4 Fifo Handler exampleLineo: Introduction to Real-time Linux ... Day 2

Page 18: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

int init_module( void) {

RT_TASK * rttask_struct; void * rttask_code; int rttask_param; int rttask_stack; int rttask_priority; int rttask_use_fp; void * rttask_signal;

printk(" servofifoh: started, position=% i\n", position); rt_set_oneshot_mode(); start_rt_timer( 0);

/* set up the fifo */ (A) fifo=rtf_create_named("myfifo"); (B) fifo=rtf_create("myfifo",4000) (C) fifo=rtf_open("/dev/my_fifo",4000) ;

/* set up the fifo */ (A) rtf_create(fifo,fifo_handler); (B) rtf_create_handler(fifo,fifo_handler); (C) fifo_handler = &fifo_handler;

/* Set up the rt task */ rttask_struct = &task_data; rttask_code = task_code; rttask_stack = 3000; rttask_param = 0; rttask_priotity = 0; rttask_use_fp = 0; rttask_signal = 0;

rt_task_init( rttask_struct, rttask_code, rttask_param, rttask_stack, rttask_priority, rttask_use_fp, rttask_signal); /* Start up the rt task */ rt_task_resume(rttask_struct);

return 0;

- 16 -

1.8.4 Fifo Handler example

Page 19: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

}

void cleanup_module( void) {

RT_TASK * rttask_struct;

/* Set up the rt task */ rttask_struct = &task_data;

/* Stop the rt task */ rt_task_delete(rttask_struct);

/* close the fifo */ (A) rtf_close_named ("myfifo"); (B) rtf_close(fifo); (C) rtf_destroy(fifo);

stop_rt_timer(); printk(" servofifoh: stopped, position=% i\n", position);

}

We need this user task for this example.

// lesson: fifo handler; us_appl.c; compile with:// gcc -Wall -O2 -I/usr/src/rtai/include -DRTAI_RTF_NAMED -o us_appl us_appl.c

#include <stdio.h>#include <stdlib.h>#include <rtai.h>#include <rtai_fifos.h>

char* fifo_device[]={"/dev/rtf0","/dev/rtf1","/dev/rtf2","/dev/rtf3", "/dev/rtf4","/dev/rtf5","/dev/rtf6","/dev/rtf7"};

int main(int argc,char* argv[]) { int position=(argc==2)? atoi(argv[1]) : 0; int fifo_number;

/* open the fifo */ (A) fifo_number=open("myfifo",O_RDONLY); (B) fifo_number=rtf_openfifo("myfifo"); (C) fifo_number=rtf_getfifobyname("myfifo");

- 17 -

1.8.4 Fifo Handler exampleLineo: Introduction to Real-time Linux ... Day 2

Page 20: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

int fd=open(fifo_device[fifo_number],O_WRONLY); write(fd,&position,sizeof(int)); close(fd);

return 0;}

1.9 Lab: Servo FIFO Handler

Action Description

Get the code

mkdir -p /$HOME/ClassLabs/Servo/fifoh/ Make a working directory.

cd /$HOME/ClassLabs/Servo/fifoh/ Go to the working directory.

cp -av /mnt/train/code/ClassLabs/Servo/fifoh/* . Copy the files.

Complete the code.

kedit servofifoh.c Edit the code to make the correctFIFO calls.

Make the target.

cc -I/usr/src/linux/include -I/usr/src/rtai/include-I/usr/src/rtai/newfifos -O2 -DRTAI_RTF_NAMED-DMODULE -D__KERNEL__ -c servofifoh.c

Make the User application.

cc -I/usr/src/rtai/include -I/usr/src/rtai/newfifos -O2-DRTAI_RTF_NAMED -o us_appl us_appl.c

As root sh servofifoh_load Load the driver.

/sbin/lsmod

This lists all the kernel modulespresently loaded and who is usingthem. You should see servofifoh atthe top of the list, because it was thelast module loaded.

As root sh spos1 Set the position via the FIFO.

As root sh spos2 Set the position via the FIFO.

As root sh servofifoh_unload Unload the driver.

As root type dmesg Display "printk" messages.

- 18 -

1.9 Lab: Servo FIFO Handler

Page 21: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

2 Shared Memory

2.1 Memory Allocation and Shared Memory

Shared Memory

An alternative communication mechanism to FIFOs is shared memory, inwhich a portion of physical memory, in kernel space, is set aside for sharingbetween Linux processes and real-time Linux tasks.

The decision to use FIFOs versus shared memory should be based on thenatural communication model of the application. For control applicationsinvolving processes that execute cyclically based on the expiration of aninterval timer, where data queuing is the exception rather than the rule,shared memory is a good choice for communication.

Shared memory works by allocating, in kernel space, a chunk of memory. A device driver (using mmap) provides the user with a read/write address inuser space. Kernel functions do the same mapping for the kernel (real-time) tasks. Shared memory needs to be identified with an ID.

- 19 -

2 Shared MemoryLineo: Introduction to Real-time Linux ... Day 2

Page 22: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

Kernel Functions

void *rtai_kmalloc (unsigned long name, int size); Allocate a block of shared memory and tag it as used.

void rtai_kfree (unsigned long name); Free a previously allocated block of memory.

User Space Functions

void *rtai_malloc (unsigned long name, int size); Allocate a block of shared memory and tag it as used.

void rtai_free (unsigned long name, void *adr); Free a previously allocated block of memory.

void *rtai_malloc_adr (void *addr, unsigned long name, int size); Allocate a block of shared memory at a specified user address and tag it as used.

2.2 Shared Memory

2.2.1 Instructions Instead of parameter passing via FIFO, the time value should now be sent to the real-time taskwith the help of a shared memory area.

2.2.2 Assignment

For the real-time module, take the (example) solution from Exercise 1.0 and complete thenecessary lines of code so that the time for signal generation is sent directly from sharedmemory. Use the (example) solution from us_appl.c in the fifo example as the basis for the userspace position generation task. However, replace the FIFO functions with the appropriateshared memory instructions.

2.2.3 Tips us_appl.c requires the header files rtai_shm.h, sys/mman.h and fcntl.h.

2.2.4 Shared Memory Example

// shared memory; servoshm.c from scratch; compile with // gcc -O2 -I/usr/src/linx/include -I/usr/src/rtai/include //-D__KERNEL__ -DMODULE -c servoshm.c

#include <linux/module.h> #include <linux/kernel.h>

- 20 -

2.2 Shared Memory

Page 23: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

#include <linux/version.h> #include <asm/io.h>

#include <rtai.h> #include <rtai_sched.h> #include <rtai_shm.h>

#define PORT 0x378 static RT_TASK task_data; static int position= 0; MODULE_PARM( position," i");

static int *position_shm=0;

#define TIME1ms 1000000#define TIME19ms 19000000

void task_code( int arg) {

RTIME nowns = rt_get_time_ns();

/* start an infinite loop */ for (;;) {

(A) position=position_shm; (B) position=&position_shm; (C) position=*position_shm;

/* set port output on */ outb(0xff,PORT);

/* wait a while (on time) */ nowns += TIME1ms + position; rt_sleep_until (nano2count(nowns));

/* set port output off */ outb(0x00,PORT);

/* wait a while (off time) */ nowns += TIME19ms - position; rt_sleep_until (nano2count(nowns));

/* end of loop */ }}

- 21 -

2.2.4 Shared Memory ExampleLineo: Introduction to Real-time Linux ... Day 2

Page 24: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

int init_module( void) {

RT_TASK * rttask_struct; void * rttask_code; int rttask_param; int rttask_stack; int rttask_priority; int rttask_use_fp; void * rttask_signal;

printk(" servoshm: started, position=% i\n", position); rt_set_oneshot_mode(); start_rt_timer( 0);

/* setup the shared memory */ (A)position_shm=rtai_malloc(0x1234,sizeof(int)); (B)position_shm=rtai_kmalloc(0x1234,sizeof(int)); (C)position_shm=kmalloc(0x1234,sizeof(int));

/* Set up the rt task */ rttask_struct = &task_data; rttask_code = task_code; rttask_param = 0; rttask_stack = 3000; rttask_priotity = 0; rttask_use_fp = 0; rttask_signal = 0;

rt_task_init( rttask_struct, rttask_code, rttask_param, rttask_stack, rttask_priority, rttask_use_fp, rttask_signal);

/* Start up the rt task */ rt_task_resume(rttask_struct);

return 0; }

void cleanup_module( void) {

RT_TASK * rttask_struct;

- 22 -

2.2.4 Shared Memory Example

Page 25: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

/* Set up the rt task */ rttask_struct = &task_data;

/* Stop the rt task */ rt_task_delete(rttask_struct);

/* close the shared memory */ (A) kfree(position_shm); (B) rtai_kfree(0X1234); (C) rtai_kfree(position_shm);

stop_rt_timer(); printk(" servoshm: stopped, position=% i\n", position); }

We also need the following task for this example.

// lesson 2.3: Shared Memory us_appl.c; compile with:// gcc -Wall -O2 -I/usr/src/rtai/include -o us_appl us_appl.c

#include <stdio.h>#include <stdlib.h>

#include <sys/mman.h>#include <fcntl.h>

#include <rtai.h>#include <rtai_shm.h>

int main(int argc,char* argv[]) { int position=(argc==2)? atoi(argv[1]) : 0; int * position_shm;

/* set up the shared memory */ (A) position_shm=rtai_mmap(0x1234,sizeof(int)); (B) position_shm=rtai_malloc(0x1234,sizeof(int)); (C) position_shm=malloc(sizeof(int));

*position_shm=position;

- 23 -

2.2.4 Shared Memory ExampleLineo: Introduction to Real-time Linux ... Day 2

Page 26: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

/* close the shared memory */ (A) rtai_free(0x1234,position_shm); (B) free(position_shm); (C) rtai_munmap(0x1234,position_shm);

return 0;}

- 24 -

2.2.4 Shared Memory Example

Page 27: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

2.3 Lab: Basic Servo Shared Memory

Action Description

Get the code.

mkdir -p /$HOME/ClassLabs/Servo/shm/

Make a working directory.

cd /$HOME/ClassLabs/Servo/shm/ Go to the working directory.

cp -av/mnt/train/code/ClassLabs/Servo/shm/* ./

Copy the files.

Complete the code.

kedit servoshm.c Edit the code to make the correct calls.

Make the target.

make

Make the shared memory node (do this once only).

(as root) mknod /dev/rtai_shm c 10 254

(as root) sh servoshm_load Load the driver.

/sbin/lsmodThis lists all the kernel modules presently loaded andwho is using them. You should see servoshm at thetop of the list, because it was the last module loaded.

As root run sh spos1 Set the position via the FIFO.

As root sh spos2 Set the position via the FIFO.

As root sh servoshm_unload Unloads the driver.

As root type dmesg Displays "printk" messages.

- 25 -

2.3 Lab: Basic Servo Shared MemoryLineo: Introduction to Real-time Linux ... Day 2

Page 28: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

3 Real-Time Debugging

3.1 Debugging in General

There are many techniques available to the developer at this stage to help debuga new piece of real time code.

printk is not advised in a real time task, rt_printk should be used instead.

Instrument your task: Create two static global vars RTIME startns,stopns Put a startns = rt_get_time_ns(); at the start of your code Put a stopns = rt_get_time_ns(); at the end of your code When you unload the module use printk(" task run time (ns ) = %d \n",(int) ( stopns- startns));

- 26 -

3 Real-Time Debugging

Page 29: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

Run the task inside a "do" loop to allow the task to run once or a limitednumber of times. The following is an example:

static RTIME startns,stopns;// tcount = 0 task will run once// tcount = -1 task will run forever// tcount > 0 task will run tcount cyclesstatic int tcount = 0;

void rt_task ( int arg ){ startns = rt_get_time_ns(); do {

// Task code

// decrement the counter if it is positive if ( tcount > 0 ) tcount--; } while ( tcount ); stopns = rt_get_time_ns();

}

...

...

void cleanup_module(void ){ printk("task time = %d ns \n",(int) ( stopns-startns));}

This will give us a chance to run our task for a short number of cycles andget some run time information on how long it took the task to run. We can vary the number of cycles by using tcount as a MODULE_PARAM.

Timing measurements can also be inserted into the body of the code.

3.2 Debugging in User Space

The principal tool for debugging user space processes is GDB (GnuDeBugger).

GDB’s command language is quite powerful and flexible, but the userinterface is a rather primitive command-line tool.

- 27 -

3.2 Debugging in User SpaceLineo: Introduction to Real-time Linux ... Day 2

Page 30: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

Several GUIs have been developed to work with GDB and give it a moremodern user-friendly appearance.

We will use a GUI called DDD.

GDB can only examine the system state (data values etc,) when the targetprogram is halted at a break point.

To get the DEBUG information into a program, you need to use the -gcompile flag.

3.3 Debugging in User Space Lab

Take this simple program:

/* * simple debug program * compile with gcc -g -o simpleDebug simpleDebug.c * debug with ddd simpleDebug */

#include <stdio.h>

int sleep(int);

int count = 0;int main( int argc , char * argv[] ){ int done = 0;

while(!done) { printf("%s hello debug .. count = %d \n",argv[0],count); count++; sleep(1); } exit(0);}

Create and compile this program (in $HOME/ClassLabs/Debug).

Use DDD to debug, and set a break point at the count++ line.

Hover, with the mouse, over "done" and set its value to 1.

- 28 -

3.3 Debugging in User Space Lab

Page 31: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

Note that you can only inspect/change values when the program is stoppedat a break point.

3.4 RTAI Debugging in Kernel Space

When debugging in kernel space an additional problem arises.

When the kernel stops at a break point, the debugger also stops.

You can solve this problem by using two machines with network and seriallinks.

We will use a program called minicom to set up the serial links later.

3.5 RTAI Debugging Data

There is a solution to the problem of kernel debugging stoping the process toexamine and modify data.

The Lineo R2D2 tool will allow data debugging on either user tasks ormodules on local or remote systems.

These are the two main components to the R2D2 tool:

znav Connects to target system Launches debug window (DDD or R2D2)

zbroker Target system (user space) registers with zbroker.Debug requests serviced by zbroker.

3.6 Remote Runtime Data Debugger

The Remote Runtime Data Debugger (R2D2) is a tool for dynamically monitoring and altering statically allocated memory in either kernel modules or user-space processes. It isparticularly useful for monitoring the operation of a system that is basically stable but mayneed some "tuning." R2D2 allows variables to be changed while the system is running, thismeans that you can see the effect of such changes immediately.

Suppose, for example, that you are tuning a PID control loop. Using R2D2 you can monitorthe loop’s input and output and, in real-time, adjust the loop’s coefficients. R2D2 workswith both kernel modules and user-space applications.

- 29 -

3.4 RTAI Debugging in Kernel SpaceLineo: Introduction to Real-time Linux ... Day 2

Page 32: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

This is picture of the Main Znav screen:

- 30 -

3.6 Remote Runtime Data Debugger

Page 33: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

This is picture of the Target Selection screen:

- 31 -

3.6 Remote Runtime Data DebuggerLineo: Introduction to Real-time Linux ... Day 2

Page 34: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

3.7 RTAI Debugging Setup

Znav and Zbroker use configuration files to control remote (and local) access.

Znav uses /usr/local/z/etc/znav.cf to define the hosts it tries to connect to.

hosts=localhost, class214, buddy, train1, train2, train0#lang=C, Ada

zbroker uses /usr/local/z/etc/zbroker.cf to define the hosts and users it allowsaccess from.

# zbroker configuration filedaemon=y# the default is 5123port=5123# files created/written to by zbrokerlogfile=/var/log/zbroker.log

host_users = localhost, seh, roothost_users = localhost.localdomain, seh, root

host_users = train1, roothost_users = train1.lineo.com, root

host_users = train2, roothost_users = train2.lineo.com, root

# add this linehost_users = buddy, root,trainhost_users = buddy.lineo.com, root,train

- 32 -

3.7 RTAI Debugging Setup

Page 35: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

3.8 Using R2D2 for User Space

Consider this test program:

- 33 -

3.8 Using R2D2 for User SpaceLineo: Introduction to Real-time Linux ... Day 2

Page 36: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

// include files#include <stdio.h>#include <unistd.h>#include <stdlib.h>

// typedefsenum bool {No, Yes};

///////////////////////////////////////////////////////// Global data that can be seen by debugger.// You can also use a single global pointer to// data that will remain static once allocated///////////////////////////////////////////////////////unsigned char uchar = 129; char schar = 129;unsigned short int a = -1; short int b = 100;unsigned int c = 0xffffffff; int d = -8888888;unsigned long int e = 0xffffffff; long int f = -8888888; float g = 1.1; double h = 2.2; int counter = 0; char *title = "C test program - t.c"; char title_ary[]= "lineo test"; char title_ary2[]= "another lineo test"; short short_ary[4]= {1,2,3,4};

// start of main programint main(int argc, char **argv) { int ttl = 0; extern int znet_init(char *argv0, char *title); extern int znet_pass(void);

// initialize R2D2 debugging // pass the name of the program argv[0], and a text // string that will show up in R2D2 to help locate the // application under test if( znet_init(argv[0], title) < 0 ) { exit(1); } // time to live can be passed for automated testing if(argc == 2) { ttl = atoi(argv[1]); }

// loop forever for(;;) {

// do the main program function here counter++; if(ttl && counter > ttl) { exit(0); } // call debugger znet_pass(); // pause usleep(100000); }}

- 34 -

3.8 Using R2D2 for User Space

Page 37: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

This is the sequence used to debug the program:

(as root) start /usr/local/z/bin/zbrokerzbroker: daemonizing, opening logfile on /var/log/zbroker.log

Check that it is running.cat /var/log/zbroker.log

mkdir -p /$HOME/ClassLabs/R2D2

cd /$HOME/ClassLabs/R2D2

cp -av /mnt/train/code/ClassLabs/R2D2/* /$HOME/ClassLabs/R2D2

Compile the program.make

Run the program../t &

(as root or a user) start /usr/local/z/bin/znavSelect the microscope icon from the top menu. This brings up the R2D2 debugger.

Click on the target icon to bring up the "Select Debug Target" dialog.

Click the "Kernel Modules" radio button and then click on "Host." This brings up the list ofhosts that znav knows about. Select "localhost."

- 35 -

3.8 Using R2D2 for User SpaceLineo: Introduction to Real-time Linux ... Day 2

Page 38: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

3.9 Lab: Experimenting with R2D2: Target Selection

Monitor what is going on in the counter test program (t).

Click "New Target." This brings up a list of the currently registered programs on localhost.Select "t."

Click "Local Path." This brings up a file dialog so that you can load the object file for themodule you selected in the previous step. If you’re not in the ../R2D2 directory, go there andselect t.

Click Okay. You should now see a square green icon with the legend "local and remote modulenames match."

Click Okay again. The target select dialog disappears, and t’s global symbols show up in thetop window, the Symbol Information window.

- 36 -

3.9 Lab: Experimenting with R2D2: Target Selection

Page 39: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

3.10 Lab: R2D2 Module Debug

Monitor what is going on in the test program (rt_counter.o).

(as root) start /usr/local/z/bin/zbrokerzbroker: daemonizing, opening logfile on /var/log/zbroker.log

Check that it is running.cat /var/log/zbroker.log

cd $HOME/ClassLabs/R2D2

Load module rt_module. sh load

(as root or a user ) start /usr/local/z/bin/znavSelect the microscope icon from the top menu. This brings up the R2D2 debugger.

Click on the target icon to bring up the "Select Debug Target" dialog.

Click the "Kernel Modules" radio button and then click on "Host." This brings up the list ofhosts that znav knows about. Select "localhost."

Click "New Target." This brings up a list of the currently installed modules on localhost.Select "rt_counter.":

Click "Local Path." This brings up a file dialog so that you can load the object file for themodule you selected in the previous step. If you’re not in the ../R2D2 directory, go there andselect rt_counter.

Click Okay. You should now see a square green icon with the legend "local and remote modulenames match."

Click Okay again. The target select dialog disappears and rt_counter’s global symbols show upin the top window, the Symbol Information window.

- 37 -

3.10 Lab: R2D2 Module DebugLineo: Introduction to Real-time Linux ... Day 2

Page 40: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

4 Remote Debugging

4.1 Debugging in Kernel Space

Now that you have moved to kernel space, you have to figure out how todebug real-time tasks.

The problem with debugging in kernel space is that stopping a kernelmodule, at a breakpoint for example, stops the kernel.

This makes it rather difficult to work with an X-Windows based debuggerthat relies on services in the kernel.

Consequently, the liberal use of printk is a common debugging technique forkernel modules.

We will extend the previous debugging session to use a remote system as atarget. Because we are still using R2D2, this will only allow you to debugdata and not allow you to insert breakpoints, etc.

4.2 Using R2D2 for Remote Module Debug, Part 1

In order for zbroker to work properly, the /etc/hosts file in each machine mustbe set up. Call your designated remote machine "buddy" and place an entry in/etc/hosts like this:-

127.0.0.1 localhost localhost.localdomain 192.168.1.xxx buddy buddy.lineo.com

- 38 -

4 Remote Debugging

Page 41: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

On the buddy (remote) system.(as root) start /usr/local/z/bin/zbroker if you need tozbroker: daemonizing, opening logfile on /var/log/zbroker.log

Check that it is running.cat /var/log/zbroker.log

(on the remote system and on the local system) cd $HOME/ClassLabs/R2D2

Compile the program.make

Run the program on the remote system only../t &

(as root or a user) start /usr/local/z/bin/znavSelect the microscope icon from the top menu. This brings up the R2D2 debugger.

Click on the target icon to bring up the "Select Debug Target" dialog.

Click the "User Apps" radio button and then click on "Host." This brings up the list of hoststhat znav knows about. Select "buddy."

4.3 Lab: Remote R2D2, Part 2

Now select the remote user application.

Click "New Target." This brings up a list of the currently registered programs on buddy.Select "C test program - t.c."

Click "Local Path." This brings up a file dialog so that you can load the object file for themodule you selected in the previous step. If you’re not in the ../R2D2 directory, go there andselect t.

Click Okay. You should now see a square green icon with the legend "local and remotematch."

Click Okay again. The target select dialog disappears and t’s global symbols show up in thetop window, the Symbol Information window.

This time you are debugging the program on the remote system.

Repeat the required steps to debug the rt_counter module on the remote system.

4.4 Serial Port Names

Serial ports under Linux are in the /dev directory. They look (for the moment)just like regular files. Closer inspection will reveal some differences between a"port" and a "text" file.

- 39 -

4.3 Lab: Remote R2D2, Part 2Lineo: Introduction to Real-time Linux ... Day 2

Page 42: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

The "file" names for the serial ports are:

o /dev/ttyS0 o /dev/ttyS1

4.5 Serial Port Access

A package called minicom is widely used as a Linux Terminal Server. Ifrequired, the package will be installed from an RedHat Package (RPM).

Minicom is normally used for communicating with modems. It has facilities foruploading and downloading programs using the old X and Y modem protocols.

Minicom can be set up for a wide range of serial port configurations.

4.6 Serial Port Use

The best and easiest way to get to a serial port is through the minicom program.

4.7 Installing Minicom

Minicom will normally be installed using a binary RPM.

This may fail due to library dependencies.

[root@refsys2 philw]# rpm -i minicom-1.78-1.i386.rpmfailed dependencies:

libncurses.so.3.0 is needed by minicom-1.78-1

The answer is to use the source RPM and then compile it.

[root@refsys2 philw]# rpm -i -v minicom-1.78-1.src.rpmInstalling minicom-1.78-1.src.rpm

[root@refsys2 philw]# rpm -ba /usr/src/xxxxx/SPECS/minicom-1.78-1.spec[root@refsys2 philw]# rpm -ba /usr/src/xxxxx/RPMS/minicom-1.78-1.i386.rpm

xxxx is normally "redhat"

The system should now work.

4.8 Using Minicom

The minicom utility provides a simple way to get data into and out of a serialport.

- 40 -

4.5 Serial Port Access

Page 43: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

Minicom needs a configuration file.

Use minicom -s to cause minicom to start in a setup mode.

The Serial Lab will cover this setup.

4.9 Common Problems with minicom

More than one minicom is running. check with ps -ax | grep minicom kill with kill -9 < id from above>

Check that the lock file does not exist remove with rm /var/lock/LCK..ttys0

Check with dime test across pins 2 and 3 of the serial port.

A view looking down on the socket pins (male connector).

2 3 ------------- \ o x-x o o / \ o o o o / ---------

4.10 Serial Lab

This lab will help you set up the minicom package.

The man page man minicom will provide some additional information.

Use minicom -s to setup, and minicom -o to run the program.

Configure minicom to use /dev/ttyS0 9600 N 8 ( Hint Ctrl+A O ).Make sure that all HW and SW flow control is turned off.

Save the config file as default (dfl).

Exit and restart minicom to use this config file.

Test with a serial cable and a dime.

- 41 -

4.9 Common Problems with minicomLineo: Introduction to Real-time Linux ... Day 2

Page 44: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

4.11 Lab: Host/Target Debugging

Start zbroker on both machines if it isn’t already running.

On the "host" cd/$HOME/ClassLabs/R2D2, run znav.

Click on the "Step and Trace" icon on the menu bar.

In the dialog box next to "Select" the target "hostname" should show your partner’scomputer "buddy."

Click go.

This brings up DDD stopped at a "generic" break point on the target system.

4.12 Lab: Host/Target Debugging using DDD

In the DDD command window, enter this command:

loadmodule rt_counter

To view the source code, enter this command:

list rt_counter

You can now make use of all the facilities of DDD to debug the rt_counter module.

To stop a debug session, pull down the "file" menu selection and click on "detach." If you donot do this you will hang the remote system when you finish your debug session.

4.13 Lab: Host/Target Debugging using kGDB

The "normal" way of debugging a kernel on a target system is to use the kGDBprocess. Using this system the Kernel has a debugger built into the system. Theuser can enter the debugger during the boot process and then have all the"normal" debug facilities on the kernel as it starts up.

The host system is connected to the target using a serial cable.

The boot process intercept is achieved using a new kernel parameter gdb Thedebugging is then initiated at 38400 baud over /dev/ttyS1 (this can be adjustedwith the the gdbttyS= and gdbbaud= options).

The debug session can be summarized as follows:

1. Patch the kernel on the target system to support kGDB 2. Boot the target system with the gdb command line option 3. Connect the two systems together using a serial cable

- 42 -

4.11 Lab: Host/Target Debugging

Page 45: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

4. Start a debug session on the host system using the vmlinux file that wascreated for the target system.gdb vmlinux

5. Issue the remote debugging set up commands to GDB. 1. set remotebaud 38400 2. target remote /dev/ttyS0

6. Special instructions are required to debug modules

More information on this can be found at: http://oss.sgi.com/projects/kgdb/

- 43 -

4.13 Lab: Host/Target Debugging using kGDBLineo: Introduction to Real-time Linux ... Day 2

Page 46: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

5 Real-time LTT

5.1 The Linux Trace Toolkit Introduction

The Linux Trace Toolkit (LTT) consists of several elements:

Kernel instrumentation that captures events of interest and stores them inmemory.

A daemon that writes the trace buffer to a file.

A graphical application that analyzes the acquired trace file and presentsthe results in a useful form.

LTT requires modifications to the standard Linux kernel to supportinstrumentation. Recent versions of the kernel don’t require patching becauseinstrumentation is now a configuration option. Nevertheless, in order to useLTT, you may have to build a new kernel.

5.2 Linux Trace Toolkit Use

Using the Linux trace toolkit requires that a buffer be set aside from kernelmemory. It is best to do this before any large applications grab memory forthemselves. So, run a trace before you run X. Later versions of the Trace Toolkit mayremove this requirement.

Also, before running any traces you must insert the tracer module into thekernel. This will create a tracerdevice driver and a corresponding device /dev/tracer with the correct major/minor number needs to be set up.

- 44 -

5 Real-time LTT

Page 47: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

cat /proc/devices

Character devices: 1 mem 2 pty 3 ttyp 4 ttyS....150 rtai_fifo250 scmd253 tracer <-- use this number in the mknod command shown below254 pcmcia

Block devices: 1 ramdisk 2 fd 3 ide0 22 ide1

mknod /dev/tracer c 253 0

After the device is made the TraceDaemon can be run for a few seconds:

cd /usr/src/TracePackage-RTAI-22.2.4; ./trace 10 mytrace

which actually does TraceDaemon/TraceDaemon -ts$1 /dev/tracer $2.trace $2.proc

- 45 -

5.2 Linux Trace Toolkit UseLineo: Introduction to Real-time Linux ... Day 2

Page 48: Lineo: Introduction to Real-time Linux Day 2asghar/labs/day2.pdf · Lineo Education Services1 Real-Time FIFOs 1.1 Real-Time FIFOs A FIFO (First In First Out) : Is an unstructured,

Line

o Ed

ucat

ion

Serv

ices

After the files have been created, view them with the trace toolkit

cd /usr/src/TracePackage-RTAI-22.2.4; ./traceview mytrace

which actually does

TraceToolkit/TraceToolkit -g $1.trace $1.proc &

This may not work, so try TraceToolkit/TraceToolkit

and then open the files by hand from the menu selection.

- 46 -

5.2 Linux Trace Toolkit Use


Recommended