/**************************************************************************** * Copyright (c) 2006 DST Technologies Inc. All Rights Reserved. * * Module: OS TIMER * * Description: Unified APIs for interfacing with different operating systems * * Notes: This module implement APIs to interface with the following * operating system's objects: * - Timers * * This implementation provide approximate timer functionality * with a 100 ms resoultion * ***************************************************************************/ /*========================== * Includes *=========================*/ #include "os_prive.h" #include "os.h" #include #ifdef DMALLOC #include #endif /*========================= * Defines *========================*/ #define TIMER_MARK 0xAABB0000 /* Arbitrary */ #define MAX_TIMERS 8 /* Per process */ typedef struct { void (*UsrFunc) (CORE_TIMER_ID TimerId,DS_U32 TickCount, DS_U32 CallerParam); DS_U32 UsrParam; DS_U32 Delay; DS_BOOL Active; CORE_TASK_ID Tid; }TIMER_INFO; /*================================= * Local variables (per process) *================================*/ TIMER_INFO TimersInfo[MAX_TIMERS]; CORE_MUTEX_ID TimersMutex = 0; /*================================= * Local prototypes *================================*/ static void TimerTask (void *Prm); /****************************************************************************** * DS_BOOL Os_TimersInit (DS_BOOL Init) * * Description: Initialize OS timers * * Entry : Init = _TRUE_ to init * * Return: _TRUE_ If success * _FALSE_ If any error * * Notes : Must be called during each process initialization. * *****************************************************************************/ DS_BOOL Os_TimersInit (DS_BOOL Init) { if (Init) { if (TimersMutex == 0) { TimersMutex = DstCore_MutexCreate (0); if (TimersMutex == 0) { DBG_MSG ("\n\rError creating timers mutex."); return (_FALSE_); } memset (TimersInfo, 0, MAX_TIMERS * sizeof(TIMER_INFO)); } } else { if (TimersMutex) { DstCore_MutexDelete (TimersMutex); TimersMutex = 0; } } return (_TRUE_); } /*^^*************************************************************************** * CORE_TIMER_ID DstCore_TimerCreate (DS_U32 MsInterval, DS_U32 CallerParam, * void (*UserFunc) (CORE_TIMER_ID TimerId, * DS_U32 TickCount, DS_U32 CallerParam)) * * Description: Causes a periodic callback of specified interval. * * Entry : MillisecondInterval = desired interval period * * Return: Timer ID or * CORE_INVALID_ID on error * * Notes : This API is not implemented under Linux. If you need to create * a timer, create a thread for your callback function and insert * a call to DstCore_Delay() with the required timer interval. * **************************************************************************^^*/ CORE_TIMER_ID DstCore_TimerCreate (DS_U32 MsInterval, DS_U32 CallerParam, void (*UserFunc) (CORE_TIMER_ID TimerId, DS_U32 TickCount, DS_U32 CallerParam)) { DS_U32 i, RetVal = CORE_INVALID_ID; DstCore_MutexLock (TimersMutex, OS_WAIT_FOREVER); /*=================================== * Find a free timer slot *==================================*/ for (i=0; i= MAX_TIMERS) { return (CORE_INVALID_ID); } TimersInfo[TimerId].Active = _FALSE_; DstCore_TaskDelete (TimersInfo[TimerId].Tid); TimersInfo[TimerId].UsrFunc = NULL; return (CORE_OK); } /*^^*************************************************************************** * void TimerTask (void *Prm) * * Description: * * Entry : * * Return: * * Notes : **************************************************************************^^*/ static void TimerTask (void *Prm) { DS_U32 TimerIdx; TIMER_INFO *TimerP; TimerIdx = (DS_U32)Prm; TimerP = &(TimersInfo[TimerIdx]); while (TimerP->Active) { DstCore_Delay (TimerP->Delay); TimerP->UsrFunc (TimerIdx + TIMER_MARK, 0, TimerP->UsrParam); } }