Chapter 13: Porting μC/OS-II 1. Outline Requirements Hardware Software Tasks of Porting µC/OS-II OS_CPU_C.H OS_CPU_C.C OS_CPU_A.ASM Testing a port 2.

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

Interrupts Chapter 8 – pp Chapter 10 – pp Appendix A – pp 537 &
Embedded System Presentation Nguyễn Trần Quang Nguyễn Công Vũ 1µC/OS-II.
Chapter 6 Limited Direct Execution
Chapter 14 80x86, Large Model Port Porting to real mode and for the large model –Real mode: 1Mb memory space (pointers in this model require 32 bits) –In.
Department of Mathematics and Computer Science μC/OS-II 2IN60: Real-time Architectures (for automotive systems)
Architectural Support for OS March 29, 2000 Instructor: Gary Kimura Slides courtesy of Hank Levy.
LOGO Chapter 1 Interrupt handling. hardware interrupt Under x86, hardware interrupts are called IRQ's. When the CPU receives an interrupt, it stops whatever.
CSS 372 Lecture 1 Course Overview: CSS 372 Web page Syllabus Lab Ettiquette Lab Report Format Review of CSS 371: Simple Computer Architecture Traps Interrupts.
Figure 2.8 Compiler phases Compiling. Figure 2.9 Object module Linking.
Chapter 7 Interupts DMA Channels Context Switching.
RapUp Dynamic Allocation of Memory in C Last HW Exercise Review for Final Final Exam Next Thursday – Same Time / Same Place.
Hardware Support for Operating Systems Sunny Gleason Vivek Uppal COM S 414
Mehmet Can Vuran, Instructor University of Nebraska-Lincoln Acknowledgement: Overheads adapted from those provided by the authors of the textbook.
MicroC/OS-II Embedded Systems Design and Implementation.
Midterm Wednesday 11/19 Overview: 25% First Midterm material - Number/character representation and conversion, number arithmetic - DeMorgan’s Law, Combinational.
Real-Time Operating Systems Suzanne Rivoire November 20, 2002
ΜC/OS-III Tasks Akos Ledeczi EECE 354, Fall 2010 Vanderbilt University.
Introduction to Embedded Systems
System Calls 1.
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
Kernel structure & Task management © P.J.Fondse April 2010.
OPERATING SYSTEM OVERVIEW. Contents Basic hardware elements.
The  C/OS-II Real-Time Operating System.  C/OS-II Real-time kernel –Portable, scalable, preemptive RTOS –Ported to over 90 processors Pronounced “microC.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto OS-Related Hardware.
2-1 The critical section –A piece of code which cannot be interrupted during execution Cases of critical sections –Modifying a block of memory shared by.
Chapter 1 Process and Thread. 1.2 process The address space of a program – Text – Code – Stack – Heap A set of registers – PC – SP Other resources – Files.
Scheduling in µC/OS-III Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University.
CSNB374: Microprocessor Systems Chapter 5: Procedures and Interrupts.
Module R3 Process Scheduling. Module R3 involves the creation of a simple “Round Robin” dispatcher. The successful completion of this module will require.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
1 CSE451 Architectural Supports for Operating Systems Autumn 2002 Gary Kimura Lecture #2 October 2, 2002.
Operating System Structure A key concept of operating systems is multiprogramming. –Goal of multiprogramming is to efficiently utilize all of the computing.
RTX - 51 Objectives  Resources needed  Architecture  Components of RTX-51 - Task - Memory pools - Mail box - Signals.
ECGR-6185 µC/OS II Nayana Rao University of North Carolina at Charlotte.
We will focus on operating system concepts What does it do? How is it implemented? Apply to Windows, Linux, Unix, Solaris, Mac OS X. Will discuss differences.
Operating Systems 1 K. Salah Module 1.2: Fundamental Concepts Interrupts System Calls.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
6-1 Infineon 167 Interrupts The C167CS provides 56 separate interrupt sources that may be assigned to 16 priority levels. The C167CS uses a vectored interrupt.
Concurrency, Processes, and System calls Benefits and issues of concurrency The basic concept of process System calls.
Ucosii porting on pxa270  粘家華 R  林育佳 R
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
Preemptive Scheduling
Lab 3 Department of Computer Science and Information Engineering National Taiwan University Lab3 – μC/OS 2015/10/13/ 13 1.
1 J.O. KLEIN Institut Universitaire de Technologie de CACHAN Université PARIS SUD - FRANCE An Introduction to Real Time Operating System.
SOC Consortium Course Material SoC Design Laboratory Lab 8 Real-time OS - 1 Speaker: Yung-Chih Chen Advisor: Prof. Chun-Yao Wang November 17, 2003 Department.
Chapter 3: Kernel Structure 1. Objectives To understand what a task is. To learn how μ C/OS-II manages tasks. To know how an interrupt service routine.
Chapter 3: Kernel Structure 1. Objectives To understand what a task is. To learn how μ C/OS-II manages tasks. To know how an interrupt service routine.
Chapter 1: Getting Started with μC/OS-II 1. kernel Introduction 2 LinuxμC/OS-II Task (process) kernel Device driver User mode (0-3G) (Kernel mode) 3G-4G.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Chapter 5: Time Management 1. Time Management OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() 2.
Lab 5 Department of Computer Science and Information Engineering National Taiwan University Lab5 – μC/OS 2016/5/31/ 13 1.
Chapter 3: Kernel Structure
Chapter 1: Getting Started with μC/OS-II
Process concept.
Chapter 13: Porting μC/OS-II
Interrupts and signals
Microprocessor and Assembly Language
Chapter 4: Task Management
Protection of System Resources
Interrupt and Time Management in µC/OS-III
Scheduling in µC/OS-III
More examples How many processes does this piece of code create?
CSE 451: Operating Systems Autumn 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 596 Allen Center 1.
CSE 451: Operating Systems Autumn 2001 Lecture 2 Architectural Support for Operating Systems Brian Bershad 310 Sieg Hall 1.
CSE 451: Operating Systems Winter 2007 Module 2 Architectural Support for Operating Systems Brian Bershad 562 Allen Center 1.
CSE 451: Operating Systems Winter 2003 Lecture 2 Architectural Support for Operating Systems Hank Levy 412 Sieg Hall 1.
Computer System Laboratory
Chapter 14: 80x86 Port.
Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University
Presentation transcript:

