Presentation is loading. Please wait.

Presentation is loading. Please wait.

Embedded System Presentation Nguyễn Trần Quang Nguyễn Công Vũ 1µC/OS-II.

Similar presentations


Presentation on theme: "Embedded System Presentation Nguyễn Trần Quang Nguyễn Công Vũ 1µC/OS-II."— Presentation transcript:

1 Embedded System Presentation Nguyễn Trần Quang Nguyễn Công Vũ 1µC/OS-II

2 µC/OS-II 2

3 Outline Introduction to µC/OS-II Kernel Structure Task Management Time Management Semaphore Management Mutual Exclusion Semaphores Event Flag Management Message Mailbox Management Message Queue Management Memory Management 3Micro C_OS

4 Outline Introduction to µC/OS-II Kernel Structure Task Management Time Management Semaphore Management Mutual Exclusion Semaphores Event Flag Management Message Mailbox Management Message Queue Management Memory Management 4Micro C_OS

5 At the Beginning Written By J. Labrosse First published in 1992 µC/OS-II is a trade mark of Micrium. 1000s applications are using it all over the world. A good starting point to experience real-time OS Simple but yet very powerful 5Micro C_OS

6 µC/OS-II in Literature Four books were published to explain the internals: “μC/OS The Real-Time Kernel”, in 1992 “μC/OS-II The Real-Time Kernel”, in 1998 “μC/OS-II The Real-Time Kernel”, Second Edition in 2002 “μC/OS-III The Real-Time Kernel”, in 2009 6Micro C_OS

7 µC/OS-II History µC/OS-II V2.86 µC/OS-II V2.52 µC/OS-II V2.00 µC/OS V1.08 µC/OS-III µC/OS V1.08 + Memory Manager + Stack Checking + CPU Load Checking µC/OS-II V2.00 + Safety Critical + Mutexes + Semaphores + Event Flags µC/OS-II V2.52 + Timers + 250 Tasks + MMU & MPU µC/OS-II V2.86 + RR Scheduling + Infinite # of Tasks & Services 7Micro C_OS

8 µC/OS-II Features Source code Portable Preemptive Multitasking Task stack Services Interrupt management Robust & reliable … 8Micro C_OS

9 Source Code High quality Neat Consistent Organized Commented MISRA-C compliant 9Micro C_OS

10 Source Code Highly portable ANSI C Assembly is kept minimum Supports 8-, 16-, 32-, 64- bit processors Ported over 100 different processors All ports are freely available at Micrium website 10Micro C_OS

11 Amr Ali Abdel-Naby@2010Introduction to uCOS-II V2.6 11Micro C_OS

12 Preemptive Fully preemptive Always runs the highest priority task that is ready to run 12Micro C_OS

13 Multitasking Manages up to 64 tasks 8 tasks are used by µC/OS-II 56 tasks are left to applications A unique task/priority RR and FIFO are not supported 13Micro C_OS

14 Task Stacks Each task has its stack μC/OS-II allows different stack sizes Stack checking utility You can determine & decide how much stack is needed for each task 14Micro C_OS

15 Task Stacks Each task has its stack μC/OS-II allows different stack sizes Stack checking utility You can determine & decide how much stack is needed for each task 15Micro C_OS

16 Services Semaphores Mutexes Event flags Message queues Mailboxes Fixed size memory partitions Task management Time management 16Micro C_OS

17 Interrupt Management An interrupt can suspend a task execution The highest priority ready task runs after serving the interrupt Nested interrupts Up to 255 level 17Micro C_OS

18 Robust & Reliable Used by many products Support Tested & certified in safety critical systems 18Micro C_OS

19 Related Products µC/Probe µC/TCP-IP µC/FS µC/GUI µC/USB µC/CAN µC/Modbus µC/FL µC/Building Blocks µC/OSEK-VDX 19Micro C_OS

20 µC/OS-II Market Distributors all over the word Huge customer list 20Micro C_OS

21 License Educational Free Universities Non-profitable use Source Code If µC/OS will be distributed with your product as a source code Object Code If µC/OS will be distributed with your product as a binary Not royalty free 21Micro C_OS

22 Outline Introduction to µC/OS-II Kernel Structure Task Management Time Management Semaphore Management Mutual Exclusion Semaphores Event Flag Management Message Mailbox Management Message Queue Management Memory Management 22Micro C_OS

