Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 1: Getting Started with μC/OS-II"— Presentation transcript:

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

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

3 Introduction μC/OS-II Micro-Controller Operating Systems, Version 2
A very small real-time kernel. Memory footprint is about 20KB 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.

4 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.

5 Introduction μC/OS-II
Supports of various 8-bit to 64-bit platforms: x86, 68x, 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. 如果對這三者有興趣的人可以找我討論其他的project主題 在這裡demo一下 debug的環境 程式碼

6 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?

7

8 牡羊座本日運勢 心情指數: 寫程式錢應該先好好沈澱一下,但不容易如願。 心 情: 幫同學解釋一些OS上的盲點可能會有意想不到的收穫。
心  情: 寫程式錢應該先好好沈澱一下,但不容易如願。 愛  情: 幫同學解釋一些OS上的盲點可能會有意想不到的收穫。 財  運: 有一筆錢入帳,讓手頭鬆了不少(先拿去買教科書?)。 工  作: 為人辛苦為人忙,看什麼都不順眼。寫共筆前試著先到sandbox寫一些文章出出氣。 牡羊座

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

10 Example 1: Multitasking

11 Install context switch handler
main OSInit Install context switch handler OSSemCreate OSTaskCreate DOS OSStart TaskStart Multiprogramming install tick ISR Create other tasks Task1 Task10 如果沒有硬體的幫助很難達到context switch… 作業? μC/OS-II在啟動的時候有一些特別的順序 這些順序和程式設計的細節有非常大的關係 在這裡我們不仔細探討 會於適當的時候向各位報告 longmp OSSemPend random OSSemPost OSTimeDly OSSemPend random OSSemPost OSTimeDly Exit? OSTimeDlyHMSM . . .

12 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.

13 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 (V3.1+)

14 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語言的部分有時候在porting的時候還是得小改 μC/OS-II port for processor specific codes Software Hardware CPU Timer

15 includes.h

16 OS_CFG.H .

17 A semaphore (explain later)
test.c ccu206 A semaphore (explain later) Stacks (explain later)

18 test.c: main() 解釋一下 PCXXX是和PC相關的部分 其實和作業系統本身並沒有太大的關係
OSXXX幾乎都是作業系統本身的一些service

19 Install context switch handler
main OSInit Install context switch handler OSSemCreate OSTaskCreate DOS OSStart TaskStart Multiprogramming install tick ISR Create other tasks Task1 Task10 如果沒有硬體的幫助很難達到context switch… 作業? μC/OS-II在啟動的時候有一些特別的順序 這些順序和程式設計的細節有非常大的關係 在這裡我們不仔細探討 會於適當的時候向各位報告 longmp OSSemPend random OSSemPost OSTimeDly OSSemPend random OSSemPost OSTimeDly Exit? OSTimeDlyHMSM . . .

20 OSInit() Internal structures of μC/OS-II. Create housekeeping tasks.
Task ready list. Priority table. a mapping table. give a priority/pid; Task control blocks (TCB). Free pools. Create housekeeping tasks. The idle task. The statistic task. 查一下OSInit到底做了哪些事情

21 OSinit()

22 OSinit()

23 After (μC/OS-II installed)
The PC IVT Interrupt vector table (IVT) Before (DOS only) After (μC/OS-II installed) 這裡要再加入一張圖

24 PC_DOSSaveReturn() 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()

25 PC_DOSSaveReturn() 2/21 A班

26 setjmp setjmp +longjmp ≒ goto

27 longjmp

28 PC_VectSet() PC_VectSet(uCOS, OSCtxSw)
Install the context switch handler (OSCtxSw). Interrupt 0x80 (uCOS) under 80x86 family. Invoked by INT instruction. Disable interrupt Enable interrupt

29 OSSemCreate() 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()”.

30 OSTaskCreate() Functionality Task
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. 在這裡應該要注意到小型的OS和大型的OS會有什麼樣的不同 Loader?

31 Entry point of the task (a pointer to function)
OSTaskCreate() Entry point of the task (a pointer to function) OSTaskCreate( TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE - 1], ); User-specified data Top of Stack Priority (0=hightest) 在這裡應該要注意到小型的OS和大型的OS會有什麼樣的不同 Loader?

32 OSStart() OSStart() Start multitasking of μC/OS-II .
It never returns to main(). μC/OS-II is terminated if PC_DOSReturn() is called. OSStart的詳細運作過程後面章節會再做更仔細的介紹