Chapter 13: Porting μC/OS-II 1

Outline Requirements Hardware Software Tasks of Porting µC/OS-II OS_CPU_C.H OS_CPU_C.C OS_CPU_A.ASM Testing a port 2

μC/OS-II hardware/software architecture 3 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 CPU Timer Hardware Software

Requirements C compiler Interrupt support and a timer Interrupt can be disabled and enabled by C Support hardware stack The processor can load and store the stack pointer and other registers in memory (stack pointer is stored in TCB) 4

Porting Tasks of µ C/OS-II Setting the value of 2 #define constants (OS_CPU.H) – OS_CRITICAL_METHOD – OS_STK_GROWTH Declaring 11 data types (OS_CPU.H) – OS_STK Declaring 1~3 #define macros (OS_CPU.H) – Critical section Writing 10 simple functions in C (OS_CPU_C.C) – Hooks – OSTaskStkInit Writing 4 assembly language functions (OS_CPU_A.ASM) – Context switch (starting, ctx by ISR, ctx by AP) – TickISR – TickISR procedures 5

Development Tools A C compiler that generates reentrant code (each function has its stack space) A linker which is used to combine object files – Resolve references within these modules A locator which allow you to place the code and data anywhere in the memory map of the target processor 6

OS_CPU.H Compiler-Specific Data Type – BOOLEAN, INT8U, INT8S… The data type of a task’s stack and the status register – typedef unsigned int OS_STK – typedef unsigned short OS_CPU_SR 7

OS_CPU.H Critical Method – Type 1: enable and disable directly – Type 2: save the interrupt status onto the stack – Type 3: save the interrupt status into a local variables 8

