summaryrefslogtreecommitdiffstats
blob: 1ba76d45bf6f327dbdde549f0f58a10913c844df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
 * Copyright (c) 2016-2018, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 *  ======== TimerP_tirtos.c ========
 */

#include <ti/osal/TimerP.h>

#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#include <xdc/std.h>
#include <xdc/runtime/Memory.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/Types.h>
#include <xdc/runtime/Timestamp.h>

#if defined (SOC_AM437x) || defined (SOC_AM335x) || defined (SOC_AM574x) || defined (SOC_AM572x) || defined(SOC_AM571x) || defined(SOC_AM65XX) || defined(SOC_J721E)
#if defined(TIMER64_SUPPORT)
#undef   TIMER64_SUPPORT
#endif
#include <ti/sysbios/timers/dmtimer/Timer.h>
#elif defined (SOC_C6678) || defined (SOC_K2G) || defined (SOC_K2H) || defined(SOC_K2K) || defined(SOC_K2L) || defined(SOC_K2E) || defined(SOC_C6657) || defined(SOC_C6678) || defined(SOC_OMAPL137) || defined(SOC_OMAPL138)
#define  TIMER64_SUPPORT
#include <ti/sysbios/timers/timer64/Timer.h>
#else
#include <ti/sysbios/hal/Timer.h>
#endif

#include <ti/osal/src/tirtos/tirtos_config.h>

extern void Osal_DebugP_assert(int32_t expression, const char *file, int32_t line);
#define TIMOSAL_Assert(expression) (Osal_DebugP_assert(((int32_t)((expression)?1:0)),\
                                                  __FILE__, __LINE__))

extern uint32_t  gOsalTimerAllocCnt, gOsalTimerPeak;

/*!
 *  @brief    Timer structure
 */
typedef struct TimerP_tiRtos_s {
    Bool              used;
    Timer_Struct      timer;
} TimerP_tiRtos;

/* global pool of statically allocated semaphore pools */
static TimerP_tiRtos gOsalTimerPTiRtosPool[OSAL_TIRTOS_CONFIGNUM_TIMER];


void TimerP_Params_init(TimerP_Params *params)
{
    TIMOSAL_Assert((params == NULL_PTR));

    if(params != NULL_PTR)
    {
      /* Set the default values */
      params->arg         = NULL_PTR;
      params->extfreqHi   = TimerP_USE_DEFAULT;
      params->extfreqLo   = TimerP_USE_DEFAULT;
      params->name        = NULL_PTR;
      params->period      = 0;
      params->runMode     = (uint32_t)TimerP_RunMode_CONTINUOUS;
      params->startMode   = (uint32_t)TimerP_StartMode_AUTO;
      params->periodType  = (uint32_t)TimerP_PeriodType_MICROSECS;
      params->intNum      = (int32_t)TimerP_USE_DEFAULT;

#if defined (TIMER64_SUPPORT)
      params->timerMode   = TimerP_Timer64Mode_UNCHAINED;
      params->timerHalf   = TimerP_Timer64Half_DEFAULT;
#endif

#if defined (_TMS320C6X)
      params->eventId     = TimerP_USE_DEFAULT;
#endif
    }
    return;
}

TimerP_Handle TimerP_create(int32_t id,
                            TimerP_Fxn tickFxn,
                            const TimerP_Params *params)
{
    TimerP_tiRtos    *handle = NULL_PTR;
    Timer_Params      timerParams;
    Error_Block       eb;
    uint32_t          i;
    uintptr_t         key;

    /* Grab the memory */
    key = HwiP_disable();
    
    if(gOsalTimerAllocCnt==0U)
    {
		(void)memset((void *)gOsalTimerPTiRtosPool,0,sizeof(gOsalTimerPTiRtosPool));
	}
    for (i = 0; i < OSAL_TIRTOS_CONFIGNUM_TIMER; i++)
    {
        if (gOsalTimerPTiRtosPool[i].used == FALSE)
        {
            gOsalTimerPTiRtosPool[i].used = TRUE;
            /* Update statistics */
            gOsalTimerAllocCnt++;
            if (gOsalTimerAllocCnt > gOsalTimerPeak)
            {
                gOsalTimerPeak = gOsalTimerAllocCnt;
            }

            break;
        }
    }
    HwiP_restore(key);

    if (i < OSAL_TIRTOS_CONFIGNUM_TIMER)
    {
        /* Grab the memory */
        handle = (TimerP_tiRtos *) &gOsalTimerPTiRtosPool[i];
    }

    if (handle != NULL_PTR)
    {
        if (params == NULL_PTR)
        {
           Timer_construct(&(handle->timer), id, tickFxn, NULL_PTR, NULL_PTR);
        }
        else
        {
            Timer_Params_init(&timerParams);
            /*
             * Update the default initializations to user configured values
             */

            if (params->name != NULL_PTR)
            {
                timerParams.instance->name        = params->name;
            }

            /* Convert the enumerations for period type */
            switch (params->periodType)
            {
              case (uint32_t)TimerP_PeriodType_COUNTS:
                 timerParams.periodType = Timer_PeriodType_COUNTS;
                 break;

              case (uint32_t)TimerP_PeriodType_MICROSECS:
              default:
                  timerParams.periodType = Timer_PeriodType_MICROSECS;
                  break;
            }

            /* conver the enumerations for run mode */
            switch (params->runMode)
            {
               case (uint32_t)TimerP_RunMode_ONESHOT:
                   timerParams.runMode = Timer_RunMode_ONESHOT;
                   break;
               case (uint32_t)TimerP_RunMode_CONTINUOUS:
               default:
                   timerParams.runMode = Timer_RunMode_CONTINUOUS;
                   break;
            }

            /* conver the enumerations for start mode */
            switch (params->startMode)
            {
               case (uint32_t)TimerP_StartMode_USER:
                   timerParams.startMode = Timer_StartMode_USER;
                   break;
               case (uint32_t)TimerP_StartMode_AUTO:
               default:
                   timerParams.startMode = Timer_StartMode_AUTO;
                   break;
            }

            if ( params->extfreqLo != TimerP_USE_DEFAULT) {
                timerParams.extFreq.lo            = (Bits32) params->extfreqLo;
            }
            if ( params->extfreqHi != TimerP_USE_DEFAULT) {
                timerParams.extFreq.hi            = (Bits32) params->extfreqHi;
            }

            if ( params->period != 0u) {
                timerParams.period                = params->period;
            }

            if ( params->arg != NULL_PTR) {
                timerParams.arg                   = (xdc_UArg) params->arg;
            }

            if ( params->intNum != TimerP_USE_DEFAULT) {
                timerParams.intNum = params->intNum;
            }

#if defined (_TMS320C6X) && !defined(TIMER64_SUPPORT)
            if ( params->eventId != TimerP_USE_DEFAULT) {
                timerParams.eventId = params->eventId;
            }
#endif

#if defined(TIMER64_SUPPORT)
            /* conver the enumerations for timer Half */
            switch (params->timerHalf)
            {
               case TimerP_Timer64Half_LOWER:
                   timerParams.half    = Timer_Half_LOWER;
                   break;
               case TimerP_Timer64Half_UPPER:
                   timerParams.half    = Timer_Half_UPPER;
                   break;
               case TimerP_Timer64Half_DEFAULT:
               default:
                   timerParams.half    = Timer_Half_DEFAULT;
                   break;
            }
#endif
            Error_init(&eb);
            Timer_construct(&handle->timer, (int32_t)id, tickFxn, &timerParams, &eb);
        }
    }
    return ((TimerP_Handle)handle);
}

