12回答

0收藏

任务无法切换,可能是什么原因?

#开源分享 #开源分享 5586 人阅读 | 12 人回复 | 2014-06-06

程序刚开始运行的时候,在线调试跟踪,发现ucos操作系统启动之后,每个任务按照优先级高低各运行一遍之后 一直运行空闲任务,怎么回事呢,貌似任务切换不了了,但是刚开始的时候一个任务运行了一遍,可能是啥原因捏?

分享到:
回复

使用道具 举报

回答|共 12 个

倒序浏览

沙发

laoliang-67665

发表于 2014-6-6 22:23:04 | 只看该作者

请把主程序的代码和任务的代码贴上来让朕看一下
嵌入式,ucos,FPGA系统书籍作者
板凳

学生--啦啦啦

发表于 2014-6-7 07:20:09 | 只看该作者

亮点 发表于 2014-6-6 22:23
请把主程序的代码和任务的代码贴上来让朕看一下
  1. /******************** (C) COPYLEFT 2014 gly **************************
  2. * ????: main.C
  3. * °?±?: V1.0
  4. * ×÷??: ????
  5. * ???¨: ALIENTEK STM32
  6. * ????: 2014/6/6
  7. * ?è??: stm32+ucos+ucgui??°?
  8. **********************************************************************************/
  9. #include "includes.h"
  10. //?????????ó??
  11. #define START_STK_SIZE                                  64          //?è???????????ó??
  12. #define TASK_TOUCH_STK_SIZE                        1024
  13. #define LED1_STK_SIZE                                      64
  14. #define GUI_STK_SIZE                                      2048
  15. //??????????
  16. #define START_TASK_PRIO                              1    //?????????????????è????×???
  17. #define TASK_TOUCH_PRIO                                9          //touch
  18. #define LED1_TASK_PRIO                               4           //led1
  19. #define GUI_TASK_PRIO                          12          //GUI
  20. //????????
  21. OS_STK  START_TASK_STK[START_STK_SIZE];        //???¨????????????
  22. OS_STK  task_touch_stk[TASK_TOUCH_STK_SIZE];//?¨???????????????????÷????????????.
  23. OS_STK  LED1_TASK_STK[LED1_STK_SIZE];//LED????
  24. OS_STK  GUI_TASK_STK[GUI_STK_SIZE];//GUI????
  25. //?????????ù?÷
  26. void start_task(void *pdata);
  27. void led1_task(void *pdata);
  28. void gui_task(void *pdata);
  29. void touch_task(void *pdata);

  30. /***************************************************************************
  31.                                * ?÷???? *
  32. ***************************************************************************/
  33. int main(void)
  34. {       
  35.         delay_init();//??????????systick???ò?????????è??????????????delay_ms()????????TFT
  36.         BSP_Init();//°?????????       
  37.         //GUI_Init();//GUI_App()?????÷????GUI_Init()??????????????
  38.        
  39.         OSInit();//uCOS-II??????????
  40.         OSTaskCreate(start_task,(void *)0,(OS_STK *)&START_TASK_STK[START_STK_SIZE-1],START_TASK_PRIO );//???¨????????  
  41.         OSStart();//????????
  42. }

  43. /***************************************************************************
  44.                              * start_task *
  45. ***************************************************************************/
  46. void start_task(void *pdata)//????????
  47. {
  48.         OSTaskCreate(touch_task, (void *)0, &task_touch_stk[TASK_TOUCH_STK_SIZE-1], TASK_TOUCH_PRIO);
  49.         OSTaskCreate(led1_task, (void *)0, (OS_STK*)&LED1_TASK_STK[LED1_STK_SIZE-1], LED1_TASK_PRIO);
  50.         OSTaskCreate(gui_task, (void *)0, (OS_STK*)&GUI_TASK_STK[GUI_STK_SIZE-1], GUI_TASK_PRIO);
  51.         OSTaskDel(START_TASK_PRIO);//????×??í????????..
  52. }
  53. /***************************************************************************
  54.                              * led1_task *
  55. ***************************************************************************/
  56. void led1_task(void *pdata)// ??led1??????????±í?÷ ucos ??????×???
  57. {                
  58.         while(1)
  59.         {
  60.                 LED1 = !LED1;
  61.                 OSTimeDly(100);
  62.         }
  63. }
  64. /***************************************************************************
  65.                              * gui_task *
  66. ***************************************************************************/
  67. void gui_task(void *pdata)
  68. {                
  69.         while(1)
  70.         {
  71.                 GUI_App();
  72.         }
  73. }
  74. /***************************************************************************
  75.                              * touch_task *
  76. ***************************************************************************/
  77. void touch_task(void *p_arg)
  78. {
  79.         while(1)
  80.         {
  81.                 GUI_TOUCH_Exec();//??±??¤????100?÷??????????????????????????                                                                                                                                                               
  82.                 OSTimeDly(1);
  83.         }
  84. }

  85. /////////////////////////////////// end of file //////////////////////////////////////////

