1 / 71

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

Chapter 1: Getting Started with μC/OS-II. Introduction. Linux. μC/OS-II. User mode (0-3G). Task (process). Task (process). Task (process). Task (process). kernel. Task (thread). Task (thread). Task (thread). Task (thread). (Kernel mode) 3G-4G. (Kernel mode) 0G~. kernel.

ecalhoun
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Introduction Linux μC/OS-II User mode (0-3G) Task (process) Task (process) Task (process) 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.

  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. Getting started with μC/OS-II • Example 1: Multitasking • Example 2: Stack Checking • Example 3: Extension of μC/OS-II • Example 4: Portability

  9. Example 1: Multitasking

  10. main OSInit Install context switch handler OSSemCreate DOS OSTaskCreate OSStart TaskStart Multiprogramming install tick ISR Create other tasks Task10 Task1 longmp OSSemPend random OSSemPost OSTimeDly OSSemPend random OSSemPost OSTimeDly Exit? OSTimeDlyHMSM . . .

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

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

  13. 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/OS-II port for processor specific codes Software Hardware CPU Timer

  14. includes.h

  15. OS_CFG.H . . . .

  16. test.c A semaphore (explain later) Stacks (explain later)

  17. test.c: main()

  18. main OSInit Install context switch handler OSSemCreate DOS OSTaskCreate OSStart TaskStart Multiprogramming install tick ISR Create other tasks Task10 Task1 longmp OSSemPend random OSSemPost OSTimeDly OSSemPend random OSSemPost OSTimeDly Exit? OSTimeDlyHMSM . . .

  19. OSInit() • Internal structures of μC/OS-II. • Task ready list. • Priority table. • a mapping table. • give a priority/pid; return @TCB) • Task control blocks (TCB). • Free pools. • Create housekeeping tasks. • The idle task. • The statistic task.

  20. OSinit()

  21. OSinit()

  22. The PC IVT • Interrupt vector table (IVT) Before (DOS only) After (μC/OS-II installed)

  23. 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()

  24. PC_DOSSaveReturn()

  25. setjmp setjmp +longjmp ≒ goto

  26. longjmp

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

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

  29. OSTaskCreate() • Functionality • 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.

  30. OSTaskCreate() Entry point of the task (a pointer to function) • OSTaskCreate( TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE - 1], 0 ); User-specified data Top of Stack Priority (0=hightest)

  31. OSStart() • OSStart() • Start multitasking of μC/OS-II . • It never returns to main(). • μC/OS-II is terminated if PC_DOSReturn() is called.

  32. main OSInit Install context switch handler OSSemCreate DOS OSTaskCreate OSStart TaskStart Multiprogramming install tick ISR Create other tasks Task10 Task1 longmp OSSemPend random OSSemPost OSTimeDly OSSemPend random OSSemPost OSTimeDly Exit? OSTimeDlyHMSM . . .

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

  34. 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)

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

  36. Task() Semaphore operations.

  37. main OSInit Install context switch handler OSSemCreate DOS OSTaskCreate OSStart TaskStart Multiprogramming install tick ISR Create other tasks Task10 Task1 longmp OSSemPend random OSSemPost OSTimeDly OSSemPend random OSSemPost OSTimeDly Exit? OSTimeDlyHMSM . . .

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

  39. Example 1: Multitasking • Summary: • μC/OS-IIis initialized and started by calling OSInit() and OSStart(), respectively. • Before μC/OS-IIis 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. Example 2

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

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

  43. 2 Mailboxes

  44. Main() OSTaskStkInit_FPE_x86(&ptos, &pbos, &size)

  45. 同學:加油, 快下課了! 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

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

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

  48. Task1()

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

More Related