Home
Login / Signup
 di 

RTOS for Embedded Systems

Real-Time for Embedded Systems

RTOS for Embedded Systems

1. Defining the Embedded System
The term “embedded” applies to the computer/microcontroller (encapsulated by the controlled device) running the controlling application.

Real-time systems are designed to control and monitor their environment. Most of these systems are using sensors to collect environment state and use actuators to change something.
There are 2 types of real-time systems: “hard real-time” and “soft real-time”. The first type must meet deadlines with no exception. Delays are critical and may affect system reliability.

2. RTOS Structure

RTOS Overview
RTOS Overview

The heart of any real time operating system (RTOS) is the kernel. Inside the kernel is the scheduler. It is basically a set of algorithms which manage the task running order. Multitasking definition comes from the ability of the kernel to control multiple tasks that must run within time deadlines. Multitasking may give the sensation that multiple threads are running concurrently, as a matter of fact the processor runs task by task, following scheduled order.

3. Programming with RTOS

3.1 Semaphores

Semaphores

A multitasking systems needs inter processes communication. The communication depends on synchronization, which is realized with the semaphores. Depending on the application there are 3 kinds of semaphores:
Binary - this is the most used.
Mutual Exclusion (Mutex) – it’s similar to binary, but it is very useful to handle priority inheritance, deletion or recursion.
Counting – it is used when many processes access the same semaphore.

Programming Semaphores on RTAI Linux:

#include "rtai.h"                    /* RTAI configuration switches */
#include "rtai_sched.h"              /* RTAI scheduling */
#include "rtai_sem.h"                /* RTAI semaphores */

/* global variables */
#define CNTR 10
static RT_TASK t1;
static RT_TASK t2;
/* function prototypes */
void taskOne(long arg);
void taskTwo(long arg);
int objects = 0;
void tasks(void)
{
int retval;
    /* init tasks */
    retval = rt_task_init( &t1, task1, 0, 512, 0, 0, 0);
    retval = rt_task_init( &t2, task2, 0, 512, 0, 0, 0);
    /* start tasks */
    retval = rt_task_resume( &t1);
    retval = rt_task_resume( &t2);
}

void task1(long arg)
{
int i;
    for (i=0; i< CNTR; i++)
    {
        rt_printk(" task1: objects = %d \n", ++ objects);
    }
}

void task2(long arg)
{
int i;
    for (i=0; i < CNTR; i++)
    {
        rt_printk(" task2: objects = %d \n", -- objects);
    }
}

int init_module(void)
{
    printk(" start module\n");
    rt_set_oneshot_mode();                /* set the oneshot mode for the timer. */
    start_rt_timer(1);                    /* start the timer with a period 1 */
    tasks();
    printk("end module\n");
    return 0;
}

void cleanup_module(void)
{
    /* tasks end by default */
    return;
}

3.2 Message Queues

Message Queues

Two tasks can communicate using a queue. It seems that task A sends directly a message to task B, but as a matter of fact, they are sharing a storage area, and a third process (the operative system) takes care of transferring the data from one task to the other. Using only a queue the communication can only be mono directional (e.g. task A -> task B). When a bi directional (e.g. task A task B) communication is needed, than two queues must be used.

Mailbox is, as well, a type of buffer managed by the operating system. Can be used by multiple tasks and is very similar to a FIFO structure.

Using MailBox Communication on RTAI Linux:

#include "rtai.h"
#include "rtai_sched.h"
#include "rtai_mbx.h"
static RT_TASK t1;
static RT_TASK t2;
/* function prototypes */
void task1(long arg);
void task2(long arg);
/* defines */
#define MAX_NO_MESS     10
#define MAX_MESS_LENGTH 100
/* global variables */
static MBX mailboxId;
/* Create the message queue and 2 Tasks */
void message(void)
{
int retval;
    /* create FIFO mailbox */
    retval = rt_typed_mbx_init( &mailboxId, MAX_NO_MESS, FIFO_Q);
    if (0 != retval)
    {
        printk("Space mailbox not allocated.");
    }
    /* Create & Init 2 tasks */
    retval = rt_task_init( &t1, task1, 0, 512, 0, 0, 0);
    retval = rt_task_init( &t2, task2, 0, 512, 0, 0, 0);
    /* start the two tasks */
    retval = rt_task_resume( &t1);
    retval = rt_task_resume( &t2);
}

/* Task1 writes into the mailbox */
void task1(long arg)
{
int retval;
    char mess[] = "Received message from task1";
    /* send message */
    retval = rt_mbx_send( &mailboxId, mess, sizeof(mess));
    if (0 != retval)
    {
        rt_printk(" mailbox not invalid \n");
    } else {
        rt_printk(" task1: message sent to mailbox \n");}
    }
}
    /* Task2 reads from the mailbox */
void task2(long arg)
{
int retval;
    char msgBuff[MAX_MESS_LENGTH];
    /* receive message */
    retval = rt_mbx_receive_wp( &mailboxId, msgBuff, 50);
    if (-EINVAL == retval) 
    { /* task does not refer to a valid task */
        rt_printk(" mailbox not invalid\n");
    } else {
        rt_printk(" task2: receive message %s from mailbox \n", msgBuff);
    }
    /* delete mailbox */
    rt_mbx_delete( &mailboxId);
}
int init_module(void)
{
    printk(" start module \n");
    rt_set_oneshot_mode();
    start_rt_timer(1);
    message();
    printk(" end module\n");
    return 0;
}
void cleanup_module(void)
{
    stop_rt_timer();       /* stops the timer */
    rt_task_delete( &t1);
    rt_task_delete( &t2);
    return;
}

3.3 Task Scheduling

Task Scheduling

The task scheduler establishes tasks time slot. Time slots width and activation depend on available resources and priorities.

Round Robin Method
This method handles tasks with same priority, using the "time slice" concept. Each task is allowed to run for a fixed time, at the end of which it stops. The operative system then activates the next ready task.

3.4 Signals
Task can receive notifications when an event is available from external environment within interrupt lines (hardware notification) or from another task within signals (software notification). A task that was already generated can have 2 states: delivered or pending, depending by the destination.

4. Other research
Real-time domain is wide and has a lot of instruments for solving a problem. Some aspects that can be also thoroughly studied:

  • preemption
  • priority inversion
  • exceptions and interrupts
  • timer services
  • pipes
  • memory management

Read More
How to install RTAI in Ubuntu Hardy
Some RTAI documentation

Task Sceduling In RTOS

Debasis Das
ddas15847@gmail.com
www.consult-debasis.com

Except in very simple systems the task scheduling is never round robin in an RTOS. In any reasonably complex system the scheduling has to be based on priorities and also worry about priority inversion, One cannot afford to have any task locked out. A guaranteed response to interrupt in a defined response time is something that differentiates RTOS from other OS.

Who's online

There are currently 0 users and 18 guests online.

Recent comments