source: svn/trunk/zas_dstar/hal/os/src/os_lib.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: 34.4 KB
Line 
1/****************************************************************************
2 *^Copyright (c) 2006 DST Technologies Inc.  All Rights Reserved.   
3 *
4 * Module:      OSLIB
5 *
6 * Description: Unified APIs for interfacing with different operating systems
7 *              (LINUX)
8 *
9 * Notes:       This module implement APIs to interface with the following
10 *              operating system's objects:
11 *              - Tasks
12 *              - Mutexes
13 *              - Events
14 *              - Timers
15 *              - Queues
16 *              - Semaphores
17 *
18 *              General rules
19 *              -------------
20 *              - Mutexes, Events, Semaphores and Queues can be shared among
21 *                multiple processes in systems that support multi processes.
22 *              - When timeout is used, it is accurate only if the OS provide
23 *                a way of getting system ticks. Otherwise, system ticks are
24 *                assumed to be 100 per second. Either way, the timeout
25 *                resolution required by any API is 1/100 of a second.
26 *              - NONE of these APIs can be called from ISRs except for:
27 *                DstCore_EventSet
28 *                DstCore_EventReset
29 *                DstCore_QueuePost
30 *              - All objects that can be pended on are created for pending
31 *                with priority-inheritence. This is to eliminate priority
32 *                inversion.
33 *
34 ***************************************************************************/
35/*==========================
36 * Includes
37 *=========================*/
38#include <string.h>
39#include "dsthalcommon.h"
40#include "module/dstmodule.h"
41#include "os.h"
42#include "os_prive.h"
43
44#if USE_V2LIN==1
45#include "vxw_hdrs.h"
46#endif
47#define MUTEX_IC                0xfffffff0
48
49#ifdef DMALLOC
50#include <dmalloc.h>
51#endif
52
53/*==========================
54 * External declarations
55 *=========================*/
56
57/*==================================
58 * Local variables per process
59 *=================================*/
60
61/*^^***************************************************************************
62 *^CORE_TASK_ID DstCore_TaskCreate (void (*TaskFuncPtr) (void *), void *Param,
63 *                                DS_U32 Priority)
64 *
65 * Description: Dynamically create a task.
66 *
67 * Entry :  TaskFuncPtr = Pointer to task starting address
68 *          Parameter   = Parameter to be passed to the task
69 *          Priority    = Can be one of the following values:
70 *                        TASK_PRIOR_CALLER     Use caller priority
71 *                        TASK_PRIOR_LOWEST     Lowest possible priority
72 *                        TASK_PRIOR_LOW       
73 *                        TASK_PRIOR_NORMAL     
74 *                        TASK_PRIOR_HIGH       
75 *                        TASK_PRIOR_HIGHEST   
76 *                        TASK_PRIOR_IST        Highest possible priority
77 *                        If Priority is set to any value between 0-255, it
78 *                        will be passed as-is to the OS
79 *
80 * Return:  Task ID or
81 *          NULL if any error
82 *
83 * Notes :  If the required task priority is TASK_PRIOR_CALLER and this
84 *          function can not obtain caller priority for any reason, the task
85 *          will be created with TASK_PRIOR_NORMAL. This function will not
86 *          return an error if the priority of the newly created task could
87 *          not be set.
88 *
89 **************************************************************************^^*/
90CORE_TASK_ID DstCore_TaskCreate (void (*TaskFuncPtr) (void *), void *Parameter,
91                                 DS_U32 Priority)
92{
93    pthread_t TaskId;
94    DS_S32 RetVal;
95    void * (*pfnThreadFunc)(void *) = (void * (*)(void*)) TaskFuncPtr;
96
97    RetVal = pthread_create (&TaskId, NULL, pfnThreadFunc, Parameter);
98     
99    if (RetVal == 0)
100    {
101        DstCore_TaskSetPriority ((CORE_TASK_ID) TaskId, Priority);
102        return ((CORE_TASK_ID) TaskId);
103    }
104    return ((CORE_TASK_ID) 0);
105}
106
107/*^^***************************************************************************
108 *^DS_U32 DstCore_TaskSuspend (CORE_TASK_ID TaskId)
109 *
110 * Description: Suspend a task.
111 *
112 * Entry :  TaskId  = Returned by TaskCreate
113 *                    Use 0 to suspend the calling task
114 *
115 * Return:  OS_OK
116 *          OS_FAIL
117 *
118 * Notes :
119 **************************************************************************^^*/
120DS_U32 DstCore_TaskSuspend (CORE_TASK_ID TaskId)
121{
122    if (TaskId == 0)
123    {
124        TaskId = (CORE_TASK_ID)pthread_self();
125    }
126    if (pthread_kill((pthread_t)TaskId, SIGSTOP) == 0)
127    {
128        return (OS_OK);
129    }
130    return (OS_FAIL);
131}
132
133/*^^***************************************************************************
134 *^DS_U32 DstCore_TaskResume (CORE_TASK_ID TaskId)
135 *
136 * Description: Resume a previously suspended task.
137 *
138 * Entry :  TaskId  = Returned by TaskCreate
139 *
140 * Return:  OS_OK
141 *          OS_FAIL
142 *
143 * Notes :
144 **************************************************************************^^*/
145DS_U32 DstCore_TaskResume (CORE_TASK_ID TaskId)
146{
147    if (pthread_kill((pthread_t)TaskId, SIGCONT) == 0)
148    {
149        return (OS_OK);
150    }
151    return (OS_FAIL);
152}
153
154/*^^***************************************************************************
155 *^DS_U32 DstCore_TaskGetInfo (CORE_TASK_ID *TaskId)
156 *
157 * Description: Get information about the current task (the caller).
158 *
159 * Entry :  TaskId  = Pointer to CORE_TASK_ID to receive task ID.
160 *
161 * Return:  OS_OK
162 *          OS_FAIL
163 *
164 * Notes :
165 **************************************************************************^^*/
166DS_U32 DstCore_TaskGetInfo (CORE_TASK_ID *TaskId)
167{
168    *TaskId = (CORE_TASK_ID)pthread_self();
169    return (OS_OK);
170}
171
172/*^^***************************************************************************
173 *^DS_U32 DstCore_TaskSetPriority (CORE_TASK_ID TaskId, DS_U32 Priority)
174 *
175 * Description: Change task priority
176 *
177 * Entry :  TaskId      = Returned by TaskCreate.
178 *                        Use 0 to specify calling task.
179 *          Priority    = Can be one of the following values:
180 *                        TASK_PRIOR_CALLER     Use caller priority
181 *                        TASK_PRIOR_LOWEST     Lowest possible priority
182 *                        TASK_PRIOR_LOW       
183 *                        TASK_PRIOR_NORMAL     
184 *                        TASK_PRIOR_HIGH       
185 *                        TASK_PRIOR_HIGHEST   
186 *                        TASK_PRIOR_IST        Highest possible priority
187 *                        If Priority is set to any value between 0-255, it
188 *                        will be passed as-is to the OS
189 *
190 * Return:  OS_OK
191 *          OS_FAIL
192 *
193 * Notes :  If the required task priority is TASK_PRIOR_CALLER and this
194 *          function can not obtain caller priority for any reason, the task
195 *          will be created with TASK_PRIOR_NORMAL
196 *
197 **************************************************************************^^*/
198DS_U32 DstCore_TaskSetPriority (CORE_TASK_ID TaskId, DS_U32 Priority)
199{
200    CORE_TASK_ID CallerTid;
201    int Policy = SCHED_FIFO;
202    struct sched_param SchedParam;
203
204    if (TaskId == 0)
205    {
206        DstCore_TaskGetInfo (&TaskId);
207    }
208
209    if (Priority > 255)
210    {
211        switch (Priority)
212        {
213            case TASK_PRIOR_LOWEST:
214                Priority = 1;
215                break;
216            case TASK_PRIOR_LOW:
217                Priority = 20;
218                break;
219            case TASK_PRIOR_NORMAL:
220                Priority = 40;
221                break;
222            case TASK_PRIOR_HIGH:
223                Priority = 70;
224                break;
225            case TASK_PRIOR_HIGHEST:
226                Priority = 90;
227                break;
228            case TASK_PRIOR_IST:
229                Priority = 99;
230                break;
231            case TASK_PRIOR_CALLER:
232            default:
233                Priority = 40;
234                DstCore_TaskGetInfo (&CallerTid);
235                if (pthread_getschedparam((pthread_t)CallerTid, &Policy, &SchedParam)== 0)
236                {
237                    Priority = SchedParam.sched_priority;
238                }
239                break;
240        }
241    }
242    SchedParam.sched_priority = Priority;
243    if (pthread_setschedparam ((pthread_t)TaskId, Policy, &SchedParam) == 0)
244    {
245        return (OS_OK);
246    }
247    return (OS_FAIL);
248}
249
250/*^^***************************************************************************
251 *^DS_U32 DstCore_TaskDelete (CORE_TASK_ID TaskId)
252 *
253 * Description: Delete a task.
254 *
255 * Entry :  TaskId  = Returned by TaskCreate
256 *
257 * Return:  OS_OK
258 *          OS_FAIL
259 *
260 * Notes :
261 **************************************************************************^^*/
262DS_U32 DstCore_TaskDelete (CORE_TASK_ID TaskId)
263{
264    DS_S32 RetVal;
265    CORE_TASK_ID CurrentId = (CORE_TASK_ID)pthread_self();
266 
267    /* if taskid is zero, thread wants to self terminate, set value to current id */
268
269    if( TaskId == 0 )
270    {
271      TaskId = CurrentId;
272    }
273 
274    /* check if a task wants to terminate itself */
275    if (CurrentId != TaskId)
276    {
277        /*
278         * Can't terminate another thread. it must do it's self, or must
279         * modify cancellation attributes.  Without this attribute, the thread
280         * may refuse to die unless/util it calls a cancellation point, which
281         * means it calls certain system apis, blocking operation like
282         * semaphore wait, or calls pthread_testcancel () periodically...
283         */
284        pthread_cancel ((pthread_t)TaskId);         /* Send cancel request   */
285        pthread_join ((pthread_t) TaskId, NULL);    /* Wait for task to exit */
286    }
287    else
288    {
289
290        /* Thread wants to terminate itself */
291        pthread_detach( (pthread_t)CurrentId );
292        pthread_exit (&RetVal);
293    }
294    return (OS_OK);
295}
296
297/*^^***************************************************************************
298 *^CORE_SEM_ID DstCore_SemCreate (DS_U32 SemName, DS_U32 InitCount)
299 *
300 * Description: It creates a semaphore object with the specified count.
301 *
302 * Entry :  SemName     = Unique number identifying the semaphore.
303 *                        Set to NULL to create unnamed semaphore (can not be
304 *                        used for inter-process synchronization)
305 *          InitCount   = Initial semaphore count (1 to 255)
306 *                        or 0xfffffff0 for a mutex
307 *
308 * Return:  Semaphore ID or
309 *          0 if any error
310 *
311 * Notes :  The entry SemName is used only in systems that support multiple
312 *          processes.
313 *
314 **************************************************************************^^*/
315CORE_SEM_ID DstCore_SemCreate (DS_U32 SemName, DS_U32 InitCount)
316{
317#if USE_V2LIN==0
318    OBJ_OPER ObjOp;
319
320    ObjOp.Opcode = OBJ_OPCODE_CREATE;
321    ObjOp.Prm1   = SemName;
322    ObjOp.Prm2   = InitCount;
323
324    Os_PerformIoctl (DSTHWIOC_SEM_OP, &ObjOp);
325    if ((ObjOp.RetCode == OBJ_OK) && (ObjOp.Prm2 != 0))
326    {
327        return (ObjOp.Prm2);
328    }
329    INFO_MSG ("\n*** Semaphore create error 0x%lx ***\n", ObjOp.RetCode);
330#endif
331    return (0);
332}
333
334/*^^***************************************************************************
335 *^DS_U32 DstCore_SemLock (CORE_SEM_ID SemId, DS_U32 Timeout)
336 *
337 * Description:
338 *
339 * Entry :  SemId   = Returned by SemCreate
340 *          Timeout = In 1/100 of a second increments. This is ONLY accurate
341 *                    if the OS provides a way of getting system ticks/second.
342 *                    = 0 to return immidiately if the resource not available
343 *                    = WAIT_FOREVER to wait forever on the resource
344 *
345 * Return:  OS_OK
346 *          OS_TIMEOUT
347 *          OS_FAIL
348 * Notes :
349 **************************************************************************^^*/
350DS_U32 DstCore_SemLock (CORE_SEM_ID SemId, DS_U32 Timeout)
351{
352    DS_U32 RetVal = OS_FAIL;
353#if USE_V2LIN==0
354    OBJ_OPER ObjOp;
355
356    ObjOp.Prm2 = Timeout;
357    while (_TRUE_)
358    {
359        ObjOp.Opcode = OBJ_OPCODE_LOCK;
360        ObjOp.Prm1   = SemId;
361        Os_PerformIoctl (DSTHWIOC_SEM_OP, &ObjOp);
362        if (ObjOp.RetCode == OBJ_OK)
363        {
364            RetVal = OS_OK;
365            break;
366        }
367        if (ObjOp.RetCode == OBJ_TIMEOUT)
368        {
369            RetVal = OS_TIMEOUT;
370            break;
371        }
372        if (ObjOp.RetCode == OBJ_SIGNAL_RCV)
373        {
374            INFO_MSG ("\n*** Semaphore Signal Received ***\n");
375        }
376        else
377        {
378            INFO_MSG ("\n*** Semaphore lock error %ld ***\n", ObjOp.RetCode);
379            break;
380        }
381    }
382#endif
383
384    return (RetVal);
385}
386
387/*^^***************************************************************************
388 *^DS_U32 DstCore_SemUnlock (CORE_SEM_ID SemId)
389 *
390 * Description:
391 *
392 * Entry :  SemId   = Returned by SemCreate
393 *
394 * Return:  OS_OK
395 *          OS_FAIL
396 *
397 * Description: This function unlock a semaphore. The semaphore count will be
398 *               incremented if succeeds.
399 *  Note:
400 **************************************************************************^^*/
401DS_U32 DstCore_SemUnlock (CORE_SEM_ID SemId)
402{
403#if USE_V2LIN==0
404    OBJ_OPER ObjOp;
405
406    ObjOp.Opcode = OBJ_OPCODE_UNLOCK;
407    ObjOp.Prm1   = SemId;
408
409    Os_PerformIoctl (DSTHWIOC_SEM_OP, &ObjOp);
410    if (ObjOp.RetCode == OBJ_OK)
411    {
412        return (OS_OK);
413    }
414    INFO_MSG ("\n*** Semaphore unlock error %ld ***\n", ObjOp.RetCode);
415#endif
416    return (OS_FAIL);
417}
418
419/*^^***************************************************************************
420 *^DS_U32 DstCore_SemDelete (CORE_SEM_ID SemId)
421 *
422 * Description:
423 *
424 * Entry :  SemId   = Returned by SemCreate
425 *
426 * Return:  OS_OK
427 *          OS_FAIL
428 *
429 * Description: This function delete the semaphore object in the system.
430 * Notes :
431 **************************************************************************^^*/
432DS_U32 DstCore_SemDelete (CORE_SEM_ID SemId)
433{
434#if USE_V2LIN==0
435    OBJ_OPER ObjOp;
436
437    ObjOp.Opcode = OBJ_OPCODE_DELETE;
438    ObjOp.Prm1   = SemId;
439
440    Os_PerformIoctl (DSTHWIOC_SEM_OP, &ObjOp);
441    if (ObjOp.RetCode == OBJ_OK)
442    {
443        return (OS_OK);
444    }
445
446    INFO_MSG ("\n*** Semaphore delete error %ld ***\n", ObjOp.RetCode);
447#endif
448    return (OS_FAIL);
449}
450
451/*^^***************************************************************************
452 *^DS_U32 DstCore_TaskLockCreate (void)
453 *
454 * Description: Create a task locking object
455 *
456 * Entry :  None
457 *
458 * Return:  Lock object ID
459 *          0 if error
460 *
461 * Notes :  This call creates a task locking object.
462 *          The lock object is used as a parameter to DstCore_TaskLockSet() and
463 *          can be deleted when not needed by calling DstCore_TaskLockDelete().
464 *          The lock object will obtain execlusive access to a piece of code
465 *          only on a process basis. To obtain inter-process execlusive access
466 *          to a piece of code or data, use named mutexes.
467 **************************************************************************^^*/
468CORE_LOCK_ID DstCore_TaskLockCreate (void)
469{   
470    return ((CORE_LOCK_ID)(DstCore_SemCreate (0, MUTEX_IC)));
471}
472
473/*^^***************************************************************************
474 *^DS_U32 DstCore_TaskLockSet (CORE_LOCK_ID LockId)
475 *
476 * Description: Obtain execlusive access to a piece of code/data.
477 *
478 * Entry :  LockId  = Task lock ID returned by DstCore_TaskLockCreate()
479 *
480 * Return:  OS_OK
481 *          OS_FAIL
482 *
483 * Notes :  This call should be paired with DstCore_TaskLockReset. Interrupts
484 *          will not be disabled as a result of this call. This call is similar
485 *          to DstCore_MutexLock except that Locks applies to tasks in the
486 *          current process only. Mutexes, on the other hand, are system wide.
487 *          The task that resets the lock must be the same task that set it.
488 **************************************************************************^^*/
489DS_U32 DstCore_TaskLockSet (CORE_LOCK_ID LockId)
490{
491    return (DstCore_SemLock(LockId, OS_WAIT_FOREVER));
492}
493
494/*^^***************************************************************************
495 *^DS_U32 DstCore_TaskLockReset (CORE_LOCK_ID LockId)
496 *
497 * Description: Release task execlusive access to a piece of code/data.
498 *
499 * Entry :  LockId  = Task lock ID returned by DstCore_TaskLockCreate()
500 *
501 * Return:  OS_OK
502 *          OS_FAIL
503 *
504 * Notes :  This call should be paired with DstCore_TaskLockSet. Interrupts
505 *          will not be affaected as a result of this call. This call is
506 *          similar to DstCore_MutexUnlock except that Locks applies to tasks
507 *          in the current process only. Mutexes, on the other hand, are
508 *          system wide resources. The task that resets the lock must be the
509 *          same task that set it using DstCore_TaskLockSet().
510 **************************************************************************^^*/
511DS_U32 DstCore_TaskLockReset (CORE_LOCK_ID LockId)
512{
513    return (DstCore_SemUnlock(LockId));
514}
515
516/*^^***************************************************************************
517 *^DS_U32 DstCore_TaskLockDelete (CORE_LOCK_ID LockId)
518 *
519 * Description: Delete a task lock object.
520 *
521 * Entry :  LockId  = Task lock ID returned by DstCore_TaskLockCreate()
522 *
523 * Return:  OS_OK
524 *          OS_FAIL
525 *
526 * Notes :  This call should be paired with DstCore_TaskLockCreate.
527 **************************************************************************^^*/
528DS_U32 DstCore_TaskLockDelete (CORE_LOCK_ID LockId)
529{
530    return (DstCore_SemDelete(LockId));
531}
532
533/*^^***************************************************************************
534 *^CORE_MUTEX_ID DstCore_MutexCreate (DS_U32 MutexName)
535 *
536 * Description: Create a mutual execlusion object (Mutex)
537 *
538 * Entry :  MutexName = Unique number identifying the mutex.
539 *                      Set to NULL to create unnamed mutex (can not be used
540 *                      for inter-process synchronization)
541 *
542 * Return:  Mutex ID or
543 *          0 if any error
544 *
545 * Notes :  The mutex will be created in non-locked state.
546 *          The entry MutexName is used only in systems that support multiple
547 *          processes.
548 **************************************************************************^^*/
549CORE_MUTEX_ID DstCore_MutexCreate (DS_U32 MutexName)
550{
551    return (DstCore_SemCreate (MutexName, MUTEX_IC));
552}
553
554/*^^***************************************************************************
555 *^DS_U32 DstCore_MutexLock (CORE_MUTEX_ID MutexId, DS_U32 Timeout)
556 *
557 * Description: Lock a mutex with timeout.
558 *
559 * Entry :  MutexId = Returned by DstCore_MutexCreate
560 *          Timeout = In 1/100 of a second increments. This is ONLY accurate
561 *                    if the OS provides a way of getting system ticks/second.
562 *                    = 0 to return immidiately if the resource not available
563 *                    = OS_WAIT_FOREVER to wait forever on the resource
564 *
565 * Return:  OS_OK
566 *          OS_TIMEOUT
567 *          OS_FAIL
568 *
569 * Notes :
570 **************************************************************************^^*/
571DS_U32 DstCore_MutexLock (CORE_MUTEX_ID MutexId, DS_U32 Timeout)
572{     
573    return (DstCore_SemLock(MutexId, Timeout));
574}
575
576/*^^***************************************************************************
577 *^DS_U32 DstCore_MutexUnlock (CORE_MUTEX_ID MutexId)
578 *
579 * Description: Unlock a previously locked mutex
580 *
581 * Entry :  MutexId   = Returned by DstCore_MutexCreate
582 *
583 * Return:  OS_OK
584 *          OS_FAIL
585 *
586 * Notes :
587 **************************************************************************^^*/
588DS_U32 DstCore_MutexUnlock (CORE_MUTEX_ID MutexId)
589{
590    return (DstCore_SemUnlock(MutexId));
591}
592
593/*^^***************************************************************************
594 *^DS_U32 DstCore_MutexDelete (CORE_MUTEX_ID MutexId)
595 *
596 * Description: Delete a mutex
597 *
598 * Entry :  MutexId   = Returned by DstCore_MutexCreate
599 *
600 * Return:  OS_OK
601 *          OS_FAIL
602 *
603 * Notes :
604 **************************************************************************^^*/
605DS_U32 DstCore_MutexDelete (CORE_MUTEX_ID MutexId)
606{
607    return (DstCore_SemDelete(MutexId));
608}
609
610/*^^*************************************************************************
611 *^CORE_EVENT_ID DstCore_EventCreate(DS_U32 EventName, DS_BOOL bManualReset)
612 *                           
613 * Description: Creates an event object.
614 *
615 * Entry:   EventName = Unique number identifying the Event.
616 *                      Set to NULL to create unnamed event (can not be used
617 *                      for inter-process synchronization)
618 *          Signaled  = _TRUE_  to create the evenet in signaled state.
619 *                      _FALSE_ to create the evenet in non-signaled state.
620 *
621 * Return:  A handle to the event object if succeeds .
622 *          It returns 0 if the call fails.
623 *
624 * Notes:   The entry EventName is used only in systems that support multiple
625 *          processes.
626 *************************************************************************^^*/
627
628CORE_EVENT_ID DstCore_EventCreate (DS_U32 EventName, DS_BOOL Signaled)
629{
630#if USE_V2LIN==0
631    OBJ_OPER ObjOp;
632
633    ObjOp.Opcode = OBJ_OPCODE_CREATE;
634    ObjOp.Prm1   = EventName;
635    ObjOp.Prm2   = Signaled;
636
637    Os_PerformIoctl (DSTHWIOC_EVENT_OP, &ObjOp);
638    if ((ObjOp.RetCode == OBJ_OK) && (ObjOp.Prm2 != 0))
639    {
640        return (ObjOp.Prm2);
641    }
642    INFO_MSG ("\n*** Event create error %ld ***\n", ObjOp.RetCode);
643#endif
644    return (0);
645}
646
647/*^^***************************************************************************
648 *^DS_U32 DstCore_EventSet (CORE_EVENT_ID EventId)
649 *
650 * Description: Set an event to signaled state
651 *
652 * Entry :  EventId = Returned by EventCreate
653 *
654 * Return:  OS_OK
655 *          OS_FAIL
656 *
657 * Notes :
658 **************************************************************************^^*/
659DS_U32 DstCore_EventSet (CORE_EVENT_ID EventId)
660{
661#if USE_V2LIN==0
662    OBJ_OPER ObjOp;
663
664    ObjOp.Opcode = OBJ_OPCODE_SET;
665    ObjOp.Prm1   = EventId;
666
667    Os_PerformIoctl (DSTHWIOC_EVENT_OP, &ObjOp);
668    if (ObjOp.RetCode == OBJ_OK)
669    {
670        return (OS_OK);
671    }
672    INFO_MSG ("\n*** Event set error %ld ***\n", ObjOp.RetCode);
673#endif
674    return (OS_FAIL);
675}
676
677/*^^***************************************************************************
678 *^DS_U32 DstCore_EventReset (CORE_EVENT_ID EventId)
679 *
680 * Description: Set an event to non-signaled state
681 *
682 * Entry :  EventId   = Returned by EventCreate
683 *
684 * Return:  OS_OK
685 *          OS_FAIL
686 *
687 * Notes :
688 **************************************************************************^^*/
689DS_U32 DstCore_EventReset (CORE_EVENT_ID EventId)
690{
691#if USE_V2LIN==0
692    OBJ_OPER ObjOp;
693
694    ObjOp.Opcode = OBJ_OPCODE_RESET;
695    ObjOp.Prm1   = EventId;
696
697    Os_PerformIoctl (DSTHWIOC_EVENT_OP, &ObjOp);
698    if (ObjOp.RetCode == OBJ_OK)
699    {
700        return (OS_OK);
701    }
702    INFO_MSG ("\n*** Event set error %ld ***\n", ObjOp.RetCode);
703#endif
704    return (OS_FAIL);
705}
706
707/*^^***************************************************************************
708 *^DS_U32 DstCore_EventWait (CORE_EVENT_ID EventId, DS_BOOL AutoReset, DS_U32 Timeout)
709 *                       
710 * Description: Wait for an event with a timeout.
711 *
712 * Entry :  EventId   = Returned by EventCreate
713 *          AutoReset = _TRUE_ to reset the event after receiving it.
714 *                      _FALSE_ to leave the event signaled after receiving it.
715 *          Timeout   = In 1/100 of a second increments. This is ONLY accurate
716 *                      if the OS provides a way of getting system ticks/sec.
717 *                      = OS_WAIT_FOREVER to wait forever on the resource
718 *
719 * Return:  OS_OK
720 *          OS_TIMEOUT
721 *          OS_FAIL
722 *
723 * Notes :
724 *
725 **************************************************************************^^*/
726DS_U32 DstCore_EventWait (CORE_EVENT_ID EventId, DS_BOOL AutoReset, 
727                          DS_U32 Timeout)
728{
729#if USE_V2LIN==0
730    OBJ_OPER ObjOp;
731    DS_U32 RetVal = OS_FAIL;
732
733    ObjOp.Prm2 = Timeout;
734    while (_TRUE_)
735    {
736        ObjOp.Opcode  = OBJ_OPCODE_WAIT;
737        ObjOp.Prm1    = EventId;
738        ObjOp.RetCode = AutoReset;
739        Os_PerformIoctl (DSTHWIOC_EVENT_OP, &ObjOp);
740        if (ObjOp.RetCode == OBJ_OK)
741        {
742            RetVal = OS_OK;
743            break;
744        }
745        if (ObjOp.RetCode == OBJ_TIMEOUT)
746        {
747            RetVal = OS_TIMEOUT;
748            break;
749        }
750        if (ObjOp.RetCode == OBJ_SIGNAL_RCV)
751        {
752            INFO_MSG ("\n*** Event Signal Received ***\n");
753        }
754        else
755        {
756            INFO_MSG ("\n*** Event wait error %ld ***\n", ObjOp.RetCode);
757            break;
758        }
759    }
760    return (RetVal);
761#else
762    return CORE_INVALID_ID;
763#endif
764}
765
766/*^^***************************************************************************
767 *^DS_U32 DstCore_EventDelete (CORE_EVENT_ID EventId)
768 *
769 * Description: Delete an event.
770 *
771 * Entry :  EventId   = Returned by EventCreate
772 *
773 * Return:  OS_OK
774 *          OS_FAIL
775 *
776 * Notes :
777 **************************************************************************^^*/
778DS_U32 DstCore_EventDelete (CORE_EVENT_ID EventId)
779{
780#if USE_V2LIN==0
781    OBJ_OPER ObjOp;
782
783    ObjOp.Opcode = OBJ_OPCODE_DELETE;
784    ObjOp.Prm1   = EventId;
785
786    Os_PerformIoctl (DSTHWIOC_EVENT_OP, &ObjOp);
787    if (ObjOp.RetCode == OBJ_OK)
788    {
789        return (OS_OK);
790    }
791    INFO_MSG ("\n*** Event delete error %ld ***\n", ObjOp.RetCode);
792#endif
793    return (OS_FAIL);
794} 
795
796/*^^***************************************************************************
797 *^CORE_EVENT_ID DstCore_EvGroupCreate (DS_BOOL Signaled, DS_U32 SubEvents)
798 *
799 * Description: Create multiple events in an event group
800 *
801 * Entry :  Signaled  = _TRUE_  to create the events in signaled state.
802 *                      _FALSE_ to create the events in non-signaled state.
803 *          SubEvents = Number of distinct sub-events encompassed by the event.
804 *
805 * Return:  Event group ID or
806 *          NULL if any error
807 *
808 * Notes :  An event group is a max of 32 events represented by bits in a
809 *          word. An event is set if a bit is set and reset otherwise.
810 *          Event groups can not be shared between processes.
811 *
812 **************************************************************************^^*/
813CORE_EVENT_ID DstCore_EvGroupCreate(DS_BOOL Signaled, DS_U32 SubEvents)
814{
815    return (CORE_INVALID_ID);
816}
817
818/*^^***************************************************************************
819 *^DS_U32 DstCore_EvGroupSet (CORE_EVENT_ID EventId, DS_U32 Mask)
820 *
821 * Description:
822 *
823 * Entry :  EventId = Returned by EventsCreate
824 *          Mask    = Mask to set the required events in the group
825 *
826 * Return:  OS_OK
827 *          OS_FAIL
828 *
829 * Notes :
830 **************************************************************************^^*/
831DS_U32 DstCore_EvGroupSet(CORE_EVENT_ID EventId, DS_U32 Mask)
832{
833    return (OS_FAIL);
834}
835
836/*^^***************************************************************************
837 *^DS_U32 DstCore_EvGroupReset (CORE_EVENT_ID EventId, DS_U32 Mask)
838 *
839 * Description:
840 *
841 * Entry :  EventId = Returned by EventCreate
842 *          Mask    = Mask for the events to be reset in the event group
843 *
844 * Return:  OS_OK
845 *          OS_FAIL
846 *
847 * Notes :
848 **************************************************************************^^*/
849DS_U32 DstCore_EvGroupReset(CORE_EVENT_ID EventId, DS_U32 Mask)
850{
851    return (OS_FAIL);
852}
853
854/*^^***************************************************************************
855 *^DS_U32 DstCore_EvGroupWait (CORE_EVENT_ID EventId, DS_U32 *EventsPtr,
856 *                            DS_BOOL AutoReset, DS_U32 Timeout)
857 *
858 * Description:
859 *
860 * Entry :  EventId   = Returned by EventCreate
861 *          AutoReset = _TRUE_ to reset the events after receiving it.
862 *                      _FALSE_ to leave the events signaled after receiving it.
863 *          EventsPtr = Ptr to the mask of the required events bits. Any other
864 *                      pending sub-events are ingored. This ptr also receives
865 *                      the pending events on return.
866 *          Timeout   = In 1/100 of a second increments. This is ONLY accurate
867 *                      if the OS provides a way of getting system ticks/sec.
868 *                      = OS_WAIT_FOREVER to wait forever on the resource
869 *
870 * Return:  OS_OK
871 *          OS_TIMEOUT
872 *          OS_FAIL
873 *          If OS_OK is returned, *EventsPtr is set to reflect the events
874 *          that actually happened
875 *
876 * Notes :
877 **************************************************************************^^*/
878DS_U32 DstCore_EvGroupWait (CORE_EVENT_ID EventId, DS_U32 *EventsPtr, 
879                                     DS_BOOL AutoReset, DS_U32 Timeout)
880{
881    return (OS_FAIL);
882}
883
884/*^^***************************************************************************
885 *^DS_U32 DstCore_EvGroupDelete (CORE_EVENT_ID EventId)
886 *
887 * Description: Delete an event group.
888 *
889 * Entry :  EventId   = Returned by EventsCreate
890 *
891 * Return:  OS_OK
892 *          OS_FAIL
893 *
894 * Notes :
895 **************************************************************************^^*/
896DS_U32 DstCore_EvGroupDelete(CORE_EVENT_ID EventId)
897{
898    return (OS_FAIL);
899}
900
901/*^^***************************************************************************
902 *^DS_U32 DstCore_Get100HzClockTick (void)
903 *
904 * Description: Get the current clock tick based on a 100 Hz clock.
905 *
906 * Entry :  None
907 *
908 * Return:  Current system clock tick based on a 100 Hz clock.
909 *
910 * Notes :  The returned value will wraparound when reaching 0xffffffff
911 **************************************************************************^^*/
912DS_U32 DstCore_Get100HzClockTick (void)
913{
914    DS_S64 i;
915
916    i = ((DS_S64)DstCore_GetSysClockTick()) * 100;
917    return ((DS_U32)(i / DstCore_GetSysClockFreq()));
918}
919
920/*^^***************************************************************************
921 *^DS_U32 DstCore_GetSysClockTick (void)
922 *
923 * Description: Get the current clock tick based on OEM clock frequency.
924 *
925 * Entry :  None
926 *
927 * Return:  Current system clock tick based on clock frequency provided by
928 *          DstCore_GetSysClockFreq().
929 *
930 * Notes :  The returned value will wraparound when reaching 0xffffffff
931 **************************************************************************^^*/
932DS_U32 DstCore_GetSysClockTick (void)
933{
934    struct timeval tv;
935    struct timezone tz;
936    DS_S64  Milliseconds;
937
938    gettimeofday (&tv, &tz);
939    Milliseconds = ((DS_S64) tv.tv_sec * (DS_S64) 1000) + 
940                    (DS_S64) (tv.tv_usec / 1000);
941    return ((DS_U32) Milliseconds);
942}
943
944/*^^***************************************************************************
945 *^DS_U32 DstCore_GetSysClockFreq (void)
946 *
947 * Description: Get system clock frequency defined by the OEM.
948 *
949 * Entry :  None
950 *
951 * Return:  System clock frequency. If the OS does not provide a way of
952 *          getting system clock frequency, the return value will be 100.
953 *
954 * Notes :  The system clock frequency varies depending on the operating
955 *          system and OEM settings. Typically, the OEM sets the system
956 *          clock to 100 Hz, but this can vary.
957 **************************************************************************^^*/
958DS_U32 DstCore_GetSysClockFreq (void)
959{
960    return (1000);
961}
962
963/*^^***************************************************************************
964 *^DS_U32 DstCore_SetTimeOfDay (DS_U32 UtcSeconds)
965 *
966 * Description: Set system time of day
967 *
968 * Entry :  UtcSeconds = Seconds in UTC
969 *
970 * Return:  OS_OK
971 *          OS_FAIL
972 *
973 * Notes :  The time is in UTC (time elapsed since 00:00:00 Jan 1 1970)
974 *
975 **************************************************************************^^*/
976DS_U32 DstCore_SetTimeOfDay (DS_U32 UtcSeconds)
977{
978    struct timeval Time;
979
980    Time.tv_sec = UtcSeconds;
981    Time.tv_usec = 0;
982    if (settimeofday (&Time, NULL) == 0)
983    {
984        return (OS_OK);
985    }
986    return (OS_FAIL);
987}
988
989/*^^***************************************************************************
990 *^DS_U32 DstCore_GetTimeOfDay (DS_U32 *UtcSeconds)
991 *
992 * Description: Get system time of day (Coordinated Universal Time)
993 *
994 * Entry :  *UtcSeconds  = Pointer to receive seconds in UTC
995 *
996 * Return:  OS_OK
997 *          OS_FAIL
998 *
999 * Notes :  The returned time is UTC (time elapsed since 00:00:00 Jan 1 1970)
1000 *
1001 **************************************************************************^^*/
1002DS_U32 DstCore_GetLoaderVer (void)
1003{
1004#ifdef USE_CYGWIN
1005    return 0;
1006#else
1007        return Os_PerformIoctl (DSTHWIOC_GET_LOADER_VER, 0);
1008#endif
1009}
1010DS_U32 DstCore_GetTimeOfDay (DS_U32 *UtcSeconds)
1011{
1012    struct timeval Time;
1013
1014    if (gettimeofday (&Time, NULL) == 0)
1015    {
1016        *UtcSeconds = Time.tv_sec;
1017        return (OS_OK);
1018    }
1019    return (OS_FAIL);
1020}
1021
1022/*^^***************************************************************************
1023 *^DS_U32 DstCore_Delay (DS_U32 Delay)
1024 *
1025 * Description: Enter a delay state.
1026 *
1027 * Entry :  Delay   = In 1/1000 of a second increments(milliseconds).
1028 *                    This is ONLY accurate if the OS provides a way of
1029 *                    getting system ticks/second.
1030 *
1031 * Return:  OS_OK
1032 *
1033 * Notes :  This call guarantees the calling task to enter an efficient delay
1034 *          state so the OS will be able to reschedule other tasks. The caller
1035 *          will be suspended until the Delay time is elapsed.
1036 **************************************************************************^^*/
1037DS_U32 DstCore_Delay (DS_U32 Delay)
1038{
1039    DS_U32 Sec;
1040
1041    Sec = Delay / 1000;
1042    usleep ((Delay*1000) - (Sec*1000000));
1043    while (Sec)
1044    {
1045        usleep (1000000);
1046        Sec--;
1047    }
1048    return (OS_OK);
1049}
1050
1051/*^^***************************************************************************
1052 *^DS_U32 DstCore_UsDelay (DS_U32 Delay)
1053 *
1054 * Description: Enter a delay state.
1055 *
1056 * Entry :  Delay   = In micorseconds.
1057 *                    This is may not be accurate on some OSes which will
1058 *                    generally round it up to the nearest millisecond
1059 *
1060 * Return:  OS_OK
1061 *
1062 * Notes :  This call guarantees the calling task to enter an efficient delay
1063 *          state so the OS will be able to reschedule other tasks. The caller
1064 *          will be suspended until the Delay time is elapsed.
1065 *
1066 **************************************************************************^^*/
1067DS_U32 DstCore_UsDelay (DS_U32 Delay)
1068{
1069    DS_U32 Sec;
1070
1071    Sec = Delay / 1000000;
1072    usleep (Delay - (Sec*1000000));
1073    while (Sec)
1074    {
1075        usleep (1000000);
1076        Sec--;
1077    }
1078    return (OS_OK);
1079}
1080
1081DS_U32 DstCore_RegRead (DS_U32 Address, int Size)
1082{
1083#ifdef USE_CYGWIN
1084    return 0;
1085#else
1086    OBJ_OPER ObjOp;
1087
1088        if ( Size != 1 && Size != 2 && Size != 4 ) {
1089                printf("|%s| ERROR: Invalid Size=%d\n", __FUNCTION__, Size);
1090        }
1091       
1092    ObjOp.Opcode = OBJ_OPCODE_READ;
1093    ObjOp.Prm1   = Address;
1094    ObjOp.Prm2   = Size;
1095
1096    Os_PerformIoctl (DSTHWIOC_ATOMIC_REG_ACCESS, &ObjOp);
1097    if ((ObjOp.RetCode == OBJ_OK))
1098    {
1099        return (ObjOp.Prm2);
1100    }
1101    INFO_MSG ("\n*** Register Read Error %ld ***\n", ObjOp.RetCode);
1102    return (0xFFFFFFFF);
1103#endif
1104}
1105
1106DS_U32 DstCore_RegWrite (DS_U32 Address, DS_U32 Value, int Size)
1107{
1108#ifdef USE_CYGWIN
1109    return 0;
1110#else
1111    OBJ_OPER ObjOp;
1112
1113    ObjOp.Opcode = OBJ_OPCODE_WRITE;
1114    ObjOp.Prm1   = Address;
1115    ObjOp.Prm2   = Value;
1116    ObjOp.RetCode = Size;
1117
1118    Os_PerformIoctl (DSTHWIOC_ATOMIC_REG_ACCESS, &ObjOp);
1119    if ((ObjOp.RetCode == OBJ_OK))
1120    {
1121        return (0);
1122    }
1123    INFO_MSG ("\n*** Register Write Error %ld ***\n", ObjOp.RetCode);
1124    return (0xFFFFFFFF);
1125#endif
1126}
Note: See TracBrowser for help on using the repository browser.