27
Real-Time Library: RTX AG & AH 1

Real-Time Library: RTX AG & AH 1. What is an RTOS? An RTOS is a special version of an operating system that provides a more deterministic behaviour

Embed Size (px)

Citation preview

Page 1: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

Real-Time Library:RTX

AG & AH

1

Page 2: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

What is an RTOS?

An RTOS is a special version of an operating system that provides a more deterministic behaviour for the tasks under its control.

At its core is a Real-Time Executive(RTX). The RTX provides the primitives to

Create, Manage and Schedule tasks Manage and use time in controlling tasks allow inter-task communication through:

semaphores mutexes events mailboxes

4-2

Page 3: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

What is a Task?

For our purposes we can consider a task to be a basic unit of programming that the operating system controls.

Our tasks will be written in 'C' and be of the form of an endless while(1) loop.

We will use a simple RTX to create and manage our tasks. Our application programs will consist of a number of tasks

which are controlled by the RTX and that communicate with each other using some of the RTX features. Covered later!

4-3

void task1(void){ while(1) { // do task 1 }

}

void task2(void){ while(1) { // do task 2 }

}

void task3(void){ while(1) { // do task 3 }

}

Page 4: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

The Scheduler

At the Kernel of the RTX is the scheduler Responsible for allocating CPU time to the tasks. A hardware timer provides a basic "tick" to measure time. "tick" time is

configurable and affects responsiveness of the system. Default RTX tick time is 10ms.

This scheduler is essentially a timer interrupt that allots a certain amount of execution time to each task. So task1 may run for 100ms then be de-scheduled to allow task2 to run for a similar period; task 2 will give way to task3, and finally control passes back to task1.

By allocating these slices of runtime to each task in a round-robin fashion, we get the appearance of all three tasks running in parallel to each other.

4-4

Page 5: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

The Scheduler

In order to make the task-switching process happen, we have the code overhead of the RTOS and we have to dedicate a CPU hardware timer to provide the RTOS time reference.

For ARM7 and ARM9 this must be a timer provided by the microcontroller peripherals.

In a Cortex-M microcontroller, RTX will use the SysTick timer within the Cortex-M processor.

4-5

Page 6: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

The Scheduler

Each time we switch running tasks the RTOS saves the state of all the task variables to a task stack and stores the runtime information about a task in a Task Control Block.

The “context switch time”, that is, the time to save the current task state and load up and start the next task, is a crucial value and will depend on both the RTOS kernel and the design of the underlying hardware.

4-6

Page 7: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

Task states

A task can be in one of four basic states, RUNNING, READY, WAITING, or INACTIVE.

In a given system only one task can be running, that is, the CPU is executing its instructions while all the other tasks are suspended in one of the other states

If, during our RTOS program, there is no task running and no task ready to run (e.g. they are all waiting on delay functions), then RTX uses the spare runtime to call an “Idle Demon

4-7

Page 8: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

Simple Round Robin Scheduler All tasks have the same priority and tasks run in turn

on the CPU for a given number of "time quanta" or "ticks".

The running

task

Ready Queueof tasks.

Task at front of queue selected to run

Round Robin time out. Task put on end of ready queue.

CPU

8

Page 9: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

Example with three tasks T1, T2 and T3 Each task runs for the Round Robin time.

T1 T2 T3 T1 T2 T3 T1 etc.

Time

Running task

9

Page 10: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

Round Robin with task waiting queues

The running

task

Queue of ready to run tasks

Delayed and periodic

tasks

Tasks waiting for event(s)

Tasks waiting for mutex,

semaphore or mailbox

Round Robin time out

Typically a running task often has to wait for some resource therefore is removed from the CPU and is placed in a queue.

CPUTask at front of queue selected to run

Tasks becomes ready and rejoin queue of ready tasks

NB. There is a system task called os_idle_demon that runs when there are no tasks in the ready queue.

10

Page 11: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

Example with three tasks T1, T2 and T3 T1 execution time is 50ms then delays for 180ms

second,T2 and T3 never delay and the round robin time is 50ms. Assume ready queue is T1,T2,T3.

T1 T2 T3 T2 T3 T2 T1

Time ms

T1 delays itself for 180ms

0 10050 150 250200 300 350

T1 delays for 180ms

T1 now joins ready queue, at time 230ms.

T3

T1run after T2. Remember it joins the end of the queue.

11

T2

Page 12: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

Round Robin with Priority scheduling

Highest priority task is selected when CPU free. The

running task

Queue of ready Tasks queue

sorted into priority order

Tasks waiting for event(s)

Tasks waiting for mutex,

semaphore or mailbox

Round Robin time up

CPU

Delayed and periodic

tasks

Running task doesn't need to run for a certain time so relinquishes the CPU.

Tasks becomes ready and rejoin queue of ready tasks

12

Page 13: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

Round Robin with Priority Scheduling E.g.Three tasks T1, T2 with priority 4 in the ready

queue and T3 with priority 6 is in a waiting queue.T3 is an event driven task that executes in 30ms.

T1 T2 T1 T3 T2 T1 T2

Time ms

Time 125ms:T3 becomes ready and joins ready queue.

0 10050 150 250200 300 350

Time 150ms:T3 now runs because highest priority ready task.

T1

Time 180ms: T3 runs for 30ms then relinquishes CPU and joins a waiting queue again. Scheduler runs next task from the ready queue. 13

Page 14: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

Round Robin Priority scheduling with pre-emption.

Highest priority task is selected when CPU free.

Pre-empt running task if a higher priority task is ready.

The running

task

Queue of ready Tasks queue

sorted into priority order

Tasks waiting for event(s)

Tasks waiting for mutex,

semaphore or mailbox

Round Robin time up

CPU

Delayed and periodic

tasks

Running task doesn't need to run for a certain time so relinquishes the CPU.

Tasks becomes ready and rejoin queue of ready tasks

14

Page 15: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

Round Robin Priority scheduling with pre-emption.

E.g.Tasks T1, T2 with priority 4 both in the ready queue and T3 with priority 6 is in a waiting queue.

T1 T2 T1

T3

T2 T1 T2

Time ms

Time 125ms:T3 becomes ready and immediately pre-empts the currently running task. because it is higher priority.

0 10050 150 250200 300 350

T1

Time 155ms: T3 runs for 30ms then relinquishes the CPU and joins a waiting queue again. Pre-empted T1 is restarted and runs for the rest of its round robin time - assuming it is still the highest priority.

T1

Priority

4

6

5

15

Page 16: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

The ARM RL-RTX We will be using a Real Time eXecutive with the

ARM based board Keil MCB 2300

There are 35 RTX related functions providing facilities for: task creation and management time management events mutexes semaphores mailboxes

To use the RTX the file RTL.h must be included in the 'C' source file and the operating system project option set to use the RTX Kernel.

16

#include <RTL.h>

Page 17: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

The RTX Scheduler - Timer Tick Interrupt The RTX kernel for ARM7™ uses one of the standard

ARM timers to generate a periodic interrupt. This interrupt is called the RTX kernel timer tick. For

some of the RTX library functions, you must specify timeouts and delay intervals as a number of RTX kernel timer ticks.

The parameters for the RTX kernel timer are selected in the RTC_Config.c configuration file.

For example, the RTX_Config.c file for NXP LPC23XX use Timer 0, 1,2 or 3 for the RTX kernel timer.

17

Page 18: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

RTX_Config.c The timer clock value

specifies the input clock frequency and depends on CPU clock and APB clock. For a device with CPU clock 48 MHz and VPB divider 4 the peripheral clock is 12MHz and therefore the value is set to 12000000.

The time tick value specifies the interval of the periodic RTX interrupt. The value 10000 us configures timer tick period of 0.01 seconds.

18

Page 19: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

Starting the OS and Task Creation Start the OS and creating the first task with a default priority of 1.

void os_sys_init(task_name);os_sys_init (begin); // start os then start task begin

Create tasks OS_TID os_task_create(task_name, priority); t1_ID = os_tsk_create (task1, 4); t2_ID = os_tsk_create (task2, 6); t1_ID and t2_ID must have been declare globals in the program :- OS_TID t1_ID, t2_TID;

19

Page 20: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

Task Creation Start the OS and create the initial task.

void os_sys_init(task_name); /* priority defaults to 1

os_sys_init (begin); // start os then start task begin To create further tasks the initial task uses:

OS_TID os_task_create(task_name, priority); Example

OS_TID t1_ID; // define a task ID variable// now create the taskt1_ID = os_task_create (task1, 4); priority is from 1 to 255. 1 being the lowest priority.Beware: If task1 is now the highest priority it will pre-empt the current task!!

Tasks are basically 'C' functions with the special attribute __task added before the function definition.

20

Page 21: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

Other Task Management Functions os_tsk_delete - Terminates a selected task os_tsk_delete_self -Terminates the running task os_tsk_self - Returns the task ID of the running task os_tsk_pass - Passes control to the next task ready to run os_tsk_prio - Changes the priority of a given task os_tsk_prio_self - Changes the priority of the running task

21

Page 22: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

Typical task A task is nothing more than a 'C' function. The task will usually be in the form of an endless

while loop that never terminates. Many tasks are periodic E.g.

__task void mytask(void){ /* create variables for this task */

while(1){ /* do stuff */os_dly_wait(100); /* periodic - so have a rest */

}

}

22

Page 23: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

Time Management functionsvoid os_dly_wait(n_ticks)

Pauses the calling task for a specified interval.

void os_itv_set(n_ticks) Enables the calling task for periodic wake up.

void os_itv_wait(n_ticks) Pauses the calling task until the periodic wake up interval expires.

Examples

23

FIO2SET = LED_A; os_dly_wait(10); FIO2CLR = LED_A;

void task1 (void) __task { os_itv_set(20); while(1) {

FIO2PIN ^= LED_A; // toggle led os_itv_wait();

}}

Page 24: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

Event Flag Management functions Used to provide very simple inter-task signalling. os_evt_clr Clears one or more event flags of a task. os_evt_get Retrieves the event flags that caused

os_evt_wait_or to complete. os_evt_set Sets one or more event flags of a task. os_evt_wait_and Waits for one or more event flags to be set. os_evt_wait_or Waits for any one event flag to be set. isr_evt_set Sets one or more event flags of a task. Better inter-task communication methods are

available - coming soon!

24

Page 25: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

RTX Based Program Operation 'C' main function should Start OS and first task. First task will:-

initialise system ready for other tasks initialise I/O create and initialise global systems feature such as :

mutexes semaphores filesystems etc

Start the other tasks Terminate self

Now other tasks run & operate concurrently.

25

Page 26: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

A first example - RTX01.c Before any of the RTX functions can be used the

RTX must be started. os_sys_init (taskname); This function initialises and starts the RTX and then

starts the taskname task. The initial task is given the default priority of 1 NOTES:

The os_sys_init function must be called from the main C function.

The os_sys_init function NEVER returns!

26

Page 27: Real-Time Library: RTX AG & AH 1. What is an RTOS?  An RTOS is a special version of an operating system that provides a more deterministic behaviour

27

__task void begin (void) { os_tsk_prio_self(10); /* increase priority of this task */ /* initialise system peripherals etc. here */ t1_ID = os_tsk_create (task1, 4); /* start task1 */ t2_ID = os_tsk_create (task2, 2); /* start task2 */ t3_ID = os_tsk_create (task3, 1); /* start task3 */ os_tsk_delete_self ();}

int main (void) { uint32_t volatile start; /* Wait for debugger connection */ for (start = 0; start < 1000000; start++) { ; } os_sys_init (begin);/* Initialize OS and start the initial task */}

Starting the OS and the application tasks.#include <RTL.h>/* ... */OS_TID t1_ID, t2_ID, t3_ID; /* create task IDs */