📄 优先级反转.c
字号:
/***************************************************************
* 声明:
* 本程序只具备演示功能,不能保证适用于您的真实应用。如需使用,请根据
* 您的实际需要修改本程序。
*******************************************************************
* 电子科技大学嵌入式软件工程中心 版权所有
*
* Copyright (C) 2006 UESTC ESEC
**************************************************************/
/**************************************************************
* 模块: init.c
*
* 目的:
* 这个程序演示基本的天花板优先级的解决。程序启动具有三个不同优先
* 级的任务,每个任务请求,获得,释放互斥信号量,完成一系列动作。
**************************************************************/
/********************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
*
*
*********************************************************************************************************
*/
#include <stdio.h>
#include "/host/ide/workspace/ucos/src/includes.h"
/*
*********************************************************************************************************
* CONSTANTS
*********************************************************************************************************
*/
#define TASK_STK_SIZE 1024 /* Size of each task's stacks (# of WORDs) */
#define TASK_START_ID 0 /* Application tasks */
#define TASK_START_PRIO 17 /* Application tasks priorities */
#define N_TASKS 3 /* Number of tasks */
/*
*********************************************************************************************************
* VARIABLES
*********************************************************************************************************
*/
OS_STK TaskStk[N_TASKS][TASK_STK_SIZE/sizeof(OS_STK)]; /*Tasks stacks */
OS_STK TaskStartStk[TASK_STK_SIZE]; /* Startup task stack */
INT8U TaskData[N_TASKS]; /* Parameters to pass to each task */
OS_EVENT *mutex; /* 1 mutual exclusive semaphore */
/*
*********************************************************************************************************
* FUNCTION PROTOTYPES
*********************************************************************************************************
*/
static void TaskStartCreateTasks(void); /* Function prototypes of Create tasks */
void Task0(void *pdata); /* Function prototypes of tasks */
void Task1(void *pdata);
void Task2(void *pdata);
void TaskStart(void *pdata); /* Function prototypes of Startup task */
/*
*********************************************************************************************************
* MAIN
*********************************************************************************************************
*/
extern void main(void);
/*
*
* *********************************************************************************************************
*
*/
void boot_card()
{
ucBsp_init(); /* Initializa ucBsp */
main();
}
extern void OSCpuInit();
extern void OS_Sched(void);
void main (void)
{
INT8U err; /* for setting the mutex */
printk("Welcome to ucos-II\n");
OSCpuInit(); /* Install uC/OS-II's context switch vector */
OSInit(); /* Install uC/OS-II's context switch vector */
mutex=OSSemCreate(1); /* Set a mutual semaphore */
OSTaskCreate(TaskStart, (void *)0, &TaskStartStk[TASK_STK_SIZE - 1], 4);
OSStart(); /* Start multitasking */
}
/*
*********************************************************************************************************
* STARTUP TASK
*********************************************************************************************************
*/
void TaskStart (void *pdata)
{
INT16S key;
int i;
pdata = pdata; /* Prevent compiler warning */
ucos_x86_idt_set_handler(0x20,(void *)OSTickISR,0x8e00); /* Install uC/OS-II's clock tick ISR */
ucos_timer_init(); /* Reprogram tick rate */
TaskStartCreateTasks(); /* Create all the application tasks */
OSTaskSuspend(OS_PRIO_SELF); /* Suspend the TaskStart */
}
/*
***************************************************************************************************************
* CREATE TASKS
***************************************************************************************************************
*/
static void TaskStartCreateTasks (void)
{
INT8U i;
for (i = 0; i <N_TASKS; i++)
{ /* Create N_TASKS identical tasks */
TaskData[i] = i;
} /* Each task will pass its own id */
OSTaskCreate(Task0, (void *)&TaskData[0], &TaskStk[0][TASK_STK_SIZE - 1], 5);
OSTaskCreate(Task1, (void *)&TaskData[1], &TaskStk[1][TASK_STK_SIZE - 1], 6);
OSTaskCreate(Task2, (void *)&TaskData[2], &TaskStk[2][TASK_STK_SIZE - 1], 7);
}
/*
****************************************************************************************************************
* TASKS
****************************************************************************************************************
*/
void Task0 (void *pdata)
{
INT8U err;
INT8U id;
INT16U value;
id=*(int *)pdata;
for (;;)
{
printk("Task %d is waitting a event.\n",id);
OSTimeDly(200); /* Delay 200 clock tick */
printk("The event of Task %d come.\n",id);
printk("Task %d is try to get mutex.\n",id);
OSSemPend(mutex,0,&err); /* Acquire mutex */
switch(err)
{
case OS_NO_ERR:
printk("Task %d has got the mutex.\n",id);
printk("\n");
break;
default:
printk("Task %d is suspended.\n",id);
printk("\n");
}
OSTimeDly(200); /* Delay 200 clock tick */
printk("Task %d release mutex.\n",id);
OSSemPost(mutex); /* Release mutex */
}
}
void Task1 (void *pdata)
{
INT8U err;
INT8U id;
int i;
id=*(int *)pdata;
for (;;) {
printk("Task %d is waitting a event.\n",id);
OSTimeDly(100); /* Delay 100 clock tick */
printk("The event of Task %d come.\n",id);
OSTimeDly(100);
}
}
void Task2 (void *pdata)
{
INT8U err;
INT8U id;
INT16U value;
id=*(int *)pdata;
int i;
for (;;)
{
printk("Task %d is trying to get mutex.\n",id);
OSSemPend(mutex,0,&err); /* Acquire mutex */
switch(err)
{
case OS_NO_ERR:
{
printk("\n");
printk("----------------------------------\n");
printk("Task %d has got the mutex.\n",id);
OSTimeDly(200); /* Delay 100 clock tick */
break;
}
default :
{
printk("Task %d is failed to get mutex.\n",id);
printk("\n");
OSTimeDly(200); /* Delay 100 clock tick */
break;
}
}
printk("Task %d release mutex.\n",id);
printk("\n");
OSSemPost(mutex); /* Release mutex */
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -