Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
2012 / uCOS / uCOSII_ebook.pdf
Скачиваний:
90
Добавлен:
10.02.2015
Размер:
1.08 Mб
Скачать

OSCtxSw:

 

 

Save the

stack pointer at OSTCBCur->OSTCBStkPtr;

 

Call OSTaskSwHook();

(1)

OSTCBCur

= OSTCBHighRdy;

 

OSPrioCur = OSPrioHighRdy;

(2)

Load the

processor stack pointer with OSTCBHighRdy->OSTCBStkPtr;

 

POP all the processor registers from the stack;

 

Execute a Return from Interrupt instruction;

 

Listing 10.8, Pseudo-code for OSIntCtxSw()

10.03.04 OS_CPU_A.ASM, OSTickISR()

The code for this function in µC/ OS-II is identical to µC/OS and thus shouldn’t be altered.

10.04OS_CPU_C.C

A µC/OS-II port requires that you write six fairly simple C functions:

OSTaskStkInit()

OSTaskCreateHook()

OSTaskDelHook()

OSTaskSwHook()

OSTaskStatHook()

OSTimeTickHook()

The o nly function that is actually necessary is OSTaskStkInit(). The other five functions MUST be declared but don’t need to contain any code inside them.

10.04.01 OS_CPU_C.C, OSTaskStkInit()

In µC/OS,OSTaskCreate() was considered a processor specific function. It turned out that only a portion of OSTaskCreate() was actually processor specific. This portion has been extracted out of OSTaskCreate() and placed in a new function called OSTaskStkInit().

OSTaskStkInit() is only responsible for setting up the task’s stack to look as if an interrupt just occurred and

all the processor registers were pushed onto the task’s stack. To give you an example, listing 10.9 shows the µC/OS code for OSTaskCreate() for the Intel 80x86 real-mode, large model. Listing 10.10 shows the code for OSTaskStkInit() for the same processor but for µC/OS-II. As you can see by comparing the two listings, lines L10.9(2) through L10.9(18) have basically been extracted from OSTaskCreate() and placed in OSTaskStkInit(). In other words, everything after OS_EXIT_CRITICAL() L10.9(1) and calling

OSTCBInit() L10.9(19) has been moved to OSTaskStkInit().

You will notice that the code for µC/OS-II uses the new data types (see section 10.02.01, OS_CPU.H, Compiler specific data types). Also, instead of initializing all the processor registers to0x0000, I decided to initialize them with a value

that would make debugging a little easier. You should note that the initial value of a register when a task is created is not critical.

UBYTE OSTaskCreate(void (*task)(void *pd), void *pdata, void *pstk, UBYTE p)

{

UWORD OS_FAR *stk;

 

UBYTE

err;

 

OS_ENTER_CRITICAL();

 

if (OSTCBPrioTbl[p] == (OS_TCB *)0) {

 

OSTCBPrioTbl[p] = (OS_TCB *)1;

 

OS_EXIT_CRITICAL();

(1)

stk

= (UWORD OS_FAR *)pstk;

(2)

*--stk = (UWORD)FP_OFF(pdata);

(3)

*--stk = (UWORD)FP_SEG(task);

(4)

*--stk = (UWORD)FP_OFF(task);

(5)

*--stk = (UWORD)0x0202;

(6)

*--stk = (UWORD)FP_SEG(task);

(7)

*--stk = (UWORD)FP_OFF(task);

(8)

*--stk = (UWORD)0x0000;

(9)

*--stk = (UWORD)0x0000;

(10)

*--stk = (UWORD)0x0000;

(11)

*--stk = (UWORD)0x0000;

(12)

*--stk = (UWORD)0x0000;

(13)

*--stk = (UWORD)0x0000;

(14)

*--stk = (UWORD)0x0000;

(15)

*--stk = (UWORD)0x0000;

(16)

*--stk = (UWORD)0x0000;

(17)

*--stk = _DS;

(18)

err

= OSTCBInit(p, (void far *)stk); (19)

if (err == OS_NO_ERR) { if (OSRunning) {

OSSched();

}

} else {

OSTCBPrioTbl[p] = (OS_TCB *)0;

}

return (err); } else {

OS_EXIT_CRITICAL(); return (OS_PRIO_EXIST);

}

}

