1. Computing
Send to a Friend via Email

Discuss in my forum

RTLinux HOWTO

6.2 Application Using FIFO

By

First, let us consider a simple C program (filename pcaudio.c) to play music (of just two tones) through the PC speaker. For the time being, let us assume that for playing the note, we need only write to the character device /dev/rtf3. (Later, we will see a realtime time process that reads from this FIFO (/dev/rtf3) and sends to the PC speaker)

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define DELAY 30000

void make_tone1(int fd)
{
static char buf = 0;
write (fd, &buf, 1);
}

void make_tone2(int fd)
{
static char buf = 0xff;
write (fd, &buf, 1);
}

main()
{
int i, fd = open ("/dev/rtf3", O_WRONLY);
while (1)
{
for (i=0;i<DELAY;i++);
make_tone1(fd);
for (i=0;i<DELAY;i++);
make_tone2(fd);
}
}

Now, if the above shown program (pcaudio.c) is compiled and run, it should create regular sound patters corresponding to a square wave. But before that we need a module for reading from '/dev/rtf3' and sending the corresponding data to the PC speaker. This realtime program can be found at the rtlinux source tree (/usr/src/rtlinux/examples/sound/) . Insert the module sound.o using the command 'insmod'.

Since we have inserted a module for reading from the device, we can now execute our program (compile using 'gcc' and then execute the corresponding 'a.out'. So the process produces somewhat regular tones, when there is no other (time consuming) process in the system. But, when the X server is started in another console, there comes more prolonged silence in the tone. Finally, when a 'find' command (for a file in /usr directory) is executed, the sound pattern is completely distorted. The reason behind this is that, we are writing the data onto the FIFO in non-realtime.

We will, now, see how to run this process in realtime, so that the sound is produced without any kind of disturbance. First, we will convert the above program into a realtime program. (Filename rtaudio.c)

#include <rtl.h>
#include <pthread.h>
#include <rtl_fifo.h>
#include <time.h>

#define FIFO_NO 3
#define DELAY 30000
pthread_t thread;

void * sound_thread(int fd)
{
int i;
static char buf = 0;
while (1)
{
for(i=0; i<DELAY; i++);
buf = 0xff;
rtf_put(FIFO_NO, &buf, 1);

for(i=0;i<DELAY;i++);
buf = 0x0;
rtf_put(FIFO_NO, &buf, 1);
}
return 0;
}

int init_module(void)
{
return pthread_create(&thread, NULL, sound_thread, NULL);
}

void cleanup_module(void)
{
pthread_delete_np(thread);
}

If not already done, 'plug in' the module sound.o into the kernel. Compile the above program by writing a Makefile for it (as said earlier), thus producing the module 'rtaudio.o'. Before inserting this module, one more thing. Note that the above program runs into infinite loop. Since, we have not included code for the thread to sleep or stop, the thread will not cease its execution. In short, your PC speaker will go on producing the tone, and you will have to restart your computer for doing anything else.

So, let us change the code a little bit (only in the function sound_thread()) by asking the thread itself to make the delay between tones.

void * sound_thread(int fd)
{
static char buf = 0;

pthread_make_periodic_np (pthread_self(), gethrtime(), 500000000);

while (1)
{
pthread_wait_np();
buf = (int)buf^0xff;
rtf_put(FIFO_NO, &buf, 1);
}
return 0;
}

This time we can stop the process by just removing the module by using the 'rmmod' command.

Here, we have seen how realtime FIFOs can be used for inter-process communication. Also the real need for RTLinux can be understood from the above example.
...............................................................................
RTLinux HOWTO Table of Content
1. Introduction
  - 1.1 Purpose
  - 1.2 Who should read this HOWTO
  - 1.3 Acknowledgement
  - 1.4 Feedback
  - 1.5 Distribution Policy
2. Installing RTLINUX
3. Why RTLinux
4. Writing RTLinux Programs
  - 4.1 Introduction to writing modules
  - 4.2 Creating RTLinux Threads
  - 4.3 An example program
5. Compiling and Executing
6. Inter-Process Communication
  - 6.1 Realtime FIFO
  - 6.2 Application Using FIFO
7. What next

©2014 About.com. All rights reserved.