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

📄 dvb_timer.c

📁 DVB软件,基于CT216软件的开发源程序.
💻 C
字号:
/**************************************************************************

		(C)Copyright Cheertek Inc. 2002-2005,
		   K700, all right reserved.

		Product : STB Firmware

****************************************************************************/
#define DVB_TIMER_ENABLE	/* Let the definition always on top for useful the included files. */

#include <stdio.h>
#include <string.h>
#include "ap_defs.h"

#include "ct_os.h"
#include "task_cfg.h"
#include "dvb_fp.h"
#include "dvb_timer.h"

#if 1
#define DVB_TIMER_MSG(p)
#else
#define DVB_TIMER_MSG(p)			printf p
#endif

#if 1
#define DVB_TIMER_DBG(p)
#else
#define DVB_TIMER_DBG(p) 		printf p
#endif


/*****************************************************************/
#define DVB_TIMER_QUEUE_SIZE				256
#define DVB_TIMER_MAX_NUMBERS			10

typedef struct
{
	CTOS_TIMER		stTimer;
	PF_DVB_TIMER_CB	pfCallback;
	u32				u32TimeInterval;
	bool8			b8InUse;
	bool8			b8Enabled;
} ST_DVB_TIMER;

static CTOS_QUEUE		_stMsgQueue, *_pstMsgQueue = NULL;
static u32				_au32MsgQueue[DVB_TIMER_QUEUE_SIZE];

static ST_DVB_TIMER		_astDvbTimer[DVB_TIMER_MAX_NUMBERS];

/*****************************************************************/
static void dvb_timer_timeout(u32 u32TimerID)
{
	ST_DVB_TIMER*	pstDvbTimer = &_astDvbTimer[u32TimerID];
	MSG_PARAMETER	stSendMsg;
	
	if (pstDvbTimer->b8InUse == FALSE)	return;
	if (pstDvbTimer->b8Enabled == FALSE)	return;
	if (pstDvbTimer->pfCallback == NULL)	return;

	stSendMsg.u8Cmd = (u8)u32TimerID;
	
	DVB_TIMER_DBG(("[DVB_TIMER] timer %d entry\n", u32TimerID));
	if (CT_OS_PutMsg(_pstMsgQueue, &stSendMsg, CTOS_NO_WAIT) != EN_CTOS_SUCCESS)
	{
//		DVB_TIMER_MSG("[DVB_TIMER] send message failed (%ld)\n", u32TimerID);
//      DVB_TIMER_MSG(("[DVB_TIMER] send message failed (%ld): Call Back 0x%08lX\n", u32TimerID, (u32)_astDvbTimer[u32TimerID].pfCallback));
	}	
	CT_OS_ControlTimer(&pstDvbTimer->stTimer, EN_CTOS_TIMER_DISABLE);
	
}

/*****************************************************************/
static u8 dvb_timer_get_empty_id(void)
{
	u8	u8Idx;
	
	for (u8Idx = 0; u8Idx < DVB_TIMER_MAX_NUMBERS; u8Idx++)
	{
		if (_astDvbTimer[u8Idx].b8InUse == FALSE)
			break;
	}
	
	return u8Idx;
}

/*****************************************************************/
void DVB_TimerInitialize(void)
{
	DVB_TIMER_DBG("[DVB_TIMER] initialize!\n");
	if (!_pstMsgQueue)
	{
		if (CT_OS_CreateMsgQueue(
				&_stMsgQueue, "DVBTMQ", _au32MsgQueue, DVB_TIMER_QUEUE_SIZE, EN_CTOS_SUSPEND_FIFO) != EN_CTOS_SUCCESS)
		{
			DVB_TIMER_MSG("[DVB_TIMER] create message queue failed\n");
			return;
		}
		
		_pstMsgQueue = &_stMsgQueue;
	}

	memset(_astDvbTimer, 0, sizeof(_astDvbTimer));
}

/*****************************************************************/
void* DVB_TimerCreate(u32 u32TimeInterval, PF_DVB_TIMER_CB pfTimerCallback)
{
	ST_DVB_TIMER*	pstDvbTimer;
	u8				u8TimerID;

    extern EN_CTOS_STATUS CT_OS_MS_CreateTimer(CTOS_TIMER *pTimer, u8 *pu8Name, EN_CTOS_TIMER_TYPE enType, CTOS_TIMER_CALLBACK_FN fnCallBack, u32 u32Id, u32 u32MSTime);	
    
	DVB_TIMER_DBG("[DVB_TIMER] create!\n");

	u8TimerID = dvb_timer_get_empty_id();
	if (u8TimerID >= DVB_TIMER_MAX_NUMBERS)
	{
		DVB_TIMER_MSG(("no empty timer\n"));
		return NULL;
	}
	
	pstDvbTimer = &_astDvbTimer[u8TimerID];

	if (CT_OS_MS_CreateTimer(
			&pstDvbTimer->stTimer, "DVBTM", EN_CTOS_TIMER_CONTINUOUS,
			dvb_timer_timeout, u8TimerID, u32TimeInterval) != EN_CTOS_SUCCESS)
	{
		DVB_TIMER_MSG("[DVB_TIMER] create timer failed\n");
		return NULL;
	}
	
	pstDvbTimer->b8InUse         = TRUE;
	pstDvbTimer->pfCallback      = pfTimerCallback;
	pstDvbTimer->u32TimeInterval = u32TimeInterval;

	return pstDvbTimer;
}