23 Outline Introduction to µC/OS-II Kernel Structure Task Management Time Management Semaphore Management Mutual Exclusion Semaphores Event Flag Management Message Mailbox Management Message Queue Management Memory Management 23Micro C_OS

24 Kernel 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 24Micro C_OS

25 Critical Sections 2 macros protect critical sections They enable/disable interrupts Can be used by your application Not a good programming style Applications may crash Processor & tool chain specific OS_ENTER_CRITICAL(); /* Critical Code */ OS_EXIT_CRITICAL(); 25Micro C_OS

26 Tasks Distributors all over the word Huge customer list 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 26Micro C_OS

27 Task States 27Micro C_OS

28 Task Control Blocks µC/OS-II uses TCBs for task management Every task is assigned a TCB when created 28Micro C_OS

29 Task Control Blocks cont’d A TCB contains: Task’s priority Task’s state A pointer to the task’s top of stack Other task’s related data TCBs reside in RAM 29Micro C_OS

30 Ready 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 30Micro C_OS

31 Task Scheduling Task-level scheduling is performed by OS_Sched ISR-level scheduling is handled by OSIntExit Task Level context switch 31Micro C_OS

32 Locking & 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: OSSchedLock & OSSchedUnlock After calling OSSchedLock, your application should not call a service that suspends execution Your application will crash LPT HPT LPT OSSchedLockOSSchedUnlock HPT is ready here 32Micro C_OS

33 Idle Task Executed when there is no other task ready to run Always set to the lowest priority 33Micro C_OS

34 Statistics 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 34Micro C_OS

35 Statistics Task void main(void){ OSInit();... Create your startup task TaskStart()... OSStart(); } void TaskStart(void * pdata){ OSStatInit();... } 35Micro C_OS

36 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: ISR nesting Function call nesting Local variables 36Micro C_OS

37 Clock Tick µC/OS-II requires a periodic time source to keep track of time delays & timeouts Ticker interrupts must be enabled after starting multitasking The clock tick is serviced by calling OSTimeTick from the tick ISR void main(void){ OSInit();... Enable Ticker Interrupt /* Mistake */... OSStart(); } 37Micro C_OS

38 µ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 void main(void){ OSInit();... OSStart(); } 38Micro C_OS

39 Starting µC/OS-II Multitasking is started by calling OSStart after creating at least 1 task void main(void){ OSInit();... Create at least 1 task here... OSStart(); } 39Micro C_OS

40 Obtaining the Current Version By calling OSVersion 40Micro C_OS

41 Outline Introduction to µC/OS-II Kernel Structure Task Management Time Management Semaphore Management Mutual Exclusion Semaphores Event Flag Management Message Mailbox Management Message Queue Management Memory Management 41Micro C_OS

42 Outline Introduction to µC/OS-II Kernel Structure Task Management Time Management Semaphore Management Mutual Exclusion Semaphores Event Flag Management Message Mailbox Management Message Queue Management Memory Management 42Micro C_OS

43 43Micro C_OS

44 Creating a Task, OSTaskCreate INT8U OSTaskCreate (void (*task)(void *pd), void *pdata, OS_STK *ptos, INT8U prio) task: A pointer to the task's code pdata: A pointer to an optional data area which can be used to pass parameters to the task when the task first executes ptos: A pointer to the task's top of stack prio: The task's priority Return value: o No error o Priority exist o Invalid priority 44Micro C_OS

45 Creating a Task, OSTaskCreateExt INT8U OSTaskCreateExt (void (*task)(void *pd), void *pdata, OS_STK *ptos, INT8U prio, INT16U id, OS_STK *pbos, INT32U stk_size, void *pext, INT16U opt) OSTaskCreate + id: Task’s ID, not used currently pbos: A pointer to the task's bottom of stack stk_size: A pointer to the task's top of stack pext: A pointer to a user supplied memory location which is used as a TCB extension opt: Additional information (or options) about the behavior of the task 45Micro C_OS

46 Task Stacks Each task has its own stack. It must be: Declared as OS_STK Consistent & contiguous memory It can be allocated static Or dynamic OS_STK MyTaskSTack[stack_size]; OS_STK *pstk;... pstk = (OS_STK*)malloc(stack_size); 46Micro C_OS

47 Naming a Task, OSTaskNameSet void OSTaskNameSet (INT8U prio, char *pname, INT8U *err) prio: The priority of the task that you want to assign a name to pname: A pointer to an ASCII string that contains the name of the task err: o No error o Task does not exist. o Name too long o A null pointer is passed for the name. o Invalid priority 47Micro C_OS