OS_CPU.H 9 #if OS_CRITICAL_METHOD == 1 #define OS_ENTER_CRITICAL() asm CLI #define OS_EXIT_CRITICAL() asm STI #endif #if OS_CRITICAL_METHOD == 2 #define OS_ENTER_CRITICAL() asm {PUSHF; CLI} #define OS_EXIT_CRITICAL() asm POPF #endif #if OS_CRITICAL_METHOD == 3 #define OS_ENTER_CRITICAL() (cpu_sr = OSCPUSaveSR()) #define OS_EXIT_CRITICAL() (OSCPURestoreSR(cpu_sr)) #endif X86 port

OS_CPU.H OS_TASK_SW() – a macro that is invoked when µC/OS-II switches from a low-priority task to the highest-priority task. OS_TASK_SW() is called from user program. – OSIntExit() is called from ISR In this procedure (OS_Task_SW())… – Throw a software trap – The interrupt handler should vector to the assembly language function OSCtxSw() 10 X86 port #define OS_TASK_SW() asm INT uCOS

OS_CPU.H 11 typedef unsigned short OS_CPU_SR /*define sie of CPU status register*/

OS_CPU_C.C OSTaskStkInit() OSTaskCreateHook() OSTaskDelHook() OSTaskSwHook() OSTaskIdleHook() OSTaskStatHook() OSTimeTickHook() OSInitHookBegin() 12

OSTaskStkInit() This function is called by OSTaskCreate() and OSTAskCreateExt() to initialize the stack frame of a task as an interrupt has just occurred and all the processor registers have been pushed onto that stack. 13

14 PSW PC PSW PC Stack (Task1) Regs PSW PC PSW PC Stack (Task3) Regs main1() main2() main3() code (Task1) code (Task2) code (Task3) tcb = getHPT() asm {sp = tcb->sp} asm {popa} asm {iret} tcb = getHPT() asm {sp = tcb->sp} asm {popa} asm {iret} startHighRdy sp pc main() fun1() os_start() main() fun1() os_start() Stack (main) xxx ooo zzz kkk ggg fff qqqqq xxx ooo zzz kkk ggg fff qqqqq Ret. adrs pdata Ret. adrs pdata Ret. adrs pdata Ret. adrs pdata PSW PC PSW PC Stack (Task3) Regs Ret. adrs pdata Ret. adrs pdata

15 main1() xxx main1() xxx code (Task1) sp pc PSW PC PSW PC Regs tcb = getHPT() asm {sp = tcb->sp} asm {popa} asm {iret} tcb = getHPT() asm {sp = tcb->sp} asm {popa} asm {iret} startHighRdy local variables local variables PSW PC PSW PC Stack (Task3) Regs main2() main3() code (Task2) code (Task3) Ret. adrs pdata Ret. adrs pdata PSW PC PSW PC Stack (Task3) Regs Ret. adrs pdata Ret. adrs pdata Ret. adrs pdata Ret. adrs pdata