/*****************************************************************/
void DVB_TimerStart(void* pDvbTimer)
{
	ST_DVB_TIMER*	pstDvbTimer = (ST_DVB_TIMER*)pDvbTimer;
	
	DVB_TIMER_DBG("[DVB_TIMER] enable!\n");
	if(pstDvbTimer == NULL)	return;
	
	if (CT_OS_ControlTimer(&pstDvbTimer->stTimer, EN_CTOS_TIMER_ENABLE) == EN_CTOS_SUCCESS)
		pstDvbTimer->b8Enabled = TRUE;
}

/*****************************************************************/
void DVB_TimerStop(void* pDvbTimer)
{
	ST_DVB_TIMER*	pstDvbTimer = (ST_DVB_TIMER*)pDvbTimer;

	DVB_TIMER_DBG("[DVB_TIMER] disable!\n");
	if(pstDvbTimer == NULL)	return;
	
	if (CT_OS_ControlTimer(&pstDvbTimer->stTimer, EN_CTOS_TIMER_DISABLE) == EN_CTOS_SUCCESS)
		pstDvbTimer->b8Enabled = FALSE;
}

/*****************************************************************/
void DVB_TimerReset(void* pDvbTimer, u32 u32TimeInterval, PF_DVB_TIMER_CB pfTimerCallback)
{
	ST_DVB_TIMER*	pstDvbTimer = (ST_DVB_TIMER*)pDvbTimer;
	bool8			b8Enabled  = FALSE;

	DVB_TIMER_DBG("[DVB_TIMER] reset!\n");
	if(pstDvbTimer == NULL)	return;

	/* Get the timer's current status. */
	b8Enabled = pstDvbTimer->b8Enabled;
	
	if (pfTimerCallback)
		pstDvbTimer->pfCallback = pfTimerCallback;

	//if (pstDvbTimer->u32TimeInterval != u32TimeInterval)
	{
	    extern EN_CTOS_STATUS CT_OS_MS_ResetTimer(CTOS_TIMER *pTimer, EN_CTOS_TIMER_TYPE enType, CTOS_TIMER_CALLBACK_FN fnCallBack, u32 u32MSTime);
		/* It must disabled the timer before reset it. */
		CT_OS_ControlTimer(&pstDvbTimer->stTimer, EN_CTOS_TIMER_DISABLE);
		
		if (CT_OS_MS_ResetTimer(
				&pstDvbTimer->stTimer, EN_CTOS_TIMER_CONTINUOUS, 
				dvb_timer_timeout, u32TimeInterval) == EN_CTOS_SUCCESS)
		{
			pstDvbTimer->u32TimeInterval = u32TimeInterval;
		}

		/*
		 * After reset timer, the timer will be disabled.
		 * Enable it, if it is enabled before reset.
		 */
		if (b8Enabled)
			CT_OS_ControlTimer(&pstDvbTimer->stTimer, EN_CTOS_TIMER_ENABLE);
	}
}

/*****************************************************************/
void DVB_TimerDelete(void** ppDvbTimer)
{
	ST_DVB_TIMER*	pstDvbTimer;
	
	if (*ppDvbTimer == NULL)	return;
	
	pstDvbTimer = (ST_DVB_TIMER*)*ppDvbTimer;
	pstDvbTimer->b8InUse    = FALSE;
	pstDvbTimer->b8Enabled  = FALSE;
	pstDvbTimer->pfCallback = NULL;
	
	CT_OS_DeleteTimer(&pstDvbTimer->stTimer);
	
	*ppDvbTimer = NULL;
}

/*****************************************************************/

bool8 DVB_TimerRunEvent(u32 u32TimeoutMS) /* run on main task */
{
	MSG_PARAMETER	stRecvMsg;
	u32				u32RecvMsgLen;
	ST_DVB_TIMER*	pstDvbTimer;
	u8				u8TimerID;

    if (_pstMsgQueue==NULL)
    {
        return FALSE;
    }

    if(u32TimeoutMS>0)
    {
    	if (u32TimeoutMS / 10 == 0)
    		u32TimeoutMS = 1;
    	else
    		u32TimeoutMS /= 10;
    }
    	
	if (CT_OS_GetMsg(_pstMsgQueue, &stRecvMsg, &u32RecvMsgLen, u32TimeoutMS) != EN_CTOS_SUCCESS)
		return FALSE;
	
	u8TimerID  = stRecvMsg.u8Cmd;
	pstDvbTimer = &_astDvbTimer[u8TimerID];
	
	if ((pstDvbTimer->b8InUse    != FALSE) &&
	    (pstDvbTimer->b8Enabled  != FALSE) &&
	    (pstDvbTimer->pfCallback != NULL ) )
	{
		/* stop the timer until the event done! */
		//CT_OS_ControlTimer(&pstDvbTimer->stTimer, EN_CTOS_TIMER_DISABLE);

		DVB_TIMER_DBG(("[DVB_TIMER] timer %d event\n", u8TimerID));
		/* The callback returns TRUE for continue, FALSE for stop timer. */
		if (pstDvbTimer->pfCallback() == TRUE)
		{
			CT_OS_ControlTimer(&pstDvbTimer->stTimer, EN_CTOS_TIMER_ENABLE);
		}
	}
	
	return TRUE;
}

⌨️ 快捷键说明

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