复制代码
地板

学生--啦啦啦

发表于 2014-6-7 07:27:26 | 只看该作者

本帖最后由 学生--啦啦啦 于 2014-6-7 08:33 编辑

第一句的 delay_init()是设置systick中断,按照设定的时间发生中断。其他的是参照卢老师的视频教程和带ucos的模板弄的,我想改个模板出来,放到开发板上能运行,可以随便加东西的那种。
UCGUI/亮点嵌入式开发http://brightpoint.taobao.com/








我弄好了,卢老师,从新弄了一遍,一点点实验,好使了,具体啥原因目前不知道。
5#

735054479_3015043

发表于 2014-6-8 12:19:15 | 只看该作者

哈哈,今天天气不错
6#

laoliang-67665

发表于 2014-6-9 10:16:25 | 只看该作者

学生--啦啦啦 发表于 2014-6-7 07:27
第一句的 delay_init()是设置systick中断,按照设定的时间发生中断。其他的是参照卢老师的视频教程和带ucos ...

SYSTICK中断的启动最好在启动任务中进行,道理你懂的
如果你初始化时间长,SYSTICK中断在OS已经启动之前到来,逻辑上就错了
嵌入式,ucos,FPGA系统书籍作者
7#

学生--啦啦啦

发表于 2014-6-10 11:28:21 | 只看该作者

亮点 发表于 2014-6-9 10:16
SYSTICK中断的启动最好在启动任务中进行,道理你懂的
如果你初始化时间长,SYSTICK中断在OS已经启动之前 ...

恩,“SYSTICK中断的启动最好在启动任务中进行,道理你懂的”这句话理解
但是systick中断函数在OS启动之前到来也没事啊,操作系统没启动,中断函数里面相当于执行空语句,他先判断操作系统是否启动,如果启动了里面的函数才起作用,如果没启动,什么也不执行!
void SysTick_Handler(void)
{
        OSIntEnter();
        OSTimeTick();
        OSIntExit();
}
OSIntEnter()、OSTimeTick()、OSIntExit(),这三个函数里面都是if (OSRunning == OS_TRUE)才作相应的事情。所以OS启动之前,systick到来也无影响!
void  OSIntEnter (void)
{
    if (OSRunning == OS_TRUE) {
        if (OSIntNesting < 255u) {
            OSIntNesting++;                      /* Increment ISR nesting level                        */
        }
    }
}