Context switch 16 OSMboxPost() { //... OS_Sched() OSPrioHighRdy=//Get pointer to HPT ready to run INT 0x80//OS_TASK_SW //... } PSW PC PSW PC //(4) ISR (interrupt service routine) PUSHA OSTCBCur->OSTCBStkPtr = SP SP = OSTCBHighRdy->OSTCBStkPtr POPA IRET PSW PC PSW PC Stack (task1) Stack (task2) (3) PUSH PSW PUSH PC+4 (3) PUSH PSW PUSH PC+4 Regs OSTCBStkPtr myFunction() { OSMBoxPost() } (2) Exception (software interrupt) (1) Mode Change (function call) Regs OSTCBStkPtr TCB (task1) TCB (task2) main2() { xxx yyy zzz } Ret. adrs pdata Ret. adrs pdata Ret. adrs pdata Ret. adrs pdata (4) POP PSW POP PC+4 (4) POP PSW POP PC+4

Quiz 17 Ret. addr pdata Ret. addr pdata PSW & PC registers

Pseudo code for OSTaskStkInit() 18 Ret. addr pdata Ret. addr pdata PSW & PC registers

OSTaskStkInit() Under μC/OS-II, a task looks like a C function with one argument. – Push the argument onto the stack – Pass the argument in one or more registers 19

OSTaskStkInit()- pdata passed to the stack 20 PSW & PC Regs Ret. adrs pdata Ret. adrs pdata

BC dc d7A 0031

22 bp Ret. ADRS

OSCTXSW 23

OSTaskStkInit()- pdata passed to the stack 24

OSTaskStkInit() – passed in register Because the compiler passed arguments to a function in registers, we need to find out which register is used to store pData 25

Process Termination 26

Stack layout void OSTaskDelSelf() { OSTaskDel(OS_PRIO_SELF); }

Stack layout OSTaskDel(OS_PRIO_SELF) { /*…*/ }

Stack layout OSTaskDel(OS_PRIO_SELF) { /*…*/ } 0xFF

Hook Functions If (OS_CPU_HOOK_EN == 1) – Hook functions are in OS_CPU_C.C If (OS_CPU_HOOK_EN == 0) – In other files 30

OSTaskCreateHook() & OSTaskInitHook() These functions are called when… – After: OS setting up most of OS_TCB – Before the OS_TCB is linked to the active task chain and before the task is made ready to run Interrupt has been enabled OSTaskInitHook is called immediately before OSTaskCreateHook 31

OSTaskDelHook() Called by OSTaskDel() It is called before unlinking the task from OS’s internal linked list of active tasks. This function is called with interrupt disabled 32

OSTaskSwHook() Called by OSCtxSw and OSIntCtxSw Two variables is meaningful – OSTCBCur//old task – OSTCBHighRdy //new task This function is called with interrupt disabled 33

OSTaskStatHook() This function is called once every second by OSTaskStat() 34

OSTimeTickHook() This function is called by OSTimeTick() OSTimeTick() is called before the tick start to process in order to give your port or application first claim to the tick. 35

OSTaskIdleHook() We can bring the processor to the power saving mode by placing “stop” instruction here. void OSTaskIdleHook(void) { asm(“STOP”); } 36

OSInitHookBegin()/ OSInitHookEnd() OSInitHookBegin() is called immediately upon entering OSInit(). OSInitHookEnd() is called at the end of OSInit(). 37

OS_CPU_A.ASM OSStartHighRdy() OSCtxSw() OSIntCtxSw() OSTickISR() 38

OSStartHighRdy() This function is called by OSStart() to start the highest priority task ready-to-run. OSStartHighRdy() assumes that OSTCBHighRdy() points to the TCB of the task with the highest priority. – OSTCBHighRdy() is set by OSStart() The function only does half a context switch – Restoring the registers of the highest priority task – Not saving the register of the previous task 39

Pseudocode 1.Call OSTaskSwHook 2.Set OSRunning = true 3.Get the stack pointer stack pointer = OSTCBHighRdy -> OSTCBStkPtr; 4.Restore all processor registers from the task’s stack 5.Execute “Return from interrupt” 40

OSCtxSw() A task level context switch is accomplished by issuing a software interrupt instruction. The sequence of events that leads µC/OS-II to vector to OSCtxSw() is as follows: – The current task calls a system call which causes a higher priority task ready to run. At the end of the service call, the OS calls OSSched(). – OSSched() loads the address of the highest priority task into OSTCBHighRdy and then executes the software interrupt or trap instruction by invoking the macro OS_TASK_SW() 41 #define OS_TASK_SW() asm INT uCOS

Pseudocode 42 The machine has saved the return address and status word void OSCtxSw(void) { Save processor registers; Save the current task ’ s stack pointer into\\ the current task ’ s OS_TCB: OSTCBCur->OSTCBStkPtr = Stack pointer; Call user definable OSTaskSwHook(); OSTCBCur = OSTCBHighRdy; OSPrioCur = OSPrioHighRdy; Get the stack pointer of the task to resume: Stack pointer = OSTCBHighRdy->OSTCBStkPtr; Restore all processor registers from the new task ’ s stack; Execute a return from interrupt instruction; }

OSTickISR() You MUST enable ticker interrupts AFTER multitasking has started, i.e. after calling OSStart(). – You should initialize and tick interrupts in the first task that executes following a call to OSStart(). 43

Pseudocode 44 void OSTickISR(void) { Save processor registers; Call OSIntEnter() or increment OSIntNesting; if (OSIntNesting == 1) OSTCBCur->OSTCBStkPtr = stack pointer Call OSTimeTick(); Call OSIntExit(); Restore processor registers; Execute a return from interrupt instruction; }

OSIntExit() void OSIntExit (void) { if (OSIntNesting == 0) { if (OSLockNesting == 0) { OSIntExitY = OSUnMapTbl[OSRdyGrp]; OSPrioHighRdy = (INT8U)((OSIntExitY << 3) + OSUnMapTbl[OSRdyTbl[OSIntExitY]]); if (OSPrioHighRdy != OSPrioCur) { OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy]; OSIntCtxSw(); } OS_EXIT_CRITICAL(); } 45

OSIntCtxSw() This function is called by OSIntExit() to perform context switch from ISR. Because it is called from ISR, all registers are properly saved. 46

Pseudocode 47 void OSIntCtxSw(void) { Call user-definable OSTaskSwHook(); OSTCBCur = OSTCBHighRdy; OSPrioCur = OSPrioHighRdy; Get the stack pointer of the task to resume: Stack pointer = OSTCBHighRdy->OSTCBStkPtr; Restore all processor registers from the new task’s stack; Execute a “return from interrupt” instruction; }

Pseudocode – an Old Version 48 void OSIntCtxSw(void) { Adjust the stack pointer to remove calls to: OSIntExit(), OSIntCtxSw() and possibly the push of the processor status word; Save the current task’s stack pointer into the current task’s OS_TCB: OSTCBCur->OSTCBStkPtr = Stack pointer; Call user definable OSTaskSwHook(); OSTCBCur = OSTCBHighRdy; OSPrioCur = OSPrioHighRdy; Get the stack pointer of the task to resume: Stack pointer = OSTCBHighRdy->OSTCBStkPtr; Restore all processor registers from the new task’s stack; Execute a return from interrupt instruction; }

49

Testing of a Port Steps – Ensure that the code compiles, assembles and links – Verify OSTaskStkInit and OSStartHighRdy – Verify OSCtxSw – Verify OSIntCtxSw and OSTickISR 50

Code Compiling, Assembling and Linking Ensure that the code compiles, assembles and links void main() { OSInit(); OSStart(); } 51

Verifying of OSTaskStkInit and OSStartHighRdy 1.Disable the statistic task OS_TASK_STAT_EN = 0 2.Step over the function OSInit() and then step into the for OSStart() 3.Step into OSStartHighRdy 4.Switch to the “Idle” task? – The register order  OSTaskStkInit – “Return” to the idle thread  OSStartHighRdy 52

Verifying of OSCtxSw() void main() { OSInit(); OSTaskCreate(TestTask, NULL, stack, 0); OSStart(); } Void TestTask(void *pData) { while(1) { OSTimeDly(1); } 53

Verifying of OSCtxSw() OSTimeDly()  OS_Sched()  OSCtxSw () When the return from interrupt is executed, the you should be in OS_TaskIdle. 54

Verifying of OSIntCtxSw() and OSTickISR() Installing of a timer interrupt handler Call OSTimeDly(1) to result in a context switch to the idle thread by using OSIntCtxSw() and OSTickISR() 55

Verifying of OSIntCtxSw() and OSTickISR() void main(void) { OSInit(); install the clock tick ISR; OSTaskCreate(TestTask); OSStart(); } Void TestTask(void *pData) { disable interrupt initialize the clock tick interrupt; enable interrupt while (1) { OSTimeDly(1); printf(“%d”, OSTimeGet()); } 56