1 / 56

Chapter 13: Porting μC /OS-II

Chapter 13: Porting μC /OS-II. 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. μC/OS-II hardware/software architecture. Application Code (test.c). Processor independent implementations Scheduling policy

dkershaw
Télécharger la présentation

Chapter 13: Porting μ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 13: Porting μC/OS-II

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

  3. μC/OS-II hardware/software architecture 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

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

  5. 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) • TickISRprocedures

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

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

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

  9. OS_CPU.H • #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

  10. 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() X86 port #define OS_TASK_SW() asm INT uCOS

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

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

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

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

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

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

  17. Quiz registers PSW & PC Ret. addr pdata

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

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

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

  21. BC45 5dc6 0022 0080 5d7A 0031

  22. Ret. ADRS bp

  23. OSCTXSW

  24. OSTaskStkInit()- pdata passed to the stack

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

  26. Process Termination

  27. Stack layout @OSTaskDelSelf void OSTaskDelSelf() { OSTaskDel(OS_PRIO_SELF); }

  28. Stack layout @OSTaskDel OSTaskDel(OS_PRIO_SELF) { /*…*/ /*…*/ }

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

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

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

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

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

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

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

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

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

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

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

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

  41. 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() #define OS_TASK_SW() asm INT uCOS

  42. Pseudocode 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; }

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

  44. Pseudocode 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; }

  45. 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(); } }

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

  47. Pseudocode 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; }

  48. Pseudocode – an Old Version 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

More Related