Chinaunix首页 | 论坛 | 博客
  • 博客访问: 431594
  • 博文数量: 103
  • 博客积分: 1455
  • 博客等级: 上尉
  • 技术积分: 1380
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-15 22:17
文章分类

全部博文(103)

文章存档

2013年(4)

2012年(99)

我的朋友

分类: LINUX

2012-11-09 09:49:43

初始化idletask的代码如下

点击(此处)折叠或打开

  1. /*
  2. *********************************************************************************************************
  3. * INITIALIZATION
  4. * CREATING THE IDLE TASK
  5. *
  6. * Description: This function creates the Idle Task.
  7. *
  8. * Arguments : none
  9. *
  10. * Returns : none
  11. *********************************************************************************************************
  12. */

  13. static void OS_InitTaskIdle (void)
  14. {
  15. #if OS_TASK_NAME_SIZE > 7
  16.     INT8U err;
  17. #endif


  18. #if OS_TASK_CREATE_EXT_EN > 0
  19.     #if OS_STK_GROWTH == 1
  20.     (void)OSTaskCreateExt(OS_TaskIdle,
  21.                           (void *)0, /* No arguments passed to OS_TaskIdle() */
  22.                           &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1], /* Set Top-Of-Stack */
  23.                           OS_TASK_IDLE_PRIO, /* Lowest priority level */
  24.                           OS_TASK_IDLE_ID,
  25.                           &OSTaskIdleStk[0], /* Set Bottom-Of-Stack */
  26.                           OS_TASK_IDLE_STK_SIZE,
  27.                           (void *)0, /* No TCB extension */
  28.                           OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);/* Enable stack checking + clear stack */
  29.     #else
  30.     (void)OSTaskCreateExt(OS_TaskIdle,
  31.                           (void *)0, /* No arguments passed to OS_TaskIdle() */
  32.                           &OSTaskIdleStk[0], /* Set Top-Of-Stack */
  33.                           OS_TASK_IDLE_PRIO, /* Lowest priority level */
  34.                           OS_TASK_IDLE_ID,
  35.                           &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1], /* Set Bottom-Of-Stack */
  36.                           OS_TASK_IDLE_STK_SIZE,
  37.                           (void *)0, /* No TCB extension */
  38.                           OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);/* Enable stack checking + clear stack */
  39.     #endif
  40. #else
  41.     #if OS_STK_GROWTH == 1
  42.     (void)OSTaskCreate(OS_TaskIdle,
  43.                        (void *)0,
  44.                        &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1],
  45.                        OS_TASK_IDLE_PRIO);
  46.     #else
  47.     (void)OSTaskCreate(OS_TaskIdle,
  48.                        (void *)0,
  49.                        &OSTaskIdleStk[0],
  50.                        OS_TASK_IDLE_PRIO);
  51.     #endif
  52. #endif

  53. #if OS_TASK_NAME_SIZE > 14
  54.     OSTaskNameSet(OS_TASK_IDLE_PRIO, (INT8U *)"uC/OS-II Idle", &err);
  55. #else
  56. #if OS_TASK_NAME_SIZE > 7
  57.     OSTaskNameSet(OS_TASK_IDLE_PRIO, (INT8U *)"OS-Idle", &err);
  58. #endif
  59. #endif
  60. }

设置一个字节大小的变量err
当系统可以创建OSTaskCreateExt()即用户可以额外的附加数据域的任务创建
并且OS_STK_GROWTH == 1即栈的地址是从高地址到低地址的
使用OSTaskCreateExt(OS_TaskIdle,
                          (void *)0,                 /* No arguments passed to OS_TaskIdle() */
                          &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1], /* Set Top-Of-Stack    */
                          OS_TASK_IDLE_PRIO,                       /* Lowest priority level */
                          OS_TASK_IDLE_ID,
                          &OSTaskIdleStk[0],                         /* Set Bottom-Of-Stack  */
                          OS_TASK_IDLE_STK_SIZE,
                          (void *)0,                                 /* No TCB extension   */
                          OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);/* Enable stack checking + clear stack  */
 
创建idle任务,函数的9个参数分别为,任务代码的指针(任务函数名),pdata传递给任务的参数指针,
分配给任务的堆栈的栈顶指针,分配给任务的优先级数据,(之后是Ext后缀的任务创建函数才有的)
特殊id(目前版本未使用),栈底指针(用于堆栈的检验),
size用于指定堆栈的容量(如果栈的单位容量为4个字节,size为1000,那么栈的大小为4×1000个字节)
指向用户附加数据域的指针(附加的哦),最后,有三个选项,分别表示堆栈检验,堆栈清零和浮点操作的使能。
 
当使能HIGH-LOW栈的时候,堆栈的top指针指向数组最后一位,当LOW-HIGH时,堆栈的top指向数组头
数组的内存分配一定是从低地址向高地址,跟大端小端系统木有联系
 
如果Ext的使能没有打开,就是用(void)OSTaskCreate(OS_TaskIdle,
                       (void *)0,
                       &OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1],
                       OS_TASK_IDLE_PRIO);
创建任务,其参数与前面的ext函数的前四个参数含义相同,
 
创建完成后,如果OS_TASK_NAME_SIZE足够大,使用OSTaskNameSet函数设置taskname

