Sunteți pe pagina 1din 26

uCOS II

RTOS

BY: Meet

Advantage/Disadvantage of Using RTOS


Advantage
Split application to multi tasks, simplify the design, easy to be understand, expend and maintenance Time latency is guaranteed Higher system reliability

Disadvantage
More RAM/ROM usage 2~5% CPU overhead

What is uCOS II?


Micro-Controller Operating Systems, Version 2 A very small real-time kernel. Memory footprint is about 20KB for a fully functional kernel.

Feature of uC/OSII
Portable : Preemptive : portable C, minimum uC dependent ASM uC/OS-II is a fully preemptive real-time kernel ROMable & Scalable Multitasking : uC/OS-II can manage up to 64 tasks Deterministic : Execution time of all uC/OS-II functions and service are deterministic Deterministic : Each task requires its own different stack size Services : Mailboxes, Queues, Semaphores, fixed-sized memory partitions, time-related functions Int. can be nested up to 255 levels deep Robust and Reliable

Interrupt Management

Real Time Kernel on uCOS

Task states
C/OS-II is a multitasking operating system. Each task is an infinite loop and can be in any one of the following five states:

Dormant Ready Running Waiting Interrupt service routine

Term
Multitasking
The process of scheduling and switching the CPU between several tasks

Task
A simple program which thinks it has the CPU all to itself.

Kernel
The part of the multitasking system responsible for the management of tasks and communication between tasks

Task Switch
Kernel saves the current task's context (CPU registers) onto the current task's stack. Kernel load new task's context from the tasks stack of new task

TCB
Task Control Block

Scheduler
Also called the dispatcher, is the part of the kernel which is responsible for determining which task will run next

Non-preemptive scheduling
Each task is required to explicitly give up control of the CPU. (ex. System API Call) cooperative multitasking; tasks cooperate with each other to gain control of the CPU.

Preemptive Kernel In it, when an event makes a higher priority task ready to run, the current task is immediately suspended and the higher priority task is given control of the CPU Reentrancy A reentrant function is a function which can be used by more than one task without fear of data corruption.

uC/OS II Overview
Externa l IRQ TaskA TaskB { () { () {
Task Scheduler

Task C()

IRQ1 IRQ2 IRQ3

Timer

MultiTasking Overview

OSTaskCreate

Create tasks with the given arguments. Tasks become ready after they are created. Task

An active entity which could do some computations. Priority, CPU registers, stack, text, housekeeping status. Task never return;
int main (void) { OSInit(); OSTaskCreate(); OSTaskCreate(); OSStart(); return 0; // Never return }

Task Function

INT8U OSTaskCreate (void (*task)(void *pd), void *pdata, Private Data OS_STK *ptos, For Task INT8U prio)
Priority of created task Note that OSTaskCreate can be called by main when OSStart is not invoked, but it can also be called in tasks when more task should be created at the run time Stack For Task

INT8U OSTaskCreateExt(void (*task)(void *pd),void *pdata,OS_STK *ptos,INT8U prio,INT16U id,OS_STK *pbos,INT32U stk_size,void *pext,INT16U opt)

task-Pointer to task's code. pdata-Pointer to optional data area; used to pass parameters to the task at start of execution. prio-The task's unique priority number; the lower the number the higher the priority. id-The task's identification number . stk_size-Size of the stack in number of elements. If OS_STK is set to INT8U, stk_size corresponds to the number of bytes available. If OS_STK is set to INT16U, stk_size contains the number of 16-bit entries available. Finally, if OS_STK is set to INT32U, stk_size contains the number of 32-bit entries available on the stack. pext-Pointer to a user-supplied Task Control Block (TCB) extension. opt-The lower 8 bits are reserved by C/OS-II. The upper 8 bits control application-specific options. Select an option by setting the corresponding bit(s).

W A IT IN G

O S T a s k D e l( )

O O O O O O O

S S S S S S S

M Q Q S T T T

B o x P o s t()O S P o s t() O S P o s tF ro n t() e m P o s t() O S a s k R e s u m eO ( S) i m e D l y R e s Ou mS im e T ic k ( ) O S

M B o x P e n d () Q P e n d () S e m T a s T e i (m ) T im P e n k S u s e D ly e D ly d () p e n d () () H M S M ()

O S T a s k C re a te () O S T a s k C re a te E x t()

D O R M A N T

R E A D Y

O S S ta rt() O S In t E x it ( ) O S _T A S K _S W ()

In t e r r u p t

R U N N IN G
O S In t E x it ( )

IS R

O S T a s k D e l( ) T a s k is P r e e m p t e d O S T a s k D e l( )

Semaphore

Multiple concurrent threads of execution within an application must be able to


Synchronize their execution Coordinate mutually exclusive access to shared resources

RTOS provides a semaphore object and associated semaphore management services

Semaphore operations now defined as: wait(S)


void wait(semaphoreS) { S.value--; if (S.value< 0) then { add this process to S.L; block(); } }

signal(S)
void signal(semaphoreS) { S.value++; if (S.value<= 0){ remove a process Pfrom S.L; wakeup(P); } }

Deadlock:
Let S and Q be two semaphores, initialized to 1. P0: wait(S) pass P1: wait(Q) pass P0: wait(Q) block P1: wait(S) block

When a semaphore is first created, kernel assigns


An semaphore control block (SCB) A unique ID A value (binary or a count) A task-waiting list

Different types of semaphores

Binary semaphores

0: the semaphore is considered unavailable 1: the semaphore is considered available

Counting semaphores Mutual-exclusion (mutex) semaphores

Mutual Exclusion Semaphores(Mutex)

A special binary semaphore that supports


Ownership Recursive access Task deletion safety One or more protocols avoiding problems inherent to mutual exclusions Locked(1) and unlocked (0)

The states of a mutex

recursive mutex

A mailbox is for data exchanging between tasks.

Mail Box

A mailbox consists of a data pointer and a wait-list. The message in the mailbox is retrieved. If the mailbox is empty, the task is immediately blocked and moved to the wait-list. A time-out value can be specified. A message is posted in the mailbox. If there is already a message in the mailbox, then an error is returned (not overwritten). If tasks are waiting for a message from the mailbox, then the task with the highest priority is removed from the wait-list and scheduled to run.

OSMboxPend():

OSMboxPost():

Message Qeue

A message queue consists of an array of elements and a wait-list. Different from a mailbox, a message queue can hold many data elements (in a FIFO basis). As same as mailboxes, there can be multiple tasks pend/post to a message queue. OSQPost(): a message is appended to the queue. The highest-priority pending task (in the wait-list) receives the message and is scheduled to run, if any. OSQPend(): a message is removed from the array of elements. If no message can be retrieved, the task is moved to the wait-list and becomes blocked.

Message qeue

The state diagram for a message queue:

THANK YOU

S-ar putea să vă placă și