Amr Ali Abdel-Naby@2010 Introduction to uCOS-II V2.6• Mutual Exclusion Semaphores • Event Flag Management • Message Mailbox Management • Message Queue Management • Memory Management... T
Trang 1Introduction to uCOS-II V2.6
Trang 2About SwiftACT
• A Technology services startup company
o Under establishment
• Areas of specialties:
o Mobile telecommunication services development
o Embedded systems development
Trang 3Amr Ali Abdel-Naby@2010 Introduction to uCOS-II V2.6
About Me
• Graduated 2004
o ECE, ASU: 5 yrs distinction
• 5+ years in embedded systems development
Trang 4• Materials in this course is the property of Amr Ali Abdel-Naby
• Reproduction or transmission of the materials in any manner without the copyright owner permission is a law violation
Trang 5Amr Ali Abdel-Naby@2010 Introduction to uCOS-II V2.6
Course References
• MicroC/OS-II The Real-Time Kernel, 2nd Edition, by Jean J Labrosse
Trang 6• Mutual Exclusion Semaphores
• Event Flag Management
• Message Mailbox Management
• Message Queue Management
• Memory Management
Trang 7Amr Ali Abdel-Naby@2010 Introduction to uCOS-II V2.6
• Mutual Exclusion Semaphores
• Event Flag Management
• Message Mailbox Management
• Message Queue Management
• Memory Management
Trang 8Kernel Architecture
µC/OS-II Port (Processor Specific Code)
µC/OS-II (Processor Independent
code)
µC/OS-II Configuration (Application Specific)
Application SW (Your Code)
HW
Trang 9Amr Ali Abdel-Naby@2010 Introduction to uCOS-II V2.6
Critical Sections
• 2 macros protect critical sections
o They enable/disable interrupts.
• Can be used by your application
o Not a good programming style
o Applications may crash.
• Processor & tool chain specific
OS_ENTER_CRITICAL();
/* Critical Code */
OS_EXIT_CRITICAL();
Trang 10• Lowest priority is defined
as OS_LOWEST_PRIO
/* Endless Loop Task */
void My_Task (void * pdata){
for(;;){
/* Your Code */
}
}
/* Run To Completion Task */
void My_Task (void * pdata){
/* Your Code */
OSTaskDel(OS_PRIO_SELF);
}
Up to 56 application tasks
0 1 2 3
…
60 61 62 63
Priority
Used by the system
May be used
in the future extension of μC/OS-II
Trang 11Amr Ali Abdel-Naby@2010 Introduction to uCOS-II V2.6
Task States
Trang 12Task Control Blocks
• µC/OS-II uses TCBs for task management
• Every task is assigned a TCB when created
Trang 13Amr Ali Abdel-Naby@2010 Introduction to uCOS-II V2.6
Task Control Blocks cont’d
• A TCB contains:
o Task’s priority
o Task’s state
o A pointer to the task’s top of stack
o Other task’s related data
• TCBs reside in RAM
Trang 14Ready List
• The kernel maintains a list of tasks that can be ready to run
• The highest priority task is kept at the beginning of the task
Trang 15Amr Ali Abdel-Naby@2010 Introduction to uCOS-II V2.6
Task Scheduling
• Task-level scheduling is performed by OS_Sched
• ISR-level scheduling is handled by OSIntExit
Trang 16Task Level Context Switch
• OS_TASK_SW is a macro used by μC/OS-II to perform the context switch
o It simply saves registers of the preempted task & loads registers of the task to be resumed.
• The context switch can be viewed at three moments:
o The context at the call
o Saving the current task’s context
o Resuming the highest priority ready task
Trang 17Amr Ali Abdel-Naby@2010 Introduction to uCOS-II V2.6
Task Level Context Switch cont’d
SP
R1 R2 R3 R4 PC
Low Memory Low Memory
High Memory High Memory
Trang 18Task Level Context Switch cont’d
SP
R1 R2 R3 R4 PC
Low Memory Low Memory
PSW R1
R3 R4
PC R2
Trang 19Amr Ali Abdel-Naby@2010 Introduction to uCOS-II V2.6
Task Level Context Switch cont’d
SP
R1 R2 R3 R4 PC
Low Memory Low Memory
High Memory High Memory
PSW R1
R3 R4
PC R2
OSTCBCur
Trang 20Locking & Unlocking the Scheduler
• A mechanism used by a task to keep control of the CPU, even if there are higher priority tasks ready
• Two kernel services are provided & must be used in pairs:
o OSSchedLock & OSSchedUnlock
• After calling OSSchedLock, your application should not call a service that suspends execution
o Your application will crash.
Trang 21Amr Ali Abdel-Naby@2010 Introduction to uCOS-II V2.6
Idle Task
• Executed when there is no other task ready to run
o Always set to the lowest priority
Trang 22Statistics Task
• Its priority is higher than IDLE task by 1
• It provides runtime statistics
• It is called OS_TaskStat & it is called every second
• It tells you how long was the CPU used by your application
• To use it, you must call OSStatInit from the first & the only task created during initialization
OSStatInit();
}
Trang 23Amr Ali Abdel-Naby@2010 Introduction to uCOS-II V2.6
Interrupts Under µC/OS-II
• You should keep ISRs as short as possible
• Interrupts either use an interrupt stack or task stack
• Stack size must account for:
o ISR nesting
o Function call nesting
o Local variables
Trang 24Interrupts Under µC/OS-II cont’d
Trang 25Amr Ali Abdel-Naby@2010 Introduction to uCOS-II V2.6
Interrupts Under µC/OS-II cont’d
Trang 26Clock Tick
• µC/OS-II requires a periodic time source to keep track of time delays & timeouts
o > 10 Hz & < 100 Hz
• Ticker interrupts must be enabled after starting multitasking
• The clock tick is serviced by calling OSTimeTick from the tick ISR
Trang 27Amr Ali Abdel-Naby@2010 Introduction to uCOS-II V2.6
µC/OS-II Initialization
• µC/OS-II requires that OSInit is called before any other service
• It initializes all µC/OS-II variables & data structures
• It creates idle & statistics tasks
Trang 29Amr Ali Abdel-Naby@2010 Introduction to uCOS-II V2.6
Obtaining the Current Version
• By calling OSVersion