点击(此处)折叠或打开

  1. /*
  2. *********************************************************************************************************
  3. * ASSIGN A NAME TO A TASK
  4. *
  5. * Description: This function is used to set the name of a task.
  6. *
  7. * Arguments : prio is the priority of the task that you want the assign a name to.
  8. *
  9. * pname is a pointer to an ASCII string that contains the name of the task. The ASCII
  10. * string must be NUL terminated.
  11. *
  12. * err is a pointer to an error code that can contain one of the following values:
  13. *
  14. * OS_NO_ERR if the requested task is resumed
  15. * OS_TASK_NOT_EXIST if the task has not been created or is assigned to a Mutex
  16. * OS_ERR_TASK_NAME_TOO_LONG if the name you are giving to the task exceeds the
  17. * storage capacity of a task name as specified by
  18. * OS_TASK_NAME_SIZE.
  19. * OS_ERR_PNAME_NULL You passed a NULL pointer for 'pname'
  20. * OS_PRIO_INVALID if you specified an invalid priority:
  21. * A higher value than the idle task or not OS_PRIO_SELF.
  22. *
  23. * Returns : None
  24. *********************************************************************************************************
  25. */
  26. #if OS_TASK_NAME_SIZE > 1
  27. void OSTaskNameSet (INT8U prio, INT8U *pname, INT8U *err)
  28. {
  29.     INT8U len;
  30.     OS_TCB *ptcb;
  31. #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
  32.     OS_CPU_SR cpu_sr = 0;
  33. #endif



  34. #if OS_ARG_CHK_EN > 0
  35.     if (err == (INT8U *)0) { /* Validate 'err' */
  36.         return;
  37.     }
  38.     if (prio > OS_LOWEST_PRIO) { /* Task priority valid ? */
  39.         if (prio != OS_PRIO_SELF) {
  40.             *err = OS_PRIO_INVALID; /* No */
  41.             return;
  42.         }
  43.     }
  44.     if (pname == (INT8U *)0) { /* Is 'pname' a NULL pointer? */
  45.         *err = OS_ERR_PNAME_NULL; /* Yes */
  46.         return;
  47.     }
  48. #endif
  49.     OS_ENTER_CRITICAL();
  50.     if (prio == OS_PRIO_SELF) { /* See if caller desires to set it's own name */
  51.         prio = OSTCBCur->OSTCBPrio;
  52.     }
  53.     ptcb = OSTCBPrioTbl[prio];
  54.     if (ptcb == (OS_TCB *)0) { /* Does task exist? */
  55.         OS_EXIT_CRITICAL(); /* No */
  56.         *err = OS_TASK_NOT_EXIST;
  57.         return;
  58.     }
  59.     if (ptcb == (OS_TCB *)1) { /* Task assigned to a Mutex? */
  60.         OS_EXIT_CRITICAL(); /* Yes */
  61.         *err = OS_TASK_NOT_EXIST;
  62.         return;
  63.     }
  64.     len = OS_StrLen(pname); /* Yes, Can we fit the string in the TCB? */
  65.     if (len > (OS_TASK_NAME_SIZE - 1)) { /* No */
  66.         OS_EXIT_CRITICAL();
  67.         *err = OS_ERR_TASK_NAME_TOO_LONG;
  68.         return;
  69.     }
  70.     (void)OS_StrCopy(ptcb->OSTCBTaskName, pname); /* Yes, copy to TCB */
  71.     OS_EXIT_CRITICAL();
  72.     *err = OS_NO_ERR;
  73. }
  74. #endif

设置必要的局部变量,设置参数
如果OS_ARG_CHK_EN > 0即使能变量检查位
idle的优先级一般被定义为OS_LOWEST_PRIO
那么检查指针参数是否指向空指针,检查优先级是否大于OS_LOWEST_PRIO
关中断
得到优先级,然后使用OSTCBPrioTbl[prio]函数返回prio优先级对应的任务TCB指针
如果为空,返回错误
求出strlen如果过长返回超出长度错误
一切ok使用OS_StrCopy(ptcb->OSTCBTaskName, pname);将字符串pname拷贝到TCB的taskname中去
开中断
err返回NO_ERR

点击(此处)折叠或打开

  1. /*
  2. *********************************************************************************************************
  3. * IDLE TASK
  4. *
  5. * Description: This task is internal to uC/OS-II and executes whenever no other higher priority tasks
  6. * executes because they are ALL waiting for event(s) to occur.
  7. *
  8. * Arguments : none
  9. *
  10. * Returns : none
  11. *
  12. * Note(s) : 1) OSTaskIdleHook() is called after the critical section to ensure that interrupts will be
  13. * enabled for at least a few instructions. On some processors (ex. Philips XA), enabling
  14. * and then disabling interrupts didn't allow the processor enough time to have interrupts
  15. * enabled before they were disabled again. uC/OS-II would thus never recognize
  16. * interrupts.
  17. * 2) This hook has been added to allow you to do such things as STOP the CPU to conserve
  18. * power.
  19. *********************************************************************************************************
  20. */

  21. void OS_TaskIdle (void *p_arg)
  22. {
  23. #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
  24.     OS_CPU_SR cpu_sr = 0;
  25. #endif



  26.     (void)p_arg; /* Prevent compiler warning for not using 'parg' */
  27.     for (;;) {
  28.         OS_ENTER_CRITICAL();
  29.         OSIdleCtr++;
  30.         OS_EXIT_CRITICAL();
  31.         OSTaskIdleHook(); /* Call user definable HOOK */
  32.     }
  33. }

TaskIdle的函数实现:
死循环:
关中断,idle计数器自加,开中断,用户自定义程序TaskIdleHook
 
 
 
 
阅读(2974) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~