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

📄 timers.c

📁 一个完整的rtos源代码.运行在嵌入80186 cpu上.提供4个任务,信号灯,定时器,schedule等.
💻 C
字号:
/* **************************************************************************************
 *  Copyright (c) 2001 ZORAN Corporation, All Rights Reserved
 *  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF ZORAN CORPORATION
 *
 *  File: "timers.c"                                            12/13/01
 *
 * Description:
 * ============
 * Functions to handle periodic function calls
 * 
 * Log:
 * ====
 * $Revision: 8 $
 * Last Modified by $Author: Leslie $ at $Modtime: 03-06-11 21:36 $ 
 ****************************************************************************************
 * Updates:
 ****************************************************************************************
 * $Log: /SourceCode/I64_Common/I64_Reference/Kernel/Timers/Timers.c $
 * 
 * 8     03-06-11 21:36 Leslie
 * Change MAX_TIMERS_CNT to be 12 for non SERVO_TEST mode
 * 
 * 7     6/11/03 12:01a Nmaurer
 * Add another two timers for SERVO_TEST
 * 
 * 6     03-05-22 10:51 Leslie
 * Add Timers
 * 
 * 5     1/20/03 4:53a Hamadk
 * Increased the number of timers in SERVO_TEST mode
 * 
 * 4     11/10/02 9:09 Yarone
 * Added printout in case of create fail
 * 
 * 2     6/30/02 9:45 Yarone
 * Correct set freq to act like initian freq setting (freq in MSec)
 * 
 * 1     27/06/02 11:26 Atai
 * merging with 5e golden version
 * 
 * 5     24/04/02 14:01 Nirm
 * - Fixed a potential problem: When a Timer is created as ONCE, then it
 * was not possible to re-install it from within its callback routine.
 * 
 * 4     23/04/02 9:26 Nirm
 * - Added dependency in "Config.h".
 * 
 * 3     10/04/02 11:28 Ettim
 * Added timer_service_is_active() method.
 * 
 * 2     26/03/02 19:15 Nirm
 * - Various consmetic fixes;
 * - Code cleanup.
 * 
 * 1     25/03/02 13:02 Nirm
 * Preliminary version.
 * 
 * 4     17/02/02 11:40 Atai
 * Correct to absolute include path
 * 
 * 3     2/11/02 8:38 Yarone
 * Added change of frerquency to timers
 * 
 * 2     1/13/02 9:29 Yarone
 * Added attributes of enable/disable and once
 * 
 * 1     12/13/01 18:25 Yarone
 * 
 **************************************************************************************** */

#include "Config.h"		// Global Configuration - do not remove!

#include "Kernel\Timers\Timers.h"
#include "Kernel\Timers\TimersImpl.h"
#include "Kernel\uITRON\rtos.h"


/////////////////////////////////////////////////////////////////////////////
// Constants and Enumerations

#ifdef	SERVO_TEST
#define MAX_TIMERS_CNT	12
#else
#define MAX_TIMERS_CNT	12
#endif


/////////////////////////////////////////////////////////////////////////////
// Structures

typedef struct TimerInfo_TAG {
	UINT16 uIntervalInTicks;
	UINT16 uTicksLeft;
	TIMER_ATTRIBUTES uAttrs;
	LPTIMERPROC lpTimerFunc; 
} TimerInfo;


/////////////////////////////////////////////////////////////////////////////
// Globals and Singletons

static TimerInfo g_aTimers[MAX_TIMERS_CNT];


/////////////////////////////////////////////////////////////////////////////
// System-Timer ISR

/**********************************************************************************
 * Purpose          : Call the priodic functions 
 * Input Parameters : 
 * Return Value     : 
 * Description		: calls every callback function in the array in order of appearance
 * Comments		 	: Should be called every OS timer tick
 **********************************************************************************/

void timer_service_ISR(void)
{
	UINT8 i;

	for (i=0; i < MAX_TIMERS_CNT; i++)
	{
		if ((NULL != g_aTimers[i].lpTimerFunc) && 
			(TIMER_ENABLED == (g_aTimers[i].uAttrs & TIMER_ENABLE_MASK)) && 
			(0 == --g_aTimers[i].uTicksLeft))
		{
			g_aTimers[i].uTicksLeft= g_aTimers[i].uIntervalInTicks;

			// Disable the Timer, if it should be invoked only once
			if (TIMER_ONCE == (g_aTimers[i].uAttrs & TIMER_ONCE_MASK))
				g_aTimers[i].uAttrs &= ~TIMER_ENABLED;

			// Invoke the associated Callback
			(g_aTimers[i].lpTimerFunc)(i+1);
		}
	}
}


/////////////////////////////////////////////////////////////////////////////
// Implementation of Public Services

/**********************************************************************************
 * Purpose          : Creates a Timer service, which call some callback function
 *					  periodically.
 * Input Parameters : i_lpfTimerFunc - the function to be called every interval
 *					: Freq - the frequency in mSec (will be rounded to the timer 
 *					:		 frequency)
 *					: uAttributes - bitwise-or combination of timer attributes.
 * Return Value     : A handle to the Timer on success; NULL on failure.
 * Description		: 
 * Comments		 	: Functions called periodically are called in interrupt context so 
 *					: they must not call blocking services and should be as short as
 *					: possible!
 **********************************************************************************/

