Ucos ii creates the first task 2

Source: Internet
Author: User

 

Ucos ii creates the first task 2

The following describes the task creation process.

Is the Implementation Program for creating task functions.

Int8u ostaskcreate (void (* task) (void * PD), void * pdata, OS _stk * ptos, int8u PRIO)

{

Void * PSP;

Int8u err;

 

 

If (PRIO> OS _lowest_prio) {(1)

Return (OS _prio_invalid );

}

OS _enter_critical ();

If (ostcbpriotbl [PRIO] = (OS _tcb *) 0) {(2)

Ostcbpriotbl [PRIO] = (OS _tcb *) 1; (3)

OS _exit_critical (); (4)

PSP = (void *) ostaskstkinit (task, pdata, ptos, 0); (5)

Err = ostcbinit (PRIO, PSP, (void *) 0, 0, 0, (void *) 0, 0); (6)

If (ERR = OS _no_err) {(7)

OS _enter_critical ();

Ostaskctr ++; (8)

Ostaskcreatehook (ostcbpriotbl [PRIO]); (9)

OS _exit_critical ();

If (osrunning) {(10)

Ossched (); (11)

}

} Else {

OS _enter_critical ();

Ostcbpriotbl [PRIO] = (OS _tcb *) 0; (12)

OS _exit_critical ();

}

Return (ERR );

} Else {

OS _exit_critical ();

Return (OS _prio_exist );

}

}

 

1. ostaskcreate () first checks whether the priority assigned to the task is valid [l4.1 (1)]. The task priority must be between 0 and OS _lowsest_prio. OS _lowsest_prio is defined in the OS _cfg.h file as follows: # define OS _lowest_prio 12. Why is it 12 instead of 64? If you have any understanding, you can tell mcu21.

2. Then, make sure that the task [l4.1 (2)] has not been created in the specified priority. When μC/OS-ⅱ is used, each task has a specific priority. If a priority is idle, μ c/OS-ⅱ reserves this priority by placing a non-null pointer in ostcbpriotbl [] [l4.1 (3)]. This allows ostaskcreate () to re-allow the interruption of [l4.1 (4)] When setting other parts of the task data structure.

3. Then, ostaskcreate () calls ostaskstkinit () [l4.1 (5)], which is responsible for creating the stack of the task. That's right, it's ostaskstkinit (). When the system was transplanted, we worked so hard to write it in OS _cpu_c.c. It turned out that we were here. The ostaskstkinit () function returns the new stack top (PSP) and stores it in 0s_tcb of the task. Note that you must set the fourth parameter opt passed to the ostaskstkinit () function to 0, because ostaskcreate () is different from ostaskcreateext, it does not support setting different options for the task creation process, so no options can be passed to ostaskstkinit () through the OPT parameter ().

4. once the ostaskstkinit () function completes the stack creation task, ostaskcreate () calls ostcbinit () [l4.1 (6)], which is also the core of task creation, obtains and initializes an OS _tcb instance from the idle OS _tcb pool. The ostcbinit () function first obtains an OS _tcb from the OS _tcb buffer pool. If the OS _tcb pool has an idle OS _tcb, It is initialized. Note that once the OS _tcb is assigned, the creator of the task already has it. Even if other tasks are created in the kernel, these new tasks cannot perform any operation on the assigned OS _tcb. Therefore, ostcbinit () allows interruption and continues initializing the data unit of OS _tcb.

5. after returning from ostcbinit (), ostaskcreate () needs to check the Returned Code [l4.1 (7)]. If the return code is successful, ostaskctr [l4.1 (8)] is added. ostaskctr is used to save the number of generated tasks. If ostcbinit () fails to return, set the entry of ostcbpriotbl [PRIO] to 0 [l4.1 (12)] to give up the priority of the task.

6. Then, ostaskcreate () calls ostaskcreatehook () [l4.1 (9)], and ostaskcreatehook () is a user-defined function used to extend the ostaskcreate () function. This step may be unavailable. In the mcu21 project, ostaskcreatehook () was not written during migration ()Of, So this function is not called ..

7. if the ostaskcreate () function is called during the execution of a task (that is, osrunning is set to true [l4.1 (10)]), the task scheduling function is called [l4.1 (11)] to determine whether a new task has a higher priority than the original task. If the new task has a higher priority, the kernel will switch from the old task to the new task. If the new task has been created before the multi-task scheduling starts (that is, if you have not called osstart (), the job scheduling function OS _sched () will not be called.

After the above seven steps, a task is roughly created.

These two sentences are used multiple times in the above program, which is very important and used multiple times.

OS _enter_critical (); // Guanzhong disconnection

 

OS _exit_critical (); // interrupt

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.