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

📄 kernel.c

📁 AVR单片机 C语言程序设计经典实用
💻 C
字号:
//******************************************************************************
// File Name : Kernel.c
// Author    : Steaven
// Created   : 2008-06-09
// Modified  : 
// Revision  : V0.0
//******************************************************************************

#include "app.h"

//Task Control Block structure
typedef struct
{
	INT16U wTimerPeriod;
	INT16U wTimerCount;
	INT16U wEvent;
}OS_TCB;

//structure array definition
OS_TCB OSTCB[cMaxTask];

//******************************************************************************
// Function    : OS_Event_Pend
// Input       : wTaskPrio - priority of task
// Output      : event of task
// Description : Inquiry event of task indexed by priority and then clear it
//******************************************************************************
INT16U OS_Event_Pend(INT8U bTaskPrio)
{
	INT16U wTaskEvent;
	wTaskEvent = OSTCB[bTaskPrio].wEvent;
	OSTCB[bTaskPrio].wEvent &= ~wTaskEvent;
	return(wTaskEvent);
}

//******************************************************************************
// Function    : OSEventSend
// Input       : wTaskPrio - priority of task
//               wTaskEvent - event to be sent                 
// Output      : none
// Description : Send an event to task indexed by priority
//******************************************************************************
void OS_Event_Post(INT8U bTaskPrio,INT16U wTaskEvent)
{
	OSTCB[bTaskPrio].wEvent |= (1 << wTaskEvent);
}

//******************************************************************************
// Function    : OS_Task_Create
// Input       : wTaskPrio - priority of task
//               wTimerPeriod - period of task
// Output      : none
// Description : Set time period of a task to create it
//******************************************************************************
void OS_Task_Create(INT8U bTaskPrio,INT16U wTimerPeriod)
{
	OSTCB[bTaskPrio].wTimerPeriod = wTimerPeriod;
}

//******************************************************************************
// Function    : OS_Task_Start
// Input       : wTaskPrio - priority of task
//               wTimerCount - Initial time count of a task
// Output      : none
// Description : Set initial time count of a task
//******************************************************************************
void OS_Task_Init(INT8U bTaskPrio,INT16U wTimerCount)
{
	OSTCB[bTaskPrio].wTimerCount = wTimerCount;
}

//******************************************************************************
// Function    : OS_Task_Switch
// Input       : wTaskPrio - priority of task
// Output      : none
// Description : inquiry the task in ready status.if there's no ready task with
//               priority higher than current one,the task would run
//******************************************************************************
INT16U OS_Task_Switch(INT8U bTaskPrio)
{
	INT8U bTemp;
	for(bTemp = 0;bTemp < bTaskPrio;bTemp++)
	{
		if(OSTCB[bTemp].wEvent != 0)
		{
			return(true);
		}
	}
	return(false);
}

//******************************************************************************
// Function    : OS_Task_Update
// Input       : none
// Output      : none
// Description : increase timercount of all tasks in each SYS clock ISR.if task
//               is timeout,Kernel send a timer event to the task
//******************************************************************************
void OS_Task_Update(void)
{
	INT8U bTemp;
	for(bTemp = 0;bTemp < cMaxTask;bTemp++)
	{
		OSTCB[bTemp].wTimerCount++;
		if(OSTCB[bTemp].wTimerCount == OSTCB[bTemp].wTimerPeriod)
		{
			OSTCB[bTemp].wTimerCount = 0;
			OS_Event_Post(bTemp,0);
		}
	}
}

//******************************************************************************
// Function    : OS_Init
// Input       : none
// Output      : none
// Description : Initlization of TCBs
//******************************************************************************
void OS_Init(void)
{
	INT8U bTemp;
	for(bTemp = 0;bTemp < cMaxTask;bTemp++)
	{
		OSTCB[bTemp].wTimerPeriod = 0;
		OSTCB[bTemp].wTimerCount = 0;
		OSTCB[bTemp].wEvent = 0;
	}
}

//******************************************************************************
// Function    : OS_Start
// Input       : none
// Output      : none
// Description : Initlization of tasks and Start Kernel
//******************************************************************************
void OS_Start(void)
{
	Task_Init();
	Task_Start();
}

//===============================END OF FILE==================================//

⌨️ 快捷键说明

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