Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Real-Time Library: RTX AG & AH 1

2 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

3 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 }

4 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

5 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

6 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

7 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

8 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 Queue of tasks. Task at front of queue selected to run Round Robin time out. Task put on end of ready queue. CPU 8

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

10 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. CPU Task 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

11 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. T1T2T3T2T3T2T1 Time ms T1 delays itself for 180ms 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

12 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

13 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. T1T2T1T3T2T1T2 Time ms Time 125ms: T3 becomes ready and joins ready queue 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

14 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

15 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. T1T2T1 T3 T2T1T2 Time ms Time 125ms: T3 becomes ready and immediately pre-empts the currently running task. because it is higher priority 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

16 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

17 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

18 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  The time tick value specifies the interval of the periodic RTX interrupt. The value us configures timer tick period of 0.01 seconds. 18

19 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  C reate 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

20 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 task t1_ID = os_task_create (task1, 4); priority is from 1 to 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

21 Other Task Management Functions os_tsk_delete - Terminates a selected task os_tsk_delete_self -Terminates the running ta sk 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

22 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

23 Time Management functions void 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(); }

24 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

25 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

26 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

27 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 < ; start++) { ; } os_sys_init (begin);/* Initialize OS and start the initial task */ } Starting the OS and the application tasks. #include /*...*/ OS_TID t1_ID, t2_ID, t3_ID; /* create task IDs */


Download ppt "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."

Similar presentations


Ads by Google