UINT8 timer_service_create(LPTIMERPROC i_lpfTimerFunc, UINT16 uFreq, 
						   TIMER_ATTRIBUTES uAttributes)
{
	STATUS_REG SRkeep;
	UINT8 i;
	UINT8 handle= NULL;
	UINT16 uTicksCnt= (uFreq / RTOS_TICK_MS);

	SRkeep= InterruptDisable();

	for (i=0; i < MAX_TIMERS_CNT; i++)
	{	
		// find the first vacant space
		if (NULL == g_aTimers[i].lpTimerFunc)
		{
			handle= (i+1);
			g_aTimers[i].uIntervalInTicks= g_aTimers[i].uTicksLeft= uTicksCnt;
			g_aTimers[i].uAttrs= uAttributes;
			g_aTimers[i].lpTimerFunc= i_lpfTimerFunc;
			break;
		}
	}

	set_SR(SRkeep);
	if( NULL==handle )
	{
		tr_printf(("Timer Create failed !!!!\n\n"));
	}	
	return handle;
}


/**********************************************************************************
 * Purpose          : Remove a function from the periodic calls 
 * Input Parameters : hTimer - A handle to the timer returned from a previous
 *							   call to timer_service_create().
 * Return Value     : TRUE if the timer was successfully removed; FALSE otherwise.
 * Description		: 
 * Comments		 	: 
 **********************************************************************************/

BOOL timer_service_delete(UINT8 hTimer)
{
	BOOL ret= FALSE;
	STATUS_REG SRkeep;

	SRkeep= InterruptDisable();

	if ((hTimer > 0) && (NULL != g_aTimers[hTimer-1].lpTimerFunc))
	{
		g_aTimers[hTimer-1].lpTimerFunc= NULL;
		ret= TRUE;
	}

	set_SR(SRkeep);
	
	return ret;
}


/**********************************************************************************
 * Purpose          : Start a timer count 
 * Input Parameters : hTimer - A handle to the timer returned from a previous
 *							   call to timer_service_create().
 *					: bResetCount - if the counter is to be reset or the counting continued
 * Return Value     : TRUE if the Timer was successfully enabled; FALSE otherwise.
 * Description		: 
 * Comments		 	: can be used to restart a count of a counter that has not yet elapsed
 **********************************************************************************/

BOOL timer_service_enable(UINT8 hTimer, BOOL bResetCount)
{
	BOOL ret= FALSE;
	STATUS_REG SRkeep;

	SRkeep= InterruptDisable();

	if ((hTimer > 0) && (NULL != g_aTimers[hTimer-1].lpTimerFunc))
	{
		g_aTimers[hTimer-1].uAttrs |= TIMER_ENABLED;

		if (bResetCount)
			g_aTimers[hTimer-1].uTicksLeft= g_aTimers[hTimer-1].uIntervalInTicks;

		ret= TRUE;
	}

	set_SR(SRkeep);
	
	return ret;
}


/**********************************************************************************
 * Purpose          : Stop a timer count 
 * Input Parameters : hTimer - A handle to the timer returned from a previous
 *							   call to timer_service_create().
 * Return Value     : TRUE if the Timer was successfully disabled; FALSE otherwise.
 * Description		: 
 * Comments		 	: 
 **********************************************************************************/

BOOL timer_service_disable(UINT8 hTimer)
{
	BOOL ret= FALSE;
	STATUS_REG SRkeep;

	SRkeep= InterruptDisable();

	if ((hTimer > 0) && (NULL != g_aTimers[hTimer-1].lpTimerFunc))
	{
		g_aTimers[hTimer-1].uAttrs &= ~TIMER_ENABLED;

		ret= TRUE;
	}

	set_SR(SRkeep);

	return ret;
}


/**********************************************************************************
 * Purpose          : Change the timer time period 
 * Input Parameters : hTimer - A handle to the timer returned from a previous
 *							   call to timer_service_create().
 *					: uNewFreq - the new frequency
 * Return Value     : TRUE if the Timer's frequency was successfully changed;
 *					  FALSE otherwise.
 * Description		: 
 * Comments		 	: Effect is from next even (current count is not effected)
 **********************************************************************************/

BOOL timer_service_set_freq(UINT8 hTimer, UINT16 uNewFreq)
{
	BOOL ret= FALSE;
	STATUS_REG SRkeep;

	SRkeep= InterruptDisable();

	if ((hTimer > 0) && (NULL != g_aTimers[hTimer-1].lpTimerFunc))
	{
		g_aTimers[hTimer-1].uIntervalInTicks= uNewFreq/RTOS_TICK_MS;
		ret= TRUE;
	}

	set_SR(SRkeep);
	
	return ret;
}


/**********************************************************************************
 * Purpose          : Getting the indication whether a timer is disabled or enabled.
 * Input Parameters : hTimer - A handle to the timer returned from a previous
 *							   call to timer_service_create().
 * Return Value     : TRUE if the Timer is enabled
 *					  FALSE otherwise.
 * Description		: 
 **********************************************************************************/
BOOL timer_service_is_active(UINT8 hTimer)
{
	BOOL ret= FALSE;
	STATUS_REG SRkeep;

	SRkeep= InterruptDisable();

	if ((hTimer > 0) && (NULL != g_aTimers[hTimer-1].lpTimerFunc))
	{
		if ( TIMER_ENABLED == (g_aTimers[hTimer-1].uAttrs & TIMER_ENABLE_MASK))
			ret = TRUE;
	}

	set_SR(SRkeep);
	
	return ret;

}

⌨️ 快捷键说明

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