48 Outline Introduction to µC/OS-II Kernel Structure Task Management Time Management Semaphore Management Mutual Exclusion Semaphores Event Flag Management Message Mailbox Management Message Queue Management Memory Management 48Micro C_OS

49 Outline Introduction to µC/OS-II Kernel Structure Task Management Time Management Semaphore Management Mutual Exclusion Semaphores Event Flag Management Message Mailbox Management Message Queue Management Memory Management 49Micro C_OS

50 Time Management APIs Available time operations are: Delaying a task Resuming a delayed task System time getting & setting 50Micro C_OS

51 Delaying a Task, OSTimeDly void OSTimeDly (INT16U ticks) ticks: The time delay that the task will be suspended in number of clock ticks time 10ms OSTickISR() All HPT LPT 5ms OSTimeDly(1) 51Micro C_OS

52 Delaying a Task, OSTimeDlyHMSM INT8U OSTimeDlyHMSM (INT8U hours, INT8U minutes, INT8U seconds, INT16U milli) hours: The number of hours that the task will be delayed minutes: The number of minutes seconds: The number of seconds milli: The number of milliseconds Return value: o No error o Zero delay o Invalid minutes, seconds, & milli seconds 52Micro C_OS

53 Resuming a Delayed Task, OSTimeDlyResume INT8U OSTimeDlyResume (INT8U prio) prio: Specifies the priority of the task to resume Return value: o No error o Invalid priority o Task is not delayed. o Task does not exist. 53Micro C_OS

54 System Time, OSTimeSet void OSTimeSet (INT32U ticks) ticks: Specifies the new value that OSTime needs to take 54Micro C_OS

55 System Time, OSTimeGet INT32U OSTimeGet (void) Return value: o The current value of OSTime 55Micro C_OS

56 Outline Introduction to µC/OS-II Kernel Structure Task Management Time Management Semaphore Management Mutual Exclusion Semaphores Event Flag Management Message Mailbox Management Message Queue Management Memory Management 56Micro C_OS

57 Outline Introduction to µC/OS-II Kernel Structure Task Management Time Management Semaphore Management Mutual Exclusion Semaphores Event Flag Management Message Mailbox Management Message Queue Management Memory Management 57Micro C_OS

58 Semaphores Semaphores are 16 bit unsigned integers used for resource sharing or to signal the occurrence of an event OSSemCreate() OSSemDel() OSSemPost() OSSemAccept() OSSemPend() OSSemQuery() OSSemPost() N N OSSemAccept() ISR Task 58Micro C_OS

59 Semaphores Functions: Creating a Semaphore, OSSemCreate Deleting a Semaphore, OSSemDel Waiting on a Semaphore (Blocking), OSSemPend Signaling a Semaphore, OSSemPost Getting a Semaphore without Waiting, OSSemAccept Obtaining the Status of a Semaphore, OSSemQuery Creating a Semaphore, OSSemCreate Obtaining the Status of a Semaphore, OSSemQuery 59Micro C_OS

60 Outline Introduction to µC/OS-II Kernel Structure Task Management Time Management Semaphore Management Mutual Exclusion Semaphores Event Flag Management Message Mailbox Management Message Queue Management Memory Management 60Micro C_OS

61 Mutexes They are used to gain exclusive access to resources. They reduce priority inversion problem (priority ceiling protocol is supported). OSMutexCreate() OSMutexDel() OSMutexPost() OSMutexPend() OSMutexAccept() OSMutexQuery() Task 61Micro C_OS

62 Creating a Mutex, OSMutexCreate OS_EVENT *OSMutexCreate (INT8U prio, INT8U *err) prio: Ceiling priroity err: o No error o Called from ISR o Priority exists o Invalid priority o No available resources Return value: o Non-null for successful creation o Null for failure 62Micro C_OS

63 Deleting a Mutex, OSMutexDel OS_EVENT *OSMutexDel (OS_EVENT *pevent, INT8U opt, INT8U *err) pevent: A pointer to the mutex to be deleted opt: Delete options o Delete always o Delete if no pending tasks err: o No error o Called from ISR o Invalid option o There are tasks pending. o pevent is null. o pevent is not a semaphore. 63Micro C_OS