Listing 10.9, OSTaskCreate() for µC/OS

void *OSTaskStkInit (void (*task)(void *pd), void *pdata, void *ptos, INT16U opt)

{

INT16U *stk;

opt

= opt;

stk

= (INT16U *)ptos;

*stk-- = (INT16U)FP_SEG(pdata);

*stk-- = (INT16U)FP_OFF(pdata); *stk-- = (INT16U)FP_SEG(task); *stk-- = (INT16U)FP_OFF(task); *stk-- = (INT16U)0x0202; *stk-- = (INT16U)FP_SEG(task); *stk-- = (INT16U)FP_OFF(task); *stk-- = (INT16U)0xAAAA; *stk-- = (INT16U)0xCCCC; *stk-- = (INT16U)0xDDDD; *stk-- = (INT16U)0xBBBB; *stk-- = (INT16U)0x0000; *stk-- = (INT16U)0x1111; *stk-- = (INT16U)0x2222; *stk-- = (INT16U)0x3333; *stk-- = (INT16U)0x4444;

*stk = _DS;

return ((void *)stk);

}

Listing 10.10, OSTaskStkInit() for µC/OS-II

10.04.02 OS_CPU_C.C, OSTaskCreateHook()

OSTaskCreateHook() is a function that did not exist in µC/OS. If you are simply migrating from µC/OS to µC/OS-II then you can simply declare an empty function as shown in listing 10.11. You should note that if I didn’t assign ptcb to ptcb then some compilers would generate a warning indicating that the argument ptcb is not used.

#if OS_CPU_HOOKS_EN OSTaskCreateHook(OS_TCB *ptcb)

{

ptcb = ptcb;

}

#endif

Listing 10.11, OSTaskCreateHook() for µC /OS-II

You should also wrap the function declaration with the conditional compilation directive. The code for

OSTaskCreateHook() is generated only if OS_CPU_HOOKS_EN is set to 1 in OS_CFG.H. This

allows the user of your port to redefine all the hook functions in a different file.

10.04.03 OS_CPU_C.C, OSTaskDelHook()

OSTaskDelHook() is a function that did not exist in µC/OS. Again, if you are migrating from µC/OS to µC/OS-II then you can simply declare an empty function as shown in listing 10.12. You should note that if I didn’t assign ptcb to ptcb then some compilers would generate a warning indicating that the argument ptcb is not used.

#if OS_CPU_HOOKS_EN OSTaskDelHook(OS_TCB *ptcb)

{

ptcb = ptcb;

}

#endif

Listing 10.12, OSTaskDelHook() for µC/OS-II

You should also wrap the function declaration with the conditional compilation directive. The code for

OSTaskDelHook() is generated only if OS_CPU_HOOKS_EN is set to 1 in OS_CFG.H. This allows

the user of your port to redefine all the hook functions in a different file.

10.04.04 OS_CPU_C.C, OSTaskSwHook()

OSTaskSwHook()is also function that did not exist in µC/OS. If you are migrating from µC/OS to µC/OS-II then you can simply declare an empty function as shown in listing 10.13.

#if OS_CPU_HOOKS_EN OSTaskSwHook(void)

{

}

#endif

Listing 10.13, OSTaskSwHook() for µC/OS-II

You should also wrap the function declaration with the conditional compilation directive. The code for

OSTaskSwHook() is generated only if OS_CPU_HOOKS_EN is set to 1 in OS_CFG.H.

10.04.05 OS_CPU_C.C, OSTaskStatHook()

OSTaskStatHook() is also function that did not exist in µC/OS. You can simply declare an empty function as shown in listing 10.14.

Соседние файлы в папке uCOS