33 Install context switch handler
main OSInit Install context switch handler OSSemCreate OSTaskCreate DOS OSStart TaskStart Multiprogramming install tick ISR Create other tasks Task1 Task10 如果沒有硬體的幫助很難達到context switch… 作業? μC/OS-II在啟動的時候有一些特別的順序 這些順序和程式設計的細節有非常大的關係 在這裡我們不仔細探討 會於適當的時候向各位報告 longmp OSSemPend random OSSemPost OSTimeDly OSSemPend random OSSemPost OSTimeDly Exit? OSTimeDlyHMSM . . .

34 TaskStart() OSTaskCreate( TaskStart, (void *)0,
&TaskStartStk[TASK_STK_SIZE - 1], 0); TaskStart() void TaskStart (void *pdata) for (i=0 to 9) { OSTaskCeate } Wait one second

35 TaskStart() 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 (x86 real mode) Interrupt descriptors (x86 protected mode)

36 TaskStartCreateTasks()
Entry point of the created task Argument: character to print Stack Priority

37 Task() Semaphore operations.

38 Install context switch handler
main OSInit Install context switch handler OSSemCreate OSTaskCreate DOS OSStart TaskStart Multiprogramming install tick ISR Create other tasks Task1 Task10 如果沒有硬體的幫助很難達到context switch… 作業? μC/OS-II在啟動的時候有一些特別的順序 這些順序和程式設計的細節有非常大的關係 在這裡我們不仔細探討 會於適當的時候向各位報告 longmp OSSemPend random OSSemPost OSTimeDly OSSemPend random OSSemPost OSTimeDly Exit? OSTimeDlyHMSM . . .

39 Semaphores A semaphore consists of a wait list and an integer counter.
OSSemPend(): 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. OSSemPost(): Counter++ If the value of the semaphore ≧ 0, then a task in the wait list is removed from the wait list. Reschedule if needed.

40 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_DOSSaveReturn(). A context switch handler is installed by calling PC_VectSet(). One user task must be created first! Shared resources can be protected by semaphores. OSSemPend(), OSSemPost(). 40

41 Example 2

42 Install context switch handler
main OSInit Install context switch handler OSTaskStkInit_FPE_X86 & OSTaskCreateExt DOS OSStart TaskStart Multiprogramming install tick ISR Create other tasks Task1 Task4 Task5 OSMboxPost &OSMobxPend Update the display OSTaskStkChk OSMboxPost

43 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.

44 2 Mailboxes

45 Main() OSTaskStkInit_FPE_x86(&ptos, &pbos, &size)

46 OSTaskStkInit_FPE_x86()
OSTaskStkInit_FPE_x86(&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. For context switches. ptos 同學:加油, 快下課了! size pbos

47 OSCreateTaskExt() OSTaskCreatExt( TaskStart, (void *)0, ptos,
TASK_START_PRIO, TASK_START_ID, pbos, size, OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR ); User supplied data which can be used to extend TCB options

48 The dummy loop wait for ‘ESC’
TaskStart() Create 2 mailboxes The dummy loop wait for ‘ESC’

49 Task1()

50 Task2 and Task3 for (i=0; i<499; i++) { dymmy[i] = ‘?’; }

51 Task4 and Task5 ??? OSTimeDlyHMSM(0, 0, 1, 0);

52 MailBox OSMboxPend(): OSMboxPost():
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.

53 OSTaskStkCheck() 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 OSTaskStkCheck() is called. μC/OS-II does not automatically check stacks.

54 (kernel service routines) (interrupt service routines)
(Linux 2.6) stack User mode Kernel mode (kernel service routines) stack stack stack Kernel mode (interrupt service routines)

55 (kernel service routines) (interrupt service routines)
(Linux 2.4) stack User mode Kernel mode (kernel service routines) stack stack Kernel mode (interrupt service routines)

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

57 Example2: 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.

58 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

59 Example 3

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

61

62 .

63

64 Task 2, 3, 4 are functionally identical.

65 Message Queues 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). As same as mailboxes, there can be multiple tasks pend/post to a message queue. OSQPost(): a message is appended to the queue. The highest-priority 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 wait-list and becomes blocked.

66 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, OSTaskSwHook () is called every time when context switch occurs. The hooks are specified in the compiling time in μC/OS-II : μC/OS-II is an embedded OS. OS_CFG.H (OS_CPU_HOOKS_EN = 0) Many OS’s can register and un-register hooks.

67 User Customizable Hooks for μC/OS-II
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)

68 OSTaskStatHook()

69 OSTaskSwHook() Elapsed time for the current task
OSTCBCur TCB of the current task OSTCBHighRdyTCB of the new task

70 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

71 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?


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

Similar presentations


Ads by Google