SPIRO THE TECH GURU

Back

HOW TO WRITE A PROGRAM IN RTOS

By Admin on


RTOS:

Real-time operating system (RTOS) when certain operations are critical and must be completed correctly and within a certain amount of time.


Basic terms:


Task:

A task is a sequence of instructions, sometimes done repetitively, to perform an action (e.g. read a keypad, display a message on an LCD, flash an LED or generate a waveform). In other words, it’ usually a small program inside a bigger one.



Interrupt:

An interrupt is an internal or external hardware event that causes program execution to be suspended. Interrupts must be enabled for an interrupt to occur.

Task’s priority:

 task’s priority suggests the task’s importance relative to other tasks.

Context switch:

A task switch occurs when one task suspends running and another starts or resumes running. It may also be called a context switch.

Preemption:

Preemption occurs when a task is interrupted and another task is made ready to run. An alternative to a preemptive system is a cooperative system, in which a task must voluntarily relinquish control of the processor before another task may run.

Delay:

A delay is an amount of time (often specified in milliseconds) during which a task’s execution can be suspended.

Event:

An event is an occurrence of something (e.g. a key was pressed, an error occurred or an expected response failed to occur) that a task can wait for.

Examples of events include:

• • an interrupt,

• • an error occurring,

 • a timer timing out,

• • a periodic interrupt,

• • a resource being freed,

• • an I/O pin changing state,

• • a key on a keypad being pressed,

• • an RS-232 character being received or transmitted and

• • information being passed from one part of your application to another.

Intertask communication:

Intertask communication is an orderly means of passing information from one task to another following some well-established programming concepts. 

-Semaphores, 

-messages, 

-message queues 

-event flags 

can be used to pass information in one form or another between tasks

Task’s state:

A task’s state describes what the task is currently doing. Tasks change from one state to another via clearly defined rules. Common task states might be 

-ready / eligible, 

-running, 

-delayed, 

-waiting, 

-stopped and 

-destroyed / uninitialized.

Reentrancy:

A reentrant function can be used simultaneously in one or more parts of an application without corrupting data. If the function is not written to be reentrant, simultaneous calls may corrupt the function’s internal data, with unpredictable results in the application.

Resources:

A resource is something within your program that can be used by other parts of the program.

Multitasking:

In order to multitask, such that all tasks appear to run concurrently, some mechanism must exist to pass control of the processor and its resources from one task to another. This is the job of the scheduler.




Cooperative Scheduling:

high-priority eligible task cannot run until a lower-priority one has relinquished control of the processor via an explicit context switch.

Preemptive Scheduling:

A preemptive scheduler can cause the current task (i.e. the task that’s currently running) to be preempted by another one. Preemption occurs when a task with higher priority than the current task becomes eligible to run. Because it can occur at any time, preemption requires the use of interrupts and stack management to guarantee the correctness of the context switch.

Semaphores:

There are two types of semaphores: 

-binary semaphores 

- counting semaphores. 

Binary semaphores :

A binary semaphore can take on only two values, 0 or 1.

Counting semaphores:

A counting semaphore can take on a range of values based on its size – for example, an 8-bit counting semaphore’s value can range from 0 to 255. Counting semaphores can also be 16-bit or 32-bit.

Event flags:

Event flags are one such use for binary semaphores – they indicate the occurrence of an event. If a semaphore is initialized to 0, it means that the event has not yet occurred. When the event occurs, the semaphore is set to 1 by signaling the semaphore.

Messages:

Messages provide a means of sending arbitrary information to a task. The information might be a number, a string, an array, a function, a pointer or anything else. Every message in a system can be different, as long as both the sender and the recipient of the particular message understand its contents. Even the type of message can even change from one message to the next, as long as the sender and recipient are aware of this.

Priority Inversions:

Priority inversions occur when a high-priority task is waiting for a resource controlled by a low-priority task. The high-priority task must wait until the low-priority task releases the resource, whereupon it can continue. As a result, the priority of the high-priority task is effectively reduced to that of the low-priority task.

Example:

#include "main.h"

#include <rtos.h>

void Task1( void )

{

while (1) 

{

task_switch();

}

}

int main( void )

{

Init();

SInit();

OSCreateTask(Task1, priority);

while (1) 

{

OSSched();

}

}

OSInit(): OSInit() initializes all of  data structures, pointers and counters, and must be called before any other calls

OSSched():OSSched() is Salvo’s multitasking scheduler. Only tasks which are in the eligible state can run, and each call to OSSched() results in the most eligible task running until the next context switch within that task. In order for multitasking to continue, OSSched() must be called repeatedly.

OSCreateTask(Task1, priority):To create a task, call OSCreateTask() with a task starting address, a tcb pointer and a priority as parameters.

                                                           





SPIRO Google Plus