/*
 *  ======== TimerP_delete ========
 */
TimerP_Status TimerP_delete(TimerP_Handle handle)
{
    TIMOSAL_Assert((handle == NULL_PTR));
    TimerP_Status ret = TimerP_OK;
    uintptr_t   key;
    TimerP_tiRtos *timer = (TimerP_tiRtos *)handle;
    
    if(timer != NULL_PTR)
    {
      Timer_destruct(&timer->timer);
      key = HwiP_disable();
      timer->used = FALSE;
      /* Found the osal timer object to delete */
      if (gOsalTimerAllocCnt > 0U)
      {
        gOsalTimerAllocCnt--;
      }
      HwiP_restore(key);
      ret = TimerP_OK;
    } 
    else
    {
      ret = TimerP_FAILURE;
    }

    return (ret);
}


TimerP_Status TimerP_start(TimerP_Handle handle)
{
    TIMOSAL_Assert((handle == NULL_PTR));
    uintptr_t   key;
    TimerP_tiRtos *timer = (TimerP_tiRtos *)handle;

   /* disable interrupts if an interrupt could lead to
    * another call to Timer_start()*/
    key = Hwi_disable();
    Timer_stop((Timer_Handle)&timer->timer);
    Timer_start((Timer_Handle)&timer->timer);
    (void)Hwi_restore((uint32_t)key);
    return (TimerP_OK);
}

TimerP_Status TimerP_stop(TimerP_Handle handle)
{
    TIMOSAL_Assert((handle == NULL_PTR));
    uintptr_t   key;
    TimerP_tiRtos *timer = (TimerP_tiRtos *)handle;

   /* disable interrupts if an interrupt could lead to
    * another call to Timer_stop()*/
    key = Hwi_disable();
    Timer_stop((Timer_Handle)&timer->timer);
    (void)Hwi_restore((uint32_t)key);
    return (TimerP_OK);
}

TimerP_Status TimerP_setPeriodMicroSecs(TimerP_Handle handle, uint32_t microsecs)
{
  TIMOSAL_Assert((handle == NULL_PTR));
  uintptr_t   key;
  TimerP_tiRtos *timer = (TimerP_tiRtos *)handle;
  TimerP_Status ret = TimerP_OK;

  key = Hwi_disable();
  if (Timer_setPeriodMicroSecs((Timer_Handle)&timer->timer, microsecs)!=1U) {
    ret = TimerP_FAILURE;
  }
  if (ret == TimerP_OK) {
    Timer_start((Timer_Handle)&timer->timer);
  }
  (void)Hwi_restore((uint32_t)key);
  return(ret);
}

uint64_t TimerP_getTimeInUsecs(void)
{
    Types_Timestamp64 bios_timestamp64;
    Types_FreqHz bios_freq;
    uint64_t cur_ts, freq;

    Timestamp_get64(&bios_timestamp64);
    Timestamp_getFreq(&bios_freq);

    cur_ts = ((uint64_t) bios_timestamp64.hi << 32) | bios_timestamp64.lo;
    freq = ((uint64_t) bios_freq.hi << 32) | bios_freq.lo;

    return (cur_ts*1000000u)/freq;
}

/* Nothing past this point */