Table Of ContentμC/ OS-III
TM
The Real-Time Kernel
User’s Manual
Weston, FL 33326
Micriμm Press
1290 Weston Road, Suite 306
Weston, FL 33326
USA
www.micrium.com
Designations used by companies to distinguish their products are often claimed as trademarks.
In all instances where Micriμm Press is aware of a trademark claim, the product name appears in
initial capital letters, in all capital letters, or in accordance with the vendor’s capatilization
preference. Readers should contact the appropriate companies for more complete information
on trademarks and trademark registrations. All trademarks and registerd trademarks in this book
are the property of their respective holders.
Copyright © 2010 by Micriμm Press except where noted otherwise. Published by Micriμm Press.
All rights reserved. Printed in the United States of America. No part of this publication may be
reproduced or distributed in any form or by any means, or stored in a database or retrieval
system, without the prior written permission of the publisher; with the exception that the
program listings may be entered, stored, and executed in a computer system, but they may not
be reproduced for publication.
The programs and code examples in this book are presented for instructional value. The
programs and examples have been carefully tested, but are not guaranteed to any particular
purpose. The publisher does not offer any warranties and does not guarantee the accuracy,
adequacy, or completeness of any information herein and is not responsible for any errors and
ommissions. The publisher assumes no liability for damages resulting from the use of the
information in this book or for any infringement of the intellectual property rights of third parties
that would result from the use of this information.
For bulk orders, please contact Micrium Press at: +1 954 217 2036
ISBN: 978-0-9823375-9-2
600-uCOS-III-Users-Manual-002
Table of Contents
Preface..................................................................................................13
Chapter 1 Introduction ..........................................................................................15
1-1 Foreground/Background Systems ......................................................16
1-2 Real-Time Kernels ................................................................................17
1-3 RTOS (Real-Time Operating System) ..................................................19
1-4 μC/OS-III ...............................................................................................19
1-5 μC/OS, μC/OS-II and μC/OS-III Features Comparison ......................24
1-6 How the Book is Organized .................................................................26
1-7 μC/Probe ..............................................................................................26
1-8 Conventions .........................................................................................27
1-9 Chapter Contents .................................................................................28
1-10 Licensing ..............................................................................................32
1-11 Contacting Micrium ..............................................................................32
Chapter 2 Directories and Files ............................................................................33
2-1 Application Code .................................................................................36
2-2 CPU .......................................................................................................37
2-3 Board Support Package (BSP) ............................................................38
2-4 μC/OS-III, CPU Independent Source Code ........................................39
2-5 μC/OS-III, CPU Specific Source Code ................................................43
2-6 μC/CPU, CPU Specific Source Code ..................................................44
2-7 μC/LIB, Portable Library Functions .....................................................46
2-8 Summary ..............................................................................................47
Chapter 3 Getting Started with μC/OS-III ............................................................51
3-1 Single Task Application .......................................................................52
3-2 Multiple Tasks Application with Kernel Objects .................................60
3
Table of Contents
Chapter 4 Critical Sections ...................................................................................69
4-1 Disabling Interrupts ..............................................................................70
4-1-1 Measuring Interrupt Disable Time .......................................................70
4-2 Locking the Scheduler .........................................................................71
4-2-1 Measuring Scheduler Lock Time .........................................................72
4-3 μC/OS-III Features with Longer Critical Sections ...............................73
4-4 Summary ..............................................................................................74
Chapter 5 Task Management ...............................................................................75
5-1 Assigning Task Priorities .....................................................................84
5-2 Determining the Size of a Stack ..........................................................86
5-3 Detecting Task Stack Overflows .........................................................87
5-4 Task Management Services ................................................................91
5-5 Task Management Internals ................................................................92
5-5-1 Task States ...........................................................................................92
5-5-2 Task Control Blocks (TCBs) .................................................................97
5-6 Internal Tasks .....................................................................................106
5-6-1 The Idle Task (OS_IdleTask()) ............................................................107
5-6-2 The Tick Task (OS_TickTask()) ..........................................................109
5-6-3 The Statistic Task (OS_StatTask()) ....................................................116
5-6-4 The Timer Task (OS_TmrTask()) ........................................................119
5-6-5 The ISR Handler Task (OS_IntQTask()) .............................................120
5-7 Summary ............................................................................................121
Chapter 6 The Ready List ...................................................................................123
6-1 Priority Levels .....................................................................................124
6-2 The Ready List ...................................................................................128
6-3 Adding Tasks to the Ready List ........................................................131
6-4 Summary ............................................................................................132
Chapter 7 Scheduling ..........................................................................................133
7-1 Preemptive Scheduling ......................................................................134
7-2 Scheduling Points ..............................................................................136
7-3 Round-Robin Scheduling ..................................................................138
7-4 Scheduling Internals ..........................................................................141
7-4-1 OSSched() ..........................................................................................142
7-4-2 OSIntExit() ...........................................................................................143
4
7-4-3 OS_SchedRoundRobin() ....................................................................144
7-5 Summary ............................................................................................146
Chapter 8 Context Switching ..............................................................................147
8-1 OSCtxSw() ..........................................................................................150
8-2 OSIntCtxSw() ......................................................................................153
8-3 Summary ............................................................................................155
Chapter 9 Interrupt Management .......................................................................157
9-1 Handling CPU Interrupts ....................................................................158
9-2 Typical μC/OS-III Interrupt Service Routine (ISR) .............................159
9-3 Short Interrupt Service Routine (ISR) ................................................162
9-4 All Interrupts Vector to a Common Location ....................................163
9-5 Every Interrupt Vectors to a Unique Location ..................................165
9-6 Direct and Deferred Post Methods ...................................................166
9-6-1 Direct Post Method ............................................................................166
9-6-2 Deferred Post Method .......................................................................169
9-7 Direct vs. Deferred Post Method .......................................................172
9-8 The Clock Tick (or System Tick) ........................................................173
9-9 Summary ............................................................................................175
Chapter 10 Pend Lists (or Wait Lists) ...................................................................177
10-1 Summary ............................................................................................182
Chapter 11 Time Management .............................................................................183
11-1 OSTimeDly() ........................................................................................184
11-2 OSTimeDlyHMSM() ............................................................................189
11-3 OSTimeDlyResume() ..........................................................................191
11-4 OSTimeSet() and OSTimeGet() ..........................................................192
11-5 OSTimeTick() ......................................................................................192
11-6 Summary ............................................................................................192
Chapter 12 Timer Management ............................................................................193
12-1 One-Shot Timers ................................................................................195
12-2 Periodic (no initial delay) ....................................................................196
12-3 Periodic (with initial delay) .................................................................196
5
Table of Contents
12-4 Timer Management Internals .............................................................197
12-4-1 Timer Management Internals - Timers States ..................................197
12-4-2 Timer Management Internals - OS_TMR ...........................................198
12-4-3 Timer Management Internals - Timer Task .......................................200
12-4-4 Timer Management Internals - Timer List .........................................203
12-5 Summary ............................................................................................208
Chapter 13 Resource Management ......................................................................209
13-1 Disable/Enable Interrupts ..................................................................212
13-2 Lock/Unlock .......................................................................................214
13-3 Semaphores .......................................................................................215
13-3-1 Binary Semaphores ............................................................................217
13-3-2 Counting Semaphores .......................................................................224
13-3-3 Notes on Semaphores .......................................................................226
13-3-4 Semaphore Internals (for resource sharing) .....................................227
13-3-5 Priority Inversions ..............................................................................232
13-4 Mutual Exclusion Semaphores (Mutex) ............................................234
13-4-1 Mutual Exclusion Semaphore Internals ............................................239
13-5 Should You Use a Semaphore Instead of a Mutex? ........................245
13-6 Deadlocks (or Deadly Embrace) ........................................................245
13-7 Summary ............................................................................................250
Chapter 14 Synchronization .................................................................................251
14-1 Semaphores .......................................................................................252
14-1-1 Unilateral Rendezvous .......................................................................254
14-1-2 Credit Tracking ...................................................................................257
14-1-3 Multiple Tasks Waiting on a Semaphore ..........................................259
14-1-4 Semaphore Internals (for synchronization) .......................................260
14-2 Task Semaphore ................................................................................267
14-2-1 Pending (i.e., Waiting) on a Task Semaphore ...................................268
14-2-2 Posting (i.e., Signaling) a Task Semaphore ......................................269
14-2-3 Bilateral Rendezvous .........................................................................271
14-3 Event Flags .........................................................................................273
14-3-1 Using Event Flags ..............................................................................275
14-3-2 Event Flags Internals .........................................................................279
14-4 Synchronizing Multiple Tasks ............................................................286
14-5 Summary ............................................................................................288
6
Chapter 15 Message Passing ...............................................................................289
15-1 Messages ...........................................................................................290
15-2 Message Queues ...............................................................................290
15-3 Task Message Queue ........................................................................292
15-4 Bilateral Rendezvous .........................................................................293
15-5 Flow Control .......................................................................................294
15-6 Keeping the Data in Scope ................................................................296
15-7 Using Message Queues .....................................................................299
15-8 Clients and Servers ............................................................................307
15-9 Message Queues Internals ................................................................308
15-10 Summary ............................................................................................311
Chapter 16 Pending On Multiple Objects .............................................................313
16-1 Summary ............................................................................................321
Chapter 17 Memory Management ........................................................................323
17-1 Creating a Memory Partition .............................................................324
17-2 Getting a Memory Block from a Partition .........................................328
17-3 Returning a Memory Block to a Partition ..........................................329
17-4 Using Memory Partitions ...................................................................330
17-5 Summary ............................................................................................333
Chapter 18 Porting μC/OS-III ................................................................................335
18-1 μC/CPU ...............................................................................................338
18-2 μC/OS-III Port .....................................................................................341
18-3 Board Support Package (BSP) ..........................................................343
18-4 Summary ............................................................................................345
Chapter 19 Run-Time Statistics ............................................................................347
19-1 General Statistics – Run-Time ...........................................................348
19-2 Per-Task Statistics – Run-Time .........................................................352
19-3 Kernel Object – Run-Time ..................................................................355
19-4 OS_DBG.C – Static ............................................................................358
19-5 OS_CFG_APP.C – Static ....................................................................371
19-6 Summary ............................................................................................373
7
Table of Contents
Appendix A μC/OS-III API Reference Manual .......................................................375
A-1 Task Management .............................................................................376
A-2 Time Management .............................................................................378
A-3 Mutual Exclusion Semaphores – Resource Management ...............379
A-4 Event Flags – Synchronization ..........................................................380
A-5 Semaphores – Synchronization .........................................................381
A-6 Task Semaphores – Synchronization ................................................382
A-7 Message Queues – Message Passing ..............................................383
A-8 Task Message Queues – Message Passing .....................................384
A-9 Pending on Multiple Objects .............................................................385
A-10 Timers .................................................................................................386
A-11 Fixed-Size Memory Partitions – Memory Management ...................387
OSCtxSw()...........................................................................................388
OSFlagCreate()....................................................................................390
OSFlagDel().........................................................................................392
OSFlagPend()......................................................................................394
OSFlagPendAbort().............................................................................398
OSFlagPendGetFlagsRdy().................................................................401
OSFlagPost().......................................................................................403
OSIdleTaskHook()...............................................................................405
OSInit().................................................................................................407
OSInitHook()........................................................................................409
OSIntCtxSw().......................................................................................410
OSIntEnter().........................................................................................412
OSIntExit()............................................................................................413
OSMemCreate()...................................................................................414
OSMemGet()........................................................................................417
OSMemPut()........................................................................................419
OSMutexCreate().................................................................................421
OSMutexDel()......................................................................................423
OSMutexPend()...................................................................................425
OSMutexPendAbort()..........................................................................428
OSMutexPost()....................................................................................430
OSPendMulti().....................................................................................432
OSQCreate()........................................................................................436
OSQDel()..............................................................................................438
OSQFlush()..........................................................................................440
OSQPend()...........................................................................................442
8
Description:Micriμm Press. 1290 Weston Road, Suite 306. Weston, FL 33326. USA www. micrium.com. Designations used by companies to distinguish their products are