source: svn/trunk/zas_dstar/hal/os/src/os_timer.c @ 2

Last change on this file since 2 was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
File size: 5.5 KB
Line 
1/****************************************************************************
2 * Copyright (c) 2006 DST Technologies Inc.  All Rights Reserved.   
3 *
4 * Module:      OS TIMER
5 *
6 * Description: Unified APIs for interfacing with different operating systems
7 *
8 * Notes:       This module implement APIs to interface with the following
9 *              operating system's objects:
10 *              - Timers
11 *
12 *              This implementation provide approximate timer functionality
13 *              with a 100 ms resoultion
14 *
15 ***************************************************************************/
16
17/*==========================
18 * Includes
19 *=========================*/
20#include "os_prive.h"
21#include "os.h"
22#include <string.h>
23
24#ifdef DMALLOC
25#include <dmalloc.h>
26#endif
27
28/*=========================
29 * Defines
30 *========================*/
31#define TIMER_MARK              0xAABB0000      /* Arbitrary    */
32#define MAX_TIMERS              8               /* Per process  */
33
34typedef struct
35{
36    void (*UsrFunc) (CORE_TIMER_ID TimerId,DS_U32 TickCount, DS_U32 CallerParam);
37    DS_U32          UsrParam;
38    DS_U32          Delay;
39    DS_BOOL            Active;
40    CORE_TASK_ID    Tid;
41}TIMER_INFO;
42
43/*=================================
44 * Local variables (per process)
45 *================================*/
46TIMER_INFO      TimersInfo[MAX_TIMERS];
47CORE_MUTEX_ID   TimersMutex = 0;
48
49/*=================================
50 * Local prototypes
51 *================================*/
52static void TimerTask (void *Prm);
53
54/******************************************************************************
55 * DS_BOOL Os_TimersInit (DS_BOOL Init)
56 *
57 * Description: Initialize OS timers
58 *
59 * Entry :  Init    = _TRUE_ to init
60 *
61 * Return:  _TRUE_    If success
62 *          _FALSE_   If any error
63 *
64 * Notes :  Must be called during each process initialization.
65 *
66 *****************************************************************************/
67DS_BOOL Os_TimersInit (DS_BOOL Init)
68{
69    if (Init)
70    {
71        if (TimersMutex == 0)
72        {
73            TimersMutex = DstCore_MutexCreate (0);
74            if (TimersMutex == 0)
75            {
76                DBG_MSG ("\n\rError creating timers mutex.");
77                return (_FALSE_);
78            }
79            memset (TimersInfo, 0, MAX_TIMERS * sizeof(TIMER_INFO));
80        }
81    }
82    else
83    {
84        if (TimersMutex)
85        {
86            DstCore_MutexDelete (TimersMutex);
87            TimersMutex = 0;
88        }
89    }
90    return (_TRUE_);
91}
92
93/*^^***************************************************************************
94 * CORE_TIMER_ID DstCore_TimerCreate (DS_U32 MsInterval, DS_U32 CallerParam,
95 *                                  void (*UserFunc) (CORE_TIMER_ID TimerId,
96 *                                  DS_U32 TickCount, DS_U32 CallerParam))
97 *
98 * Description: Causes a periodic callback of specified interval.
99 *
100 * Entry :  MillisecondInterval = desired interval period
101 *
102 * Return:  Timer ID or
103 *          CORE_INVALID_ID on error
104 *
105 * Notes :  This API is not implemented under Linux. If you need to create
106 *          a timer, create a thread for your callback function and insert
107 *          a call to DstCore_Delay() with the required timer interval.
108 *
109 **************************************************************************^^*/
110CORE_TIMER_ID DstCore_TimerCreate (DS_U32 MsInterval, DS_U32 CallerParam,
111                                void (*UserFunc) (CORE_TIMER_ID TimerId,
112                                DS_U32 TickCount, DS_U32 CallerParam))
113{
114    DS_U32 i, RetVal = CORE_INVALID_ID;
115
116    DstCore_MutexLock (TimersMutex, OS_WAIT_FOREVER);
117
118    /*===================================
119     * Find a free timer slot
120     *==================================*/
121    for (i=0; i<MAX_TIMERS; i++)
122    {
123        if (TimersInfo[i].UsrFunc == NULL)
124        {
125            TimersInfo[i].UsrFunc = UserFunc;
126            TimersInfo[i].UsrParam = CallerParam;
127            TimersInfo[i].Delay = MsInterval;
128            TimersInfo[i].Active = _TRUE_;
129            TimersInfo[i].Tid = DstCore_TaskCreate (TimerTask, (void *)i, 
130                                                    TASK_PRIOR_CALLER);
131            if (TimersInfo[i].Tid != 0)
132            {
133                RetVal = TIMER_MARK + i;
134            }
135            else
136            {
137                TimersInfo[i].UsrFunc = NULL;
138            }
139            break;
140        }
141    }
142    DstCore_MutexUnlock (TimersMutex);
143    return (RetVal);
144}
145
146/*^^***************************************************************************
147 * DS_U32 DstCore_TimerDelete(CORE_TIMER_ID TimerId)
148 *
149 * Description:
150 *
151 * Entry :  TimerId = Timer Id
152 *
153 * Return:  OS_OK
154 *          OS_FAIL
155 *
156 * Notes :
157 **************************************************************************^^*/
158DS_U32 DstCore_TimerDelete(CORE_TIMER_ID TimerId)
159{
160    TimerId -= TIMER_MARK;
161    if (TimerId >= MAX_TIMERS)
162    {
163        return (CORE_INVALID_ID);
164    }
165    TimersInfo[TimerId].Active = _FALSE_;
166    DstCore_TaskDelete (TimersInfo[TimerId].Tid);
167    TimersInfo[TimerId].UsrFunc = NULL;
168    return (CORE_OK);
169}
170
171/*^^***************************************************************************
172 * void TimerTask (void *Prm)
173 *
174 * Description:
175 *
176 * Entry : 
177 *
178 * Return: 
179 *
180 * Notes :
181 **************************************************************************^^*/
182static void TimerTask (void *Prm)
183{
184    DS_U32 TimerIdx;
185    TIMER_INFO *TimerP;
186
187    TimerIdx = (DS_U32)Prm;
188    TimerP = &(TimersInfo[TimerIdx]);
189    while (TimerP->Active)
190    {
191        DstCore_Delay (TimerP->Delay);
192        TimerP->UsrFunc (TimerIdx + TIMER_MARK, 0, TimerP->UsrParam);
193    }
194}
Note: See TracBrowser for help on using the repository browser.