You are on page 1of 31

Micro C/OS II 作業系統

作者 : 陳鍾誠
單位 : 金門技術學院資管系
Email: ccc@kmit.edu.tw
URL : http://ccc.kmit.edu.tw

日期 : 110/11/02
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

2 陳鍾誠 - 110/11/02
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.

3 陳鍾誠 - 110/11/02
 Supports of various 8-bit to 64-bit platforms: x
86, 68x, MIPS, 8051, etc
 Easy for development: Borland C++compiler
and DOS (optional).
 However, uC/OS-II still lacks of the following f
eatures:
 Resource synchronization protocols.
 Sporadic task support.
 Soft-real-time support.

4 陳鍾誠 - 110/11/02
 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?

5 陳鍾誠 - 110/11/02
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 to
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 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.

6 陳鍾誠 - 110/11/02
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).

7 陳鍾誠 - 110/11/02
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)
8 陳鍾誠 - 110/11/02
Critical Section
 A critical section is a portion of code that is not safe from race con
ditions 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 ov
erheads.
 A RTOS often use interrupts disabling/enabling to protect critical secti
ons.
 Once interrupts are disabled, neither context switches nor any other IS
R’s can occur.
 Interrupt latency is vital to an RTOS!
 Interrupts should be disabled as short as possible to improve the resp
onsiveness.
 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!

9 陳鍾誠 - 110/11/02
Real Time System
背景程式
前景程式

中斷發生

10 陳鍾誠 - 110/11/02
11 陳鍾誠 - 110/11/02
12 陳鍾誠 - 110/11/02
任務狀態 (Task state)
 Dormant 睡眠
 Ready 備妥
 Running 執行中
 ISR 中斷
 Waiting 等待

OSStart()
OSIntExit()
13 陳鍾誠 - 110/11/02
OS_TASK_SW()
任務狀態圖
Waiting 等待

OSTaskDel()
OSMBoxPend()
OSMBoxPost() OSQPend() ISR 中斷
OSQPost() OSSemPend()
Dormant 睡眠 OSPostFront() OSTaskSuspend()
OSSemPost() OSTimeDly()
OSTaskRusume() OSTimeDlyHMSM() OSIntExit
OSTaskDlyResume()
OSTaskDel()
OSTaskCreate() OSTimeTick() 中斷
OSTaskCreateExt()

Preempted
Ready 備妥 Running 執行中
OSStart()
OSIntExit()
14 陳鍾誠 - 110/11/02
OS_TASK_SW()
15 陳鍾誠 - 110/11/02
TCB (Task Control Block)

16 陳鍾誠 - 110/11/02
Ready List

17 陳鍾誠 - 110/11/02
Ready List

18 陳鍾誠 - 110/11/02
Ready List - Coding Style
 Ready List
 if ((OSRdyTbl[prio >> 3] &= ~OSMapTbl[prio & 0x07]) == 0)
OSRdyGrp &= ~OSMapTbl[prio >> 3];

19 陳鍾誠 - 110/11/02
Task Scheduling

20 陳鍾誠 - 110/11/02
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 a
nd PSW of the highest-priority ready task from its stack.

 Task-level scheduling will emulate that as if preemption/scheduli


ng 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 r


eady task to prevent another ISR’s from making some tasks read
y.

21 陳鍾誠 - 110/11/02
OSSchedLock()

22 陳鍾誠 - 110/11/02
OSSchedUnLock()

23 陳鍾誠 - 110/11/02
Statistics Task

24 陳鍾誠 - 110/11/02
Interrupts

25 陳鍾誠 - 110/11/02
Initialize

26 陳鍾誠 - 110/11/02
Initialize

27 陳鍾誠 - 110/11/02
Starting

28 陳鍾誠 - 110/11/02
29 陳鍾誠 - 110/11/02
任務函數 (1)
 OSTaskCreate()
 建立任務
 OSTaskCreateExt()
 另一種建立任務的函數
 OSTaskDel()
 結束任務
 OSStart()
 啟動 uCOSII 系統,此時系統會由備妥佇列中取出任務執行,此函數永遠不會結束
 OSIntExit()
 系統已經離開中斷狀態,此時會從備妥的佇列中取出任務來執行。
 OS_TASK_SW()
 系統執行任務切換,此時會從備妥的佇列中取出任務來執行。
 Preempted
 目前任務被更高優先權的任無所佔,因而被切換到備妥佇列。
 Interrput
 中斷發生了,系統進入中斷服務程式。

30 陳鍾誠 - 110/11/02
任務函數 (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任務延遲取消。

31 陳鍾誠 - 110/11/02

You might also like