64 Deleting a Mutex, OSMutexDel cont’d OS_EVENT *OSMutexDel (OS_EVENT *pevent, INT8U opt, INT8U *err) Return value: Null if successful 64Micro C_OS

65 Waiting on a Mutex (Blocking), OSMutexPend void OSMutexPend (OS_EVENT *pevent, INT16U timeout, INT8U *err) pevent: A pointer to the mutex to acquire timeout: o 0  wait for ever o !0  wait with specific timeout err: o No error o Timeout o pevent is not a mutex. o pevent is null. o Called from ISR 65Micro C_OS

66 Signaling a Mutex, OSMutexPost INT8U OSMutexPost (OS_EVENT *pevent) pevent: A pointer to the mutex to release Return value: o No error o Not mutex owner o Called from ISR o pevent is not a mutex. o pevent is null. 66Micro C_OS

67 Getting a Mutex without Waiting, OSMutexAccept INT8U OSMutexAccept (OS_EVENT *pevent, INT8U *err) pevent: A pointer to the mutex to acquire err: o No error o Called from ISR o pevent is not a mutex. o pevent is null. Return value: o 1  resource available o 0  case of error or mutex is no available 67Micro C_OS

68 Obtaining the Status of a Mutex, OSMutexQuery INT8U OSMutexQuery (OS_EVENT *pevent, OS_MUTEX_DATA *pdata) pevent: A pointer to the desired mutex pdata: A pointer to the returned mutex information Return value: o No error o Called from ISR o pevent is not a mutex. o pevent is null. 68Micro C_OS

69 Outline Introduction to µC/OS-II Kernel Structure Task Management Time Management Semaphore Management Mutual Exclusion Semaphores Event Flag Management Message Mailbox Management Message Queue Management Memory Management 69Micro C_OS

70 Event Flags Used for synchronization of tasks with the occurrence of multiples of events Events are grouped o 8, 16 or 32 bits per group Types of synchronization o ORed: Any event occurred o ANDed: All events occured 70Micro C_OS

71 Amr Ali Abdel-Naby@2010Introduction to uCOS-II V2.6 Event Flags cont’d 71Micro C_OS

72 Event Flags Management APIs OSFlagPost() OSFlagCreate () OSFlagDel() OSFlagPost () OSFlagAccept () OSFlagPend () OSQFlagQuery () ISR Task ISR Task OSFlagAccept () OSQFlagQuery () 72Micro C_OS

73 Outline Introduction to µC/OS-II Kernel Structure Task Management Time Management Semaphore Management Mutual Exclusion Semaphores Event Flag Management Message Mailbox Management Message Queue Management Memory Management 73Micro C_OS

74 Message Mailboxes A message mailbox is a μC/OS-II object that allows a task or an ISR to send pointer- sized variable to another task. OSMboxCreate() Message OSMboxPost() OSMboxAccept() OSMboxPost() OSMboxPend() OSMboxAccept() OSMboxQuery() ISR Task 74Micro C_OS

75 Outline Introduction to µC/OS-II Kernel Structure Task Management Time Management Semaphore Management Mutual Exclusion Semaphores Event Flag Management Message Mailbox Management Message Queue Management Memory Management 75Micro C_OS

76 Message Queues A message queue is a μC/OS-II object that allows a task or an ISR to send pointer-sized variables to another task. OSQCreate() Message OSQPost() OSQPostFront() OSQFlush() OSQAccept() OSQPostOpt() OSQPost() OSQPostFront() OSQFlush() OSQPostOpt() OSQPend() OSQAccept() OSQQuery() ISR Task 76Micro C_OS

77 Outline Introduction to µC/OS-II Kernel Structure Task Management Time Management Semaphore Management Mutual Exclusion Semaphores Event Flag Management Message Mailbox Management Message Queue Management Memory Management 77Micro C_OS

78 Memory Management Fixed-size memory block management o To prevent fragmentation Multiple partitions can be created with different sizes. Blocks allocated from certain partitions must be returned back to the same partitions 78Micro C_OS

79 Memory Management 79Micro C_OS

80 Memory Management 80Micro C_OS

81 Memory Management APIs Available memory operations are: o Creating a memory partition o Obtaining a memory block o Returning a memory block o Obtaining status of a memory partition o Naming a memory partition o Getting the name of a memory partition 81Micro C_OS


Download ppt "Embedded System Presentation Nguyễn Trần Quang Nguyễn Công Vũ 1µC/OS-II."

Similar presentations


Ads by Google