source: svn/zas_dstar/hal/os/src/dstoslayer.c @ 22

Last change on this file since 22 was 22, checked in by phkim, 11 years ago
  1. phkim
  2. newcon3sk 를 kctv 로 브랜치 함
File size: 38.9 KB
Line 
1/****************************************************************************
2 *_Copyright (c) 2004 DST Technologies Inc.  All Rights Reserved.
3 *
4 * Module:      dstos.c
5 *
6 * Description: DST HAL OS wrapper functions
7 ***************************************************************************/
8
9#include <pthread.h>
10#include <errno.h>
11#include <stdio.h>
12#include <string.h>
13#include <stdlib.h>
14#include <sys/time.h>
15#include <unistd.h>
16#include "dstoslayer.h"
17#include "os.h"
18#include "os_prive.h"
19
20#if USE_V2LIN==1
21#include "vxw_hdrs.h"
22#endif
23
24#ifdef DMALLOC
25#include <dmalloc.h>
26#endif
27
28/*-------------------------------------------------------------------------------
29        Configuration Definitions
30 *------------------------------------------------------------------------------*/
31#define DEBUG_SEM                               0                       // Semaphore Debug Message »ðÀÔ ¿©ºÎ.
32
33int os_debug = 0;
34
35/*-------------------------------------------------------------------------------
36        Local Variables
37 *------------------------------------------------------------------------------*/
38DS_TASK_T *task_list = NULL;
39#if USE_V2LIN==0
40OS_SEMAPHORE_ID task_list_lock = (OS_SEMAPHORE_ID)NULL;
41#else
42pthread_mutex_t task_list_lock = PTHREAD_MUTEX_INITIALIZER;
43#endif
44static int g_TaskCount = 0;
45
46/*-------------------------------------------------------------------------------
47        Local Functions
48 *------------------------------------------------------------------------------*/
49static void lockTaskList(void);
50static void unlockTaskList(void);
51static DS_U32 taskInit(DS_TASK_T *p_new_task, void (*func)(DS_U32), char *name, DS_U16 prio, DS_U16 stacksize, DS_U32 arg);
52static DS_U32 taskDelete(DS_U32 taskId);
53
54#if 0
55___Thread_API_________________()
56#endif
57void OS_Init(void)
58{
59    static DS_BOOL bInit = DS_FALSE;
60    DS_TASK_T *t;
61   
62    if ( bInit == DS_TRUE )
63        return;
64   
65    bInit = DS_TRUE;
66    t = (DS_TASK_T *)malloc(sizeof(DS_TASK_T));
67        if (!t) {
68                printf("|%s| ERROR, Out of resources\n", __FUNCTION__);
69                return;
70        }
71        memset( t, 0, sizeof(DS_TASK_T) );
72       
73        taskInit(t, (void (*)(DS_U32))OS_Init, "Main", 0, 0, 0);
74        t->taskId = OS_GetSelfTaskId();
75        t->pid = getpid();
76}
77
78#ifndef OS_SpawnTask
79static void OS_StartThread(void *arg)
80{
81        DS_TASK_T *t = (DS_TASK_T *)arg;
82        void      (*funcPtr)(DS_U32);
83        DS_U32    funcArg;
84
85        lockTaskList();
86
87    funcPtr = t->funcPtr;
88    funcArg = t->arg;
89        t->taskId = (DS_U32)OS_GetSelfTaskId();
90        t->pid = (int)getpid();
91    t->status = DS_TSTAT_READY;
92   
93        unlockTaskList();
94       
95        printf("%s: PID=%d\n", t->name, t->pid);
96       
97        funcPtr( funcArg );
98       
99        OS_SelfDeleteTask();
100}
101
102DS_U32 OS_SpawnTask (void (*func)(DS_U32), char *name, DS_U16 prio, DS_U16 stacksize, DS_U32 arg)
103{
104        CORE_TASK_ID tId;
105//      void (*pFuncPtr)(void *) = (void (*)(void *))func;
106        DS_TASK_T *t;
107       
108        t = (DS_TASK_T *)malloc(sizeof(DS_TASK_T));
109        if (!t) {
110                printf("|%s| ERROR, Out of resources\n", __FUNCTION__);
111                return (DS_U32)-1;
112        }
113        memset( t, 0, sizeof(DS_TASK_T) );
114       
115        taskInit(t, func, name, prio, stacksize, arg);
116       
117        prio *= 100;
118        prio /= 255;
119       
120        tId = DstCore_TaskCreate( OS_StartThread, (void *)t, prio );
121       
122        return (DS_U32)tId;
123}
124#endif
125
126#ifndef OS_DeleteTask
127DS_U32 OS_DeleteTask(DS_U32 TaskId)
128{
129    if ( TaskId == 0 ) 
130    {       
131        OS_SelfDeleteTask();
132        return 0;
133    }
134       
135    /*
136     * Do not delete TCB of taskId here, since we cannot force any task to be deleted.
137     */
138        DstCore_TaskDelete((CORE_TASK_ID)TaskId);
139        return 0;
140}
141#endif
142
143#ifndef OS_SelfDeleteTask
144void OS_SelfDeleteTask(void)
145{
146    taskDelete(0);
147
148        pthread_exit((void *)0);
149}
150#endif
151
152#ifndef OS_SuspendTask
153DS_U32 OS_SuspendTask(DS_U32 TaskId)
154{
155        printf("%s: This function is not implemented.\n", __FUNCTION__);
156        return (DS_U32)-1;
157}
158#endif
159
160#ifndef OS_ResumeTask
161DS_U32 OS_ResumeTask(DS_U32 TaskId)
162{
163        printf("%s: This function is not implemented.\n", __FUNCTION__);
164        return (DS_U32)-1;
165}
166#endif
167
168#ifndef OS_GetSelfTaskId
169OS_TASK_ID OS_GetSelfTaskId(void)
170{
171        CORE_TASK_ID tId;
172        DstCore_TaskGetInfo(&tId);
173        return (OS_TASK_ID)tId;
174}
175#endif
176
177
178#if 0
179___Thread_Control_APIs___()
180#endif
181static void lockTaskList(void)
182{
183#if USE_V2LIN==0
184    if ( task_list_lock == (OS_SEMAPHORE_ID)NULL )
185    {
186        task_list_lock = OS_CreateMutex( "semMutex");
187        SysASSERT( task_list_lock );
188    }
189   
190    OS_TakeSemaphore( task_list_lock );
191#else
192    pthread_mutex_lock( &task_list_lock );
193#endif
194}
195
196static void unlockTaskList(void)
197{
198#if USE_V2LIN==0
199    SysASSERT( task_list_lock );
200    OS_GiveSemaphore( task_list_lock );
201#else
202    pthread_mutex_unlock( &task_list_lock );
203#endif
204}
205
206#if USE_V2LIN==1
207/*****************************************************************************
208** link_susp_task - appends a new task pointer to a linked list of task pointers
209**                 for tasks suspended on the object owning the list.
210*****************************************************************************/
211void link_susp_task(DS_TASK_T ** list_head, DS_TASK_T * new_entry)
212{
213    DS_TASK_T **i = list_head;
214   
215        if (!new_entry) 
216                return;
217   
218    lockTaskList();
219   
220        new_entry->nxt_susp = NULL;
221
222        while (*i) { 
223                if (*i==new_entry) {
224                        //TRACEF("warning: double entry");
225                        printf("!!! Same entry is queued\n");
226                        *i = (*i)->nxt_susp;    // remove the task
227                        continue;
228                }
229                i = &(*i)->nxt_susp;    // look for the tail
230        }
231        *i = new_entry;
232
233        /*
234         **  Initialize the suspended task's pointer back to suspend list
235         **  This is used for cleanup during task deletion.
236         */
237        //new_entry->suspend_list = *list_head;
238        new_entry->status |= DS_TSTAT_PEND;
239
240        unlockTaskList();
241}
242
243/*****************************************************************************
244** unlink_susp_task - removes task pointer from a linked list of task pointers
245**                   for tasks suspended on the object owning the list.
246*****************************************************************************/
247void unlink_susp_task(DS_TASK_T **list_head, DS_TASK_T * entry)
248{
249        DS_TASK_T **i = list_head;
250       
251        //TRACEF("%x %x", list_head, entry);
252        if (!entry) 
253                return;
254               
255        lockTaskList();
256       
257        while (*i && (*i != entry) )
258                i = &(*i)->nxt_susp;
259        if (*i) {
260                //TRACEF("%x", entry);
261                *i = (*i)->nxt_susp;    // remove the task
262                entry->nxt_susp = NULL;
263                entry->status &= ~DS_TSTAT_PEND;
264        } else {
265                //TRACEF("warning: entry not found");
266                printf("WARNING: cannot find the entry, 0x%08lX 0x%08lX\n", (DS_U32)(*list_head), (DS_U32)entry);
267        }
268       
269        unlockTaskList();
270}
271
272/*****************************************************************************
273** signal_for_my_task - searches the specified 'pended task list' for the
274**                      task to be selected according to the specified
275**                      pend order.  If the selected task is the currently
276**                      executing task, the task is deleted from the
277**                      specified pended task list and returns a non-zero
278**                      result... otherwise the pended task list is not
279**                      modified and a zero result is returned.
280*****************************************************************************/
281int signal_for_my_task(DS_TASK_T **list_head, int pend_order)
282{
283        // used in lmsgQLib.c
284        //TRACEF();
285        DS_TASK_T *signalled_task;
286        DS_TASK_T *t;
287        int result;
288
289        result = DS_FALSE;
290        //TRACEF("list head = %p", *list_head);
291        if (!list_head)
292                return result;
293        signalled_task = *list_head;
294
295        //  First determine which task is being signalled
296        if (pend_order != 0) {
297                /*
298                 **  Tasks pend in priority order... locate the highest priority
299                 **  task in the pended list.
300                 */
301                for (t = *list_head; t; t = t->nxt_susp) {
302                        if (t->priority > signalled_task->priority)
303                                signalled_task = t;
304                        //TRACEF("%x priority %d", (int)t, t->priority);
305                }
306        }
307        /*
308           else
309           **
310           ** Tasks pend in FIFO order... signal is for task at list head.
311         */
312
313        //  Signalled task located... see if it's the currently executing task.
314        if (signalled_task->taskId == OS_GetSelfTaskId()) {
315                // The currently executing task is being signalled...
316                result = DS_TRUE;
317        }
318        //TRACEF("signalled task @ %p my task @ %p", signalled_task->taskId, OS_GetSelfTaskId());
319
320        return result;
321}
322#endif
323
324DS_TASK_T *taskFind(DS_U32 taskId, int bLock)
325{
326        DS_TASK_T *t = (DS_TASK_T *)NULL;
327        DS_BOOL b_found = DS_FALSE;
328       
329        if (!taskId)
330            taskId = OS_GetSelfTaskId();
331
332    if (bLock)
333        lockTaskList();
334       
335        for (t = task_list; t != NULL; t = t->nxt_task) 
336    {
337                if (t->taskId == taskId)
338                {
339                    b_found = DS_TRUE;
340                        break;
341                }
342        }
343       
344    if (bLock)
345        unlockTaskList();
346   
347    if ( b_found )
348        return t;
349       
350        return NULL;
351}
352
353static DS_U32 taskInit(DS_TASK_T *p_new_task, void (*func)(DS_U32), char *name, DS_U16 prio, DS_U16 stacksize, DS_U32 arg)
354{
355    DS_TASK_T **i = &task_list;
356   
357    p_new_task->status = DS_TSTAT_DEAD;
358        p_new_task->funcPtr = func;
359        p_new_task->arg = arg;
360        p_new_task->taskId = -1;
361        p_new_task->priority = prio;
362        if (name)
363                strcpy(p_new_task->name, name);
364   
365    lockTaskList();
366   
367    /*
368     * Add new task to tail of task list.
369     */
370        while (*i)
371                i = &(*i)->nxt_task;            // search_last
372        *i = p_new_task;                                        // add to tail
373   
374    g_TaskCount++;
375   
376        unlockTaskList();
377
378    return OS_OK;
379}
380
381static DS_U32 taskDelete(DS_U32 taskId)
382{
383    DS_TASK_T *task;
384        DS_TASK_T **i;
385   
386    if ( taskId == 0 )
387        taskId = OS_GetSelfTaskId();
388       
389    if ( taskId != OS_GetSelfTaskId() )
390    {
391        printf("%s: This function is not implemented.\n", __FUNCTION__);
392        return (DS_U32)-1;
393    }
394   
395    task = taskFind(taskId, 1);
396    if ( task )
397    {
398        lockTaskList();
399       
400        for (i = &task_list; *i; i = &(*i)->nxt_task) {
401                if (task == *i) {
402                        *i = (*i)->nxt_task;    // remove
403                        break;
404                }
405        }
406       
407        g_TaskCount--;
408   
409        unlockTaskList();
410       
411        return 0;
412    }
413   
414    return (DS_U32)-1;
415}
416
417char *taskName(OS_TASK_ID tid)
418{
419    DS_TASK_T *t;
420   
421        if (tid == 0)
422                tid=OS_GetSelfTaskId();
423   
424    t = taskFind(tid, 0);
425        if ( t == (DS_TASK_T *)NULL )
426            return ((char *)NULL);
427       
428        return (t->name);   
429}
430
431OS_TASK_ID taskNameToId(char *name)
432{
433    DS_TASK_T **i;
434
435        for (i = &task_list; *i; i = &(*i)->nxt_task) {
436                if (i && strcmp((*i)->name, name) == 0) {
437                        return (*i)->taskId;
438                }
439        }
440       
441        return (OS_TASK_ID)-1;
442}
443
444int taskPriorityGet(OS_TASK_ID tid, int *pPriority)
445{
446    DS_TASK_T *t;
447        int       priority = -1;
448
449        if (tid == 0)
450        {
451                tid=OS_GetSelfTaskId();
452        }
453       
454        t = taskFind( tid, 0 );
455        if ( t == (DS_TASK_T *)NULL )
456            return -1;
457       
458        priority = t->priority;
459        if (priority < 0)
460        {
461                return -1;
462        }
463   
464        (*pPriority) = priority;
465       
466        return 0;
467}
468
469static int taskGetStatus(int pid, DS_U32 *p_sp, DS_U32 *p_ip)
470{
471    *p_sp = 0;
472    *p_ip = 0;
473   
474    return 0;
475}
476
477
478void taskShow(OS_TASK_ID tid)
479{
480        DS_TASK_T *t;
481        DS_U32 sp, ip;
482#ifdef _MAKEFILE_INCLUDE_TSHELL_
483        DS_U32 *stackPtr;
484        int i, j;
485#endif
486        char *libname, *funcname;
487       
488        t = taskFind(tid, 0);
489        if (t == NULL)
490                return;
491
492    sp = ip = 0;
493    if ( taskGetStatus( t->pid, &sp, &ip ) )
494        sp = ip = 0;
495           
496    if ( ip )
497    {
498//        libname = FindLibrary( ip );
499    }
500    else
501    {
502        libname = (char *)NULL;
503        funcname = (char *)NULL;
504    }
505#ifdef _MAKEFILE_INCLUDE_TSHELL_
506    funcname = find_function( ip );
507    printf("<<< Task/Thread Information >>>\n");
508    printf("    PID: %d, TID: 0x%04X\n", (int)t->pid, (int)t->tid );
509    printf("    SP : 0x%08lX, IP: 0x%08lX\n", sp, ip );
510    printf("    Symbol: %s\n", funcname ? funcname : "Unknown" );
511    printf("    Library: %s\n", libname ? libname : "Unknown" );
512
513    printf("<<< Stack Contents >>>\n");
514    stackPtr = (DS_U32 *)sp;
515    for ( i=0, j=0; i<128 && j<10; i++ ) 
516    {
517        libname = FindLibrary( stackPtr[i] );
518        funcname = find_function( stackPtr[i] );
519        if ( libname && funcname )
520        {
521                    printf("      SP[%d] = 0x%08lX (%s)\n", i, stackPtr[i], funcname ? funcname : "Unknown or arguments");
522                    j++;
523                }
524        }
525#else
526    printf("    PID: %d, TID: 0x%04X\n", t->pid, (int)t->taskId );
527    printf("    SP : 0x%08lX, IP: 0x%08lX\n", sp, ip );
528    printf("    Library: %s\n", libname ? libname : "Unknown" );
529#endif
530}
531
532void taskShowAll()
533{
534        DS_TASK_T *t;
535       
536        DS_U32 sp, ip;
537        char *libname, *funcname;
538
539    lockTaskList();
540   
541        printf("\t TASK NUM=%d\n", (int) g_TaskCount);
542        printf("\t-------------------------------------------------------------\n");
543#ifdef _MAKEFILE_INCLUDE_TSHELL_
544        printf("\t%10s\tPID (TID)\tPR(ST)\tSTACK\t\t   PC\tSymbol\tLibrary\r\n", "NAME");
545#else
546        printf("\t%10s\tPID (TID)\tPR(ST)\tSTACK\t\t   PC\tLibrary\r\n", "NAME");
547#endif
548        printf("\t-------------------------------------------------------------\n");
549       
550        for (t = task_list; t; t = t->nxt_task)
551        {
552            sp = ip = 0;
553            if ( taskGetStatus( t->pid, &sp, &ip ) )
554                sp = ip = 0;
555           
556            if ( ip )
557            {
558//              libname = FindLibrary( ip );
559            }
560            else
561            {
562            libname = (char *)NULL;
563            funcname = (char *)NULL;
564        }
565#ifdef _MAKEFILE_INCLUDE_TSHELL_
566        funcname = find_function( ip );
567                printf("\t%10s\t%4d (0x%4x)\t%d(%d)\t0x%08lX\t0x%08lX\t%s\t%s", t->name,
568                       (int) t->pid,
569                       (int) t->taskId,
570                       (int) t->priority, (int) t->status, sp, ip, funcname ? funcname : "Unknown", libname ? libname : "Unknown\n" );
571#else
572                printf("\t%10s\t%4d (0x%4x)\t%d(%d)\t0x%08lX\t0x%08lX\t%s\n", t->name,
573                       (int) t->pid,
574                       (int) t->taskId,
575                       (int) t->priority, (int) t->status, sp, ip, libname ? libname : "Unknown\n" );
576#endif
577        }
578        printf("\t-------------------------------------------------------------\n");
579       
580        unlockTaskList();
581}
582
583#if 0
584___Time_API__________________()
585#endif
586#ifndef OS_GetTicksPerSecond
587DS_U32 OS_GetTicksPerSecond (void)
588{
589       
590}
591#endif
592
593#ifndef OS_Delay
594void OS_Delay(DS_U32 Ticks)
595{
596        usleep((Ticks) * (1000000/OS_GetTicksPerSecond()));
597}
598#endif
599
600#ifndef OS_mDelay
601void OS_mDelay(DS_U32 milliseconds)
602{
603        if (milliseconds)
604                usleep((milliseconds) * 1000);
605}
606#endif
607
608#ifndef OS_GetTickCount
609DS_U32 OS_GetTickCount(void)
610{
611        return DstCore_Get100HzClockTick();
612}
613#endif
614
615#if 0
616___Semaphore_API____________()
617#endif
618
619#ifndef OS_CreateCountingSemaphore
620OS_SEMAPHORE_ID OS_CreateCountingSemaphore (const char *name, DS_U32 options, DS_U32 count)
621{
622        CORE_SEM_ID semId;
623
624    OS_Init();
625   
626#if USE_V2LIN==0
627        semId = DstCore_SemCreate( 0, count );
628#else
629    semId = (CORE_SEM_ID)semCCreate( 0, (int)count );
630#endif
631
632        if ( DEBUG_SEM )
633                fprintf(stderr, "|%s| SemId=0x%lX\n", __FUNCTION__, semId);
634       
635        return (OS_SEMAPHORE_ID)semId;
636}
637#endif
638
639#ifndef OS_CreateBinarySemaphore
640OS_SEMAPHORE_ID OS_CreateBinarySemaphore(const char *name, DS_U32 options, DS_U32 count)
641{
642        CORE_SEM_ID semId;
643
644    OS_Init();
645   
646#if USE_V2LIN==0
647        semId = DstCore_SemCreate( 0, 1 );
648        if ( semId && count == 0 ) {
649                DstCore_SemLock( semId, OS_WAIT_FOREVER );
650        }
651#else
652    semId = (CORE_SEM_ID)semBCreate( 0, (int)count );
653#endif
654        if ( DEBUG_SEM )
655                fprintf(stderr, "|%s| SemId=0x%lX\n", __FUNCTION__, semId);
656
657    if ( semId == 0 )
658    {
659        printf("\n*** Semaphore create error ***\n" );
660    }
661   
662        return (OS_SEMAPHORE_ID)semId;
663}
664#endif
665
666#ifndef OS_DeleteSemaphore
667DS_U32 OS_DeleteSemaphore (OS_SEMAPHORE_ID SemId)
668{
669    DS_U32 RetVal = 0;
670
671        if ( DEBUG_SEM )
672                fprintf(stderr, "|%s| SemId=0x%lX\n", __FUNCTION__, SemId);
673
674#if USE_V2LIN==0
675        DstCore_SemDelete( (CORE_SEM_ID)SemId );
676
677        return 0;
678#else
679   
680    RetVal = semDelete( (SEM_ID)SemId );
681    if ( RetVal == 0 )
682        return (OS_OK);
683
684    printf("\n*** Semaphore delete error %ld ***\n", RetVal);
685    return (OS_FAIL);
686#endif
687}
688#endif
689
690#ifndef OS_TakeSemaphore
691DS_U32 OS_TakeSemaphore (OS_SEMAPHORE_ID SemId)
692{
693    DS_U32 RetVal = 0;
694#if USE_V2LIN==0
695        if ( DEBUG_SEM )
696                fprintf(stderr, "|%s| SemId=0x%lX\n", __FUNCTION__, SemId);
697
698        return DstCore_SemLock( (CORE_SEM_ID)SemId, OS_WAIT_FOREVER );
699#else
700    RetVal = semTake( (SEM_ID)SemId, OS_WAIT_FOREVER );
701    if ( RetVal == 0 )
702    {
703        return OS_OK;
704    }
705    else if ( RetVal == S_objLib_OBJ_TIMEOUT )
706    {
707        RetVal = OS_TIMEOUT;
708    }
709    else 
710    {
711        printf("\n*** Semaphore lock error %ld ***\n", RetVal );
712        RetVal = OS_FAIL;
713    }
714   
715    return (RetVal);
716#endif
717}
718#endif
719
720#ifndef OS_TakeSemaphore_Wait
721DS_U32 OS_TakeSemaphore_Wait(OS_SEMAPHORE_ID SemId, DS_U32 timeout)
722{
723    DS_U32 RetVal = 0;
724   
725        if ( DEBUG_SEM )
726                fprintf(stderr, "|%s| SemId=0x%lX, timeout=0x%lX\n", __FUNCTION__, SemId, timeout);
727#if USE_V2LIN==0
728        return DstCore_SemLock( (CORE_SEM_ID)SemId, timeout );
729#else
730    RetVal = semTake( (SEM_ID)SemId, (int)timeout );
731    if ( RetVal == 0 )
732    {
733        return OS_OK;
734    }
735    else if ( RetVal == S_objLib_OBJ_TIMEOUT )
736    {
737        RetVal = OS_TIMEOUT;
738    }
739    else 
740    {
741        printf("\n*** Semaphore lock error %ld ***\n", RetVal );
742        RetVal = OS_FAIL;
743    }
744   
745    return RetVal;
746#endif
747}
748#endif
749
750#ifndef OS_TakeSemaphore_NoWait
751DS_U32 OS_TakeSemaphore_NoWait(OS_SEMAPHORE_ID SemId)
752{
753    DS_U32 RetVal = 0;
754   
755        if ( DEBUG_SEM )
756                fprintf(stderr, "|%s| SemId=0x%lX\n", __FUNCTION__, SemId);
757#if USE_V2LIN==0
758        return DstCore_SemLock( (CORE_SEM_ID)SemId, 0 );
759#else
760    RetVal = semTake( (SEM_ID)SemId, (int)0 );
761    if ( RetVal == 0 )
762    {
763        return OS_OK;
764    }
765    else if ( RetVal == S_objLib_OBJ_TIMEOUT )
766    {
767        RetVal = OS_TIMEOUT;
768    }
769    else 
770    {
771        printf("\n*** Semaphore lock error %ld ***\n", RetVal );
772        RetVal = OS_FAIL;
773    }
774   
775    return RetVal;
776#endif
777}
778#endif
779
780#ifndef OS_GiveSemaphore
781DS_U32 OS_GiveSemaphore(OS_SEMAPHORE_ID SemId)
782{
783    DS_U32 RetVal = 0;
784
785        if ( DEBUG_SEM )
786                fprintf(stderr, "|%s| SemId=0x%lX\n", __FUNCTION__, SemId);
787
788#if USE_V2LIN==0
789        return DstCore_SemUnlock( (CORE_SEM_ID)SemId );
790#else
791   
792    RetVal = semGive( (SEM_ID)SemId );
793    if ( RetVal == 0 )
794        return (OS_OK);
795
796    printf("\n*** Semaphore unlock error %ld ***\n", RetVal);
797   
798    return RetVal;
799#endif
800}
801#endif
802
803#ifndef OS_FlushSemaphore
804DS_U32 OS_FlushSemaphore(OS_SEMAPHORE_ID SemId)
805{
806       
807        return 0;
808}
809#endif
810
811#if 0
812___Mutex_API__________________()
813#endif
814
815#ifndef OS_CreateMutex
816OS_MUTEX_ID OS_CreateMutex(const char *name)
817{
818    OS_Init();
819   
820#if USE_V2LIN==0
821        return (OS_MUTEX_ID)DstCore_MutexCreate(0);
822#else
823    return (OS_MUTEX_ID)semMCreate(0);
824#endif
825}
826#endif
827
828#ifndef OS_DeleteMutex
829DS_U32 OS_DeleteMutex(OS_MUTEX_ID mutexId)
830{
831#if USE_V2LIN==0
832        DstCore_MutexDelete((CORE_MUTEX_ID)mutexId);
833       
834        return (0);
835#else
836    DS_U32 retVal = 0;
837   
838    retVal = semDelete( (SEM_ID)mutexId );
839    if ( retVal == 0 )
840        return (OS_OK);
841
842    printf("\n*** Semaphore delete error %ld ***\n", retVal);
843    return (OS_FAIL);
844#endif
845}
846#endif
847
848#ifndef OS_TakeMutex
849DS_U32 OS_TakeMutex(OS_MUTEX_ID mutexId)
850{
851#if USE_V2LIN==0
852        return DstCore_MutexLock((CORE_MUTEX_ID)mutexId, OS_WAIT_FOREVER);
853#else
854    DS_U32 retVal = 0;
855   
856    retVal = semTake( (SEM_ID)mutexId, OS_WAIT_FOREVER );
857    if ( retVal == 0 )
858        return (OS_OK);
859
860    printf("\n*** Mutex Take error %ld ***\n", retVal);
861               
862    return retVal;
863#endif
864}
865#endif
866
867#ifndef OS_TakeMutex_NoWait
868DS_U32 OS_TakeMutex_NoWait(DS_U32 mutexId)
869{
870#if USE_V2LIN==0
871        return DstCore_MutexLock((CORE_MUTEX_ID)mutexId, 0);
872#else
873    DS_U32 retVal = 0;
874   
875    retVal = semTake( (SEM_ID)mutexId, OS_WAIT_FOREVER );
876    if ( retVal == 0 )
877    {
878        return (OS_OK);
879    }
880    else if ( retVal == S_objLib_OBJ_TIMEOUT )
881    {
882        retVal = OS_TIMEOUT;
883    }
884   
885    printf("\n*** Mutex Take error %ld ***\n", retVal);
886       
887    return retVal;
888#endif
889}
890#endif
891
892#ifndef OS_GiveMutex
893DS_U32 OS_GiveMutex(DS_U32 mutexId)
894{
895#if USE_V2LIN==0
896        return DstCore_MutexUnlock((CORE_MUTEX_ID)mutexId);
897#else
898    DS_U32 RetVal = 0;
899   
900    RetVal = semGive( (SEM_ID)mutexId );
901    if ( RetVal == 0 )
902        return (OS_OK);
903
904    printf("\n*** Mutex unlock error %ld ***\n", RetVal);
905   
906    return RetVal;
907#endif
908}
909#endif
910
911
912#if 0
913___Dynamic_Memory_API______________()
914#endif
915
916#define MAX_FUNC_NAME 32 // Çã¿ëÇÒ ÃÖ´ë ÇÔ¼ö À̸§ ±æÀÌ
917#define MAX_COUNT 2000 // °ü¸®ÇÒ °¹¼ö
918
919static struct MEM_LIST
920{
921        char func[MAX_FUNC_NAME+1];
922        int nLine;
923        void *p;
924        int nSize;
925        unsigned int tick;
926} memlist[MAX_COUNT];
927
928void Print_All_MemUnit(void)
929{
930        int i = 0, nSum = 0, nCount = 0;
931        printf("|------------+----------+----------------------------------+------|\n");
932        printf("|   Address  |    Size  |             Function             | Line |\n");
933        printf("|------------+----------+----------------------------------+------|\n");
934        for (i = 0; i < MAX_COUNT; i++)
935        {
936                if (memlist[i].p == 0) continue;
937                printf("| 0x%08X | %8d | %32s | %4d | %4d |\n", (int)memlist[i].p, memlist[i].nSize, memlist[i].func, memlist[i].nLine, (int)(OS_GetTickCount() - memlist[i].tick)/100 );
938                nSum+=memlist[i].nSize;
939                nCount++;
940        }
941        printf("|------------+----------+----------------------------------+------|\n");
942        printf("| %10d | %8d |                                         |\n", nCount, nSum);
943        printf("|------------+----------+----------------------------------+------|\n");
944} 
945
946static void Add_MemUint(const char* func, int nLine, void *p, int nSize)
947{
948        int i = 0;
949        for (i = 0; i < MAX_COUNT; i++)
950        {
951                if (memlist[i].p != 0) continue;
952                if (strlen(func) > MAX_FUNC_NAME) 
953                {
954                        memcpy(memlist[i].func, func, MAX_FUNC_NAME);
955                        memlist[i].func[MAX_FUNC_NAME] = 0;
956                }
957                else
958                {
959                        strcpy(memlist[i].func, func);
960                }
961                memlist[i].nLine = nLine;
962                memlist[i].p = p;
963                memlist[i].nSize = nSize;
964                memlist[i].tick = OS_GetTickCount();
965                break;
966        }
967        //Print_MemUnit();
968}
969
970
971static void Del_MemUnit(void *p, const char * func, int nLine)
972{
973        int i = 0;
974        if (p == 0) return;
975        for (i = 0; i < MAX_COUNT; i++)
976        {
977                if (memlist[i].p != p) continue;
978                memlist[i].p = 0;
979                break;
980        }
981        if (i >= MAX_COUNT)
982        {
983#if 0
984                printf("\n\n\n\nTry to delete unallocated memory 0x%08X. %s %d\n\n\n", (int)p, func, nLine);
985                Print_All_MemUnit();
986                OS_Delay(5);
987#endif
988        }
989        //Print_MemUnit();
990}
991
992void *_OS_malloc(unsigned int size, const char* func, int nLine)
993{
994        void *p = 0;
995        if (size == 0) return 0;
996        p = malloc(size);
997        Add_MemUint(func, nLine, p, size);
998        return p;       
999}
1000
1001void *_OS_calloc(unsigned int count, unsigned int size, const char* func, int nLine)
1002{
1003        void *p = 0;
1004        if (count == 0 || size == 0) return 0;
1005        p = calloc(count, size);
1006        Add_MemUint(func, nLine, p, size);
1007        return p;       
1008}
1009
1010void *_OS_realloc(void* memory, unsigned int size, const char* func, int nLine)
1011{
1012        void *p = 0;
1013        Del_MemUnit(memory, func, nLine);
1014        p = realloc(memory, size);     
1015        Add_MemUint(func, nLine, p, size);
1016        return p;       
1017}
1018
1019void _OS_free(void *where, const char* func, int nLine)
1020{
1021        if (where == 0) return;
1022        Del_MemUnit(where, func, nLine);
1023        free(where);
1024        where = 0;
1025}
1026
1027void *OS_malloc2(unsigned int size)
1028{
1029        void *p = 0;
1030        if (size == 0) return 0;
1031        p = malloc(size);
1032//      Add_MemUint(func, nLine, p, size);
1033        return p;       
1034}
1035
1036void *OS_calloc2(unsigned int count, unsigned int size)
1037{
1038        void *p = 0;
1039        if (count == 0 || size == 0) return 0;
1040        p = calloc(count, size);
1041//      Add_MemUint(func, nLine, p, size);
1042        return p;       
1043}
1044
1045void *OS_realloc2(void* memory, unsigned int size)
1046{
1047        void *p = 0;
1048//      Del_MemUnit(memory, func, nLine);
1049        p = realloc(memory, size);     
1050//      Add_MemUint(func, nLine, p, size);
1051        return p;       
1052}
1053
1054void OS_free2(void *where)
1055{
1056        if (where == 0) return;
1057//      Del_MemUnit(memBlock, func, nLine);
1058        free(where);
1059        where = 0;
1060}
1061
1062#if 0
1063___Message_Queue_API____________()
1064#endif
1065#ifndef OS_CreateMessageQueue
1066DS_U32 OS_CreateMessageQueue (const char *name, DS_U32 option, DS_U32 maxMessage, DS_U32 messageLength)
1067{
1068        CORE_QUEUE_ID qId;
1069       
1070        OS_Init();
1071   
1072#if USE_V2LIN==0
1073        qId = DstCore_QueueCreate( (DS_U32)name, maxMessage, messageLength );
1074#else
1075    qId = (CORE_QUEUE_ID)msgQCreate( maxMessage, messageLength, option );
1076#endif
1077
1078        return (DS_U32)qId;
1079}
1080#endif
1081
1082#ifndef OS_SendMessage
1083DS_U32 OS_SendMessage (DS_U32 qId, DS_U32 *pBuffer, DS_U32 nBytes)
1084{
1085#if USE_V2LIN==0
1086        return DstCore_QueuePost( (CORE_QUEUE_ID)qId, pBuffer, nBytes );
1087#else
1088    DS_U32 err;
1089   
1090    err = msgQSend( (MSG_Q_ID)qId, (char *)pBuffer, nBytes, OS_WAIT_FOREVER, 0 );
1091   
1092    if ( err )
1093    {
1094        printf("\n*** Queue Send error %ld ***\n", err);
1095        return OS_FAIL;
1096    }
1097    else
1098        return OS_OK;
1099#endif
1100}
1101#endif
1102
1103#ifndef OS_ReceiveMessage
1104DS_U32 OS_ReceiveMessage(DS_U32 qId, DS_U32 *msgBuf, DS_U32 maxLen, DS_U32 *retLen)
1105{
1106#if USE_V2LIN==0
1107        return DstCore_QueueWait( (CORE_QUEUE_ID)qId, msgBuf, OS_WAIT_FOREVER, retLen );
1108#else
1109    int msgLen;
1110   
1111    msgLen = msgQReceive( (MSG_Q_ID)qId, (char *)msgBuf, maxLen, OS_WAIT_FOREVER );
1112    if ( msgLen > 0 )
1113    {
1114        *retLen = (DS_U32)msgLen;
1115        return OS_OK;
1116    }
1117   
1118    return OS_FAIL;
1119#endif
1120}
1121#endif
1122
1123#ifndef OS_ReceiveMessage_Wait
1124DS_U32 OS_ReceiveMessage_Wait(DS_U32 qId, DS_U32 *msgBuf, DS_U32 maxLen, DS_U32 *retLen, DS_U32 timeOut)
1125{
1126#if USE_V2LIN==0
1127        return DstCore_QueueWait( (CORE_QUEUE_ID)qId, msgBuf, timeOut, retLen );
1128#else
1129    int msgLen;
1130   
1131    msgLen = msgQReceive( (MSG_Q_ID)qId, (char *)msgBuf, maxLen, timeOut );
1132    if ( msgLen > 0 )
1133    {
1134        *retLen = (DS_U32)msgLen;
1135        return OS_OK;
1136    }
1137   
1138    if (errno == S_objLib_OBJ_TIMEOUT)
1139        return OS_TIMEOUT;
1140
1141    printf("\n*** Queue Recv error 0x%X ***\n", errno);
1142   
1143    return OS_FAIL;
1144#endif
1145}
1146#endif
1147
1148#ifndef OS_ReceiveMessage_NoWait
1149DS_U32 OS_ReceiveMessage_NoWait(DS_U32 qId, DS_U32 *msgBuf, DS_U32 maxLen, DS_U32 *retLen)
1150{
1151#if USE_V2LIN==0
1152        return DstCore_QueueWait( (CORE_QUEUE_ID)qId, msgBuf, 0, retLen );
1153#else
1154    int msgLen;
1155   
1156    msgLen = msgQReceive( (MSG_Q_ID)qId, (char *)msgBuf, maxLen, 0 );
1157    if ( msgLen > 0 )
1158    {
1159        *retLen = (DS_U32)msgLen;
1160        return OS_OK;
1161    }
1162
1163    if (errno == S_objLib_OBJ_TIMEOUT || errno == S_objLib_OBJ_UNAVAILABLE)
1164        return OS_TIMEOUT;
1165
1166    printf("\n*** Queue Recv error 0x%X ***\n", errno);
1167   
1168    return OS_FAIL;
1169#endif
1170}
1171#endif
1172
1173#ifndef OS_DeleteMessageQueue
1174DS_U32 OS_DeleteMessageQueue(DS_U32 qId)
1175{
1176#if USE_V2LIN==0
1177        return DstCore_QueueDelete( (CORE_QUEUE_ID)qId );
1178#else
1179    DS_U32 err;
1180   
1181    err = msgQDelete( (MSG_Q_ID)qId );
1182    if ( err )
1183        return OS_FAIL;
1184   
1185    return OS_OK;
1186#endif
1187}
1188#endif
1189
1190#if 0
1191___Semaphore_Test_Routines______________()
1192#endif
1193//
1194//      Option
1195//              0: Test infinite timeout routines.
1196//              1: Test finite timeout routines.
1197//              2: Test no timeout routines.
1198//
1199static OS_SEMAPHORE_ID semId1, semId2;
1200void tSemProd( DS_U32 arg )
1201{
1202        int *pResult = (int *)arg;
1203        int i;
1204        DS_U32 ret=0;
1205       
1206        printf("|%s| entry.\n", __FUNCTION__);
1207
1208        while(*pResult)
1209                OS_mDelay(100);
1210       
1211        i = 0;
1212        while ( 1 ) {
1213            OS_mDelay(1000);
1214           
1215                printf("[%s] give semaphore to tSemCons. [%d]\n", __FUNCTION__, i);
1216                ret = OS_GiveSemaphore( semId1 );
1217                if ( ret ) {
1218                        printf("|%s| ERROR, LINE=%d\n", __FUNCTION__, __LINE__);
1219                        break;
1220                }
1221#if 0
1222                if ( i==5 ) {
1223                        printf("[%s] Wait for semaphore from tSemCons. timeout = 100[%d]\n", __FUNCTION__, i);
1224                        ret = OS_TakeSemaphore_Wait( semId2, 100 );
1225                        if ( ret ) {
1226                                printf("|%s:%d| ret = %s[%ld] ==> %s\n", __FUNCTION__, __LINE__,
1227                                                        ret == OS_TIMEOUT ? "TIMEOUT" :
1228                                                        ret == OS_FAIL ? "FAIL" :
1229                                                        ret == OS_NOT_SUPPORTED ? "NOT SUPPORTED" : "Unknown", ret, ret == OS_TIMEOUT ? "TIMEOUT OK" : "TIMEOUT FAIL" );
1230                                break;
1231                        }
1232                } else {
1233                        printf("[%s] Wait for semaphore from tSemCons. [%d]\n", __FUNCTION__, i);
1234                        ret = OS_TakeSemaphore_Wait( semId2, OS_WAIT_FOREVER );
1235                        if ( ret ) {
1236                                printf("|%s| ERROR, LINE=%d\n", __FUNCTION__, __LINE__);
1237                                break;
1238                        }
1239                }
1240                if ( ++i > 5 )
1241#endif
1242                        break;
1243        }
1244       
1245        if ( i == 5 )
1246                *pResult = 1;
1247        else
1248                *pResult = -1;
1249
1250fprintf(stderr, "|%s:%d|\n", __FUNCTION__, __LINE__);
1251        OS_DeleteTask(0);
1252}
1253
1254void tSemCons( DS_U32 arg )
1255{
1256        int *pResult = (int *)arg;
1257        int i;
1258        DS_U32 ret;
1259       
1260        printf("|%s| entry.\n", __FUNCTION__);
1261       
1262        while(*pResult)
1263                OS_mDelay(100);
1264       
1265        i = 0;
1266        while ( 1 ) {
1267                printf("[%s] Wait for semaphore from tSemProd. [%d]\n", __FUNCTION__, i);
1268                ret = OS_TakeSemaphore_Wait( semId1, 200 );
1269                //ret = OS_TakeSemaphore_Wait( semId1, OS_WAIT_FOREVER);
1270                if ( ret ) {
1271                        printf("|%s| ERROR, LINE=%d\n", __FUNCTION__, __LINE__);
1272                        break;
1273                }
1274#if 0
1275                if ( i == 5 ) {
1276                        printf("[%s] Delay 1000msec.\n", __FUNCTION__);
1277                        OS_mDelay(1000);
1278                }
1279
1280                printf("[%s] give semaphore to tSemProd. [%d]\n", __FUNCTION__, i);
1281                ret = OS_GiveSemaphore( semId2 );
1282                if ( ret ) {
1283                        printf("|%s| ERROR, LINE=%d\n", __FUNCTION__, __LINE__);
1284                        break;
1285                }
1286                               
1287                if ( ++i > 5 )
1288#endif
1289                        break;
1290        }
1291       
1292        if ( i == 6 )
1293                *pResult = 1;
1294        else
1295                *pResult = -1;
1296
1297
1298fprintf(stderr, "|%s:%d|\n", __FUNCTION__, __LINE__);           
1299        OS_DeleteTask(0);
1300}
1301
1302void test_semaphore(DS_U32 Option)
1303{
1304        int ret = 0;
1305        static int test_result1 = -1;
1306        static int test_result2 = -1;
1307        DS_U32 Result;
1308
1309        //
1310        // Create 2 semaphores.
1311        //
1312        if (Option)
1313            semId1 = OS_CreateBinarySemaphore( 0, 0, 0 );
1314    else           
1315            semId1 = OS_CreateCountingSemaphore( 0, 0, 0 );
1316        if ( semId1 == 0 ) {
1317                printf("|%s:%d| ERROR: cannot create semaphore.\n", __FUNCTION__, __LINE__);
1318                ret = -1;
1319                goto done;
1320        }
1321       
1322        if (Option)
1323            semId2 = OS_CreateBinarySemaphore( 0, 0, 0 );
1324    else           
1325            semId2 = OS_CreateCountingSemaphore( 0, 0, 0 );
1326        if ( semId2 == 0 ) {
1327                printf("|%s:%d| ERROR: cannot create semaphore.\n", __FUNCTION__, __LINE__);
1328                ret = -1;
1329            goto done;
1330        }
1331   
1332        //
1333        // Create the product task.
1334        //
1335        Result = OS_SpawnTask( tSemProd, "tSemProd", 60, 0, (DS_U32)&test_result1 );
1336        if ( Result == 0 ) {
1337                printf("|%s:%d| ERROR: cannot create thread.\n", __FUNCTION__, __LINE__);
1338                ret = -1;
1339                goto done;
1340        }
1341       
1342        //
1343        // Create the consumer task.
1344        //
1345        Result = OS_SpawnTask( tSemCons, "tSemCons", 60, 0, (DS_U32)&test_result2 );
1346        if ( Result == 0 ) {
1347                printf("|%s:%d| ERROR: cannot create thread.\n", __FUNCTION__, __LINE__);
1348                ret = -1;
1349                goto done;
1350        }
1351
1352
1353        //
1354        // Starts the task.
1355        //
1356        test_result1 = 0;
1357        test_result2 = 0;
1358        while ( test_result1 == 0 || test_result2 == 0 ) {
1359                OS_mDelay(1000);
1360                fprintf(stderr, ".\n");
1361        }
1362       
1363        if ( test_result1 > 0 && test_result2 > 0 )
1364                ret = 1;
1365        else
1366                ret = -1;
1367
1368done:
1369        if ( ret < 0 )
1370                printf("*** Semaphore Test: FAIL ***\n");
1371        else
1372                printf("*** Semaphore Test: PASS ***\n");
1373
1374        OS_DeleteSemaphore( semId1 );
1375        OS_DeleteSemaphore( semId2 );
1376}
1377
1378
1379#if 0
1380___Mutex_Test_Routines______________()
1381#endif
1382//
1383//      Option
1384//              0: Test infinite timeout routines.
1385//              1: Test finite timeout routines.
1386//              2: Test no timeout routines.
1387//
1388static OS_MUTEX_ID mtxId1, mtxId2;
1389void tMutexProd( DS_U32 arg )
1390{
1391        int *pResult = (int *)arg;
1392        int i;
1393        DS_U32 ret;
1394       
1395        printf("|%s| entry.\n", __FUNCTION__);
1396
1397        ret = OS_TakeMutex( mtxId1 );
1398        if ( ret ) {
1399                printf("|%s| ERROR, LINE=%d\n", __FUNCTION__, __LINE__);
1400        }
1401
1402        while(*pResult)
1403                OS_mDelay(100);
1404       
1405        i = 0;
1406        while ( 1 ) {
1407                printf("[%s] give semaphore to tMutexCons. [%d]\n", __FUNCTION__, i);
1408                ret = OS_GiveMutex(     mtxId1 );
1409                if ( ret ) {
1410                        printf("|%s| ERROR, LINE=%d\n", __FUNCTION__, __LINE__);
1411                        break;
1412                }
1413               
1414                OS_Delay(1);
1415               
1416                printf("[%s] Wait for semaphore from tMutexCons. [%d]\n", __FUNCTION__, i);
1417                ret = OS_TakeMutex( mtxId1 );
1418                if ( ret ) {
1419                        printf("|%s| ERROR, LINE=%d\n", __FUNCTION__, __LINE__);
1420                        break;
1421                }
1422               
1423                if ( ++i > 5 )
1424                        break;
1425        }
1426
1427        ret = OS_GiveMutex(     mtxId1 );
1428        if ( ret ) {
1429                printf("|%s| ERROR, LINE=%d\n", __FUNCTION__, __LINE__);
1430        }
1431               
1432        printf("%s | i = %d , pResult = 0x%x\n",__FUNCTION__,i,(int)pResult);
1433        if ( i == 6 )
1434                *pResult = 1;
1435        else
1436                *pResult = -1;
1437               
1438        OS_mDelay(1000);
1439}
1440
1441void tMutexCons( DS_U32 arg )
1442{
1443        int *pResult = (int *)arg;
1444        int i;
1445        DS_U32 ret;
1446
1447        printf("|%s| entry.\n", __FUNCTION__);
1448
1449        ret = OS_TakeMutex(     mtxId1 );
1450        if ( ret ) {
1451                printf("|%s| ERROR, LINE=%d\n", __FUNCTION__, __LINE__);
1452        }       
1453
1454        while(*pResult)
1455                OS_mDelay(100);
1456       
1457        i = 0;
1458        while ( 1 ) {
1459                printf("[%s] Wait for semaphore from tMutexProd. [%d]\n", __FUNCTION__, i);
1460                ret = OS_TakeMutex(     mtxId1 );
1461                if ( ret ) {
1462                        printf("|%s| ERROR, LINE=%d\n", __FUNCTION__, __LINE__);
1463                        break;
1464                }
1465
1466                printf("[%s] give semaphore to tMutexProd. [%d]\n", __FUNCTION__, i);
1467                ret = OS_GiveMutex(     mtxId1 );
1468                if ( ret ) {
1469                        printf("|%s| ERROR, LINE=%d\n", __FUNCTION__, __LINE__);
1470                        break;
1471                }
1472                               
1473                if ( ++i > 5 )
1474                        break;
1475        }
1476       
1477
1478        printf("[%s] give semaphore to tMutexProd. [%d]\n", __FUNCTION__, i);
1479        ret = OS_GiveMutex(     mtxId1 );
1480        if ( ret ) {
1481                printf("|%s| ERROR, LINE=%d\n", __FUNCTION__, __LINE__);
1482        }
1483                               
1484        printf("%s | i = %d, pResult = 0x%x\n",__FUNCTION__,i,(int)pResult);
1485       
1486        if ( i == 6 )
1487                *pResult = 1;
1488        else
1489                *pResult = -1;
1490
1491        OS_mDelay(1000);
1492}
1493
1494void test_mutex(DS_U32 Option)
1495{
1496        int ret = 0;
1497        static int test_result1 = -1;
1498        static int test_result2 = -1;
1499        DS_U32 Result;
1500       
1501        //
1502        // Create 2 semaphores.
1503        //
1504        mtxId1 = OS_CreateMutex( 0 );
1505        if ( mtxId1 == 0 ) {
1506                printf("|%s:%d| ERROR: cannot create semaphore.\n", __FUNCTION__, __LINE__);
1507                ret = -1;
1508                goto done;
1509        }
1510       
1511        mtxId2 = OS_CreateMutex( 0 );
1512        if ( mtxId2 == 0 ) {
1513                printf("|%s:%d| ERROR: cannot create semaphore.\n", __FUNCTION__, __LINE__);
1514                ret = -1;
1515                goto done;
1516        }
1517       
1518        //
1519        // Create the product task.
1520        //
1521
1522        printf("t1 = 0x%x, t2 = 0x%x\n",(int)&test_result1,(int)&test_result2);
1523        Result = OS_SpawnTask( tMutexProd, 0, 60, 8*1024, (DS_U32)&test_result1 );
1524        if ( Result == 0 ) {
1525                printf("|%s:%d| ERROR: cannot create thread.\n", __FUNCTION__, __LINE__);
1526                ret = -1;
1527                goto done;
1528        }
1529
1530        //
1531        // Create the consumer task.
1532        //
1533        Result = OS_SpawnTask( tMutexCons, 0, 60, 8*1024, (DS_U32)&test_result2 );
1534        if ( Result == 0 ) {
1535                printf("|%s:%d| ERROR: cannot create thread.\n", __FUNCTION__, __LINE__);
1536                ret = -1;
1537                goto done;
1538        }
1539
1540        //
1541        // Starts the task.
1542        //
1543        test_result1 = 0;
1544        test_result2 = 0;
1545       
1546       
1547        while ( test_result1 == 0 || test_result2 == 0 ) {
1548                OS_mDelay(1000);
1549                printf(".");
1550        }
1551       
1552        if ( test_result1 > 0 && test_result2 > 0 )
1553                ret = 1;
1554        else
1555                ret = -1;
1556       
1557done:
1558        if ( ret < 0 )
1559                printf("*** Mutex Test: FAIL ***\n");
1560        else
1561                printf("*** Mutex Test: PASS ***\n");
1562       
1563        OS_DeleteMutex( mtxId1 );
1564        OS_DeleteMutex( mtxId2 );
1565}
1566
1567
1568#if 0
1569___Message_Test_Routines______________()
1570#endif
1571//
1572//      Option
1573//              0: Test infinite timeout routines.
1574//              1: Test finite timeout routines.
1575//              2: Test no timeout routines.
1576//
1577typedef struct tag_MyMessage {
1578        DS_U32 dummy;
1579        DS_U32 senderID;
1580        DS_U32 Length;
1581        DS_U32 Command;
1582        DS_U32 CommandCPL;
1583        DS_U32 du;
1584               
1585} MyMessage;
1586
1587#define N_TEST_MSG      100
1588
1589static DS_U32 msgId1, msgId2;
1590void tMsgProd( DS_U32 arg )
1591{
1592        int *pResult = (int *)arg;
1593        int i;
1594        DS_U32 ret;
1595        CORE_TASK_ID tId;
1596        MyMessage smsg, rmsg;
1597        DS_U32 len;
1598       
1599        DstCore_TaskGetInfo( &tId );
1600       
1601        printf("|%s| entry. tId = 0x%08lX\n", __FUNCTION__, tId);
1602
1603        while(*pResult)
1604                OS_mDelay(100);
1605       
1606        i = 0;
1607        while ( 1 ) {
1608                printf("[%s] give message to tMsgCons. [%d]\n", __FUNCTION__, i);
1609               
1610                smsg.senderID = (DS_U32)tId;
1611                smsg.Length = 1;
1612                smsg.Command = 0xAAAAAAAA;
1613                smsg.CommandCPL = ~smsg.Command;
1614                ret = OS_SendMessage( msgId2, (DS_U32 *)&smsg, sizeof(MyMessage) );
1615                if ( ret ) {
1616                        printf("|%s| ERROR, LINE=%d, ret = %ld\n", __FUNCTION__, __LINE__, ret);
1617                        break;
1618                }
1619               
1620                printf("[%s] Wait for message from tMsgCons. [%d]\n", __FUNCTION__, i);
1621                if ( i!=N_TEST_MSG ) {
1622                        ret = OS_ReceiveMessage_Wait( msgId1, (DS_U32 *)&rmsg, sizeof(MyMessage), &len, OS_WAIT_FOREVER );
1623                        if ( ret ) {
1624                                printf("|%s| ERROR, LINE=%d, ret = %ld\n", __FUNCTION__, __LINE__, ret);
1625                                break;
1626                        }
1627                } else {
1628                        ret = OS_ReceiveMessage_Wait( msgId1, (DS_U32 *)&rmsg, sizeof(MyMessage), &len, 100 );
1629                        if ( ret ) {
1630                                printf("|%s:%d| ret = %s[%ld] ==> %s\n", __FUNCTION__, __LINE__, 
1631                                                        ret == OS_TIMEOUT ? "TIMEOUT" : 
1632                                                        ret == OS_FAIL ? "FAIL" :
1633                                                        ret == OS_NOT_SUPPORTED ? "NOT SUPPORTED" : "Unknown", ret, ret == OS_TIMEOUT ? "TIMEOUT OK" : "TIMEOUT FAIL" );
1634                                break;
1635                        }
1636                }
1637                printf("[%s]     MyMessage.senderID = 0x%08lX\n", __FUNCTION__, rmsg.senderID );
1638                printf("[%s]     MyMessage.Length = %ld\n", __FUNCTION__, rmsg.Length);
1639                printf("[%s]     MyMessage.Command = 0x%08lX\n", __FUNCTION__, rmsg.Command );
1640                printf("[%s]     MyMessage.CommandCPL = 0x%08lX\n", __FUNCTION__, rmsg.CommandCPL );
1641               
1642                if ( ++i > N_TEST_MSG )
1643                        break;
1644        }
1645       
1646        if ( i == N_TEST_MSG )
1647                *pResult = 1;
1648        else
1649                *pResult = -1;
1650}
1651
1652void tMsgCons( DS_U32 arg )
1653{
1654        int *pResult = (int *)arg;
1655        int i;
1656        CORE_TASK_ID tId;
1657        MyMessage smsg, rmsg;
1658        DS_U32 len;
1659        DS_U32 ret;
1660
1661        DstCore_TaskGetInfo( &tId );
1662       
1663        printf("|%s| entry. tId = 0x%08lX\n", __FUNCTION__, tId);
1664       
1665        while(*pResult)
1666                OS_mDelay(100);
1667       
1668        i = 0;
1669        while ( 1 ) {
1670                printf("[%s] Wait for message from tMsgProd. [%d]\n", __FUNCTION__, i);
1671                ret = OS_ReceiveMessage_Wait( msgId2, (DS_U32 *)&rmsg, sizeof(MyMessage), &len, OS_WAIT_FOREVER );
1672                if ( ret ) {
1673                        printf("|%s| ERROR, LINE=%d, ret = %ld\n", __FUNCTION__, __LINE__, ret);
1674                        break;
1675                }
1676
1677                if ( i == N_TEST_MSG ) {
1678                        printf("[%s] Delay 1000msec.\n", __FUNCTION__);
1679                        OS_mDelay(1000);
1680                }
1681                printf("[%s]     MyMessage.senderID = 0x%08lX\n", __FUNCTION__, rmsg.senderID );
1682                printf("[%s]     MyMessage.Length = %ld\n", __FUNCTION__, rmsg.Length);
1683                printf("[%s]     MyMessage.Command = 0x%08lX\n", __FUNCTION__, rmsg.Command );
1684                printf("[%s]     MyMessage.CommandCPL = 0x%08lX\n", __FUNCTION__, rmsg.CommandCPL );
1685
1686                printf("[%s] give message to tMsgProd. [%d]\n", __FUNCTION__, i);
1687                smsg.senderID = (DS_U32)tId;
1688                smsg.Length = 1;
1689                smsg.Command = 0x55aa55aa;
1690                smsg.CommandCPL = ~smsg.Command;
1691                ret = OS_SendMessage( msgId1, (DS_U32 *)&smsg, sizeof(MyMessage) );
1692                if ( ret ) {
1693                        printf("|%s| ERROR, LINE=%d, ret = %ld\n", __FUNCTION__, __LINE__, ret);
1694                        break;
1695                }
1696                               
1697                if ( ++i > N_TEST_MSG )
1698                        break;
1699        }
1700       
1701        if ( i == (N_TEST_MSG+1) )
1702                *pResult = 1;
1703        else
1704                *pResult = -1;
1705}
1706
1707void test_message(DS_U32 Option)
1708{
1709        int ret = 0;
1710        static int test_result1 = -1;
1711        static int test_result2 = -1;
1712        DS_U32 Result;
1713       
1714        //
1715        // Create 2 semaphores.
1716        //
1717        msgId1 = OS_CreateMessageQueue ( "msgQ1", 0, 20, sizeof(MyMessage) );
1718        if ( msgId1 == 0 ) {
1719                printf("|%s:%d| ERROR: cannot create message queue.\n", __FUNCTION__, __LINE__);
1720                ret = -1;
1721                goto done;
1722        }
1723       
1724        msgId2 = OS_CreateMessageQueue ( "msgQ2", 0, 20, sizeof(MyMessage) );
1725        if ( msgId2 == 0 ) {
1726                printf("|%s:%d| ERROR: cannot create message queue.\n", __FUNCTION__, __LINE__);
1727                ret = -1;
1728                goto done;
1729        }
1730       
1731        //
1732        // Create the product task.
1733        //
1734        Result = OS_SpawnTask( tMsgProd, 0, 60, 8*1024, (DS_U32)&test_result1 );
1735        if ( Result == 0 ) {
1736                printf("|%s:%d| ERROR: cannot create thread.\n", __FUNCTION__, __LINE__);
1737                ret = -1;
1738                goto done;
1739        }
1740       
1741        //
1742        // Create the consumer task.
1743        //
1744        Result = OS_SpawnTask( tMsgCons, 0, 60, 8*1024, (DS_U32)&test_result2 );
1745        if ( Result == 0 ) {
1746                printf("|%s:%d| ERROR: cannot create thread.\n", __FUNCTION__, __LINE__);
1747                ret = -1;
1748                goto done;
1749        }
1750
1751        //
1752        // Starts the task.
1753        //
1754        test_result1 = 0;
1755        test_result2 = 0;
1756        while ( test_result1 == 0 || test_result2 == 0 ) {
1757                OS_mDelay(1000);
1758                printf("test_result1: %d, test_result2: %d\n", test_result1, test_result2);
1759//              msgQList( stdout, 0 );
1760        }
1761       
1762        if ( test_result1 > 0 && test_result2 > 0 )
1763                ret = 1;
1764        else
1765                ret = -1;
1766       
1767done:
1768        if ( ret < 0 )
1769                printf("*** MessageQueue Test: FAIL ***\n");
1770        else
1771                printf("*** MessageQueue Test: PASS ***\n");
1772       
1773        OS_DeleteMessageQueue( msgId1 );
1774        OS_DeleteMessageQueue( msgId2 );
1775}
1776
1777DS_U32 OS_ReadRegister( DS_U32 Address )
1778{
1779        return DstCore_RegRead( Address, 4 );
1780}
1781
1782DS_U32 OS_WriteRegister( DS_U32 Address, DS_U32 Value )
1783{
1784        return DstCore_RegWrite(Address, Value, 4);
1785}
1786
1787DS_U32 OS_ReadRegisterWord( DS_U32 Address )
1788{
1789        return DstCore_RegRead( Address, 2 );
1790}
1791
1792DS_U32 OS_WriteRegisterWord( DS_U32 Address, DS_U32 Value )
1793{
1794        return DstCore_RegWrite(Address, Value, 2);
1795}
1796
1797DS_U32 OS_ReadRegisterByte( DS_U32 Address )
1798{
1799        return DstCore_RegRead( Address, 1 );
1800}
1801
1802DS_U32 OS_WriteRegisterByte( DS_U32 Address, DS_U32 Value )
1803{
1804        return DstCore_RegWrite(Address, Value, 1);
1805}
1806
1807DS_U32 OS_GetLoaderVer(void)
1808{
1809        return DstCore_GetLoaderVer();
1810}
1811
1812#if USE_V2LIN==1
1813void print_msgq(int mem)
1814{
1815    msgQList( stdout, mem );
1816}
1817 
1818void print_sem(int mem)
1819{
1820    semList(stdout, mem);
1821}
1822#endif
Note: See TracBrowser for help on using the repository browser.