Chapter 1 Getting Started with COSII 1 User

  • Slides: 69
Download presentation
Chapter 1: Getting Started with μC/OS-II 1

Chapter 1: Getting Started with μC/OS-II 1

User mode (0 -3 G) μC/OS-II Linux Task (thread) kernel Device driver Task (process)

User mode (0 -3 G) μC/OS-II Linux Task (thread) kernel Device driver Task (process) kernel (Kernel mode) 3 G-4 G Introduction Device driver 2

Introduction • μC/OS-II – Micro-Controller Operating Systems, Version 2 – A very small real-time

Introduction • μC/OS-II – Micro-Controller Operating Systems, Version 2 – A very small real-time kernel. • Memory footprint is about 20 KB for a fully functional kernel. • Source code is about 5, 500 lines, mostly in ANSI C. • It’s source is open but not free for commercial usages. 3

Introduction • μC/OS-II – Preemptible priority-driven real-time scheduling. • 64 priority levels (max 64

Introduction • μC/OS-II – Preemptible priority-driven real-time scheduling. • 64 priority levels (max 64 tasks) • 8 (/1/2) reserved for μC/OS-II • Each task is an infinite loop. – Deterministic execution times for most μC/OS-II functions and services. – Nested interrupts could go up to 256 levels. 4

Introduction • μC/OS-II – Supports of various 8 -bit to 64 -bit platforms: x

Introduction • μC/OS-II – Supports of various 8 -bit to 64 -bit platforms: x 86, 68 x, MIPS, 8051, etc – Easy for development: Borland C++ compiler and DOS (optional). • However, μC/OS-II still lacks of the following features: – Resource synchronization protocols. – Sporadic task support. – Soft real-time support. 5

Introduction • Getting started with μC/OS-II! – See how a μC/OS-II program looks like.

Introduction • Getting started with μC/OS-II! – See how a μC/OS-II program looks like. – Learn how to write a skeleton program for μC/OS-II. • How to initialize μC/OS-II? • How to create tasks? • How to use inter-task communication mechanisms? • How to catch system events? 6

Getting started with μC/OS-II • Example 1: Multitasking • Example 2: Stack Checking •

Getting started with μC/OS-II • Example 1: Multitasking • Example 2: Stack Checking • Example 3: Extension of μC/OS-II • Example 4: Portability 7

Example 1: Multitasking 8

Example 1: Multitasking 8

main OSInit Install context switch handler OSSem. Create DOS (bootloader) OSTask. Create OSStart Task.

main OSInit Install context switch handler OSSem. Create DOS (bootloader) OSTask. Create OSStart Task. Start μC/OS-II (Multiprogramming) install tick ISR Create other tasks Task 10 OSSem. Pend random OSSem. Post OSTime. Dly longmp Exit? OSTime. Dly. HMSM . . . 9

Example 1: Multitasking • 13 tasks run concurrently. – 2 internal tasks: The idle

Example 1: Multitasking • 13 tasks run concurrently. – 2 internal tasks: The idle task and the statistic task. – 11 user tasks: Randomly print numbers onto the screen. • Focus: System initialization and task creation. 10

Example 1: Multitasking • Files – The main program (test. c) – The big

Example 1: Multitasking • Files – The main program (test. c) – The big include file (includes. h) – The configuration of μC/OS-II (os_cfg. h) for each application • Tools needed: – Borland C++ compiler (V 3. 1+) 11

The μC/OS-II File Structure Application Code (test. c) Processor independent implementations • Scheduling policy

The μC/OS-II File Structure Application Code (test. c) Processor independent implementations • Scheduling policy • Event flags • Semaphores • Mailboxes • Event queues • Task management • Time management • Memory management Application Specific Configurations OS_CFG. H • Max # of tasks • Max Queue length • … μC/OS-II port for processor specific codes Software Hardware CPU Timer 12

includes. h 13

includes. h 13

OS_CFG. H . . 14

OS_CFG. H . . 14

test. c A semaphore (explain later) Stacks (explain later) 15

test. c A semaphore (explain later) Stacks (explain later) 15

test. c: main() 16

test. c: main() 16

main OSInit Install context switch handler OSSem. Create DOS OSTask. Create OSStart Task. Start

main OSInit Install context switch handler OSSem. Create DOS OSTask. Create OSStart Task. Start Multiprogramming install tick ISR Create other tasks Task 10 OSSem. Pend random OSSem. Post OSTime. Dly longmp Exit? OSTime. Dly. HMSM . . . 17

OSInit() • Internal structures of μC/OS-II. – – Task ready list. Priority table. Task

OSInit() • Internal structures of μC/OS-II. – – Task ready list. Priority table. Task control blocks (TCB). Free pools. • Create housekeeping tasks. – The idle task. – The statistic task. 18

OSinit() 19

OSinit() 19

OSinit() 20

OSinit() 20

The PC IVT • Interrupt vector table (IVT) Before (DOS only) After (μC/OS-II installed)

The PC IVT • Interrupt vector table (IVT) Before (DOS only) After (μC/OS-II installed) 21

PC_DOSSave. Return() • Save the current status of DOS for the future restoration. –

PC_DOSSave. Return() • Save the current status of DOS for the future restoration. – Interrupt vectors and the RTC tick rate. • Set a global returning point by calling setjmp(). – μC/OS-II can come back here when it terminates. – PC_DOSReturn() 22

PC_DOSSave. Return() 23

PC_DOSSave. Return() 23

setjmp +longjmp ≒ goto 24

setjmp +longjmp ≒ goto 24

longjmp 25

longjmp 25

PC_Vect. Set() • PC_Vect. Set(u. COS, OSCtx. Sw) – Install the context switch handler

PC_Vect. Set() • PC_Vect. Set(u. COS, OSCtx. Sw) – Install the context switch handler (OSCtx. Sw). – Interrupt 0 x 08 (u. COS) under 80 x 86 family. • Invoked by INT instruction. Disable interrupt Enable interrupt 26

OSSem. Create() • Create a semaphore for resource synchronization. – To protect non-reentrant codes.

OSSem. Create() • Create a semaphore for resource synchronization. – To protect non-reentrant codes. • The created semaphore becomes a mutual exclusive mechanism if “ 1” is given as the initial value. • In this example, a semaphore is created to protect the standard C library “random()”. 27

OSTask. Create() • Functionality – Create tasks with the given arguments. – Tasks become

OSTask. Create() • Functionality – 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. • The μC/OS-II picks up the highest-priority task to run on context-switching. – Tightly coupled with ISR. 28

OSTask. Create() • OSTask. Create( Entry point of the task (a pointer to function)

OSTask. Create() • OSTask. Create( Entry point of the task (a pointer to function) User-specified Task. Start, data (void *)0, &Task. Start. Stk[TASK_STK_SIZE - 1], 0 Top of Stack Priority ); (0=hightest) 29

OSStart() • OSStart() – Start multitasking of μC/OS-II. – It never returns to main().

OSStart() • OSStart() – Start multitasking of μC/OS-II. – It never returns to main(). – μC/OS-II is terminated if PC_DOSReturn() is called. 30

main OSInit Install context switch handler OSSem. Create DOS OSTask. Create OSStart Task. Start

main OSInit Install context switch handler OSSem. Create DOS OSTask. Create OSStart Task. Start Multiprogramming install tick ISR Create other tasks Task 10 OSSem. Pend random OSSem. Post OSTime. Dly longmp Exit? OSTime. Dly. HMSM . . . 31

OSTask. Create( Task. Start, (void *)0, &Task. Start. Stk[TASK_STK_SIZE - 1], 0); Task. Start()

OSTask. Create( Task. Start, (void *)0, &Task. Start. Stk[TASK_STK_SIZE - 1], 0); Task. Start() void Task. Start (void *pdata) for (i=0 to 9) { OSTask. Ceate } Wait one second 32

Task. Start() • OS_ENTER_CRITICAL()/OS_EXIT_CRITICAL() – Enable/disable most interrupts. – An alternative way to accomplish

Task. Start() • OS_ENTER_CRITICAL()/OS_EXIT_CRITICAL() – Enable/disable most interrupts. – An alternative way to accomplish mutual exclusion. • No rescheduling is possible during the disabling of interrupts. (different from semaphores) – Processor specific. • CLI/STI (x 86 real mode) • Interrupt descriptors (x 86 protected mode) 33

Task. Start. Create. Tasks() Entry point of the created task Argument: character to print

Task. Start. Create. Tasks() Entry point of the created task Argument: character to print Stack Priority 34

Task() Semaphore operations. 35

Task() Semaphore operations. 35

main OSInit Install context switch handler OSSem. Create DOS OSTask. Create OSStart Task. Start

main OSInit Install context switch handler OSSem. Create DOS OSTask. Create OSStart Task. Start Multiprogramming install tick ISR Create other tasks Task 10 OSSem. Pend random OSSem. Post OSTime. Dly longmp Exit? OSTime. Dly. HMSM . . . 36

Semaphores • A semaphore consists of a wait list and an integer counter. –

Semaphores • A semaphore consists of a wait list and an integer counter. – OSSem. Pend(): • Counter- • If the value of the semaphore < 0, then the task is blocked and moved to the wait list immediately. • A time-out value can be specified. – OSSem. Post(): • Counter++ • If the value of the semaphore ≧ 0, then a task in the wait list is removed from the wait list. • Reschedule if needed. 37

Example 1: Multitasking Summary: • μC/OS-II is initialized and started by calling OSInit() and

Example 1: Multitasking Summary: • μC/OS-II is initialized and started by calling OSInit() and OSStart(), respectively. • Before μC/OS-II is started, – The DOS status is saved by calling PC_DOSSave. Return(). – A context switch handler is installed by calling PC_Vect. Set(). – One user task must be created first! • Shared resources can be protected by semaphores. – OSSem. Pend(), OSSem. Post(). 38

Example 2 39

Example 2 39

main OSInit Install context switch handler OSTask. Stk. Init_FPE_X 86 & OSTask. Create. Ext

main OSInit Install context switch handler OSTask. Stk. Init_FPE_X 86 & OSTask. Create. Ext OSStart DOS Task. Start Multiprogramming install tick ISR Create other tasks Update the display Task 1 Task 4 Task 5 OSTask. Stk. Chk OSMbox. Post &OSMobx. Pend OSMbox. Post 40

Example 2: Stack Checking • Five tasks do jobs on message sending/receiving. – More

Example 2: Stack Checking • Five tasks do jobs on message sending/receiving. – More task creation options • Better judgment on stack sizes – Stack usage of each task • Different stack sizes for tasks – Emulation of floating point operations • 80386 or lower-end CPU’s – Communication through mailbox • Only the pointer is passed. 41

2 Mailboxes 42

2 Mailboxes 42

Main() OSTask. Stk. Init_FPE_x 86(&ptos, &pbos, &size) 43

Main() OSTask. Stk. Init_FPE_x 86(&ptos, &pbos, &size) 43

OSTask. Stk. Init_FPE_x 86() • OSTask. Stk. Init_FPE_x 86(&ptos, &pbos, &size) – Passing the

OSTask. Stk. Init_FPE_x 86() • OSTask. Stk. Init_FPE_x 86(&ptos, &pbos, &size) – Passing the original top address, bottom address, and size of the stack. – On the return, arguments are modified, and some stack space are reserved for the floating point library. ptos • For context switches. size pbos 44

OSCreate. Task. Ext() • OSTask. Creat. Ext( Task. Start, (void *)0, ptos, TASK_START_PRIO, TASK_START_ID,

OSCreate. Task. Ext() • OSTask. Creat. Ext( Task. Start, (void *)0, ptos, TASK_START_PRIO, TASK_START_ID, pbos, size, User supplied data which can be used to extend TCB (void *)0, OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR ); options 45

Task. Start() Create 2 mailboxes The dummy loop wait for ‘ESC’ 46

Task. Start() Create 2 mailboxes The dummy loop wait for ‘ESC’ 46

Task 1() 47

Task 1() 47

Task 2 and Task 3 for (i=0; i<499; i++) { dymmy[i] = ‘? ’;

Task 2 and Task 3 for (i=0; i<499; i++) { dymmy[i] = ‘? ’; } 48

Task 4 and Task 5 ? ? ? OSTime. Dly. HMSM(0, 0, 1, 0);

Task 4 and Task 5 ? ? ? OSTime. Dly. HMSM(0, 0, 1, 0); 49

Mail. Box • A mailbox is for data exchanging between tasks. – A mailbox

Mail. Box • A mailbox is for data exchanging between tasks. – A mailbox consists of a data pointer and a wait-list. • OSMbox. Pend(): – 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. • OSMbox. Post(): – 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. 50

OSTask. Stk. Check() • Check for stack overflow. – bos < (tos – stack

OSTask. Stk. Check() • Check for stack overflow. – bos < (tos – stack length) – Local variables, arguments for procedure calls, temporary storage for ISR’s. – μC/OS-II can check for stack overflow on the creation of tasks and when OSTask. Stk. Check() is called. – μC/OS-II does not automatically check stacks. 51

(Linux 2. 6) stack User mode Kernel mode (kernel service routines) Kernel mode (interrupt

(Linux 2. 6) stack User mode Kernel mode (kernel service routines) Kernel mode (interrupt service routines) stack 52

(Linux 2. 4) stack User mode Kernel mode (kernel service routines) stack Kernel mode

(Linux 2. 4) stack User mode Kernel mode (kernel service routines) stack Kernel mode (interrupt service routines) 53

μC/OS-II Kernel mode (kernel service routines) stack Kernel mode (interrupt service routines) 54

μC/OS-II Kernel mode (kernel service routines) stack Kernel mode (interrupt service routines) 54

Example 2: Stack Checking • Summary: – Local variable, function calls, and ISR’s will

Example 2: Stack Checking • Summary: – Local variable, function calls, and ISR’s will utilize the stack space of user tasks. – ISR will use the stack of the interrupted task. – If floating-point operations are needed, then some stack space should be reserved. – Mailboxes can be used to synchronize the work of tasks. 55

Example 3: Extension of μC/OS-II • A Pointer to from the TCB of each

Example 3: Extension of μC/OS-II • A Pointer to from the TCB of each task to a user-provided data structure – Passing user-specified data structures on task creations or have application-specific usage. • Message queues – More than one pointers • Demonstration on how to use OS hooks to receive/process desired event from the μC/OS -II 56

Example 3 57

Example 3 57

User-defined data structure to pass to tasks Message queue and an array of event

User-defined data structure to pass to tasks Message queue and an array of event 58

59

59

61

61

Task 2, 3, 4 are functionally identical. 62

Task 2, 3, 4 are functionally identical. 62

Message Queues • A message queue consists of an array of elements and a

Message Queues • A message queue consists of an array of elements and a waitlist. • 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 highestpriority 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 waitlist and becomes blocked. 63

Hooks • A hook function will be called by μC/OS-II when the corresponding event

Hooks • A hook function will be called by μC/OS-II when the corresponding event occurs. – Event handlers could be in user programs. – For example, OSTask. Sw. Hook () is called every time when context switch occurs. • The hooks are specified in the compiling time in μC/OSII : – μC/OS-II is an embedded OS. • OS_CFG. H (OS_CPU_HOOKS_EN = 0) – Many OS’s can register and un-register hooks. 64

User Customizable Hooks for μC/OS-II • • • void void void OSInit. Hook. Begin

User Customizable Hooks for μC/OS-II • • • void void void OSInit. Hook. Begin (void) OSInit. Hook. End (void) OSTask. Create. Hook (OS_TCB *ptcb) OSTask. Del. Hook (OS_TCB *ptcb) OSTask. Idle. Hook (void) OSTask. Stat. Hook (void) OSTask. Sw. Hook (void) OSTCBInit. Hook (OS_TCB *ptcb) OSTime. Tick. Hook (void) 65

OSTask. Stat. Hook() 66

OSTask. Stat. Hook() 66

OSTask. Sw. Hook() Elapsed time for the current task OSTCBCur TCB of the current

OSTask. Sw. Hook() Elapsed time for the current task OSTCBCur TCB of the current task OSTCBHigh. Rdy TCB of the new task 67

Example 3: Extension of μC/OS-II • Summary: – Message queues can be used to

Example 3: Extension of μC/OS-II • Summary: – Message queues can be used to synchronize among tasks. • Multiple messages can be held in a queue. • Multiple tasks can “pend”/“post” to message queues simultaneously. – Hooks can be used to do some user-specific computations on certain OS events occurs. • They are specified in the compiling time. • A Pointer to from the TCB of each task to a user-provided data structure 68

Getting Started with μC/OS-II? Getting Started with μC/OS-II : – How the control flows

Getting Started with μC/OS-II? Getting Started with μC/OS-II : – How the control flows among procedures? – How tasks are created? – How tasks are synchronized by semaphore, mailbox, and message queues? – How the space of a stack is utilized? – How to capture system events? – How to write a dummy μC/OS-II program? 69