void  OSTimeTick (void)
{
    OS_TCB    *ptcb;
#if OS_TICK_STEP_EN > 0u
    BOOLEAN    step;
#endif
#if OS_CRITICAL_METHOD == 3u                               /* Allocate storage for CPU status register     */
    OS_CPU_SR  cpu_sr = 0u;
#endif



#if OS_TIME_TICK_HOOK_EN > 0u
    OSTimeTickHook();                                      /* Call user definable hook                     */
#endif
#if OS_TIME_GET_SET_EN > 0u
    OS_ENTER_CRITICAL();                                   /* Update the 32-bit tick counter               */
    OSTime++;
    OS_EXIT_CRITICAL();
#endif
    if (OSRunning == OS_TRUE) {
#if OS_TICK_STEP_EN > 0u
        switch (OSTickStepState) {                         /* Determine whether we need to process a tick  */
            case OS_TICK_STEP_DIS:                         /* Yes, stepping is disabled                    */
                 step = OS_TRUE;
                 break;

            case OS_TICK_STEP_WAIT:                        /* No,  waiting for uC/OS-View to set ...       */
                 step = OS_FALSE;                          /*      .. OSTickStepState to OS_TICK_STEP_ONCE */
                 break;

            case OS_TICK_STEP_ONCE:                        /* Yes, process tick once and wait for next ... */
                 step            = OS_TRUE;                /*      ... step command from uC/OS-View        */
                 OSTickStepState = OS_TICK_STEP_WAIT;
                 break;

            default:                                       /* Invalid case, correct situation              */
                 step            = OS_TRUE;
                 OSTickStepState = OS_TICK_STEP_DIS;
                 break;
        }
        if (step == OS_FALSE) {                            /* Return if waiting for step command           */
            return;
        }
#endif
        ptcb = OSTCBList;                                  /* Point at first TCB in TCB list               */
        while (ptcb->OSTCBPrio != OS_TASK_IDLE_PRIO) {     /* Go through all TCBs in TCB list              */
            OS_ENTER_CRITICAL();
            if (ptcb->OSTCBDly != 0u) {                    /* No, Delayed or waiting for event with TO     */
                ptcb->OSTCBDly--;                          /* Decrement nbr of ticks to end of delay       */
                if (ptcb->OSTCBDly == 0u) {                /* Check for timeout                            */

                    if ((ptcb->OSTCBStat & OS_STAT_PEND_ANY) != OS_STAT_RDY) {
                        ptcb->OSTCBStat  &= (INT8U)~(INT8U)OS_STAT_PEND_ANY;          /* Yes, Clear status flag   */
                        ptcb->OSTCBStatPend = OS_STAT_PEND_TO;                 /* Indicate PEND timeout    */
                    } else {
                        ptcb->OSTCBStatPend = OS_STAT_PEND_OK;
                    }

                    if ((ptcb->OSTCBStat & OS_STAT_SUSPEND) == OS_STAT_RDY) {  /* Is task suspended?       */
                        OSRdyGrp               |= ptcb->OSTCBBitY;             /* No,  Make ready          */
                        OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
                    }
                }
            }
            ptcb = ptcb->OSTCBNext;                        /* Point at next TCB in TCB list                */
            OS_EXIT_CRITICAL();
        }
    }
}



void  OSIntExit (void)
{
#if OS_CRITICAL_METHOD == 3u                               /* Allocate storage for CPU status register */
    OS_CPU_SR  cpu_sr = 0u;
#endif



    if (OSRunning == OS_TRUE) {
        OS_ENTER_CRITICAL();
        if (OSIntNesting > 0u) {                           /* Prevent OSIntNesting from wrapping       */
            OSIntNesting--;
        }
        if (OSIntNesting == 0u) {                          /* Reschedule only if all ISRs complete ... */
            if (OSLockNesting == 0u) {                     /* ... and not locked.                      */
                OS_SchedNew();
                OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
                if (OSPrioHighRdy != OSPrioCur) {          /* No Ctx Sw if current task is highest rdy */
#if OS_TASK_PROFILE_EN > 0u
                    OSTCBHighRdy->OSTCBCtxSwCtr++;         /* Inc. # of context switches to this task  */
#endif
                    OSCtxSwCtr++;                          /* Keep track of the number of ctx switches */
                    OSIntCtxSw();                          /* Perform interrupt level ctx switch       */
                }
            }
        }
        OS_EXIT_CRITICAL();
    }
}
8#

韩解嘲

发表于 2014-6-10 13:44:57 | 只看该作者

啦啦越来越厉害了
9#

韩解嘲

发表于 2014-6-10 13:46:02 | 只看该作者

啦啦越来越厉害了
10#

学生--啦啦啦

发表于 2014-6-10 13:47:16 | 只看该作者

浅伏 发表于 2014-6-10 13:46
啦啦越来越厉害了

我上面写得对吗?大神!
您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条