【undone】
Task (Task) For UC / OS-II, just like the process corresponds to Windows, is an important part of the system kernel.
In this example, the task is established with OSTASKCREATEXT and uses custom Task_User_Data to count the usage of the stack. Statistics use the custom statistical function OSTASKSTATHOOK, which is the maximum difference from the book. In the os_cfg.h file, you need to define #define os_cpu_hooks_en 0, otherwise you cannot use a custom statistical function.
#include "incrudes.h"
/ ************************************************** *********************************************************** *******% constants ************************************************** *********************************************************** *************** / # Define task_start_stk_size 512 # define task1_stk_size 512 # define task2_stk_size 256
#define task_start_prio 4 # define task1_prio 5 # Define Task2_Prio 6
#define n_tasks 2 / * Semi-Tasks outside TaskStart * /
/ ************************************************** *********************************************************** ******** Types of***************************************** *********************************************************** *************** / TYPEDEF STRUCT {INT32U Max; INT32U min; int32u ave; int32u cur;} task_user_data; / ************** *********************************************************** **************************************** Variables ******** *********************************************************** ********************************************************* /
OS_STK TaskStartStk [TASK_START_STK_SIZE]; / * different for different stack task definition * / OS_STK Task1Stk [TASK1_STK_SIZE]; OS_STK Task2Stk [TASK2_STK_SIZE]; TASK_USER_DATA TaskUserData [N_TASKS]; / * save stack usage * /
/ ************************************************** *********************************************************** ******** Function protoypes ***************************************************** *********************************************************** **************** /
void Task1 (void * data); / * Function prototypes of tasks * / void Task2 (void * data); void TaskStart (void * data); / * Function prototypes of Startup task * / static void TaskStartCreateTasks (void); void OSTaskStatHook (void);
/ * $ Page * / / ***************************************************** *********************************************************** ************* Main ****************************************************** *********************************************************** ******************** /
Void main (void) {pc_dispclrscr (Disp_fgnd_white DISP_BGND_BLACK); / * Clear the screen * /
Osinit (); / * Initialize UC / OS-II * /
Pc_dossavereturn (); / * save environment to return to dos * / pc_vectset (ucos, osctxsw); / * Install UC / OS-II's Context Switch Vector * /
OSTaskCreateExt (TaskStart, (void *) 0, & TaskStartStk [TASK_START_STK_SIZE - 1], TASK_START_PRIO, TASK_START_PRIO, & TaskStartStk [0], TASK_START_STK_SIZE, (void *) 0, 0); OSStart (); / * Start multitasking * /} / * *********************************************************** *********************************************************** ****** Startup Task ********************************************************** *********************************************************** ************** / VOID TASKSTART (VOID * PDATA) {# if os_critical_method == 3 / * Allocate Storage for CPU Status Register * / OS_CPU_SR CPU_SR; #ENDIF INT16S Key;
PDATA = PDATA; / * Prevent Compiler Warning * /
OS_ENTER_CRITICAL (); pc_vectset (0x08, OsticksR); / * Install UC / OS-II's Clock Tick ISR * / PC_SETTICKRATE (OS_TICKS_PER_SEC); / * Reprogram Tick Rate * / OS_EXIT_CRITICAL ();
Osstatinit (); / * Initialize UC / OS-II's Statistics * /
TaskStartCreateTasks (); / * CREATE All the application tasks * /
PC_DISPSTR (0, 0, (INT8U *) "ID Maxstk Minstk Ave Cur CNT", Disp_FGND_WHITE); for (;;) {
IF (PC_GetKey (& Key) == True) {/ * See if key HAS been PRESSED * / IF (key == 0x1b) {/ * yes, See if it's the escape key * / pc_dosreturn (); / * return to dos * /}} Ostimedlyhmsm (0, 0, 1, 0); / * Wait One Second * /}}
/ ************************************************** *********************************************************** ******** CREATE TASKS *********************************************************** *********************************************************** **************** /
Static void taskstartcreatetasks (void) {
MEMSET (& TaskUserData [0], 0, n_tasks * sizeof (task_user_data)); TaskUserData [0] .min = 0xfffffffff; / * In order to be able to be the minimum number * / taskuserdata [1] .min = 0xfffffffffff; ostaskcreateext (Task1, void *) 0, & Task1Stk [TASK1_STK_SIZE - 1], TASK1_PRIO, TASK1_PRIO, & Task1Stk [0], TASK1_STK_SIZE, (void *) 0, OS_TASK_OPT_STK_CHK OS_TASK_OPT_STK_CLR); OSTaskCreateExt (Task2, (void *) 0, & Task2Stk [TASK2_STK_SIZE - 1] , Task2_prio, task2_prio, & task2stk [0], task2_stk_size, (void *) 0, OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);}
/ ************************************************** *********************************************************** ******** Tasks *************************************** *********************************************************** *************** / VOID TASK1 (void * pdata) {pdata = pdata; for (;;) {ostimedly (2);}}
Void Task2 (Void * PDATA) {INT16U As; PDATA = PDATA; AS = 32; for (;;) {ostimedly (10);}}
/ ************************************************** *********************************************************** ******** Custom Statistics Task ****************************************************** *********************************************************** ****************** / VOID OSTASKSTATHOK (VOID) {INT8U I, S; OS_STK_DATA STKDATA; INT32U STKUSED; static int32u callcnt; static int32u tasktotal [n_tasks]; char STR [ 50]; Callcnt; for (i = task1_prio; i
/ ************************************************** *********************************************************** ******** OS Initialization Hook * (End) ***************************************************** *********************************************************** ********************* / VOID OSISINIOKEND (VOID) {} / *************************** *********************************************************** *********************************** Task Creation Hook ********** *********************************************************** ****************************************************** / VOID OSTASKCREATEHOK ( OS_TCB * PTCB) {PTCB = PTCB; / * Prevent Compiler Warning * /}
/ ************************************************** *********************************************************** ******** Task deletion hook ******************************************************** *********************************************************** ***************** / VOID OSTASKDELHOOK (OS_TCB * PTCB) {PTCB = PTCB; / * prevent compiler warning * /}
/ ************************************************** *********************************************************** ******* iDLE Task hook ******************************************************** *********************************************************** ***************** / VOID OSTASKIDEHOK (VOID) {}
Void OstasksWhook (void) {
}
/ ************************************************** *********************************************************** ******** OSTCBINIT () hook ************************************************************ *********************************************************** ****************** / VOID OSTCBINITOOK (OS_TCB * PTCB) {PTCB = PTCB; / * Prevent Compiler Warning * /} / **************** *********************************************************** ****************************************************** Tick Hook * *********************************************************** *********************************************************** ***** / void ostimetickhook (void) {}