getting started with the µc/os-iii real time kernel akos ledeczi eece 6354, fall 2015 vanderbilt...

13
Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University

Upload: malcolm-oliver

Post on 01-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University

Getting Started with theµC/OS-III

Real Time Kernel

Akos LedecziEECE 6354, Fall 2015Vanderbilt University

Page 2: Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University

Multitasking

• Also called multithreading or concurrent programming• Multiple, sequential tasks (or threads)

– Creating the illusion of having multiple CPUs– The task body is just a C function

• Each task has its own stack– The same body can be reused for multiple tasks

• Synchronization and communication are very important and complicated

• Advantages:– Modular code– Manages complexity inherent in RT systems– Cleaner and easier to understand and maintain

Page 3: Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University

Multitasking cont’d.• Because of the need to respond to timing demands made by different stimuli/responses,

the system architecture must allow for fast switching between stimulus handlers.• Because of different priorities, unknown ordering and different timing requirements of

different stimuli, a simple sequential loop is not usually adequate.• Real-time systems are therefore usually designed as cooperating processes with a real-time

kernel controlling these processes.

Page 4: Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University

What is an RTK?• The main task of an RTK is to manage time and the resources of a

(typically embedded) computer – Multitasking

• Creation• Scheduling• Synchronization• Communication

– Resource management:• Memory• IO

– Interrupt management– Time management

• What is an RTOS?– An RTK plus higher level services such as file system, networking, GUI, etc.

Page 5: Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University

µC/OS-III: Creating and Initializing an App

• Start in main():– Disable interrupts– Initialize OS– Create a single Task using TaskCreate()– Start OS: start multitasking and switch to the

highest priority enabled task

Page 6: Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University

OS Initialization

• Initializes internal data structures• Creates up to 5 Tasks:– Idle Task (lowest priority task that runs when

nothing else is available for running; it never blocks)

– Tick Task (keeps track of time)– Statistics (optional)– Timer (optional)– Interrupt queue management (optional)

Page 7: Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University

Task Creation• OSTaskCreate()• 13 arguments:– Task Control Block (TCB): data structure that the OS uses

to manage the task and store all relevant info about it (e.g. stack pointer, priority, pointers to manage queues, etc.)

– Name– Function pointer to actual code– Argument for the task (e.g., a pointer to a task-specific

memory making the C function reusable for multiple tasks)– Stack pointer, watermark, size– Error code– Etc.

Page 8: Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University

Initial task

• Initialize hardware and CPU related things• Set up tick interrupt (rate =

OSCfg_TickRateHz)• Enable interrupts• Create additional tasks (optional)• Infinite loop:– Inside there must be a blocking call

Page 9: Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University

Critical Sections

• Code that needs to run indivisibly– Access to shared resources, for example,

hardware device, shared variable, data structures, etc.

• How?• Disable interrupts• Lock the scheduler• Use semaphores• Finer control (on a task by task basis)• More overhead

Page 10: Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University

Semaphores• Dijkstra in 1959• “Key” to “locked code.” You need to acquire it to access the code.• Semaphore operations are “atomic.”• Binary and counting semaphores• Can be used for resource sharing and synchronization• Functions:

OSSemCreate()

OSSemPend()

OSSemPost()

OSSemDel()

OSSemSet()

OSSemPendAbort()

Page 11: Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University

Binary Semaphores

• Accessing a printer

• Hiding behind an API

Page 12: Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University

Binary Semaphores cont’d.OS_SEM MySem;

void main()

{

OS_ERR err;

OSSemCreate(&MySem, ”My Semaphore”, 1, &err);

}

void Task1 (void *p_arg)

OS_ERR err;

CPU_TS ts;

while (1) {

OSSemPend(&MySem, 0, OS_OPT_PEND_BLOCKING, &ts, &err);

/*critical section */

OSSemPost(&MySem, OS_OPT_POST_1, &err);

/* check err */

}

}

Page 13: Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University

Counting Semaphores

• When multiple resources/resource elements are available

• E.g., memory pool or circular buffer• Initialize semaphore to the number of

items available• Need to manage consumed/available

items• Pend() waits on 0, otherwise,

decrements counter and returns• Post() increments counter