microc/os-ii : the real-time kernel prof. sung jo kim school of computer science and engineering

40
22年 6年 27年 MicroC/OS-II(Ch1) 1 MicroC/OS-II MicroC/OS-II : The Real- : The Real- Time Kernel Time Kernel Prof. Sung Jo Kim Prof. Sung Jo Kim School of Computer Science and School of Computer Science and Engineering Engineering Chung-Ang Univ. Chung-Ang Univ.

Upload: kamea

Post on 04-Jan-2016

73 views

Category:

Documents


1 download

DESCRIPTION

MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School of Computer Science and Engineering Chung-Ang Univ. INTRODUCTION. Brief History. Developed in C by Jean Labrosse in 1992 Currently, maintained by Micrium Inc. ( http://www.micrium.com/products/rtos/kernel/rtos.html ) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 1

MicroC/OS-IIMicroC/OS-II : The Real-Time : The Real-Time KernelKernel

Prof. Sung Jo KimProf. Sung Jo KimSchool of Computer Science and EngineeringSchool of Computer Science and Engineering

Chung-Ang Univ.Chung-Ang Univ.

Page 2: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 2

INTRODUCTIONINTRODUCTION

Page 3: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 3

Brief HistoryBrief History

Developed in C by Jean Labrosse in 1992Developed in C by Jean Labrosse in 1992 Currently, maintained by Currently, maintained by MicriumMicrium Inc. ( Inc. (

http://www.micrium.com/products/rtos/kernel/rtos.html) )

The current version is The current version is C/OS-II C/OS-II v3.10(v2.52 v3.10(v2.52 for textbookfor textbook))

Page 4: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 4

Brief HistoryBrief History

The new version The new version C/OS-IIC/OS-III I provides more provides more

featuresfeatures• Round-robin schedulingRound-robin scheduling

– Allow multiple tasks to run at the same priority levelAllow multiple tasks to run at the same priority level

• Near zero interrupt disable timeNear zero interrupt disable time• Unlimited number of application tasksUnlimited number of application tasks• Error checking and more. Error checking and more. 

  

Page 5: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 5

Brief HistoryBrief History

LicensingLicensing• Free distribution of Free distribution of C/OS-II C/OS-II source and object csource and object c

ode to accredited Colleges and Universities w/o rode to accredited Colleges and Universities w/o requiring licenseequiring license

• ‘‘Object Code Distribution License’ is required for Object Code Distribution License’ is required for commercial productscommercial products

Page 6: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 6

Brief HistoryBrief History C/OS-IIC/OS-II

• A highly portable, ROMable, scalable, A highly portable, ROMable, scalable, preemptive, real-time, deterministic multitasking preemptive, real-time, deterministic multitasking kernel for microprocessors, microcontrollers and kernel for microprocessors, microcontrollers and DSP-based devicesDSP-based devices

• Actual target: embedded systemActual target: embedded system• Easily portable to many processor (Easily portable to many processor (

http://www.micrium.com/products/rtos/kernel/ports.html) )

Page 7: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 7

Major available portsMajor available ports• ACTEL (Cortex-M1)ACTEL (Cortex-M1)• ALTERA (NIOS II)ALTERA (NIOS II)• Analog Devices (ADSP-21xx)Analog Devices (ADSP-21xx)• ARM by various 3ARM by various 3rdrd-party (Samsung, TI, PHILIPS, etc.)-party (Samsung, TI, PHILIPS, etc.)• ATMEL (SAM7, SAM9)ATMEL (SAM7, SAM9)• Energy Micro (Cortex-M4F)Energy Micro (Cortex-M4F)• Freescale (9S08)Freescale (9S08)• Fujitsu (FR-50, SPARClite)Fujitsu (FR-50, SPARClite)• Infineon (TriCore, 80C16x)Infineon (TriCore, 80C16x)• Intel (80x86)Intel (80x86)• Lattice(Mico32)Lattice(Mico32)

Page 8: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 8

Major available portsMajor available ports• LUMINARY Micro (Cortex-M3)LUMINARY Micro (Cortex-M3)• Microchip (PIC24)Microchip (PIC24)• MIPS Technologies (M14K)MIPS Technologies (M14K)• NXP (ARM7, ARM9)NXP (ARM7, ARM9)• Renesas (H8)Renesas (H8)• SAMSUNG (ARM7, ARM0)SAMSUNG (ARM7, ARM0)• ST (STR7, STR9)ST (STR7, STR9)• TI (MSP-430, TMS320)TI (MSP-430, TMS320)• Toshiba (Cortex-M3)Toshiba (Cortex-M3)• XILINX (MicroBlaze)XILINX (MicroBlaze)• Zilog (eZ80, Z-80 and Z-180)Zilog (eZ80, Z-80 and Z-180)

Page 9: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 9

C/OS-II C/OS-II FeaturesFeatures Open source codeOpen source code PortablePortable

• Written in ANSI C + target-specific code written Written in ANSI C + target-specific code written in assembly languagein assembly language

• Run on most 8-, 16-, 32- or 64-bit platformsRun on most 8-, 16-, 32- or 64-bit platforms• Portable data typesPortable data types

– typedef unsigned chartypedef unsigned char INT8UINT8U

– typedef unsigned inttypedef unsigned int INT16UINT16U

– typedef unsigned shorttypedef unsigned short INT16U (for 32-bit)INT16U (for 32-bit)

– typedef unsigned longtypedef unsigned long INT32UINT32U

Page 10: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 10

ROMableROMable• Designed for embedded applicationsDesigned for embedded applications• Embedded as part of productsEmbedded as part of products

Scalability using conditional compilationScalability using conditional compilation• May contain only needed features for a small May contain only needed features for a small

footprintfootprint• Depending on the processor, the size can be Depending on the processor, the size can be

reduced as small as between 5KB to 24KBreduced as small as between 5KB to 24KB

Page 11: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 11

Fully preemptible real-time, deterministic, Fully preemptible real-time, deterministic, multitasking kernel for microprocessors, multitasking kernel for microprocessors, microcontrollers and DSPsmicrocontrollers and DSPs

MultitaskingMultitasking• Manage uManage up to p to 6464 tasks tasks

– 8 system tasks 8 system tasks and and 56 application tasks56 application tasks– UUp to 63 app tasks allowedp to 63 app tasks allowed

• No round-robin allowedNo round-robin allowed– EEach task has a unique 64 priority levelsach task has a unique 64 priority levels

Page 12: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 12

DeterministicDeterministic• The execution time for most of the functions and The execution time for most of the functions and

services is both constant and known in advanceservices is both constant and known in advance• The execution time is independent of the number The execution time is independent of the number

of tasks currently runningof tasks currently running• ExceptionException

– OSTimeTick()OSTimeTick()– Some event flag services(e.g., Some event flag services(e.g., OSFlagCreate(), OSFlagCreate(),

OSFlagPost(),OSFlagPost(), etc.) etc.)

Page 13: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 13

Task stacksTask stacks• Each tasks requires its own stackEach tasks requires its own stack• Different stack sizes for different tasksDifferent stack sizes for different tasks• Exact size can be determined by stack-checking Exact size can be determined by stack-checking

featurefeature– Reduce the amount of RAM needed by an each Reduce the amount of RAM needed by an each

application codeapplication code– Use Use OSTaskStkChk()OSTaskStkChk()

Page 14: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 14

System servicesSystem services• Semaphores Semaphores • Mutual Exclusion Semaphores (to reduce priority Mutual Exclusion Semaphores (to reduce priority

inversions) inversions) • Event Flags Event Flags • Message Mailboxes Message Mailboxes • Message Queues Message Queues • Task Management (Create, Delete, Change Task Management (Create, Delete, Change

Priority, Suspend/Resume etc.) Priority, Suspend/Resume etc.) • Fixed Sized Memory Block Management Fixed Sized Memory Block Management • Time Management Time Management • Timer Management Timer Management

Page 15: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 15

Interrupt management: 255 levelsInterrupt management: 255 levels Robust and ReliableRobust and Reliable

• Used in 100s of commercial apps since 1992Used in 100s of commercial apps since 1992• Suitable for Safety Critical Systems common to Suitable for Safety Critical Systems common to

Aviation and Medical productsAviation and Medical products• Certifiable for use in Safety Critical SystemsCertifiable for use in Safety Critical Systems

– A Validation Suite provides all of the documentation A Validation Suite provides all of the documentation necessary to deliver µC/OS-II as a pre-certifiable necessary to deliver µC/OS-II as a pre-certifiable software component for safety critical systemssoftware component for safety critical systems

– Include avionics RTCA DO-178B and EUROCAE/ Include avionics RTCA DO-178B and EUROCAE/ ED-12B, medical FDA 510(k), and IEC 61508 ED-12B, medical FDA 510(k), and IEC 61508 standard for transportation and nuclear systemsstandard for transportation and nuclear systems

Page 16: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 16

Robust and ReliableRobust and Reliable• Revised to follow most of the 127 MISRA C Revised to follow most of the 127 MISRA C

rules rules – The source code for µC/OS-II is now 99% compliant The source code for µC/OS-II is now 99% compliant

with the Motor Industry Software Reliability with the Motor Industry Software Reliability Association (MISRA) C Coding Standards. Association (MISRA) C Coding Standards.

– Improve the safety, reliability and predictability of C Improve the safety, reliability and predictability of C programs in critical automotive systems. programs in critical automotive systems.

– Members of the MISRA consortium : Delco Members of the MISRA consortium : Delco Electronics, Ford Motor Company, Jaguar Cars Ltd., Electronics, Ford Motor Company, Jaguar Cars Ltd., Lotus Engineering, Lucas Electronics, Rolls-Royce, Lotus Engineering, Lucas Electronics, Rolls-Royce, Rover Group Ltd., etc.Rover Group Ltd., etc.

Page 17: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 17

ApplicationsApplications AvionicsAvionics Medical equipment/devicesMedical equipment/devices Data communications equipmentData communications equipment White goods (Appliances)White goods (Appliances) Mobile phones, PDAs, MIDsMobile phones, PDAs, MIDs Industrial controlsIndustrial controls Consumer electronicsConsumer electronics AutomotiveAutomotive A wide range of embedded applicationsA wide range of embedded applications

Page 18: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 18

Chapter 1Chapter 1

Getting Started with Getting Started with C/OS-IIC/OS-II

Page 19: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 19

Installing Installing C/OS-II C/OS-II The installation environmentsThe installation environments

• Compiler: the Borland Turbo C++ 4.5Compiler: the Borland Turbo C++ 4.5• SW platform: Win95/98/Me/NT/2000/XP SW platform: Win95/98/Me/NT/2000/XP

computercomputer EExample #1: Basic multitaskingxample #1: Basic multitasking

• Each of 10 tasks displays a specific number at Each of 10 tasks displays a specific number at arbitrary locations on the screenarbitrary locations on the screen

• ##Tasks: 13Tasks: 13– 2 2 internal tasks: internal tasks:

– The idle task (OS_TaskIdle)(): Executed when all tasks are The idle task (OS_TaskIdle)(): Executed when all tasks are waiting either for events or for time to expirewaiting either for events or for time to expire

– A task for CPU usage statisticsA task for CPU usage statistics

– OtheOther 11 tasks are created (Listing1.1 on pp3)r 11 tasks are created (Listing1.1 on pp3)

Page 20: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 20

Installing Installing C/OS-II C/OS-II

Listing 1.2: Listing 1.2: main()main()• OsInit()OsInit()

– Invoked before any other servicesInvoked before any other services– Create two tasksCreate two tasks

– An idle task: Execute when no other task is ready to runAn idle task: Execute when no other task is ready to run– A statistic task: Compute CPU usageA statistic task: Compute CPU usage

• PC_DOSSaveReturn()PC_DOSSaveReturn()– Save the processors’s registers for proper return to DOSSave the processors’s registers for proper return to DOS– Must be called before setting Must be called before setting C/OS-II C/OS-II ccontext-switch vectorontext-switch vector

• PC_VectSet(uCos, OSCtxSw)PC_VectSet(uCos, OSCtxSw)– Install the Install the C/OS-II C/OS-II context-switch handlercontext-switch handler

– uCos: The interrupt vector # (0 ~ 255)uCos: The interrupt vector # (0 ~ 255)– OSCtxSw: The address of the interrupt/exception handlerOSCtxSw: The address of the interrupt/exception handler

Page 21: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 21

Installing Installing C/OS-II C/OS-II

Listing 1.2Listing 1.2• OSSemCreate(INT16U cnt)OSSemCreate(INT16U cnt)

– Create a binary semaphore to protect the random-number Create a binary semaphore to protect the random-number generator functiongenerator function

– Return a handle to the semaphore used for its referenceReturn a handle to the semaphore used for its reference

• OSStart()OSStart()– Start multitasking and give control to Start multitasking and give control to C/OS-II C/OS-II – At least one task should be created before being calledAt least one task should be created before being called

Listing 1.3: Listing 1.3: TaskStart()TaskStart()• pdata = pdata just for fake reference to avoid compiler pdata = pdata just for fake reference to avoid compiler

warningwarning• TaskStartDispInit()TaskStartDispInit()

– Make 25 consecutive calls to PC_DispStr(x,y,s,color) to display Make 25 consecutive calls to PC_DispStr(x,y,s,color) to display an ASCII string s in color from (x,y)an ASCII string s in color from (x,y)

Page 22: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 22

Installing Installing C/OS-II C/OS-II

Listing 1.3: Listing 1.3: TaskStart()TaskStart()• PC_VectSet(0x08, OSTickISP): Replace the PC_VectSet(0x08, OSTickISP): Replace the

address of the DOS tick service with one used by address of the DOS tick service with one used by C/OS-IIC/OS-II

• PPC-SetTickRate(OS_TICKS_PER_SEC): C-SetTickRate(OS_TICKS_PER_SEC): CChange hange the tick rate to 200 rather than 18.2Hzthe tick rate to 200 rather than 18.2Hz

• OOSStatInit(): Determine the speed of CPU to find SStatInit(): Determine the speed of CPU to find out the actual usage of the CPUout the actual usage of the CPU

• TaskStartCreateTasks(): Create N_TASKS TaskStartCreateTasks(): Create N_TASKS identical tasksidentical tasks

Page 23: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 23

Installing Installing C/OS-II C/OS-II

Listing 1.3: Listing 1.3: TaskStart()TaskStart()• TaskStartDisp(): Display various information at TaskStartDisp(): Display various information at

the bottom of the DOS windowthe bottom of the DOS window• PC_GetKey(): Check if a key is pressedPC_GetKey(): Check if a key is pressed• OSTimeDlyHMSM(0, 0, 1, 0): Suspend the OSTimeDlyHMSM(0, 0, 1, 0): Suspend the

current task for 1 sec. to initiate next most current task for 1 sec. to initiate next most important taskimportant task

Page 24: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 24

Installing Installing C/OS-II C/OS-II

Listing 1.4: Listing 1.4: TaskStartCreateTasks()TaskStartCreateTasks()• OSTaskCreate(Task, (void *) OSTaskCreate(Task, (void *)

&TaskData[i],&TaskStk[i][TASK_STK_SIZE-&TaskData[i],&TaskStk[i][TASK_STK_SIZE-1], i+1)1], i+1)

– Task() places an ASCII character at a random locationTask() places an ASCII character at a random location– Create tasks with priorities 1 through 10Create tasks with priorities 1 through 10

Page 25: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 25

Installing Installing C/OS-II C/OS-II

Listing 1.5: Listing 1.5: Task()Task()• OSSemPend(RandomSem, 0, &err)OSSemPend(RandomSem, 0, &err)

– Acquire the semaphore to guard access to random# Acquire the semaphore to guard access to random# generatorgenerator

– Timeout: The value of 0 means no timeoutTimeout: The value of 0 means no timeout

• OSTimeDly(1)OSTimeDly(1)– Notify Notify C/OS-II C/OS-II that it’s donethat it’s done– Delay the current task for 1 clock tick (5ms)Delay the current task for 1 clock tick (5ms)

Page 26: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 26

Installing Installing C/OS-II C/OS-II

Example #2 showsExample #2 shows• The amount of stack space used by each taskThe amount of stack space used by each task• The amount of free stack spaceThe amount of free stack space• The execution time of the stack-checking functionThe execution time of the stack-checking function

– Useful when we don’t know Useful when we don’t know – How much stack space needs to be allocated for each taskHow much stack space needs to be allocated for each task– How much execution time it takes to determine each task How much execution time it takes to determine each task

stack sizestack size

Page 27: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 27

Installing Installing C/OS-II C/OS-II

Listing 1.7: Listing 1.7: main()main()• PC_ElapsedInit()PC_ElapsedInit()

– Initialize the elapsed-time-measurement functionInitialize the elapsed-time-measurement function

– Measure the execution time of PC_ElapsedStart() and Measure the execution time of PC_ElapsedStart() and PC_ElapsedStop() PC_ElapsedStop()

• OSTaskStkInit_FPE_x86(&pptos,&ppbos,&psizeOSTaskStkInit_FPE_x86(&pptos,&ppbos,&psize))

– Modify the top-of-stack pointerModify the top-of-stack pointer

– Called prior to calling OSTaskCreateExt()Called prior to calling OSTaskCreateExt()

– Initialize the stack frame of each task for ft-pt Initialize the stack frame of each task for ft-pt operations for Borland v3.x and 4.5x compilersoperations for Borland v3.x and 4.5x compilers

Page 28: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 28

Installing Installing C/OS-II C/OS-II

Listing 1.7: Listing 1.7: main()main()• OSTaskCreateExt(): Also, modify the stack and OSTaskCreateExt(): Also, modify the stack and

check the stack size at run timecheck the stack size at run time– Pass the new TOS pointer modified by Pass the new TOS pointer modified by

OSTaskStkInit_FPE_x86()OSTaskStkInit_FPE_x86()– Pass a task ID, which can be any valuePass a task ID, which can be any value– Pass the new size modified by Pass the new size modified by

OSTaskStkInit_FPE_x86()OSTaskStkInit_FPE_x86()– Pass the new TOS pointer modified by Pass the new TOS pointer modified by

OSTaskStkInit_FPE_x86()OSTaskStkInit_FPE_x86()– PPass a TCB extension pointerass a TCB extension pointer– A A set of options necessary for stack-size checking and set of options necessary for stack-size checking and

stack clearingstack clearing

Page 29: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 29

Installing Installing C/OS-II C/OS-II

Listing 1.8: Listing 1.8: TaskStart()TaskStart()• TaskStartDispInit() initializes the displayTaskStartDispInit() initializes the display• OOSMboxCreate(void *msg)SMboxCreate(void *msg)

– CCreate and initialize a mailboxreate and initialize a mailbox– EEmpty when msg is NULLmpty when msg is NULL– AAllow tasks and ISRs to send a pointer-sized variable to one llow tasks and ISRs to send a pointer-sized variable to one

or more tasksor more tasks

• TaTaskskStStartartCrCreateeateTaTaskssks(())– CCreate 6 tasks using OSTaskCreateExt()reate 6 tasks using OSTaskCreateExt()

• OOSTimeDly(OS_TICKS_SEC)STimeDly(OS_TICKS_SEC)– DDelay TaskSart() for OS_TICKS_SEC tickselay TaskSart() for OS_TICKS_SEC ticks

Page 30: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 30

Installing Installing C/OS-II C/OS-II

Listing 1.9: Listing 1.9: Task1()Task1()• Check the size of the stack for each of 7 Check the size of the stack for each of 7

application tasksapplication tasks• 6 6 tasks created by TaskStart() and TaskStart() tasks created by TaskStart() and TaskStart()

itselfitself• PC_ElapsedStop() returns the time difference in PC_ElapsedStop() returns the time difference in

secsec• OSTaskStkChk()OSTaskStkChk()

– Determine the actual stack usage of a taskDetermine the actual stack usage of a task– Two argumentsTwo arguments

– The task priority of the task to checkThe task priority of the task to check– A pointer to a data structure to hold task’s stack informationA pointer to a data structure to hold task’s stack information

Page 31: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 31

Installing Installing C/OS-II C/OS-II

• PC_DispStr(x, y, *s, color)PC_DispStr(x, y, *s, color)– x and y: Specify the coordinates (col, row) where the x and y: Specify the coordinates (col, row) where the

1st char appears1st char appears– s: A pointer to the array of chars to displays: A pointer to the array of chars to display– color: Specify the color combination of the chars to be color: Specify the color combination of the chars to be

displayeddisplayed

TTask2()’s wheel spins clockwise at 5 rotations ask2()’s wheel spins clockwise at 5 rotations per second while Task3()’s 2.5 rotations per per second while Task3()’s 2.5 rotations per secondsecond

Page 32: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 32

Installing Installing C/OS-II C/OS-II

Listing 1.11Listing 1.11• OOSMboxPost(pevent, msg)SMboxPost(pevent, msg)

– pevent: a pointer to the mailboxpevent: a pointer to the mailbox– msg: the actual message sent to the taskmsg: the actual message sent to the task

• OOSMBoxPend(AckMbox, 0, &err)SMBoxPend(AckMbox, 0, &err)– Task4() waits for an Ack from Task5Task4() waits for an Ack from Task5– TThe 2he 2ndnd argument specifies a timeout as an integral argument specifies a timeout as an integral

number of clock ticksnumber of clock ticks

Page 33: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 33

Installing Installing C/OS-II C/OS-II

Example #3 uses (Fig. 1.5)Example #3 uses (Fig. 1.5)• TThe TCB extension capabilityhe TCB extension capability• TThe user-defined context-switch hookhe user-defined context-switch hook• TThe user-defined statistic-task hookhe user-defined statistic-task hook• MMessage queuesessage queues

Listing 1.13Listing 1.13• TTASK_USER_DATA holds additional information about ASK_USER_DATA holds additional information about

a taska task– Task nameTask name– The number of times that a task has executedThe number of times that a task has executed– Task execution timeTask execution time– Total task execution timeTotal task execution time

• OS_EVENT (Listing 6.1 on pp154): Maintain the state of OS_EVENT (Listing 6.1 on pp154): Maintain the state of an ECBan ECB

Page 34: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 34

Installing Installing C/OS-II C/OS-II

Listing 1.15, Listing 1.15, TaskStart()TaskStart()• OSQCreate(start, size) creates a message queueOSQCreate(start, size) creates a message queue

– start: the base address of the message storage areastart: the base address of the message storage area

– Size: the number of entries of the message storage areaSize: the number of entries of the message storage area

– Returned value: a pointer to the ECB allocated to the Returned value: a pointer to the ECB allocated to the queuequeue

• TaskStartCreateTasks()TaskStartCreateTasks()– Create six tasksCreate six tasks

– Each task is assigned an entry in the TaskUserData[] Each task is assigned an entry in the TaskUserData[] arrayarray

Page 35: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 35

Installing Installing C/OS-II C/OS-II

Listing 1.16, Listing 1.16, Task1() ~ Task4()Task1() ~ Task4()• OSQPend((*pevent, timeout, *err)OSQPend((*pevent, timeout, *err)

– pevent: a pointer to the queue from which the pevent: a pointer to the queue from which the messages are receivedmessages are received

– TimeoutTimeout– Maximum timeout = 65535Maximum timeout = 65535

– Wait forever if timeout = 0Wait forever if timeout = 0

– Returned value: a message sent by a task or an ISRReturned value: a message sent by a task or an ISR

Page 36: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 36

Installing Installing C/OS-II C/OS-II

Listing 1.17, Listing 1.17, C/OS-II’C/OS-II’s hookss hooks• If OS_CPU_HOOKS_EN = 0, we can declare the If OS_CPU_HOOKS_EN = 0, we can declare the

hook function in a different filehook function in a different file LListing 1.18, isting 1.18, empty hook functionsempty hook functions LListing 1.19, isting 1.19, OSTaskSwHook()OSTaskSwHook()

• Called when Called when C/OS-II C/OS-II switches from a low switches from a low priority to a higher priority taskpriority to a higher priority task

• PPC_ElapsedStop() return the execution time of C_ElapsedStop() return the execution time of the task being switched outthe task being switched out

Page 37: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 37

Installing Installing C/OS-II C/OS-II

• TTask control block(OS_TCB) (pp81)ask control block(OS_TCB) (pp81)– Maintain the task state when being preemptedMaintain the task state when being preempted

– OSTCBCur points to the TCB of the current taskOSTCBCur points to the TCB of the current task

– OSTCBExtPtrOSTCBExtPtr– A pointer to a user-definable TCB extensionA pointer to a user-definable TCB extension

– Only used by OSTaskCreateExt()Only used by OSTaskCreateExt()

Page 38: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 38

Installing Installing C/OS-II C/OS-II

Listing 1.20, Listing 1.20, OSTaskStatHook()OSTaskStatHook()• Called every second from the ststistics task Called every second from the ststistics task

OSTaskStat()OSTaskStat()• DispTaskStat(i)DispTaskStat(i)

– Display individual statistics on the screenDisplay individual statistics on the screen

– Display each task nameDisplay each task name

Page 39: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 39

Installing Installing C/OS-II C/OS-II

Example #4Example #4• A port is some processor-specific codeA port is some processor-specific code• Create 10 identical tasks, each running 200 times Create 10 identical tasks, each running 200 times

per secondper second• Each task computes the sine and cosine of an Each task computes the sine and cosine of an

angleangle• The angle is offset by 36 degreesThe angle is offset by 36 degrees• Every time the task executes, the angle is Every time the task executes, the angle is

incremented by 0.01incremented by 0.01

Page 40: MicroC/OS-II : The Real-Time Kernel Prof. Sung Jo Kim School  of Computer Science and Engineering

23年 4月 20日 MicroC/OS-II(Ch1) 40

Installing Installing C/OS-II C/OS-II

Listing 1.21, Listing 1.21, TaskStartCreateTasks()TaskStartCreateTasks()• ((void *)&TaskData[i]void *)&TaskData[i]

– An argument passed to a task when it is first startedAn argument passed to a task when it is first started

– OS_TASK_OPT_SAVE_FPOS_TASK_OPT_SAVE_FP– Save floating-point registers during a context switchSave floating-point registers during a context switch

LListing 1.22, Task()isting 1.22, Task()• OOSTimeDly(1)STimeDly(1)

– EEach task is delayed by 1 tick(50ms)ach task is delayed by 1 tick(50ms)

– EEach task executes 200 times per secondach task executes 200 times per second