ucos创建自己的任务

OSTaskCreate

Description

Tasks must be created in order for µC/OS-III to recognize them as tasks. You create a task by

calling OSTaskCreate() and by providing arguments specifying to µC/OS-III how the task will

be managed. Tasks are always created in the ready-to-run state.

void OSTaskCreate (OS_TCB *p_tcb,
 CPU_CHAR *p_name,
 OS_TASK_PTR p_task,
 void *p_arg,
 OS_PRIO prio,
 CPU_STK *p_stk_base,
 CPU_STK_SIZE stk_limit,
 CPU_STK_SIZE stk_size,
 OS_MSG_QTY q_size,
 OS_TICK time_quanta,
 void *p_ext,
 OS_OPT opt,
 OS_ERR *p_err)
 
 
 

Arguments

  • p_tcb is a pointer to the task’s OS_TCB to use. It is assumed that storage for the TCB of the task will be allocated by the user code. You can declare a “global” variable as follows, and pass a pointer to this variable to OSTaskCreate(): eg:OS_TCB MyTaskTCB;
  • p_name is a pointer to an ASCII string (NUL terminated) to assign a name to the task. The name can be displayed by debuggers or by µC/Probe.// 任务名字
  • p_task is a pointer to the task (i.e., the name of the function that defines the task) // 任务函数指针
   								//Listing - Task as an infinite loop
//Run to completion task:
void MyTask (void *p_arg)
 {
 OS_ERR err;
 /* Local variables */
 
 
 /* Do something with 'p_arg' */
 /* Task initialization */
 /* Task body (do some work) */
 OSTaskDel((OS_TCB *)0, &err);
 /* Check ’err” ... your code should never end up here! */
 }
  • p_arg is a pointer to an optional data area which is used to pass parameters to the task when it is created. When µC/OS-III runs the task for the first time, the task will think that it was invoked, and passed the argument p_arg. For example, you could create a generic task that handles an asynchronous serial port. p_arg can be used to pass task information about the serial port it will manage: the port address, baud rate, number of bits, parity, and more. p_arg is the argument received by the task shown below. //传递任务函数的参数

  • prio is the task priority. The lower the number, the higher the priority (i.e., the importance) of the task. If OS_CFG_ISR_POST_DEFERRED_EN is set to DEF_ENABLED in os_cfg.h, the user cannot use priority 0. Task priority must also have a lower number than OS_CFG_PRIO_MAX-1. Priorities 0, 1, OS_CFG_PRIO_MAX-2 and OS_CFG_PRIO_MAX-1 are reserved. In other words, a task should have a priority between 2 and OS_CFG_PRIO_MAX-3, inclusively.// 任务优先级

  • p_stk_base is a pointer to the task’s stack base address. The task’s stack is used to store local variables, function parameters, return addresses, and possibly CPU registers during an interrupt.// 任务栈空间基地址的指针。(先定义CPU运行时的栈空间)The task stack must be declared as follows:CPU_STK MyTaskStk[???]; The user would then pass p_stk_base the address of the first element of this array or, &MyTaskStk[0]. “???” represents the size of the stack.//基地址的指针 &MyTaskStk[0].

  • stk_limit is used to locate, within the task’s stack, a watermark limit that can be used to monitor and ensure that the stack does not overflow.//任务堆栈的限制,来确保任务的栈没有超出限制。用来进行提示和警告。

  • stk_size specifies the size of the task’s stack in number of elements. If CPU_STK is set to CPU_INT08U (see os_type.h), stk_size corresponds to the number of bytes available on the stack. If CPU_STK is set to CPU_INT16U, then stk_size contains the number of 16-bit entries available on the stack. Finally, if CPU_STK is set to CPU_INT32U, stk_size contains the number of 32-bit entries available on the stack.// 任务真正的栈空间。

  • q_size A µC/OS-III task contains an optional internal message queue (if OS_CFG_TASK_Q_EN is set to DEF_ENABLED in os_cfg.h). This argument specifies the maximum number of messages that the task can receive through this message queue. The user may specify that the task is unable to receive messages by setting this argument to 0. //

  • time_quanta the amount of time (in clock ticks) for the time quanta when round robin is enabled. If you specify 0, then the default time quanta will be used which is the tick rate divided by 10.// 时间片轮转(确保每个任务都能被执行cosiii才有),时间基准系统滴答定时器。时间片的量。

  • p_ext is a pointer to a user-supplied memory location (typically a data structure) used as a TCB extension. For example, the user memory can hold the contents of floating-point registers during a context switch. // 任务控制块的扩展信息

  • opt contains task-specific options. Each option consists of one bit. The option is selected when the bit is set. The current version of µC/OS-III supports the following options:// 提供了一些特殊选项

    • OS_OPT_TASK_NONE

      specifies that there are no options.

      OS_OPT_TASK_STK_CHK //运行任务堆栈检测

      specifies whether stack checking is allowed for the task.

      OS_OPT_TASK_STK_CLR// 是否清除

      specifies whether the stack needs to be cleared.

  • p_err is a pointer to a variable that will receive an error code.
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值