⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 优先级继承.c

📁 运行在ucos下的实验用例程序
💻 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                 512       /* Size of each task's stacks (# of WORDs)            */
#define  N_TASKS                        3        /* Number of identical tasks                          */

/*
************************************************************************************************************
*                                               VARIABLES
************************************************************************************************************
*/

OS_STK        TaskStk[N_TASKS][TASK_STK_SIZE];        /* 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
************************************************************************************************************
*/

        void  Task(void *pdata);                       /* Function prototypes of tasks                */
        void  TaskStart(void *pdata);				   /* Function prototypes of Startup task         */
static  void  TaskStartCreateTasks(void);              /* Function prototypes of Create tasks         */

/*
*************************************************************************************************************
*                                                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                    */

    OSCpuInit();                                                /* Install uC/OS-II's context switch vector */
    OSInit();                                                   /* Initialize uC/OS-II                      */

    printk("Welcome to ucos-II\n");

    mutex   = OSMutexCreate(8,&err);					        /* set a mutual semaphore  , 8 is the PIP   */

    OSTaskCreate(TaskStart,  (void *)0,  &TaskStartStk[TASK_STK_SIZE - 1], 9);

    OSStart();                                                  /* Start multitasking                       */
}

/*
**************************************************************************************************************
*                                              STARTUP TASK
**************************************************************************************************************
*/

void  TaskStart (void *pdata)
{
	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
***************************************************************************************************************
*/

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(Task, (void *)&TaskData[i], &TaskStk[i][TASK_STK_SIZE - 1], 12-i);
	}
}

/*
****************************************************************************************************************
*                                                  TASKS
****************************************************************************************************************
*/

void  Task (void *pdata)
{
    INT8U  err;
    INT8U  id;

	id=*(int *)pdata;

    for (;;)
    {
		printk("task %d try to get the mutex. \n", id);

		OSMutexPend(mutex, 0, &err);                                     /* Acquire mutex to get continue     */

		printk("task %d is getting the mutex. \n", id);

		OSTimeDlyHMSM(0, 0, 0, 200);                                     /* Wait 200 minisecond               */

        printk("task %d   releases the mutex. \n", id);

		OSMutexPost(mutex);                                              /* Release mutex                     */

		OSTimeDlyHMSM(0, 0, 0, (3-id)*150);                              /* Wait (3-id)*150 minisecond        */
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -