38 #define MHEG_TIMER_MAGIC 0xc10c0000 43 #define MHEG_TIMER_MAX_WAIT 60000 51 #define MHEG_TIMER_ACCURACY 5 53 #define SIZEOF_TIMER_EVENT_MSG 1 54 #define MAX_NUM_TIMER_EVENT_MSG 100 72 F_TimerCallback callback;
89 static void *mhegTimerListMutex = 0;
90 static void *mhegTimerCountMutex = 0;
96 static void *mhegTimerUpdateEvent = 0;
99 static void *mhegTimerTaskHandle = 0;
103 static BOOLEAN mhegTimerQuit = FALSE;
107 static BOOLEAN mhegTimerTaskRunning = FALSE;
110 static U16BIT mhegTimerIdentityCount = 0;
128 static void TimersMain(
void *pArg)
130 S32BIT eventWaitTimeout = 0;
136 mhegTimerTaskRunning = TRUE;
138 while (!mhegTimerQuit)
144 sysTime += MHEG_TIMER_ACCURACY;
150 while ((timerListHead != 0) && (timerListHead->triggerTime <= sysTime))
155 pOldTimerInfo = timerListHead;
161 timerListHead = timerListHead->next;
164 (pOldTimerInfo->callback)( TRUE, pOldTimerInfo->callerRef,
165 (H_Timer)pOldTimerInfo->timerHdl.ptr);
168 SYS_Free( pOldTimerInfo );
172 if (timerListHead == 0)
178 eventWaitTimeout = MHEG_TIMER_MAX_WAIT;
185 eventWaitTimeout = timerListHead->triggerTime - sysTime;
188 if (eventWaitTimeout > MHEG_TIMER_MAX_WAIT)
190 eventWaitTimeout = MHEG_TIMER_MAX_WAIT;
202 STB_OSReadQueue(mhegTimerUpdateEvent, (
void *) &timerEventMsg, SIZEOF_TIMER_EVENT_MSG, (U16BIT) eventWaitTimeout);
206 mhegTimerTaskRunning = FALSE;
224 F_TimerCallback callback,
void *callerRef,
225 H_Timer *pTimerHandle)
238 if (!mhegTimerCountMutex || !mhegTimerListMutex)
240 return MHERR_COMP_NOT_STARTED;
245 if (pNewTimerInfo == 0)
247 return MHERR_ALLOCATING_MEMORY;
253 pNewTimerInfo->triggerTime = sysTime + millisecs;
255 pNewTimerInfo->callback = callback;
256 pNewTimerInfo->callerRef = callerRef;
267 ++mhegTimerIdentityCount;
268 pNewTimerInfo->timerHdl.u32 = MHEG_TIMER_MAGIC | mhegTimerIdentityCount;
272 *pTimerHandle = (H_Timer)pNewTimerInfo->timerHdl.ptr;
275 if ((timerListHead == 0) ||
276 (timerListHead->triggerTime >= pNewTimerInfo->triggerTime))
282 pNewTimerInfo->next = timerListHead;
283 timerListHead = pNewTimerInfo;
288 STB_OSWriteQueue(mhegTimerUpdateEvent, (
void *) &timerEventMsg, SIZEOF_TIMER_EVENT_MSG, TIMEOUT_NOW);
298 pTimerInfo = timerListHead;
299 while ((pTimerInfo->next != 0) &&
300 (pTimerInfo->next->triggerTime <= pNewTimerInfo->triggerTime))
302 pTimerInfo = pTimerInfo->next;
306 pNewTimerInfo->next = pTimerInfo->next;
307 pTimerInfo->next = pNewTimerInfo;
330 timerHdl.ptr = timerHandle;
331 assert((timerHdl.u32 & 0xffff0000) == MHEG_TIMER_MAGIC);
333 if (mhegTimerListMutex == 0)
335 retVal = MHERR_COMP_NOT_OPEN;
343 if (timerListHead == NULL)
345 retVal = MHERR_BAD_PARAMETER;
349 if (timerListHead->timerHdl.ptr == timerHdl.ptr)
351 pOldTimerInfo = timerListHead;
352 timerListHead = timerListHead->next;
358 prevTimerInfo = timerListHead;
359 pTimerInfo = timerListHead->next;
360 while (pTimerInfo != 0)
362 if (pTimerInfo->timerHdl.ptr == timerHdl.ptr)
364 pOldTimerInfo = pTimerInfo;
365 prevTimerInfo->next = pTimerInfo->next;
368 prevTimerInfo = pTimerInfo;
369 pTimerInfo = pTimerInfo->next;
380 (pOldTimerInfo->callback)( FALSE, pOldTimerInfo->callerRef, (H_Timer)timerHdl.ptr);
383 SYS_Free(pOldTimerInfo);
388 retVal = MHERR_BAD_PARAMETER;
409 if (mhegTimerTaskHandle != 0 && mhegTimerListMutex != 0)
418 TRACE(TKEYS, (
"mhegTimerListMutex=%d", mhegTimerListMutex))
419 if (mhegTimerListMutex == NULL)
421 return MHERR_SYSTEM_RESOURCE_MUTEX;
424 TRACE(TKEYS, (
"mhegTimerCountMutex=%d", mhegTimerCountMutex))
425 if (mhegTimerCountMutex == NULL)
428 mhegTimerListMutex = 0;
429 return MHERR_SYSTEM_RESOURCE_MUTEX;
435 mhegTimerUpdateEvent =
STB_OSCreateQueue(SIZEOF_TIMER_EVENT_MSG, MAX_NUM_TIMER_EVENT_MSG);
436 if (mhegTimerUpdateEvent == NULL)
439 mhegTimerCountMutex = 0;
441 mhegTimerListMutex = 0;
442 return MHERR_SYSTEM_RESOURCE_QUEUE;
449 mhegTimerQuit = FALSE;
452 mhegTimerTaskHandle =
STB_OSCreateTask( TimersMain, NULL, 0x1000, (U8BIT)task_priority, (U8BIT *)
"MhgTimers" );
453 if (mhegTimerTaskHandle == NULL)
456 mhegTimerUpdateEvent = 0;
458 mhegTimerCountMutex = 0;
460 mhegTimerListMutex = 0;
461 return MHERR_SYSTEM_RESOURCE_TASK;
479 if (!mhegTimerTaskHandle)
481 return MHERR_COMP_NOT_OPEN;
485 mhegTimerQuit = TRUE;
488 if (mhegTimerTaskRunning)
490 STB_OSWriteQueue(mhegTimerUpdateEvent, (
void *) &timerEventMsg, SIZEOF_TIMER_EVENT_MSG, TIMEOUT_NOW);
493 while (mhegTimerTaskRunning)
499 mhegTimerTaskHandle = 0;
501 mhegTimerUpdateEvent = NULL;
505 if (mhegTimerListMutex != 0)
509 while (timerListHead != NULL)
513 pTimerInfo = timerListHead;
514 timerListHead = timerListHead->next;
515 (pTimerInfo->callback)(FALSE, pTimerInfo->callerRef, (H_Timer)pTimerInfo->timerHdl.ptr);
516 SYS_Free(pTimerInfo);
521 mhegTimerCountMutex = 0;
523 mhegTimerListMutex = 0;
U32BIT STB_OSGetClockMilliseconds(void)
Get Current Computer Clock Time.
E_MhegErr VT_TimerDestroy(H_Timer timerHandle)
Destroy the specified timer. A callback will not be invoked for this timer after this function return...
void STB_OSTaskDelay(U16BIT timeout)
Delay Task for Specifed Time Period.
E_MhegErr VT_TimerCreate(U32BIT millisecs, F_TimerCallback callback, void *callerRef, H_Timer *pTimerHandle)
Set a timer for a certain number of millseconds. When the timer expires a specified callback will be ...
void STB_OSDeleteMutex(void *mutex)
Delete a mutex.
BOOLEAN STB_OSDestroyQueue(void *queue)
Destroy Queue.
BOOLEAN STB_OSWriteQueue(void *queue, void *msg, U16BIT msg_size, U16BIT timeout)
Write a message to the queue.
void STB_OSMutexUnlock(void *mutex)
Unlock a mutex (a.k.a. 'leave', 'signal' or 'release')
void * STB_OSCreateQueue(U16BIT msg_size, U16BIT msg_max)
Create Queue of given number of messages and size of message.
void * STB_OSCreateMutex(void)
Create a mutex.
BOOLEAN STB_OSReadQueue(void *queue, void *msg, U16BIT msg_size, U16BIT timeout)
Read a message from a queue.
E_MhegErr VT_TimersExit(void)
Uninitialise the timer component - all resources are freed. Other timer functions must not be called ...
void STB_OSMutexLock(void *mutex)
Lock a mutex (a.k.a. 'enter', 'wait' or 'get').
The timer module allows the use of timers within the MHEG5 component. These timers can be set by othe...
E_MhegErr VT_TimersInit(U32BIT task_priority)
Initialise the timer component. This function must be called before any other timer functions are inv...
void * STB_OSCreateTask(void(*function)(void *), void *param, U32BIT stack, U8BIT priority, U8BIT *name)
Create a New Task to the calling process. Upon success, the created task runs on its own stack...
void STB_OSDestroyTask(void *task)
Delete Task must be called upon termination of each task as it frees all OS specific resources alloca...
Header file - Function prototypes for operating system.