1. ostaskcreate ()
Ostaskcreate () creates a new task. You can create a task before the multi-task environment starts or run the task. Note: tasks cannot be created in ISR. A task must be in an infinite loop structure.
The source code is as follows:
# If OS _task_create_en> 0/* condition compilation, whether to allow task creation */
Int8u ostaskcreate (void (* task) (void * PD),/* function pointer, void * Pd is the function parameter */
Void * pdata,/* parameter passed during Task creation */
OS _stk * ptos,/* pointer to the top of the stack Task Stack */
Int8u PRIO)/* task priority */
{
# If OS _critical_method = 3/* allocate storage for CPU Status Register */
OS _cpu_sr cpu_sr;
# Endif
OS _stk * PSP;
Int8u err;
# If OS _arg_chk_en> 0
If (PRIO> OS _lowest_prio) {/* check whether the priority is within the permitted priority */
Return (OS _prio_invalid );
}
# Endif
OS _enter_critical ();/* Guanzhong disconnection */
If (ostcbpriotbl [PRIO] = (OS _tcb *) 0) {/* determines whether the priority of a task exists. If not, set this parameter */
Ostcbpriotbl [PRIO] = (OS _tcb *) 1;/* after the priority is set to 1, the service can be interrupted ,*/
/* Do not worry about conflicts because the priority is already occupied */
OS _exit_critical ();
PSP = (OS _stk *) ostaskstkinit (task, pdata, ptos, 0 );
/* Initialize the stack. This function is related to the specific hardware. OS _cpu_c.c */
Err = OS _tcbinit (PRIO, PSP, (OS _stk *) 0, 0, 0, (void *) 0, 0);/* See the description of this function */
If (ERR = OS _no_err ){
OS _enter_critical ();
Ostaskctr ++;/* Add 1 to the task counter to count the number of running tasks */
OS _exit_critical ();
If (osrunning = true) {/* if a new task is created while the task is running, perform */
OS _sched ();/* schedule tasks to ensure that the tasks with the highest priority are in the running state */
}
} Else {
OS _enter_critical ();
Ostcbpriotbl [PRIO] = (OS _tcb *) 0;/* If the task fails to be created, set the priority to 0 and discard this */
/* The task priority can be used when other tasks are created */
/* This priority */
OS _exit_critical ();
}
Return (ERR);/* failed to create the task, error code returned */
}
OS _exit_critical ();
Return (OS _prio_exist);/* returns OS _prio_exist to indicate that the task priority already exists */
}
# Endif
2. ostaskcreateext ()
Ostaskcreateext () is an extension function of ostaskcreate (). More content settings are allowed.
The source code is as follows:
# If OS _task_create_ext_en> 0
Int8u ostaskcreateext (void (* task) (void * PD),/* same as above */
Void * pdata,/* same as above */
OS _stk * ptos,/* same as above */
Int8u Prio,/* same as above */
Int16u ID,/* task id, version 2.52, no practical effect, reserved for extension */
OS _stk * pbos,/* pointer to the bottom of the stack, used for the ostaskstkchk () function */
Int32u stk_size,/* specifies the size of the task stack, determined by the OS _stk type */
Void * pext,/* defines the pointer of the data structure as an extension of TCB */
Int16u OPT)/* information stored in task operations, see uCOS-II.H */
{
# If OS _critical_method = 3/* allocate storage for CPU Status Register */
OS _cpu_sr cpu_sr;
# Endif
OS _stk * PSP;
Int8u err;
# If OS _arg_chk_en> 0
If (PRIO> OS _lowest_prio) {/* Make sure priority is within allowable range */
Return (OS _prio_invalid );
}
# Endif
OS _enter_critical ();
If (ostcbpriotbl [PRIO] = (OS _tcb *) 0 ){
/* Make sure task doesn' t already exist at this priority */
/* Reserve the priority to prevent others from doing... */
/*... The same thing until task is created .*/
Ostcbpriotbl [PRIO] = (OS _tcb *) 1; OS _exit_critical ();
If (OPT & OS _task_opt_stk_chk )! = 0 × 0000) |
/* See if Stack checking has been enabled */
(OPT & OS _task_opt_stk_clr )! = 0 ×0000 )){
/* See if Stack needs to be cleared */
# If OS _stk_growth = 1
(Void) memset (pbos, 0, stk_size * sizeof (OS _stk ));
# Else
(Void) memset (ptos, 0, stk_size * sizeof (OS _stk ));
# Endif
}
PSP = (OS _stk *) ostaskstkinit (task, pdata, ptos, OPT );
/* Initialize the task's stack */
Err = OS _tcbinit (PRIO, PSP, pbos, ID, stk_size, pext, OPT );
If (ERR = OS _no_err ){
OS _enter_critical ();
Ostaskctr ++;/* increment the # tasks counter */
OS _exit_critical ();
If (osrunning = true) {/* Find HPT if multitasking has started */
OS _sched ();
}
} Else {
OS _enter_critical ();
Ostcbpriotbl [PRIO] = (OS _tcb *) 0;/* Make this priority avail. To others */
OS _exit_critical ();
}
Return (ERR );
}
OS _exit_critical ();
Return (OS _prio_exist );
}
# Endif