1 / 27

Real-Time Library: RTX

Real-Time Library: RTX. AG & AH. 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

fallon
Download Presentation

Real-Time Library: RTX

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Real-Time Library:RTX AG & AH

  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

  3. What is a Task? void task1(void) { while(1) { // do task 1 } } void task2(void) { while(1) { // do task 2 } } void task3(void) { while(1) { // do task 3 } } 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. 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.

  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.

  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.

  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

  8. Simple Round Robin Scheduler CPU Task at front of queue selected to run The running task Round Robin time out. Task put on end of ready queue. Ready Queue of tasks. All tasks have the same priority and tasks run in turn on the CPU for a given number of "time quanta" or "ticks".

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

  10. Round Robin with task waiting queues 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 The running task Round Robin time out Delayed and periodic tasks Queue of ready to run tasks Tasks waiting for mutex, semaphore or mailbox Tasks waiting for event(s) 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.

  11. Example with three tasks T1, T2 and T3 T1 T2 T3 T2 T3 T2 T1 T3 T2 Time ms 0 50 100 150 200 250 300 350 T1 delays for 180ms T1run after T2. Remember it joins the end of the queue. T1 now joins ready queue, at time 230ms. T1 delays itself for 180ms 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.

  12. Round Robin with Priority scheduling CPU Highest priority task is selected when CPU free. Running task doesn't need to run for a certain time so relinquishes the CPU. The running task Round Robin time up Queue of ready Tasks queue sorted into priority order Delayed and periodic tasks Tasks waiting for mutex, semaphore or mailbox Tasks waiting for event(s) Tasks becomes ready and rejoin queue of ready tasks

  13. Round Robin with Priority Scheduling T1 T2 T1 T3 T2 T1 T2 T1 Time ms 0 50 100 150 200 250 300 350 Time 125ms:T3 becomes ready and joins ready queue. Time 180ms: T3 runs for 30ms then relinquishes CPU and joins a waiting queue again. Scheduler runs next task from the ready queue. Time 150ms:T3 now runs because highest priority ready task. 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.

  14. Round Robin Priority scheduling with pre-emption. Pre-empt running task if a higher priority task is ready. CPU Highest priority task is selected when CPU free. The running task Running task doesn't need to run for a certain time so relinquishes the CPU. Round Robin time up Queue of ready Tasks queue sorted into priority order Delayed and periodic tasks Tasks waiting for mutex, semaphore or mailbox Tasks waiting for event(s) Tasks becomes ready and rejoin queue of ready tasks

  15. Round Robin Priority scheduling with pre-emption. Priority 6 T3 5 4 T1 T2 T1 T1 T2 T1 T2 T1 Time ms 0 50 100 150 200 250 300 350 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. Time 125ms:T3 becomes ready and immediately pre-empts the currently running task. because it is higher priority. E.g.Tasks T1, T2 with priority 4 both in the ready queue and T3 with priority 6 is in a waiting queue.

  16. The ARM RL-RTX • 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. #include <RTL.h> • There are 35 RTX related functions providing facilities for: • task creation and management • time management • events • mutexes • semaphores • mailboxes We will be using a Real Time eXecutive with the ARM based board Keil MCB 2300

  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.

  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 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.

  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 • 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;

  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 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.

  21. 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

  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 */ } }

  23. Time Management functions – • void task1 (void) __task { • os_itv_set(20); • while(1) • { • FIO2PIN ^= LED_A; // toggle led • os_itv_wait(); • } • } • FIO2SET = LED_A; • os_dly_wait(10); • FIO2CLR = LED_A; 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

  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_getRetrieves the event flags that caused os_evt_wait_or to complete. os_evt_setSets one or more event flags of a task. os_evt_wait_andWaits for one or more event flags to be set. os_evt_wait_orWaits for any one event flag to be set. isr_evt_setSets one or more event flags of a task. Better inter-task communication methods are available - coming soon!

  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.

  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!

  27. Starting the OS and the application tasks. #include <RTL.h> /* ... */ OS_TID t1_ID, t2_ID, t3_ID; /* create task IDs */ 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 */ } __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 (); }

More Related