Micro C/OS II 作業系統
description
Transcript of Micro C/OS II 作業系統
日期 : 112/04/21
作者 : 陳鍾誠單位 : 金門技術學院資管系Email: [email protected] : http://ccc.kmit.edu.tw
Micro C/OS II 作業系統
2 陳鍾誠 - 112/04/21
Micro C/OS II 簡介 全名: Micro-Controller Operating Systems, Version 2
參考 Jean J. Labresse, “MicroC/OS-II: The Real-Time
Kernel,” CMP Book, ISBN:1-57820-103-9 http://www.uCOS-II.com
3 陳鍾誠 - 112/04/21
Micro C/OS II 的特性 特性
一種嵌入式即時作業系統, C 語言寫成的 Preemptible priority-driven real-time scheduling. 非常小、原始碼 5500 行,編譯後占記憶體 20KB 非開放原始碼,亦非免費,但可取得程式碼 64 priority levels (max 64 tasks) 8 reserved for uC/OS-II Each task is an infinite loop. Deterministic execution times for most uC/OS-II functions and services. Nested interrupts could go up to 256 levels.
4 陳鍾誠 - 112/04/21
Supports of various 8-bit to 64-bit platforms: x86, 68x, MIPS, 8051, etc
Easy for development: Borland C++compiler and DOS (optional).
However, uC/OS-II still lacks of the following features: Resource synchronization protocols. Sporadic task support. Soft-real-time support.
5 陳鍾誠 - 112/04/21
Getting started with uC/OS-II! See how a uC/OS-II program looks like. Learn how to write a skeleton program for uC/OS-
II. How to initialize uC/OS-II? How to create real-time tasks? How to use inter-task communication mechanism
s? How to catch system events?
6 陳鍾誠 - 112/04/21
Mail Box A mailbox is for data exchanging between tasks.
A mailbox consists of a data pointer and a wait-list.
OSMboxPend(): The message in the mailbox is retrieved. If the mailbox is empty, the task is immediately blocked and moved t
o the wait-list. A time-out value can be specified.
OSMboxPost(): A message is posted in the mailbox. If there is already a message in the mailbox, then an error is returne
d (not overwritten). If tasks are waiting for a message from the mailbox, then the task wit
h the highest priority is removed from the wait-list and scheduled to run.
7 陳鍾誠 - 112/04/21
Message Queue
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).
8 陳鍾誠 - 112/04/21
Hooks (User Customizable) void OSInitHookBegin (void) void OSInitHookEnd (void) void OSTaskCreateHook (OS_TCB *ptcb) void OSTaskDelHook (OS_TCB *ptcb) void OSTaskIdleHook (void) void OSTaskStatHook (void) void OSTaskSwHook (void) void OSTCBInitHook (OS_TCB *ptcb) void OSTimeTickHook (void)
9 陳鍾誠 - 112/04/21
Critical Section A critical section is a portion of code that is not safe from race co
nditions because of the use of shared resources. They can be protected by interrupt disabling/enabling interrupts or se
maphores. The use of semaphores often imposes a more significant amount of
overheads. A RTOS often use interrupts disabling/enabling to protect critical sect
ions. Once interrupts are disabled, neither context switches nor any other
ISR’s can occur. Interrupt latency is vital to an RTOS! Interrupts should be disabled as short as possible to improve the res
ponsiveness. It must be accounted as a blocking time in the schedulability analysis. Interrupt disabling must be used carefully:
E.g., if OSTimeDly() is called with interrupt disabled, the machine might hang!
10 陳鍾誠 - 112/04/21
Real Time System
中斷發生
背景程式
前景程式
11 陳鍾誠 - 112/04/21
12 陳鍾誠 - 112/04/21
13 陳鍾誠 - 112/04/21
任務狀態 (Task state)
OSStart()OSIntExit()OS_TASK_SW()
Dormant 睡眠 Ready 備妥 Running 執行中 ISR 中斷 Waiting 等待
14 陳鍾誠 - 112/04/21
任務狀態圖Waiting 等待
Dormant 睡眠
Ready 備妥 Running 執行中
ISR 中斷
中斷
OSIntExit
OSMBoxPend()OSQPend()OSSemPend()OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
OSMBoxPost()OSQPost()OSPostFront()OSSemPost()OSTaskRusume()OSTaskDlyResume()OSTimeTick()
OSTaskDel()
OSTaskDel()OSTaskCreate()OSTaskCreateExt()
Preempted
OSStart()OSIntExit()OS_TASK_SW()
15 陳鍾誠 - 112/04/21
16 陳鍾誠 - 112/04/21
TCB (Task Control Block)
17 陳鍾誠 - 112/04/21
Ready List
18 陳鍾誠 - 112/04/21
Ready List
19 陳鍾誠 - 112/04/21
Ready List - Coding Style
Ready List if ((OSRdyTbl[prio >> 3] &= ~OSMapTbl[prio & 0x07]) == 0)
OSRdyGrp &= ~OSMapTbl[prio >> 3];
20 陳鍾誠 - 112/04/21
Task Scheduling
21 陳鍾誠 - 112/04/21
Task Scheduling A context switch must save all CPU registers and PSW of the pre
empted task onto its stack, and then restore the CPU registers and PSW of the highest-priority ready task from its stack.
Task-level scheduling will emulate that as if preemption/scheduling is done in an ISR. OS_TASK_SW() will trigger a software interrupt. The interrupt is directed to the context switch handler OSCtxSw(),
which is installed when uC/OS-II is initialized.
Interrupts are disabled during the locating of the highest-priority ready task to prevent another ISR’s from making some tasks ready.
22 陳鍾誠 - 112/04/21
OSSchedLock()
23 陳鍾誠 - 112/04/21
OSSchedUnLock()
24 陳鍾誠 - 112/04/21
Statistics Task
25 陳鍾誠 - 112/04/21
Interrupts
26 陳鍾誠 - 112/04/21
Initialize
27 陳鍾誠 - 112/04/21
Initialize
28 陳鍾誠 - 112/04/21
Starting
29 陳鍾誠 - 112/04/21
30 陳鍾誠 - 112/04/21
任務函數 (1) OSTaskCreate()
建立任務 OSTaskCreateExt()
另一種建立任務的函數 OSTaskDel()
結束任務 OSStart()
啟動 uCOSII 系統,此時系統會由備妥佇列中取出任務執行,此函數永遠不會結束 OSIntExit()
系統已經離開中斷狀態,此時會從備妥的佇列中取出任務來執行。 OS_TASK_SW()
系統執行任務切換,此時會從備妥的佇列中取出任務來執行。 Preempted
目前任務被更高優先權的任無所佔,因而被切換到備妥佇列。 Interrput
中斷發生了,系統進入中斷服務程式。
31 陳鍾誠 - 112/04/21
任務函數 (2) OSMBoxPend()
任務等待訊息郵件盒,因而進入等待狀態。 OSQPend()
任務等待佇列。 OSSemPend()
任務等待號誌 (Semaphore) ,拿到號誌才能取得對周邊的控制權。 OSTaskSuspend()
任務自行暫時擱置。 OSTaskResume()
任務重新開始。 OSTimeDly()
任務延遲一段時間,以 Time Click ( 時間節拍 ) 為單位。 OSTimeDlyHMSM()
任務延遲一段時間,以時、分、秒為單位。 OSMBoxPost()
任務收到所等待的訊息佇列盒。 OSQPost()
任務收到佇列。 OSSemPost()
任務交出號誌 Semaphore 。 OSTaskDlyResume()
uCOSII\LPC210x\CH11_uCOS-II\lpc2100\Os_cpu_a.s任務延遲取消。