本帖最后由 wuxxxxx炫 于 2018-1-26 17:05 编辑
//任务优先级
#define HIGH_TASK_PRIO 7
//任务堆栈大小
#define HIGH_STK_SIZE 128
//任务控制块
OS_TCB High_TaskTCB;
//任务堆栈
CPU_STK HIGH_TASK_STK[HIGH_STK_SIZE];
void high_task(void *p_arg);
//任务优先级
#define MIDDLE_TASK_PRIO 8
//任务堆栈大小
#define MIDDLE_STK_SIZE 128
//任务控制块
OS_TCB Middle_TaskTCB;
//任务堆栈
CPU_STK MIDDLE_TASK_STK[MIDDLE_STK_SIZE];
void middle_task(void *p_arg);
//任务优先级
#define LOW_TASK_PRIO 9
//任务堆栈大小
#define LOW_STK_SIZE 128
//任务控制块
OS_TCB Low_TaskTCB;
//任务堆栈
CPU_STK LOW_TASK_STK[LOW_STK_SIZE];
void low_task(void *p_arg);
//高优先级任务的任务函数
void high_task(void *p_arg)
{
u8 num;
OS_ERR err;
CPU_SR_ALLOC();
POINT_COLOR = BLACK;
OS_CRITICAL_ENTER();
LCD_DrawRectangle(5,110,115,314); //画一个矩形
LCD_DrawLine(5,130,115,130); //画线
POINT_COLOR = BLUE;
LCD_ShowString(6,111,110,16,16,"High Task");
OS_CRITICAL_EXIT();
while(1)
{
OSTimeDlyHMSM(0,0,0,500,OS_OPT_TIME_PERIODIC,&err); //延时500ms
num++;
printf("high task Pend Mutex\r\n");
OSMutexPend (&TEST_MUTEX,0,OS_OPT_PEND_BLOCKING,0,&err); //请求互斥信号量
printf("high task Running!\r\n");
LCD_Fill(6,131,114,313,lcd_discolor[num%14]); //填充区域
LED1 = ~LED1;
OSMutexPost(&TEST_MUTEX,OS_OPT_POST_NONE,&err); //释放互斥信号量
printf("high task Post Mutex\r\n");
OSTimeDlyHMSM(0,0,0,500,OS_OPT_TIME_PERIODIC,&err); //延时500ms
}
}
//中等优先级任务的任务函数
void middle_task(void *p_arg)
{
u8 num;
OS_ERR err;
CPU_SR_ALLOC();
POINT_COLOR = BLACK;
OS_CRITICAL_ENTER();
LCD_DrawRectangle(125,110,234,314); //画一个矩形
LCD_DrawLine(125,130,234,130); //画线
POINT_COLOR = BLUE;
LCD_ShowString(126,111,110,16,16,"Middle Task");
OS_CRITICAL_EXIT();
while(1)
{
num++;
printf("middle task Running!\r\n");
LCD_Fill(126,131,233,313,lcd_discolor[13-num%14]); //填充区域
LED0 = ~LED0;
OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_PERIODIC,&err); //延时1s
}
}
//低优先级任务的任务函数
void low_task(void *p_arg)
{
static u32 times;
OS_ERR err;
while(1)
{
OSMutexPend (&TEST_MUTEX,0,OS_OPT_PEND_BLOCKING,0,&err);//请求互斥信号量
printf("low task Running!\r\n");
for(times=0;times<10000000;times++)
{
OSSched(); //发起任务调度
}
OSMutexPost(&TEST_MUTEX,OS_OPT_POST_NONE,&err); //释放互斥信号量
OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_PERIODIC,&err); //延时1s
}
}
代码在上面,阉割了与问题无关的部分
高优先级获得互斥信号运行后,根据优先级,运行中等优先级,为什么之后运行的又是高优先级任务,不明白!
|