xref: /utopia/UTPA2-700.0.x/modules/msos/msos/nuttx/MsOS_nuttx.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ////////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ////////////////////////////////////////////////////////////////////////////////
94 
95 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 ///
97 /// file    MsOS_nuttx.c
98 /// @brief  MStar OS Wrapper
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101 
102 
103 //-------------------------------------------------------------------------------------------------
104 // Include Files
105 //-------------------------------------------------------------------------------------------------
106 #include <unistd.h>
107 #include "MsCommon.h"
108 #include "MsOS.h"
109 #include "debug.h"
110 #include "asmCPU.h"
111 #include "drvIRQ.h"
112 #include "string.h"
113 #include "halCHIP.h"
114 #include "halMMIO.h"
115 #include "MsVersion.h"
116 #ifdef CONFIG_UTOPIA_FRAMEWORK
117 #include "utopia.h"
118 #endif
119 #include "hal_dcache.h"
120 #include "halMPool.h"
121 //-------------------------------------------------------------------------------------------------
122 // Local Defines
123 //------------------------------------------------------------------------------------------------
124 typedef signed char        _int8_t;
125 typedef unsigned char      _uint8_t;
126 
127 typedef signed short       _int16_t;
128 typedef unsigned short     _uint16_t;
129 
130 typedef signed int         _int32_t;
131 typedef unsigned int       _uint32_t;
132 
133 typedef signed long long   _int64_t;
134 typedef unsigned long long _uint64_t;
135 
136 struct shm_entry {
137 	char name[50]; // FIXME: potential bug
138 	void* addr;
139 	unsigned int size;
140 };
141 
142 #define SHM_ARRAY_LENGTH 200
143 struct shm_array {
144 	unsigned int count;
145 	struct shm_entry shm_entry[SHM_ARRAY_LENGTH];
146 };
147 
148 struct shm_array shm_array;
149 
150 /* Values for the process shared (pshared) attribute */
151 
152 #define PTHREAD_PROCESS_PRIVATE       0
153 #define PTHREAD_PROCESS_SHARED        1
154 
155 // Combine 3-B prefix with s32ID = MSOS_ID_PREFIX | s32Id
156 //  to avoid the kernel object being used before initialzed.
157 #define MSOS_ID_PREFIX              0x76540000
158 #define MSOS_ID_PREFIX_MASK         0xFFFF0000
159 #define MSOS_ID_MASK                0x0000FFFF //~MSOS_ID_PREFIX_MASK
160 
161 /* CLOCK_REALTIME refers to the standard time source.  For most implementations,
162  * the standard time source is the system timer interrupt.  However, if the
163  * platform supports an RTC, then the standard time source will be the RTC
164  * for the clock_gettime() and clock_settime() interfaces (the system timer
165  * is still the time source for all of the interfaces).
166  */
167 
168 #define CLOCK_REALTIME     0
169 
170 /* If an RTC is supported, then the non-standard CLOCK_ACTIVETIME is also
171  * supported to manage time based on the system timer interrupt separately from
172  * the RTC.  This may be necessary, for example, in certain cases where the
173  * system timer interrupt has been stopped in low power modes.
174  *
175  * CLOCK_ACTIVETIME is only recognized by clock_gettime() and clock_settime().
176  */
177 
178 #ifdef CONFIG_RTC
179 #  define CLOCK_ACTIVETIME 1
180 #else
181 #  define CLOCK_ACTIVETIME CLOCK_REALTIME
182 #endif
183 
184 /* This is a flag that may be passed to the timer_settime() function */
185 
186 #define TIMER_ABSTIME      1
187 
188 
189 //-------------------------------------------------------------------------------------------------
190 // Global Variables
191 //-------------------------------------------------------------------------------------------------
192 extern MS_U32 g_system_timer;
193 extern void Hal_Dcache_Flush(unsigned int base, unsigned int size);
194 //-------------------------------------------------------------------------------------------------
195 // Local Variables
196 //-------------------------------------------------------------------------------------------------
197 
198 //
199 // Variable-size Memory pool
200 //
201 //
202 
203 //
204 // Task Management
205 //
206 
207 //
208 // Semaphore
209 //
210 typedef int pthread_condattr_t;
211 
212 struct sem_s
213 {
214   _int16_t semcount;              /* >0 -> Num counts available */
215                                 /* <0 -> Num tasks waiting for semaphore */
216 #ifdef CONFIG_PRIORITY_INHERITANCE
217   struct semholder_s hlist;     /* List of holders of semaphore counts */
218 #endif
219 };
220 typedef struct sem_s sem_t;
221 
222 typedef struct
223 {
224     MS_BOOL                     bUsed;
225     sem_t                       stSemaphore;
226 } MsOS_Semaphore_Info;
227 
228 static MsOS_Semaphore_Info      _MsOS_Semaphore_Info[MSOS_SEMAPHORE_MAX];
229 
230 
231 //
232 // Mutex
233 //
234 struct pthread_mutexattr_s
235 {
236   _uint8_t pshared;  /* PTHREAD_PROCESS_PRIVATE or PTHREAD_PROCESS_SHARED */
237 #ifdef CONFIG_MUTEX_TYPES
238   _uint8_t type;	  /* Type of the mutex.  See PTHREAD_MUTEX_* definitions */
239 #endif
240 };
241 
242 typedef struct pthread_mutexattr_s pthread_mutexattr_t;
243 
244 struct pthread_mutex_s
245 {
246   int   pid;      /* ID of the holder of the mutex */
247   sem_t sem;      /* Semaphore underlying the implementation of the mutex */
248 #ifdef CONFIG_MUTEX_TYPES
249   _uint8_t type;   /* Type of the mutex.  See PTHREAD_MUTEX_* definitions */
250   int   nlocks;   /* The number of recursive locks held */
251 #endif
252 };
253 
254 typedef struct pthread_mutex_s pthread_mutex_t;
255 
256 typedef struct
257 {
258     MS_BOOL             bUsed;
259     MS_BOOL             bShared;
260     pthread_mutex_t     stMutex;
261     MS_U8               u8Name[MAX_MUTEX_NAME_LENGTH];
262 } MsOS_Mutex_Info;
263 
264 static MsOS_Mutex_Info          _MsOS_Mutex_Info[MSOS_MUTEX_MAX];
265 static pthread_mutex_t			_MsOS_Mutex_Mutex;
266 #define MUTEX_MUTEX_LOCK()		pthread_mutex_lock(&_MsOS_Mutex_Mutex)
267 #define MUTEX_MUTEX_UNLOCK()	pthread_mutex_unlock(&_MsOS_Mutex_Mutex)
268 
269 static pthread_mutex_t              _MsOS_Semaphore_Mutex;
270 #define MUTEX_SEMEPHORE_LOCK()		pthread_mutex_lock(&_MsOS_Semaphore_Mutex)
271 #define MUTEX_SEMEPHORE_UNLOCK()	pthread_mutex_unlock(&_MsOS_Semaphore_Mutex)
272 
273 
274 //
275 // Event Group
276 //
277 struct pthread_cond_s
278 {
279   sem_t sem;
280 };
281 
282 typedef struct pthread_cond_s pthread_cond_t;
283 
284 typedef struct
285 {
286     MS_BOOL                     bUsed;
287     MS_U32                      u32EventGroup;
288     pthread_mutex_t             stMutexEvent;
289     pthread_cond_t              stSemaphore;
290     pthread_mutex_t             stMutex;
291 } MsOS_EventGroup_Info;
292 
293 static MsOS_EventGroup_Info     _MsOS_EventGroup_Info[MSOS_EVENTGROUP_MAX];
294 static pthread_mutex_t          _MsOS_EventGroup_Mutex;
295 
296 //
297 // Queue
298 //
299 
300 //
301 // Timer
302 //
303 
304 extern int clock_gettime(clockid_t clockid, struct timespec *tp);
305 
306 //
307 // Interrupt
308 //
309 extern MS_BOOL up_interrupt_context(void);
310 
311 //-------------------------------------------------------------------------------------------------
312 // Externel Function Prototypes
313 //-------------------------------------------------------------------------------------------------
314 typedef int (*xcpt_t)(int irq, void *context);
315 extern int hal_irq_attach(int irq, xcpt_t isr);
316 extern int hal_irq_detach(int irq);
317 extern void chip_irq_mask(unsigned irq);
318 extern void chip_irq_unmask(unsigned irq);
319 
320 
321 /* Create, operate on, and destroy mutex attributes. */
322 extern int pthread_mutexattr_init(pthread_mutexattr_t *attr);
323 extern int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
324 extern int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared);
325 extern int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared);
326 
327 
328 /* The following routines create, delete, lock and unlock mutexes. */
329 extern int pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr);
330 extern int pthread_mutex_destroy(pthread_mutex_t *mutex);
331 extern int pthread_mutex_lock(pthread_mutex_t *mutex);
332 extern int pthread_mutex_trylock(pthread_mutex_t *mutex);
333 extern int pthread_mutex_unlock(pthread_mutex_t *mutex);
334 
335 /* Operations on condition variables */
336 
337 extern int pthread_condattr_init(pthread_condattr_t *attr);
338 extern int pthread_condattr_destroy(pthread_condattr_t *attr);
339 
340 /* A thread can create and delete condition variables. */
341 
342 extern int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr);
343 extern int pthread_cond_destroy(pthread_cond_t *cond);
344 
345 /* A thread can signal to and broadcast on a condition variable. */
346 
347 extern int pthread_cond_broadcast(pthread_cond_t *cond);
348 extern int pthread_cond_signal(pthread_cond_t *cond);
349 
350 /* A thread can wait for a condition variable to be signalled or broadcast. */
351 
352 extern int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);
353 
354 /* A thread can perform a timed wait on a condition variable. */
355 
356 extern int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
357                                   const struct timespec *abstime);
358 
359 /* Counting Semaphore Interfaces (based on POSIX APIs) */
360 extern int        sem_init(sem_t *sem, int pshared, unsigned int value);
361 extern int        sem_destroy(sem_t *sem);
362 extern sem_t *    sem_open(const char *name, int oflag, ...);
363 extern int        sem_close(sem_t *sem);
364 extern int        sem_unlink(const char *name);
365 extern int        sem_wait(sem_t *sem);
366 extern int        sem_trywait(sem_t *sem);
367 extern int        sem_post(sem_t *sem);
368 extern int        sem_getvalue(sem_t *sem, int *sval);
369 
370 /* To avoid nuttx crash in interrupt mode, replace printf with lib_lowprintf. */
371 extern int lib_lowprintf(const char *fmt, ...);
372 #define PRINTF lib_lowprintf
373 
374 static MSIF_Version _drv_msos_version = {
375     .DDI = { MSOS_DRV_VERSION },
376 };
377 
378 //-------------------------------------------------------------------------------------------------
379 // Macros
380 //-------------------------------------------------------------------------------------------------
381 #define MSOS_PRINT(fmt, args...)    PRINTF("[MSOS_PRINT][%06d]     " fmt, __LINE__, ## args)
382 
383 #define HAS_FLAG(flag, bit)        ((flag) & (bit))
384 #define SET_FLAG(flag, bit)        ((flag)|= (bit))
385 #define RESET_FLAG(flag, bit)      ((flag)&= (~(bit)))
386 
387 #define _TimeAbs(stTime, u32IntMs)                                                              \
388 {                                                                                               \
389     clock_gettime(CLOCK_REALTIME, &(stTime));                                                   \
390     if(u32IntMs>0)                                                                              \
391     {                                                                                           \
392         MS_U32                         u32Sec=(u32IntMs)/1000;                                  \
393         (stTime).tv_sec+=           u32Sec;                                                     \
394         (stTime).tv_nsec+=          ((u32IntMs)- (1000* u32Sec))* 1000000;                      \
395         if ((stTime).tv_nsec>= 1000000000)                                                      \
396         {                                                                                       \
397             (stTime).tv_sec++;                                                                  \
398             (stTime).tv_nsec-=      1000000000;                                                 \
399         }                                                                                       \
400     }                                                                                           \
401 }
402 
403 ////////////////////////////////////////////////////////////////////////////////
404 /// @brief \b Function  \b Name: MDrv_MSOS_GetLibVer
405 /// @brief \b Function  \b Description: Show the MSOS driver version
406 /// @param ppVersion    \b Out: Library version string
407 /// @return             \b Result
408 ////////////////////////////////////////////////////////////////////////////////
MDrv_MSOS_GetLibVer(const MSIF_Version ** ppVersion)409 MS_BOOL MDrv_MSOS_GetLibVer(const MSIF_Version **ppVersion)
410 {
411     if (!ppVersion)
412         return FALSE;
413 
414     *ppVersion = &_drv_msos_version;
415     return TRUE;
416 }
417 //-------------------------------------------------------------------------------------------------
418 /// Initialize MsOS
419 /// @return TRUE : succeed
420 /// @return FALSE : fail
421 //-------------------------------------------------------------------------------------------------
MsOS_Init(void)422 MS_BOOL MsOS_Init (void)
423 {
424     MS_U32 u32I;
425 	pthread_mutexattr_t _MsOS_Mutex_Attr;
426 
427     //
428     //mutex
429     //
430 	pthread_mutexattr_init(&_MsOS_Mutex_Attr);
431 	pthread_mutex_init(&_MsOS_Mutex_Mutex, &_MsOS_Mutex_Attr);
432 	for( u32I=0; u32I<MSOS_MUTEX_MAX; u32I++)
433     {
434         _MsOS_Mutex_Info[u32I].bUsed = FALSE;
435     }
436 
437     //
438     // Semaphore
439     //
440     pthread_mutex_init(&_MsOS_Semaphore_Mutex, &_MsOS_Mutex_Attr);
441     for( u32I=0; u32I<MSOS_SEMAPHORE_MAX; u32I++)
442     {
443         _MsOS_Semaphore_Info[u32I].bUsed = FALSE;
444     }
445 
446 	MDrv_IRQ_Init();
447 
448     //
449     // Event Group
450     //
451     pthread_mutex_init(&_MsOS_EventGroup_Mutex, &_MsOS_Mutex_Attr);
452 
453     for( u32I=0; u32I<MSOS_EVENTGROUP_MAX; u32I++)
454     {
455         _MsOS_EventGroup_Info[u32I].bUsed = FALSE;
456     }
457 
458 #ifdef CONFIG_UTOPIA_FRAMEWORK
459 	UtopiaInit();
460 #endif
461 
462     return true;
463 }
464 
465 //
466 // Memory management
467 //
468 //-------------------------------------------------------------------------------------------------
469 /// Create a variable-size memory pool dynamically
470 /// @param  u32PoolSize         \b IN: pool size in bytes
471 /// @param  u32MinAllocation    \b IN: not used
472 /// @param  pPoolAddr           \b IN: starting address for the memory pool
473 /// @param  eAttribute          \b IN: only E_MSOS_FIFO - suspended in FIFO order
474 /// @param  pPoolName           \b IN: not used
475 /// @return >=0 : assigned memory pool ID
476 /// @return < 0 : fail
477 //-------------------------------------------------------------------------------------------------
MsOS_CreateMemoryPool(MS_U32 u32PoolSize,MS_U32 u32MinAllocation,void * pPoolAddr,MsOSAttribute eAttribute,char * pPoolName)478 MS_S32 MsOS_CreateMemoryPool (MS_U32   u32PoolSize,
479                            MS_U32   u32MinAllocation,
480                            void     *pPoolAddr,
481                            MsOSAttribute eAttribute,
482                            char     *pPoolName)
483 {
484     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
485     return -1;
486 }
487 
488 //-------------------------------------------------------------------------------------------------
489 /// Delete a variable-size memory pool
490 /// @param  s32PoolId   \b IN: pool ID
491 /// @return TRUE : succeed
492 /// @return FALSE : fail
493 //-------------------------------------------------------------------------------------------------
MsOS_DeleteMemoryPool(MS_S32 s32PoolId)494 MS_BOOL MsOS_DeleteMemoryPool (MS_S32 s32PoolId)
495 {
496     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
497     return FALSE;
498 }
499 
500 //-------------------------------------------------------------------------------------------------
501 /// Get the information of a variable-size memory pool
502 /// @param  s32PoolId   \b IN: memory pool ID
503 /// @param  pPoolAddr   \b OUT: holding the starting address for the memory pool
504 /// @param  pu32PoolSize \b OUT: holding the total size of the memory pool
505 /// @param  pu32FreeSize \b OUT: holding the available free size of the memory pool
506 /// @param  pu32LargestFreeBlockSize  \b OUT: holding the size of the largest free block
507 /// @return TRUE : succeed
508 /// @return FALSE : the pool has not been created
509 //-------------------------------------------------------------------------------------------------
MsOS_InfoMemoryPool(MS_S32 s32PoolId,void ** pPoolAddr,MS_U32 * pu32PoolSize,MS_U32 * pu32FreeSize,MS_U32 * pu32LargestFreeBlockSize)510 MS_BOOL MsOS_InfoMemoryPool (MS_S32    s32PoolId,
511                           void      **pPoolAddr,
512                           MS_U32    *pu32PoolSize,
513                           MS_U32    *pu32FreeSize,
514                           MS_U32    *pu32LargestFreeBlockSize)
515 {
516     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
517     return FALSE;
518 }
519 
520 
521 //-------------------------------------------------------------------------------------------------
522 /// Allocate a memory block with 16-Byte aligned starting address from the variable-size memory pool
523 /// @param  u32Size     \b IN: request size
524 /// @param  s32PoolId   \b IN: memory pool ID
525 /// @return NULL : not enough available memory
526 /// @return Otherwise : pointer to the allocated memory block
527 //-------------------------------------------------------------------------------------------------
MsOS_AllocateMemory(MS_U32 u32Size,MS_S32 s32PoolId)528 void * MsOS_AllocateMemory (MS_U32 u32Size, MS_S32 s32PoolId)
529 {
530     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
531     return NULL;
532 }
533 
534 //-------------------------------------------------------------------------------------------------
535 /// Free a memory block from the variable-size memory pool
536 /// @param  pAddress    \b IN: pointer to previously allocated memory block
537 /// @param  s32PoolId   \b IN: memory pool ID
538 /// @return TRUE : succeed
539 /// @return FALSE : fail
540 //-------------------------------------------------------------------------------------------------
MsOS_FreeMemory(void * pAddress,MS_S32 s32PoolId)541 MS_BOOL MsOS_FreeMemory (void *pAddress, MS_S32 s32PoolId)
542 {
543     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
544     return TRUE;
545 }
546 
547 
548 //-------------------------------------------------------------------------------------------------
549 /// Reallocate a block of memory with 4-byte aligned start address from the variable-size memory pool
550 /// @param  pOrgAddress \b IN: points to the beginning of the original memory block
551 /// @param  u32NewSize  \b IN: size of new memory block
552 /// @param  s32PoolId   \b IN: memory pool ID
553 /// @return NULL : not enough available memory to expand the block or u32NewSize == 0 && pOrgAddress != NULL
554 /// @return Otherwise : pointer to the reallocated (and possibly moved) memory block
555 //  @note   reference realloc in malloc.cxx
556 //-------------------------------------------------------------------------------------------------
MsOS_ReallocateMemory(void * pOrgAddress,MS_U32 u32NewSize,MS_S32 s32PoolId)557 void * MsOS_ReallocateMemory (void *pOrgAddress, MS_U32 u32NewSize, MS_S32 s32PoolId)
558 {
559     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
560     return NULL;
561 }
562 
563 //-------------------------------------------------------------------------------------------------
564 /// Create a fixed-size memory pool dynamically
565 /// @param  u32PoolSize         \b IN: pool size in bytes
566 /// @param  u32BlockSize        \b IN: fixed block size for each allocated block in the pool
567 /// @param  pPoolAddr           \b IN: starting address for the memory pool
568 /// @param  eAttribute          \b IN: only E_MSOS_FIFO - suspended in FIFO order
569 /// @param  pPoolName           \b IN: not used
570 /// @return >=0 : assigned memory pool ID
571 /// @return < 0 : fail
572 //-------------------------------------------------------------------------------------------------
MsOS_CreateFixSizeMemoryPool(MS_U32 u32PoolSize,MS_U32 u32BlockSize,void * pPoolAddr,MsOSAttribute eAttribute,char * pPoolName)573 MS_S32 MsOS_CreateFixSizeMemoryPool (MS_U32    u32PoolSize,
574                                   MS_U32    u32BlockSize,
575                                   void      *pPoolAddr,
576                                   MsOSAttribute eAttribute,
577                                   char      *pPoolName)
578 {
579     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
580     return -1;
581 }
582 
583 //-------------------------------------------------------------------------------------------------
584 /// Delete a fixed-size memory pool
585 /// @param  s32PoolId   \b IN: pool ID
586 /// @return TRUE : succeed
587 /// @return FALSE : fail
588 //-------------------------------------------------------------------------------------------------
MsOS_DeleteFixSizeMemoryPool(MS_S32 s32PoolId)589 MS_BOOL MsOS_DeleteFixSizeMemoryPool (MS_S32 s32PoolId)
590 {
591     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
592     return FALSE;
593 }
594 
595 //-------------------------------------------------------------------------------------------------
596 /// Get the information of a fixed-size memory pool
597 /// @param  s32PoolId   \b IN: memory pool ID
598 /// @param  pPoolAddr   \b OUT: holding the starting address for the memory pool
599 /// @param  pu32PoolSize \b OUT: holding the total size of the memory pool
600 /// @param  pu32FreeSize \b OUT: holding the available free size of the memory pool
601 /// @param  pu32LargestFreeBlockSize  \b OUT: holding the size of the largest free block
602 /// @return TRUE : succeed
603 /// @return FALSE : the pool has not been created
604 //-------------------------------------------------------------------------------------------------
MsOS_InfoFixSizeMemoryPool(MS_S32 s32PoolId,void ** pPoolAddr,MS_U32 * pu32PoolSize,MS_U32 * pu32FreeSize,MS_U32 * pu32LargestFreeBlockSize)605 MS_BOOL MsOS_InfoFixSizeMemoryPool (MS_S32 s32PoolId,
606                                  void   **pPoolAddr,
607                                  MS_U32 *pu32PoolSize,
608                                  MS_U32 *pu32FreeSize,
609                                  MS_U32 *pu32LargestFreeBlockSize)
610 {
611     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
612     return FALSE;
613 }
614 
615 //-------------------------------------------------------------------------------------------------
616 /// Allocate a memory block from the fixed-size memory pool
617 /// @param  s32PoolId   \b IN: memory pool ID
618 /// @return NULL : not enough available memory
619 /// @return Otherwise : pointer to the allocated memory block
620 /// @note   watch out for alignment if u32BlockSize is not a multiple of 4
621 //-------------------------------------------------------------------------------------------------
MsOS_AllocateFixSizeMemory(MS_S32 s32PoolId)622 void * MsOS_AllocateFixSizeMemory (MS_S32 s32PoolId)
623 {
624     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
625     return NULL;
626 }
627 
628 //-------------------------------------------------------------------------------------------------
629 /// Free a memory block from the fixed-size memory pool
630 /// @param  pAddress    \b IN: pointer to previously allocated memory block
631 /// @param  s32PoolId   \b IN: memory pool ID
632 /// @return TRUE : succeed
633 /// @return FALSE : fail
634 //-------------------------------------------------------------------------------------------------
MsOS_FreeFixSizeMemory(void * pAddress,MS_S32 s32PoolId)635 MS_BOOL MsOS_FreeFixSizeMemory (void *pAddress, MS_S32 s32PoolId)
636 {
637     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
638     return FALSE;
639 }
640 
641 
642 //
643 // Task
644 //
645 //-------------------------------------------------------------------------------------------------
646 /// Create a task
647 /// @param  pTaskEntry       \b IN: task entry point
648 /// @param  u32TaskEntryData \b IN: task entry data: a pointer to some static data, or a
649 ///          small integer, or NULL if the task does not require any additional data.
650 /// @param  eTaskPriority    \b IN: task priority
651 /// @param  bAutoStart       \b IN: start immediately or later
652 /// @param  pStackBase       \b IN: task stack
653 /// @param  u32StackSize     \b IN: stack size
654 /// @param  pTaskName        \b IN: task name
655 /// @return >=0 : assigned Task ID
656 /// @return < 0 : fail
657 //-------------------------------------------------------------------------------------------------
658 #define CONFIG_USERMAIN_STACKSIZE (0x40000)
659 typedef int (*main_t)(int argc, char *argv[]);
660 extern int task_create(const char *name, int priority,
661 		int stack_size, main_t entry, const char *argv);
662 
MsOS_CreateTask(TaskEntry pTaskEntry,MS_VIRT u32TaskEntryData,TaskPriority eTaskPriority,MS_BOOL bAutoStart,void * pStackBase,MS_U32 u32StackSize,char * pTaskName)663 MS_S32 MsOS_CreateTask (TaskEntry  pTaskEntry,
664                      MS_VIRT     u32TaskEntryData,
665                      TaskPriority eTaskPriority,
666                      MS_BOOL    bAutoStart,
667                      void       *pStackBase,
668                      MS_U32     u32StackSize,
669                      char *pTaskName)
670 {
671 	return task_create(pTaskName, eTaskPriority, CONFIG_USERMAIN_STACKSIZE
672 			, (main_t)pTaskEntry, (const char*)u32TaskEntryData);
673 }
674 
675 //-------------------------------------------------------------------------------------------------
676 /// Delete a previously created task
677 /// @param  s32TaskId   \b IN: task ID
678 /// @return TRUE : succeed
679 /// @return FALSE : fail
680 //-------------------------------------------------------------------------------------------------
MsOS_DeleteTask(MS_S32 s32TaskId)681 MS_BOOL MsOS_DeleteTask (MS_S32 s32TaskId)
682 {
683     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
684     return FALSE;
685 }
686 
687 //-------------------------------------------------------------------------------------------------
688 /// Yield the execution right to ready tasks with "the same" priority
689 /// @return None
690 //-------------------------------------------------------------------------------------------------
MsOS_YieldTask(void)691 void MsOS_YieldTask (void)
692 {
693     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
694 }
695 
696 //-------------------------------------------------------------------------------------------------
697 /// Suspend the calling task for u32Ms milliseconds
698 /// @param  u32Ms  \b IN: delay 1 ~ MSOS_WAIT_FOREVER ms
699 /// @return None
700 /// @note   Not allowed in interrupt context; otherwise, exception will occur.
701 //-------------------------------------------------------------------------------------------------
MsOS_DelayTask(MS_U32 u32Ms)702 void MsOS_DelayTask (MS_U32 u32Ms)
703 {
704 	usleep(u32Ms*1000);
705 }
706 
707 //-------------------------------------------------------------------------------------------------
708 /// Delay for u32Us microseconds
709 /// @param  u32Us  \b IN: delay 0 ~ 999 us
710 /// @return None
711 /// @note   implemented by "busy waiting". Plz call MsOS_DelayTask directly for ms-order delay
712 //-------------------------------------------------------------------------------------------------
MsOS_DelayTaskUs(MS_U32 u32Us)713 void MsOS_DelayTaskUs (MS_U32 u32Us)
714 {
715     MAsm_CPU_DelayUs(u32Us);
716 }
717 
718 //-------------------------------------------------------------------------------------------------
719 /// Delay Poll for u32Us microseconds
720 /// @param  u32Us  \b IN: delay 0 ~ 999 us
721 /// @return None
722 /// @note   implemented by "busy waiting". Plz call MsOS_DelayTask directly for ms-order delay
723 //-------------------------------------------------------------------------------------------------
MsOS_DelayTaskUs_Poll(MS_U32 u32Us)724 void MsOS_DelayTaskUs_Poll(MS_U32 u32Us)
725 {
726 	MsOS_DelayTaskUs(u32Us);
727 }
728 
729 //-------------------------------------------------------------------------------------------------
730 /// Resume the specified suspended task
731 /// @param  s32TaskId   \b IN: Task ID
732 /// @return TRUE : succeed
733 /// @return FALSE : fail
734 /// @note   This API is not supported in Linux
735 //-------------------------------------------------------------------------------------------------
MsOS_ResumeTask(MS_S32 s32TaskId)736 MS_BOOL MsOS_ResumeTask (MS_S32 s32TaskId)
737 {
738     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
739     return FALSE;
740 }
741 
742 //-------------------------------------------------------------------------------------------------
743 /// Suspend the specified task
744 /// @param  s32TaskId   \b IN: Task ID
745 /// @return TRUE : succeed
746 /// @return FALSE : fail
747 /// @note   This API is not supported in Linux
748 //-------------------------------------------------------------------------------------------------
MsOS_SuspendTask(MS_S32 s32TaskId)749 MS_BOOL MsOS_SuspendTask (MS_S32 s32TaskId)
750 {
751     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
752     return FALSE;
753 }
754 
755 //-------------------------------------------------------------------------------------------------
756 /// Get a task information
757 /// @param  s32TaskId       \b IN: task ID
758 /// @param  peTaskPriority  \b OUT: ptr to task priority
759 /// @param  pStackBase      \b OUT: ptr to stack base
760 /// @param  pu32StackSize   \b OUT: ptr to stack size
761 /// @param  pTaskName       \b OUT: ptr to task name
762 /// @return TRUE : succeed
763 /// @return FALSE : the task has not been created
764 //-------------------------------------------------------------------------------------------------
MsOS_InfoTask(MS_S32 s32TaskId,TaskPriority * peTaskPriority,void * pStackBase,MS_U32 * pu32StackSize,MS_U32 * pu32StackUsed,char * pTaskName)765 MS_BOOL MsOS_InfoTask (MS_S32 s32TaskId,
766                        TaskPriority *peTaskPriority,
767                        void* pStackBase,
768                        MS_U32* pu32StackSize,
769                        MS_U32* pu32StackUsed,
770                        char *pTaskName)
771 {
772     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
773     return FALSE;
774 }
775 
776 //-------------------------------------------------------------------------------------------------
777 /// Get all task ID
778 /// @param  ps32TaskIdList   \b IN/OUT: the memory for the all task ID
779 /// @return number of task created
780 //-------------------------------------------------------------------------------------------------
MsOS_InfoTaskList(MS_S32 * ps32TaskIdList)781 MS_U32 MsOS_InfoTaskList (MS_S32* ps32TaskIdList)
782 {
783     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
784     return 0;
785 }
786 
787 
788 //-------------------------------------------------------------------------------------------------
789 /// Get current task ID
790 /// @return >=0 : current task ID
791 /// @return <0 : fail
792 //-------------------------------------------------------------------------------------------------
MsOS_InfoTaskID(void)793 MS_S32 MsOS_InfoTaskID (void)
794 {
795     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
796     return -1;
797 }
798 //-------------------------------------------------------------------------------------------------
799 /// Get thread ID of current thread/process in Linux(Kernel)
800 /// @return : current thread ID
801 //-------------------------------------------------------------------------------------------------
MsOS_GetOSThreadID(void)802 MS_S32 MsOS_GetOSThreadID (void)
803 {
804     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
805     return MsOS_InfoTaskID();
806 }
807 
808 
809 //
810 // Mutex
811 //
812 //-------------------------------------------------------------------------------------------------
813 /// Create a mutex in the unlocked state
814 /// @param  eAttribute  \b IN: E_MSOS_FIFO: suspended in FIFO order
815 /// @param  pMutexName  \b IN: mutex name
816 /// @return >=0 : assigned mutex Id
817 /// @return <0 : fail
818 /// @note   A mutex has the concept of an owner, whereas a semaphore does not.
819 ///         A mutex provides priority inheritance protocol against proiorty inversion, whereas a binary semaphore does not.
820 //-------------------------------------------------------------------------------------------------
MsOS_CreateMutex(MsOSAttribute eAttribute,char * pMutexName1,MS_U32 u32Flag)821 MS_S32 MsOS_CreateMutex ( MsOSAttribute eAttribute, char *pMutexName1, MS_U32 u32Flag)
822 {
823     MS_S32 s32Id, s32LstUnused = MSOS_MUTEX_MAX;
824     pthread_mutexattr_t _MsOS_Mutex_Attr;
825     int s32Prop = (MSOS_PROCESS_PRIVATE == u32Flag)? PTHREAD_PROCESS_PRIVATE: PTHREAD_PROCESS_SHARED;
826     MS_U8 pMutexName[MAX_MUTEX_NAME_LENGTH];
827     MS_U32 u32MaxLen;
828 
829     if (NULL == pMutexName1)
830     {
831         return -1L;
832     }
833     if (strlen(pMutexName1) >= (MAX_MUTEX_NAME_LENGTH-1))
834     {
835         MSOS_PRINT("%s: Warning strlen(%s) is longer than MAX_MUTEX_NAME_LENGTH(%d). Oversize char will be discard.\n",
836         __FUNCTION__,pMutexName1,MAX_MUTEX_NAME_LENGTH);
837     }
838     if (0 == (u32MaxLen = MIN(strlen(pMutexName1), (MAX_MUTEX_NAME_LENGTH-1))))
839     {
840         return -1L;
841     }
842     strncpy((char*)pMutexName, (const char*)pMutexName1, u32MaxLen);
843     pMutexName[u32MaxLen] = '\0';
844 
845     MUTEX_MUTEX_LOCK();
846 
847     for(s32Id=0;s32Id<MSOS_MUTEX_MAX;s32Id++)
848     {
849         // if (PTHREAD_PROCESS_SHARED == s32Prop) // @FIXME: Richard: is the mutex name always used as an id, regardless of process shared/private property?
850         {
851             if(TRUE == _MsOS_Mutex_Info[s32Id].bUsed)
852             {
853                 if (0== strcmp((const char*)_MsOS_Mutex_Info[s32Id].u8Name, (const char*)pMutexName))
854                 {
855                     break;
856                 }
857             }
858         }
859         if (FALSE==_MsOS_Mutex_Info[s32Id].bUsed  && MSOS_MUTEX_MAX==s32LstUnused)
860         {
861             s32LstUnused = s32Id;
862         }
863     }
864 
865     if ((MSOS_MUTEX_MAX==s32Id) && (MSOS_MUTEX_MAX>s32LstUnused))
866     {
867 
868         pthread_mutexattr_init(&_MsOS_Mutex_Attr);
869         pthread_mutexattr_setpshared(&_MsOS_Mutex_Attr, s32Prop);
870        //PTH_RET_CHK(pthread_mutexattr_setprotocol(&_MsOS_Mutex_Attr, PTHREAD_PRIO_INHERIT));
871      #ifdef MS_DEBUG
872         pthread_mutexattr_settype(&_MsOS_Mutex_Attr, PTHREAD_MUTEX_ERRORCHECK);
873      #endif
874         _MsOS_Mutex_Info[s32LstUnused].bUsed = TRUE;
875         strncpy((char*)_MsOS_Mutex_Info[s32LstUnused].u8Name, (const char*)pMutexName,MIN(strlen((const char*)pMutexName),(MAX_MUTEX_NAME_LENGTH-1)));
876         pthread_mutex_init(&_MsOS_Mutex_Info[s32LstUnused].stMutex, &_MsOS_Mutex_Attr);
877         s32Id = s32LstUnused;
878 
879     }
880 
881     MUTEX_MUTEX_UNLOCK();
882 
883     if(MSOS_MUTEX_MAX <= s32Id)
884     {
885         return -1L;
886     }
887 
888     s32Id |= MSOS_ID_PREFIX;
889 
890     return s32Id;
891 }
892 
893 //-------------------------------------------------------------------------------------------------
894 /// Delete the specified mutex
895 /// @param  s32MutexId  \b IN: mutex ID
896 /// @return TRUE : succeed
897 /// @return FALSE : fail
898 /// @note   It is important that the mutex be in the unlocked state when it is
899 ///            destroyed, or else the behavior is undefined.
900 //-------------------------------------------------------------------------------------------------
MsOS_DeleteMutex(MS_S32 s32MutexId)901 MS_BOOL MsOS_DeleteMutex (MS_S32 s32MutexId)
902 {
903 
904     if ( (s32MutexId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
905     {
906         return FALSE;
907     }
908     else
909     {
910         s32MutexId &= MSOS_ID_MASK;
911     }
912 
913 
914     if( (_MsOS_Mutex_Info[s32MutexId].bUsed == FALSE))
915     {
916         MSOS_PRINT("MUTEX WITH MUTEX_ID:0x%lx NOT EXIST\n",s32MutexId );
917         return FALSE;
918     }
919 
920 
921     MUTEX_MUTEX_LOCK();
922 
923     MS_ASSERT(_MsOS_Mutex_Info[s32MutexId].bUsed);
924     pthread_mutex_destroy(&_MsOS_Mutex_Info[s32MutexId].stMutex);
925     _MsOS_Mutex_Info[s32MutexId].bUsed = FALSE;
926     memset(_MsOS_Mutex_Info[s32MutexId].u8Name,0x00,sizeof(_MsOS_Mutex_Info[s32MutexId].u8Name));
927 
928     MUTEX_MUTEX_UNLOCK();
929 
930     return TRUE;
931 }
932 
933 //-------------------------------------------------------------------------------------------------
934 /// Attempt to lock a mutex
935 /// @param  s32MutexId  \b IN: mutex ID
936 /// @param  u32WaitMs   \b IN: 0 ~ MSOS_WAIT_FOREVER: suspend time (ms) if the mutex is locked
937 /// @return TRUE : succeed
938 /// @return FALSE : fail
939 //-------------------------------------------------------------------------------------------------
MsOS_ObtainMutex(MS_S32 s32MutexId,MS_U32 u32WaitMs)940 MS_BOOL MsOS_ObtainMutex (MS_S32 s32MutexId, MS_U32 u32WaitMs)
941 {
942     MS_BOOL bRet = FALSE;
943 
944 	u32WaitMs=MSOS_WAIT_FOREVER;
945 
946     if ( (s32MutexId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
947     {
948         return FALSE;
949     }
950     else
951     {
952         s32MutexId &= MSOS_ID_MASK;
953     }
954 
955     if (u32WaitMs==MSOS_WAIT_FOREVER) //blocking wait
956     {
957         if(pthread_mutex_lock(&(_MsOS_Mutex_Info[s32MutexId].stMutex)))
958         {
959             MSOS_PRINT("Mutex Name: %s\n", _MsOS_Mutex_Info[s32MutexId].u8Name);
960 			bRet = FALSE;
961         }
962 		else
963         {
964             bRet = TRUE;
965         }
966     }
967     else
968     {
969         if (pthread_mutex_trylock(&_MsOS_Mutex_Info[s32MutexId].stMutex))
970         {
971             MSOS_PRINT("Mutex Name: %s\n", _MsOS_Mutex_Info[s32MutexId].u8Name);
972             bRet = FALSE;
973         }
974 		else
975         {
976             bRet = TRUE;
977         }
978     }
979 
980 	return bRet;
981 }
982 
983 //-------------------------------------------------------------------------------------------------
984 /// Attempt to unlock a mutex
985 /// @param  s32MutexId  \b IN: mutex ID
986 /// @return TRUE : succeed
987 /// @return FALSE : fail
988 /// @note   Only the owner thread of the mutex can unlock it.
989 //-------------------------------------------------------------------------------------------------
MsOS_ReleaseMutex(MS_S32 s32MutexId)990 MS_BOOL MsOS_ReleaseMutex (MS_S32 s32MutexId)
991 {
992 	if ( (s32MutexId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
993     {
994 	    return FALSE;
995     }
996     else
997     {
998         s32MutexId &= MSOS_ID_MASK;
999     }
1000 
1001     if(pthread_mutex_unlock(&_MsOS_Mutex_Info[s32MutexId].stMutex))
1002     {
1003         MSOS_PRINT("Mutex Name: %s\n", _MsOS_Mutex_Info[s32MutexId].u8Name);
1004 		return FALSE;
1005     }
1006 
1007 	return TRUE;
1008 }
1009 
1010 //-------------------------------------------------------------------------------------------------
1011 // Get a mutex informaton
1012 // @param  s32MutexId  \b IN: mutex ID
1013 // @param  peAttribute \b OUT: ptr to suspended mode: E_MSOS_FIFO / E_MSOS_PRIORITY
1014 // @param  pMutexName  \b OUT: ptr to mutex name
1015 // @return TRUE : succeed
1016 // @return FALSE : the mutex has not been created.
1017 //-------------------------------------------------------------------------------------------------
MsOS_InfoMutex(MS_S32 s32MutexId,MsOSAttribute * peAttribute,char * pMutexName)1018 MS_BOOL MsOS_InfoMutex (MS_S32 s32MutexId, MsOSAttribute *peAttribute, char *pMutexName)
1019 {
1020     if ( (s32MutexId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
1021     {
1022         return FALSE;
1023     }
1024     else
1025     {
1026         s32MutexId &= MSOS_ID_MASK;
1027     }
1028 
1029     if(_MsOS_Mutex_Info[s32MutexId].bUsed == TRUE)
1030     {
1031         //ToDo: extend _MsOS_Mutex_Info structure ?
1032         *peAttribute = E_MSOS_FIFO; //only FIFO for eCos
1033         strncpy(pMutexName, (const char*)_MsOS_Mutex_Info[s32MutexId].u8Name,MIN(strlen((const char*)_MsOS_Mutex_Info[s32MutexId].u8Name),(MAX_MUTEX_NAME_LENGTH-1)));
1034         return TRUE;
1035     }
1036     else
1037     {
1038         return FALSE;
1039     }
1040 
1041 }
1042 
1043 //
1044 // Semaphore
1045 //
1046 //-------------------------------------------------------------------------------------------------
1047 /// Create a semaphore
1048 /// @param  u32InitCnt \b IN: initial semaphore value
1049 /// @param  eAttribute \b IN: E_MSOS_FIFO suspended in FIFO order
1050 /// @param  pSemaphoreName \b IN: semaphore name
1051 /// @return >=0 : assigned Semaphore Id
1052 /// @return <0 : fail
1053 /// @note   A semaphore does not have the concept of an owner; it is possible for one thread to lock a
1054 ///           binary semaphore and another thread to unlock it.
1055 //-------------------------------------------------------------------------------------------------
MsOS_CreateSemaphore(MS_U32 u32InitCnt,MsOSAttribute eAttribute,char * pSemaphoreName)1056 MS_S32 MsOS_CreateSemaphore (MS_U32 u32InitCnt,
1057                           MsOSAttribute eAttribute,
1058                           char *pSemaphoreName)
1059 {
1060     MS_S32 s32Id;
1061 
1062     MUTEX_SEMEPHORE_LOCK();
1063 
1064     for(s32Id=0;s32Id<MSOS_SEMAPHORE_MAX;s32Id++)
1065     {
1066         if(_MsOS_Semaphore_Info[s32Id].bUsed == FALSE)
1067         {
1068             break;
1069         }
1070     }
1071     if(s32Id < MSOS_SEMAPHORE_MAX)
1072     {
1073         _MsOS_Semaphore_Info[s32Id].bUsed = TRUE;
1074     }
1075 
1076     MUTEX_SEMEPHORE_UNLOCK();
1077 
1078     if(s32Id >= MSOS_SEMAPHORE_MAX)
1079     {
1080         return -1;
1081     }
1082 
1083     sem_init(&_MsOS_Semaphore_Info[s32Id].stSemaphore, 0, u32InitCnt);
1084     s32Id |= MSOS_ID_PREFIX;
1085 
1086     return s32Id;
1087 }
1088 
1089 //-------------------------------------------------------------------------------------------------
1090 /// Delete the specified semaphore
1091 /// @param  s32SemaphoreId  \b IN: semaphore ID
1092 /// @return TRUE : succeed
1093 /// @return FALSE : fail
1094 /// @note   It is important that there are not any threads waiting on the semaphore
1095 ///             when this function is called or the behavior is undefined.
1096 //-------------------------------------------------------------------------------------------------
MsOS_DeleteSemaphore(MS_S32 s32SemaphoreId)1097 MS_BOOL MsOS_DeleteSemaphore (MS_S32 s32SemaphoreId)
1098 {
1099     if ( (s32SemaphoreId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
1100     {
1101         return FALSE;
1102     }
1103     else
1104     {
1105         s32SemaphoreId &= MSOS_ID_MASK;
1106     }
1107 
1108     if(_MsOS_Semaphore_Info[s32SemaphoreId].bUsed == FALSE )
1109     {
1110         MSOS_PRINT("SEMAPHORE WITH SEMAPHORE_ID:0x%lx NOT EXIST\n",s32SemaphoreId );
1111         return FALSE;
1112     }
1113 
1114     sem_destroy(&_MsOS_Semaphore_Info[s32SemaphoreId].stSemaphore);
1115 
1116     MUTEX_SEMEPHORE_LOCK();
1117     _MsOS_Semaphore_Info[s32SemaphoreId].bUsed = FALSE;
1118     MUTEX_SEMEPHORE_UNLOCK();
1119     return TRUE;
1120 
1121     return FALSE;
1122 }
1123 
1124 //-------------------------------------------------------------------------------------------------
1125 /// Attempt to decrement a semaphore count
1126 /// @param  s32SemaphoreId  \b IN: semaphore ID
1127 /// @param  u32WaitMs       \b IN: 0 ~ MSOS_WAIT_FOREVER: suspend time (ms) if the semaphore count = 0
1128 /// @return TRUE : succeed
1129 /// @return FALSE : fail
1130 //-------------------------------------------------------------------------------------------------
MsOS_ObtainSemaphore(MS_S32 s32SemaphoreId,MS_U32 u32WaitMs)1131 MS_BOOL MsOS_ObtainSemaphore (MS_S32 s32SemaphoreId, MS_U32 u32WaitMs)
1132 {
1133     MS_BOOL bRet = FALSE;
1134 
1135     u32WaitMs=MSOS_WAIT_FOREVER;
1136 
1137     if ( (s32SemaphoreId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
1138     {
1139         return FALSE;
1140     }
1141     else
1142     {
1143         s32SemaphoreId &= MSOS_ID_MASK;
1144     }
1145 
1146     if (u32WaitMs==MSOS_WAIT_FOREVER) //blocking wait
1147     {
1148         bRet = sem_wait(&_MsOS_Semaphore_Info[s32SemaphoreId].stSemaphore)  >=0 ? TRUE : FALSE;
1149     }
1150 
1151     return bRet;
1152 }
1153 
1154 //-------------------------------------------------------------------------------------------------
1155 /// Increase a semaphore count
1156 /// @param  s32SemaphoreId  \b IN: semaphore ID
1157 /// @return TRUE : succeed
1158 /// @return FALSE : fail
1159 /// @note   It's possible for any thread to increase the semaphore count
1160 //-------------------------------------------------------------------------------------------------
MsOS_ReleaseSemaphore(MS_S32 s32SemaphoreId)1161 MS_BOOL MsOS_ReleaseSemaphore (MS_S32 s32SemaphoreId)
1162 {
1163     if ( (s32SemaphoreId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
1164     {
1165         return FALSE;
1166     }
1167     else
1168     {
1169         s32SemaphoreId &= MSOS_ID_MASK;
1170     }
1171 
1172     return sem_post(&_MsOS_Semaphore_Info[s32SemaphoreId].stSemaphore) >= 0 ? TRUE : FALSE;
1173 
1174 }
1175 
1176 //-------------------------------------------------------------------------------------------------
1177 // Get a semaphore informaton
1178 // @param  s32SemaphoreId  \b IN: semaphore ID
1179 // @param  pu32InitCnt     \b OUT: ptr to initial semaphore value
1180 // @param  peAttribute     \b OUT: ptr to suspended mode: E_MSOS_FIFO / E_MSOS_PRIORITY
1181 // @param  pSemaphoreName  \b OUT: ptr to semaphore name
1182 // @return TRUE : succeed
1183 // @return FALSE : the semaphore has not been created.
1184 //-------------------------------------------------------------------------------------------------
MsOS_InfoSemaphore(MS_S32 s32SemaphoreId,MS_U32 * pu32InitCnt,MsOSAttribute * peAttribute,char * pSemaphoreName)1185 MS_BOOL MsOS_InfoSemaphore (MS_S32 s32SemaphoreId, MS_U32 *pu32InitCnt, MsOSAttribute *peAttribute, char *pSemaphoreName)
1186 {
1187     if ( (s32SemaphoreId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
1188     {
1189         return FALSE;
1190     }
1191     else
1192     {
1193         s32SemaphoreId &= MSOS_ID_MASK;
1194     }
1195 
1196     return sem_getvalue(&_MsOS_Semaphore_Info[s32SemaphoreId].stSemaphore,(int *)pu32InitCnt) >= 0 ? TRUE : FALSE;
1197 }
1198 
1199 
1200 //
1201 // Event management
1202 //
1203 //-------------------------------------------------------------------------------------------------
1204 /// Create an event group
1205 /// @param  pEventName  \b IN: event group name
1206 /// @return >=0 : assigned Event Id
1207 /// @return <0 : fail
1208 //-------------------------------------------------------------------------------------------------
MsOS_CreateEventGroup(char * pEventName)1209 MS_S32 MsOS_CreateEventGroup (char *pEventName)
1210 {
1211     MS_S32 s32Id;
1212     pthread_mutexattr_t _MsOS_Mutex_Attr;
1213 
1214     pthread_mutexattr_init(&_MsOS_Mutex_Attr);
1215 #ifdef MS_DEBUG
1216     pthread_mutexattr_settype(&_MsOS_Mutex_Attr, PTHREAD_MUTEX_ERRORCHECK);
1217 #endif
1218 
1219     pthread_mutex_lock(&_MsOS_EventGroup_Mutex);
1220     for(s32Id=0; s32Id<MSOS_EVENTGROUP_MAX; s32Id++)
1221     {
1222         if(_MsOS_EventGroup_Info[s32Id].bUsed == FALSE)
1223         {
1224             break;
1225         }
1226     }
1227 
1228     if(s32Id < MSOS_EVENTGROUP_MAX)
1229     {
1230         pthread_mutex_init(&_MsOS_EventGroup_Info[s32Id].stMutexEvent, &_MsOS_Mutex_Attr);
1231         pthread_mutex_lock(&_MsOS_EventGroup_Info[s32Id].stMutexEvent);
1232         _MsOS_EventGroup_Info[s32Id].bUsed = TRUE;
1233         _MsOS_EventGroup_Info[s32Id].u32EventGroup= 0;
1234         pthread_mutex_unlock(&_MsOS_EventGroup_Info[s32Id].stMutexEvent);
1235     }
1236     pthread_mutex_unlock(&_MsOS_EventGroup_Mutex);
1237 
1238     if(s32Id >= MSOS_EVENTGROUP_MAX)
1239     {
1240         return -1;
1241     }
1242 
1243     pthread_mutex_init(&_MsOS_EventGroup_Info[s32Id].stMutex, &_MsOS_Mutex_Attr);
1244     pthread_cond_init(&_MsOS_EventGroup_Info[s32Id].stSemaphore, NULL);
1245 
1246     s32Id |= MSOS_ID_PREFIX;
1247     return s32Id;
1248     return -1;
1249 }
1250 
1251 //-------------------------------------------------------------------------------------------------
1252 /// Delete the event group
1253 /// @param  s32EventGroupId \b IN: event group ID
1254 /// @return TRUE : succeed
1255 /// @return FALSE : fail, sb is waiting for the event flag
1256 /// @note event group that are being waited on must not be deleted
1257 //-------------------------------------------------------------------------------------------------
MsOS_DeleteEventGroup(MS_S32 s32EventGroupId)1258 MS_BOOL MsOS_DeleteEventGroup (MS_S32 s32EventGroupId)
1259 {
1260     if ( (s32EventGroupId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
1261     {
1262         return FALSE;
1263     }
1264     else
1265     {
1266         s32EventGroupId &= MSOS_ID_MASK;
1267     }
1268 
1269     #if 1
1270     if( _MsOS_EventGroup_Info[s32EventGroupId].bUsed == FALSE)
1271     {
1272         MSOS_PRINT("EVENTGROUP WITH EVENTGROUP:0x%lx NOT EXIST\n",s32EventGroupId);
1273         return FALSE;
1274     }
1275     #endif
1276 
1277     pthread_cond_destroy(&_MsOS_EventGroup_Info[s32EventGroupId].stSemaphore);
1278     pthread_mutex_destroy(&_MsOS_EventGroup_Info[s32EventGroupId].stMutex);
1279     pthread_mutex_lock(&_MsOS_EventGroup_Mutex);
1280     pthread_mutex_lock(&_MsOS_EventGroup_Info[s32EventGroupId].stMutexEvent);
1281     _MsOS_EventGroup_Info[s32EventGroupId].u32EventGroup= 0;
1282     pthread_mutex_unlock(&_MsOS_EventGroup_Info[s32EventGroupId].stMutexEvent);
1283     _MsOS_EventGroup_Info[s32EventGroupId].bUsed = FALSE;
1284     pthread_mutex_unlock(&_MsOS_EventGroup_Mutex);
1285     pthread_mutex_destroy(&_MsOS_EventGroup_Info[s32EventGroupId].stMutexEvent);
1286     return TRUE;
1287 }
1288 
1289 //-------------------------------------------------------------------------------------------------
1290 /// Set the event flag (bitwise OR w/ current value) in the specified event group
1291 /// @param  s32EventGroupId \b IN: event group ID
1292 /// @param  u32EventFlag    \b IN: event flag value
1293 /// @return TRUE : succeed
1294 /// @return FALSE : fail
1295 //-------------------------------------------------------------------------------------------------
MsOS_SetEvent(MS_S32 s32EventGroupId,MS_U32 u32EventFlag)1296 MS_BOOL MsOS_SetEvent (MS_S32 s32EventGroupId, MS_U32 u32EventFlag)
1297 {
1298 
1299     if ( (s32EventGroupId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
1300     {
1301         return FALSE;
1302     }
1303     else
1304     {
1305         s32EventGroupId &= MSOS_ID_MASK;
1306     }
1307 
1308     if(up_interrupt_context())
1309     {
1310         SET_FLAG(_MsOS_EventGroup_Info[s32EventGroupId].u32EventGroup, u32EventFlag);
1311         pthread_cond_broadcast(&_MsOS_EventGroup_Info[s32EventGroupId].stSemaphore);
1312     }
1313     else
1314     {
1315         MDrv_IRQ_MaskAll();
1316         pthread_mutex_lock(&_MsOS_EventGroup_Info[s32EventGroupId].stMutexEvent);
1317         SET_FLAG(_MsOS_EventGroup_Info[s32EventGroupId].u32EventGroup, u32EventFlag);
1318         pthread_mutex_unlock(&_MsOS_EventGroup_Info[s32EventGroupId].stMutexEvent);
1319         pthread_cond_broadcast(&_MsOS_EventGroup_Info[s32EventGroupId].stSemaphore);
1320         MDrv_IRQ_UnMaskAll();
1321     }
1322     return TRUE;
1323 }
1324 
1325 //-------------------------------------------------------------------------------------------------
1326 /// Clear the specified event flag (bitwise XOR operation) in the specified event group
1327 /// @param  s32EventGroupId \b IN: event group ID
1328 /// @param  u32EventFlag    \b IN: event flag value
1329 /// @returnTRUE : succeed
1330 /// @return FALSE : fail
1331 //-------------------------------------------------------------------------------------------------
MsOS_ClearEvent(MS_S32 s32EventGroupId,MS_U32 u32EventFlag)1332 MS_BOOL MsOS_ClearEvent (MS_S32 s32EventGroupId, MS_U32 u32EventFlag)
1333 {
1334     if ( (s32EventGroupId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
1335     {
1336         return FALSE;
1337     }
1338     else
1339     {
1340         s32EventGroupId &= MSOS_ID_MASK;
1341     }
1342 
1343     if(up_interrupt_context())
1344     {
1345         RESET_FLAG(_MsOS_EventGroup_Info[s32EventGroupId].u32EventGroup, u32EventFlag);
1346     }
1347     else
1348     {
1349         MDrv_IRQ_MaskAll();
1350         pthread_mutex_lock(&_MsOS_EventGroup_Info[s32EventGroupId].stMutexEvent);
1351         RESET_FLAG(_MsOS_EventGroup_Info[s32EventGroupId].u32EventGroup, u32EventFlag);
1352         pthread_mutex_unlock(&_MsOS_EventGroup_Info[s32EventGroupId].stMutexEvent);
1353         MDrv_IRQ_UnMaskAll();
1354     }
1355     return TRUE;
1356 }
1357 
1358 //-------------------------------------------------------------------------------------------------
1359 /// Wait for the specified event flag combination from the event group
1360 /// @param  s32EventGroupId     \b IN: event group ID
1361 /// @param  u32WaitEventFlag    \b IN: wait event flag value
1362 /// @param  pu32RetrievedEventFlag \b OUT: retrieved event flag value
1363 /// @param  eWaitMode           \b IN: E_AND/E_OR/E_AND_CLEAR/E_OR_CLEAR
1364 /// @param  u32WaitMs           \b IN: 0 ~ MSOS_WAIT_FOREVER: suspend time (ms) if the event is not ready
1365 /// @return TRUE : succeed
1366 /// @return FALSE : fail
1367 //-------------------------------------------------------------------------------------------------
MsOS_WaitEvent(MS_S32 s32EventGroupId,MS_U32 u32WaitEventFlag,MS_U32 * pu32RetrievedEventFlag,EventWaitMode eWaitMode,MS_U32 u32WaitMs)1368 MS_BOOL MsOS_WaitEvent (MS_S32     s32EventGroupId,
1369                      MS_U32     u32WaitEventFlag,
1370                      MS_U32     *pu32RetrievedEventFlag,
1371                      EventWaitMode eWaitMode,
1372                      MS_U32     u32WaitMs)
1373 {
1374     MS_BOOL bRet= FALSE;
1375     struct timespec StopTime;
1376     MS_BOOL bAnd;
1377     MS_BOOL bClear;
1378 
1379     *pu32RetrievedEventFlag = 0;
1380 
1381     if (!u32WaitEventFlag)
1382     {
1383         return FALSE;
1384     }
1385 
1386     if ( (s32EventGroupId & MSOS_ID_PREFIX_MASK) != MSOS_ID_PREFIX )
1387     {
1388         return FALSE;
1389     }
1390     else
1391     {
1392         s32EventGroupId &= MSOS_ID_MASK;
1393     }
1394 
1395     bClear= ((E_AND_CLEAR== eWaitMode) || (E_OR_CLEAR== eWaitMode))? TRUE: FALSE;
1396     bAnd= ((E_AND== eWaitMode)|| (E_AND_CLEAR== eWaitMode))? TRUE: FALSE;
1397 
1398     if (u32WaitMs!= MSOS_WAIT_FOREVER) //blocking wait
1399     {
1400         _TimeAbs(StopTime, u32WaitMs);
1401     }
1402 
1403     pthread_mutex_lock(&_MsOS_EventGroup_Info[s32EventGroupId].stMutex);
1404     do{
1405         *pu32RetrievedEventFlag= HAS_FLAG(_MsOS_EventGroup_Info[s32EventGroupId].u32EventGroup, u32WaitEventFlag);
1406         if ((bAnd)? (*pu32RetrievedEventFlag== u32WaitEventFlag): (0!= *pu32RetrievedEventFlag))
1407         {
1408             break;
1409         }
1410         if (u32WaitMs== MSOS_WAIT_FOREVER) //blocking wait
1411         {
1412             pthread_cond_wait(&_MsOS_EventGroup_Info[s32EventGroupId].stSemaphore,
1413                                           &_MsOS_EventGroup_Info[s32EventGroupId].stMutex);
1414         }
1415         else
1416         {
1417             if (pthread_cond_timedwait(&_MsOS_EventGroup_Info[s32EventGroupId].stSemaphore,
1418                                                    &_MsOS_EventGroup_Info[s32EventGroupId].stMutex,
1419                                                    &StopTime))
1420             {
1421                 break;
1422             }
1423         }
1424     } while (1);
1425 
1426     pthread_mutex_unlock(&_MsOS_EventGroup_Info[s32EventGroupId].stMutex);
1427     pthread_mutex_lock(&_MsOS_EventGroup_Info[s32EventGroupId].stMutexEvent);
1428     bRet= (bAnd)? (*pu32RetrievedEventFlag== u32WaitEventFlag): (0!= *pu32RetrievedEventFlag);
1429     if (bRet && bClear)
1430     {
1431         RESET_FLAG(_MsOS_EventGroup_Info[s32EventGroupId].u32EventGroup, *pu32RetrievedEventFlag);
1432     }
1433     pthread_mutex_unlock(&_MsOS_EventGroup_Info[s32EventGroupId].stMutexEvent);
1434     return bRet;
1435 }
1436 
1437 //
1438 // Timer management
1439 //
1440 //-------------------------------------------------------------------------------------------------
1441 /// Create a Timer
1442 /// @param  pTimerCb        \b IN: timer callback function
1443 /// @param  u32FirstTimeMs  \b IN: first ms for timer expiration
1444 /// @param  u32PeriodTimeMs \b IN: periodic ms for timer expiration after first expiration
1445 ///                                0: one shot timer
1446 /// @param  bStartTimer     \b IN: TRUE: activates the timer after it is created
1447 ///                                FALSE: leaves the timer disabled after it is created
1448 /// @param  pTimerName      \b IN: Timer name (not used by eCos)
1449 /// @return >=0 : assigned Timer ID
1450 ///         <0 : fail
1451 //-------------------------------------------------------------------------------------------------
MsOS_CreateTimer(TimerCb pTimerCb,MS_U32 u32FirstTimeMs,MS_U32 u32PeriodTimeMs,MS_BOOL bStartTimer,char * pTimerName)1452 MS_S32 MsOS_CreateTimer (TimerCb   pTimerCb,
1453                          MS_U32    u32FirstTimeMs,
1454                          MS_U32    u32PeriodTimeMs,
1455                          MS_BOOL   bStartTimer,
1456                          char      *pTimerName)
1457 {
1458     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1459     return TRUE;
1460 }
1461 
1462 //-------------------------------------------------------------------------------------------------
1463 /// Delete the Timer
1464 /// @param  s32TimerId  \b IN: Timer ID
1465 /// @return TRUE : succeed
1466 /// @return FALSE : fail
1467 //-------------------------------------------------------------------------------------------------
MsOS_DeleteTimer(MS_S32 s32TimerId)1468 MS_BOOL MsOS_DeleteTimer (MS_S32 s32TimerId)
1469 {
1470     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1471     return TRUE;
1472 }
1473 
1474 //-------------------------------------------------------------------------------------------------
1475 /// Start the Timer
1476 /// @param  s32TimerId  \b IN: Timer ID
1477 /// @return TRUE : succeed
1478 /// @return FALSE : fail
1479 //-------------------------------------------------------------------------------------------------
MsOS_StartTimer(MS_S32 s32TimerId)1480 MS_BOOL MsOS_StartTimer (MS_S32 s32TimerId)
1481 {
1482     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1483     return TRUE;
1484 }
1485 
1486 //-------------------------------------------------------------------------------------------------
1487 /// Stop the Timer
1488 /// @param  s32TimerId  \b IN: Timer ID
1489 /// @return TRUE : succeed
1490 /// @return FALSE : fail
1491 /// @note   MsOS_StopTimer then MsOS_StartTimer => The timer will trigger at the same relative
1492 ///             intervals that it would have if it had not been disabled.
1493 //-------------------------------------------------------------------------------------------------
MsOS_StopTimer(MS_S32 s32TimerId)1494 MS_BOOL MsOS_StopTimer (MS_S32 s32TimerId)
1495 {
1496     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1497     return TRUE;
1498 }
1499 
1500 //-------------------------------------------------------------------------------------------------
1501 /// Reset a Timer & reset the expiration periods
1502 /// @param  s32TimerId      \b IN: Timer ID
1503 /// @param  u32FirstTimeMs  \b IN: first ms for timer expiration
1504 /// @param  u32PeriodTimeMs \b IN: periodic ms for timer expiration after first expiration
1505 ///                                0: one shot timer
1506 /// @param  bStartTimer     \b IN: TRUE: activates the timer after it is created
1507 ///                                FALSE: leaves the timer disabled after it is created
1508 /// @return TRUE : succeed
1509 /// @return FALSE : fail
1510 //-------------------------------------------------------------------------------------------------
MsOS_ResetTimer(MS_S32 s32TimerId,MS_U32 u32FirstTimeMs,MS_U32 u32PeriodTimeMs,MS_BOOL bStartTimer)1511 MS_BOOL MsOS_ResetTimer (MS_S32     s32TimerId,
1512                          MS_U32     u32FirstTimeMs,
1513                          MS_U32     u32PeriodTimeMs,
1514                          MS_BOOL    bStartTimer)
1515 {
1516     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1517     return TRUE;
1518 }
1519 
1520 
1521 //
1522 // System time
1523 //
1524 //-------------------------------------------------------------------------------------------------
1525 /// Get current system time in ms
1526 /// @return system time in ms
1527 //-------------------------------------------------------------------------------------------------
MsOS_GetSystemTime(void)1528 MS_U32 MsOS_GetSystemTime (void) //   1ms/systime
1529 {
1530     return g_system_timer;
1531 }
1532 //-------------------------------------------------------------------------------------------------
1533 ///[OBSOLETE]
1534 /// Time difference between current time and task time
1535 /// @return system time diff in ms
1536 //-------------------------------------------------------------------------------------------------
MsOS_Timer_DiffTimeFromNow(MS_U32 u32TaskTimer)1537 MS_U32 MsOS_Timer_DiffTimeFromNow(MS_U32 u32TaskTimer) //unit = ms
1538 {
1539     return (MsOS_GetSystemTime() - u32TaskTimer);
1540 }
1541 //-------------------------------------------------------------------------------------------------
1542 ///[OBSOLETE]
1543 /// Time difference between setting time and task time
1544 /// @return system time diff in ms
1545 //-------------------------------------------------------------------------------------------------
MsOS_Timer_DiffTime(MS_U32 u32Timer,MS_U32 u32TaskTimer)1546 MS_U32 MsOS_Timer_DiffTime(MS_U32 u32Timer, MS_U32 u32TaskTimer) //unit = ms
1547 {
1548     // simple minus is enough, because max(timer_counter) == max(u32)
1549     return (u32Timer - u32TaskTimer);
1550 }
1551 //
1552 // Queue
1553 //
1554 //-------------------------------------------------------------------------------------------------
1555 /// Create a Queue
1556 /// @param  pStartAddr      \b IN: It is useless now, can pass NULL.
1557 /// @param  u32QueueSize    \b IN: queue size (byte unit) : now fixed as
1558 ///                                CYGNUM_KERNEL_SYNCH_MBOX_QUEUE_SIZE * u32MessageSize
1559 /// @param  eMessageType    \b IN: E_MSG_FIXED_SIZE / E_MSG_VAR_SIZE
1560 /// @param  u32MessageSize  \b IN: message size (byte unit) for E_MSG_FIXED_SIZE
1561 ///                                max message size (byte unit) for E_MSG_VAR_SIZE
1562 /// @param  eAttribute      \b IN: E_MSOS_FIFO suspended in FIFO order
1563 /// @param  pQueueName      \b IN: queue name
1564 /// @return assigned message queue ID
1565 /// @return < 0 - fail
1566 //-------------------------------------------------------------------------------------------------
MsOS_CreateQueue(void * pStartAddr,MS_U32 u32QueueSize,MessageType eMessageType,MS_U32 u32MessageSize,MsOSAttribute eAttribute,char * pQueueName)1567 MS_S32 MsOS_CreateQueue (void         *pStartAddr,
1568                          MS_U32           u32QueueSize,
1569                       MessageType   eMessageType,
1570                          MS_U32           u32MessageSize,
1571                       MsOSAttribute eAttribute,
1572                       char         *pQueueName)
1573 {
1574     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1575     return -1;
1576 }
1577 
1578 //-------------------------------------------------------------------------------------------------
1579 /// Delete the Queue
1580 /// @param  s32QueueId  \b IN: Queue ID
1581 /// @return TRUE : succeed
1582 /// @return FALSE :  fail
1583 /// @note   It is important that there are not any threads blocked on the queue
1584 ///             when this function is called or the behavior is undefined.
1585 //-------------------------------------------------------------------------------------------------
MsOS_DeleteQueue(MS_S32 s32QueueId)1586 MS_BOOL MsOS_DeleteQueue (MS_S32 s32QueueId)
1587 {
1588     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1589     return FALSE;
1590 }
1591 
1592 //-------------------------------------------------------------------------------------------------
1593 /// Send a message to the end of the specified queue
1594 /// @param  s32QueueId  \b IN: Queue ID
1595 /// @param  pu8Message  \b IN: ptr to msg to send. NULL ptr is not allowed
1596 /// @param  u32Size     \b IN: msg size (byte)
1597 /// @param  u32WaitMs   \b IN: 0 ~ MSOS_WAIT_FOREVER: suspend time (ms) if the queue is full
1598 /// @return TRUE : succeed
1599 /// @return FALSE :  fail
1600 //-------------------------------------------------------------------------------------------------
MsOS_SendToQueue(MS_S32 s32QueueId,MS_U8 * pu8Message,MS_U32 u32Size,MS_U32 u32WaitMs)1601 MS_BOOL MsOS_SendToQueue (MS_S32 s32QueueId, MS_U8 *pu8Message, MS_U32 u32Size, MS_U32 u32WaitMs)
1602 {
1603     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1604     return FALSE;
1605 }
1606 
1607 //-------------------------------------------------------------------------------------------------
1608 /// Receive a message from the specified queue
1609 /// @param  s32QueueId      \b IN: Queue ID
1610 /// @param  pu8Message      \b OUT: msg destination
1611 /// @param  u32IntendedSize \b IN: intended msg size (byte unit) to receive:
1612 /// @param  pu32ActualSize  \b OUT: actual msg size (byte unit) received
1613 /// @param  u32WaitMs       \b IN: 0 ~ MSOS_WAIT_FOREVER: suspend time (ms) if the queue is empty
1614 /// @return TRUE : succeed
1615 /// @return FALSE :  fail
1616 //-------------------------------------------------------------------------------------------------
MsOS_RecvFromQueue(MS_S32 s32QueueId,MS_U8 * pu8Message,MS_U32 u32IntendedSize,MS_U32 * pu32ActualSize,MS_U32 u32WaitMs)1617 MS_BOOL MsOS_RecvFromQueue (MS_S32 s32QueueId, MS_U8 *pu8Message, MS_U32 u32IntendedSize, MS_U32 *pu32ActualSize, MS_U32 u32WaitMs)
1618 {
1619     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1620     return FALSE;
1621 }
1622 
1623 //-------------------------------------------------------------------------------------------------
1624 /// Receive a message from the specified queue
1625 /// @param  s32QueueId      \b IN: Queue ID
1626 /// @param  pu8Message      \b OUT: msg destination
1627 /// @param  u32IntendedSize \b IN: intended msg size (byte unit) to receive:
1628 /// @param  pu32ActualSize  \b OUT: actual msg size (byte unit) received
1629 /// @return TRUE : succeed
1630 /// @return FALSE :  fail
1631 //-------------------------------------------------------------------------------------------------
MsOS_PeekFromQueue(MS_S32 s32QueueId,MS_U8 * pu8Message,MS_U32 u32IntendedSize,MS_U32 * pu32ActualSize)1632 MS_BOOL MsOS_PeekFromQueue (MS_S32 s32QueueId, MS_U8 *pu8Message, MS_U32 u32IntendedSize, MS_U32 *pu32ActualSize)
1633 {
1634     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1635     return FALSE;
1636 }
1637 
1638 //-------------------------------------------------------------------------------------------------
1639 // Get a Queue information
1640 // @param  s32QueueId      \b IN: Queue ID
1641 // @param  pu32QueueSize   \b OUT: ptr to queue size (DW)
1642 // @param  pu32MessageSize \b OUT: ptr to message size (DW)
1643 // @param  peAttribute     \b OUT: ptr to suspended mode: E_MSOS_FIFO / E_MSOS_PRIORITY
1644 // @param  pQueueName      \b OUT: ptr to Queue name
1645 // @return TRUE : succeed
1646 // @return FALSE : the Queue has not been created
1647 //-------------------------------------------------------------------------------------------------
MsOS_InfoQueue(MS_S32 s32QueueId,MS_U32 * pu32QueueSize,MS_U32 * pu32MessageSize,MsOSAttribute * peAttribute,char * pQueueName)1648 MS_BOOL MsOS_InfoQueue ( MS_S32 s32QueueId, MS_U32 *pu32QueueSize, MS_U32 *pu32MessageSize,
1649                       MsOSAttribute *peAttribute, char *pQueueName)
1650 {
1651     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1652     return FALSE;
1653 }
1654 
1655 //
1656 // Interrupt management
1657 //
1658 //-------------------------------------------------------------------------------------------------
1659 /// Attach the interrupt callback function to interrupt #
1660 /// @param  eIntNum \b IN: Interrupt number in enumerator InterruptNum
1661 /// @param  pIntCb  \b IN: Interrupt callback function
1662 /// @return TRUE : succeed
1663 /// @return FALSE :  fail
1664 //-------------------------------------------------------------------------------------------------
MsOS_AttachInterrupt(InterruptNum eIntNum,InterruptCb pIntCb)1665 MS_BOOL MsOS_AttachInterrupt (InterruptNum eIntNum, InterruptCb pIntCb)
1666 {
1667     MDrv_IRQ_Attach(eIntNum, pIntCb);
1668 
1669     return TRUE;
1670 }
1671 
1672 //-------------------------------------------------------------------------------------------------
1673 /// Detach the interrupt callback function from interrupt #
1674 /// @param  eIntNum \b IN: Interrupt number in enumerator InterruptNum
1675 /// @return TRUE : succeed
1676 /// @return FALSE :  fail
1677 //-------------------------------------------------------------------------------------------------
MsOS_DetachInterrupt(InterruptNum eIntNum)1678 MS_BOOL MsOS_DetachInterrupt (InterruptNum eIntNum)
1679 {
1680     MDrv_IRQ_Detech(eIntNum);
1681 
1682     return TRUE;
1683 }
1684 
1685 //-------------------------------------------------------------------------------------------------
1686 /// Enable (unmask) the interrupt #
1687 /// @param  eIntNum \b IN: Interrupt number in enumerator InterruptNum
1688 /// @return TRUE : succeed
1689 /// @return FALSE :  fail
1690 //-------------------------------------------------------------------------------------------------
MsOS_EnableInterrupt(InterruptNum eIntNum)1691 MS_BOOL MsOS_EnableInterrupt (InterruptNum eIntNum)
1692 {
1693     MDrv_IRQ_UnMask(eIntNum);
1694 
1695     return TRUE;
1696 }
1697 
1698 //-------------------------------------------------------------------------------------------------
1699 /// Disable (mask) the interrupt #
1700 /// @param  eIntNum \b IN: Interrupt number in enumerator InterruptNum
1701 /// @return TRUE : succeed
1702 //-------------------------------------------------------------------------------------------------
MsOS_DisableInterrupt(InterruptNum eIntNum)1703 MS_BOOL MsOS_DisableInterrupt (InterruptNum eIntNum)
1704 {
1705     MDrv_IRQ_Mask(eIntNum);
1706 
1707     return TRUE;
1708 }
1709 
1710 //-------------------------------------------------------------------------------------------------
1711 /// Disable all interrupts (including timer interrupt), the scheduler is disabled.
1712 /// @return Interrupt register value before all interrupts disable
1713 //-------------------------------------------------------------------------------------------------
MsOS_DisableAllInterrupts(void)1714 MS_U32 MsOS_DisableAllInterrupts(void)
1715 {
1716     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1717     return 0x12345678;
1718 }
1719 
1720 //-------------------------------------------------------------------------------------------------
1721 /// Restore the interrupts from last MsOS_DisableAllInterrupts.
1722 /// @param  u32OldInterrupts \b IN: Interrupt register value from @ref MsOS_DisableAllInterrupts
1723 /// @return TRUE : succeed
1724 //-------------------------------------------------------------------------------------------------
MsOS_RestoreAllInterrupts(MS_U32 u32OldInterrupts)1725 MS_BOOL MsOS_RestoreAllInterrupts(MS_U32 u32OldInterrupts)
1726 {
1727     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1728     return TRUE;
1729 }
1730 
1731 //-------------------------------------------------------------------------------------------------
1732 /// Enable all CPU interrupts.
1733 /// @return TRUE : succeed
1734 //-------------------------------------------------------------------------------------------------
MsOS_EnableAllInterrupts(void)1735 MS_BOOL MsOS_EnableAllInterrupts(void)
1736 {
1737     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1738     return TRUE;
1739 }
1740 
1741 //-------------------------------------------------------------------------------------------------
1742 /// In Interuupt Context or not
1743 /// @return TRUE : Yes
1744 /// @return FALSE : No
1745 //-------------------------------------------------------------------------------------------------
MsOS_In_Interrupt(void)1746 MS_BOOL MsOS_In_Interrupt (void)
1747 {
1748     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1749     return TRUE;
1750 }
1751 
1752 //-------------------------------------------------------------------------------------------------
1753 /// Write back if dirty & Invalidate the cache lines in the given range
1754 /// @param  u32Start \b IN: start address (must be 16-B aligned and in cacheable area)
1755 /// @param  u32Size  \b IN: size (must be 16-B aligned)
1756 /// @return TRUE : succeed
1757 /// @return FALSE : fail due to invalide parameter
1758 //-------------------------------------------------------------------------------------------------
MsOS_Dcache_Flush(MS_VIRT u32Start,MS_SIZE u32Size)1759 MS_BOOL MsOS_Dcache_Flush( MS_VIRT u32Start, MS_SIZE u32Size )
1760 {
1761     Hal_Dcache_Flush(u32Start, u32Size);
1762     return TRUE;
1763 }
1764 
1765 //-------------------------------------------------------------------------------------------------
1766 /// Invalidate the cache lines in the given range
1767 /// @param  u32Start \b IN: start address (must be 16-B aligned and in cacheable area)
1768 /// @param  u32Size  \b IN: size (must be 16-B aligned)
1769 /// @return TRUE : succeed
1770 /// @return FALSE : fail due to invalide parameter
1771 //-------------------------------------------------------------------------------------------------
MsOS_Dcache_Invalidate(MS_VIRT u32Start,MS_SIZE u32Size)1772 MS_BOOL MsOS_Dcache_Invalidate( MS_VIRT u32Start, MS_SIZE u32Size )
1773 {
1774 
1775     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1776     return FALSE;
1777 }
1778 
1779 //-------------------------------------------------------------------------------------------------
1780 /// Write back if dirty the cache lines in the given range
1781 /// @param  u32Start \b IN: start address (must be 16-B aligned and in cacheable area)
1782 /// @param  u32Size  \b IN: size (must be 16-B aligned)
1783 /// @return TRUE : succeed
1784 /// @return FALSE : fail due to invalide parameter
1785 //-------------------------------------------------------------------------------------------------
MsOS_Dcache_Writeback(MS_VIRT u32Start,MS_SIZE u32Size)1786 MS_BOOL MsOS_Dcache_Writeback( MS_VIRT u32Start, MS_SIZE u32Size )
1787 {
1788     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1789     return FALSE;
1790 }
1791 
MsOS_VA2PA(MS_VIRT addr)1792 MS_PHY MsOS_VA2PA(MS_VIRT addr)
1793 {
1794     return HAL_MsOS_MPool_VA2PA(addr);
1795 }
1796 
MsOS_PA2KSEG0(MS_PHY addr)1797 MS_VIRT MsOS_PA2KSEG0(MS_PHY addr)
1798 {
1799     return HAL_MsOS_MPool_PA2KSEG0(addr);
1800 }
1801 
MsOS_PA2KSEG1(MS_PHY addr)1802 MS_VIRT MsOS_PA2KSEG1(MS_PHY addr)
1803 {
1804     return HAL_MsOS_MPool_PA2KSEG1(addr);
1805 }
1806 
1807 
1808 // Share memory operation
MsOS_SHM_Init(void)1809 MS_BOOL MsOS_SHM_Init(void)
1810 {
1811 	memset(&shm_array, 0, sizeof(shm_array));
1812     return TRUE;
1813 }
1814 
1815 // Share memory operation
1816 // linux behavior simulation for nuttx
1817 
MsOS_SHM_GetId(MS_U8 * pu8ClientName,MS_U32 u32BufSize,MS_U32 * pu32ShmId,MS_VIRT * pu32Addr,MS_U32 * pu32BufSize,MS_U32 u32Flag)1818 MS_BOOL MsOS_SHM_GetId(MS_U8* pu8ClientName, MS_U32 u32BufSize, MS_U32* pu32ShmId, MS_VIRT* pu32Addr, MS_U32* pu32BufSize, MS_U32 u32Flag)
1819 {
1820 	int i, match = FALSE, result = FALSE;
1821 
1822 	// search matching entry
1823 	for (i = 0; i < shm_array.count; i++)
1824 	{
1825 		if (0 == strcmp(shm_array.shm_entry[i].name, (char*)pu8ClientName))
1826 		{
1827 			match = TRUE;
1828 			break;
1829 		}
1830 	}
1831 
1832 	if (match) {
1833 		// if match, fill info
1834 		*pu32ShmId = i;
1835 		*pu32Addr = (MS_U32)(shm_array.shm_entry[i].addr);
1836 		*pu32BufSize = shm_array.shm_entry[i].size;
1837 		result = TRUE;
1838 	} else if (u32Flag == MSOS_SHM_CREATE) {
1839 		// if not match & specify to create one, register to shm_array
1840 		*pu32ShmId = shm_array.count;
1841 		*pu32Addr = (MS_U32)malloc(u32BufSize);
1842 		memset((void*)*pu32Addr, 0, u32BufSize);
1843 		*pu32BufSize = u32BufSize;
1844 		// this would cause bug, because string source may be "stack variable"
1845 		//shm_array.shm_entry[shm_array.count].name = (char*)pu8ClientName;
1846 		strcpy(shm_array.shm_entry[shm_array.count].name, (char*)pu8ClientName);
1847 		shm_array.shm_entry[shm_array.count].addr = (void*)*pu32Addr;
1848 		shm_array.shm_entry[shm_array.count].size = *pu32BufSize;
1849 		shm_array.count++;
1850 		result = TRUE;
1851 	}
1852 
1853 	return result;
1854 }
1855 
MsOS_SHM_FreeId(MS_U8 * pu8ClientName,MS_U32 u32ShmId)1856 MS_BOOL MsOS_SHM_FreeId(MS_U8* pu8ClientName, MS_U32 u32ShmId)
1857 {
1858     return TRUE;
1859 }
1860 
1861 //-------------------------------------------------------------------------------------------------
1862 /// Disable the CPU interrupt
1863 /// @return Interrupt register value before all interrupts disable
1864 //-------------------------------------------------------------------------------------------------
MsOS_CPU_DisableInterrupt(void)1865 MS_U32 MsOS_CPU_DisableInterrupt (void)
1866 {
1867     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1868     return TRUE;
1869 }
1870 
1871 //-------------------------------------------------------------------------------------------------
1872 /// Enable the CPU interrupt
1873 /// @return TRUE : succeed
1874 //-------------------------------------------------------------------------------------------------
MsOS_CPU_EnableInterrupt(void)1875 MS_BOOL MsOS_CPU_EnableInterrupt (void)
1876 {
1877     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1878     return TRUE;
1879 }
1880 
1881 //-------------------------------------------------------------------------------------------------
1882 /// Restore the CPU interrupt from last MsOS_CPU_DisableInterrupts.
1883 /// @param  u32OldInterrupts \b IN: Interrupt register value from @ref MsOS_CPU_DisableInterrupts
1884 /// @return TRUE : succeed
1885 //-------------------------------------------------------------------------------------------------
MsOS_CPU_RestoreInterrupt(MS_U32 u32OldInterrupts)1886 MS_BOOL MsOS_CPU_RestoreInterrupt (MS_U32 u32OldInterrupts)
1887 {
1888     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1889     return TRUE;
1890 }
1891 
1892 //-------------------------------------------------------------------------------------------------
1893 /// Mask all the CPU interrupt
1894 /// @return TRUE : succeed
1895 //-------------------------------------------------------------------------------------------------
MsOS_CPU_MaskAllInterrupt(void)1896 MS_BOOL MsOS_CPU_MaskAllInterrupt (void)
1897 {
1898     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1899     return TRUE;
1900 }
1901 
1902 //-------------------------------------------------------------------------------------------------
1903 /// Mask the CPU interrupt
1904 /// @param  intr_num \b IN: Interrupt number in enumerator MHAL_INTERRUPT_TYPE
1905 /// @return TRUE : succeed
1906 //-------------------------------------------------------------------------------------------------
MsOS_CPU_MaskInterrupt(MHAL_INTERRUPT_TYPE intr_num)1907 MS_BOOL MsOS_CPU_MaskInterrupt (MHAL_INTERRUPT_TYPE intr_num)
1908 {
1909     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1910     return TRUE;
1911 }
1912 
1913 //-------------------------------------------------------------------------------------------------
1914 /// UnMask the CPU interrupt
1915 /// @param  intr_num \b IN: Interrupt number in enumerator MHAL_INTERRUPT_TYPE
1916 /// @return TRUE : succeed
1917 //-------------------------------------------------------------------------------------------------
1918 extern void up_enable_irq(int irq);
1919 
MsOS_CPU_UnMaskInterrupt(MHAL_INTERRUPT_TYPE intr_num)1920 MS_BOOL MsOS_CPU_UnMaskInterrupt (MHAL_INTERRUPT_TYPE intr_num)
1921 {
1922     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1923 	up_enable_irq((int) intr_num);
1924     return TRUE;
1925 }
1926 
1927 //-------------------------------------------------------------------------------------------------
1928 /// Lock the CPU interrupt
1929 /// @return TRUE : succeed
1930 //-------------------------------------------------------------------------------------------------
MsOS_CPU_LockInterrupt(void)1931 MS_BOOL MsOS_CPU_LockInterrupt (void)
1932 {
1933     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1934     return TRUE;
1935 }
1936 
1937 //-------------------------------------------------------------------------------------------------
1938 /// UnLock the CPU interrupt
1939 /// @return TRUE : succeed
1940 //-------------------------------------------------------------------------------------------------
MsOS_CPU_UnLockInterrupt(void)1941 MS_BOOL MsOS_CPU_UnLockInterrupt (void)
1942 {
1943     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1944     return TRUE;
1945 }
1946 
1947 //-------------------------------------------------------------------------------------------------
1948 /// Attach the CPU interrupt callback function to interrupt #
1949 /// @param  eIntNum \b IN: Interrupt number in enumerator InterruptNum
1950 /// @param  pIntCb  \b IN: Interrupt callback function
1951 /// @param  dat  \b IN: Data
1952 /// @return TRUE : succeed
1953 //-------------------------------------------------------------------------------------------------
1954 extern int irq_attach(int irq, xcpt_t isr);
1955 
MsOS_CPU_AttachInterrupt(MHAL_INTERRUPT_TYPE intr_num,mhal_isr_t isr,MS_U32 dat)1956 MS_BOOL MsOS_CPU_AttachInterrupt (MHAL_INTERRUPT_TYPE intr_num, mhal_isr_t isr, MS_U32 dat)
1957 {
1958    	 irq_attach(intr_num, (xcpt_t) isr);
1959 	 return TRUE;
1960 }
1961 
1962 //-------------------------------------------------------------------------------------------------
1963 /// Detach the CPU interrupt callback function to interrupt #
1964 /// @param  eIntNum \b IN: Interrupt number in enumerator InterruptNum
1965 /// @param  pIntCb  \b IN: Interrupt callback function
1966 /// @param  dat  \b IN: Data
1967 /// @return TRUE : succeed
1968 //-------------------------------------------------------------------------------------------------
MsOS_CPU_DetachInterrupt(MHAL_INTERRUPT_TYPE intr_num)1969 MS_BOOL MsOS_CPU_DetachInterrupt (MHAL_INTERRUPT_TYPE intr_num)
1970 {
1971     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1972     return TRUE;
1973 }
1974 
1975 //-------------------------------------------------------------------------------------------------
1976 /// Attach the CPU exception callback function to interrupt #
1977 /// @param  eIntNum \b IN: Exception number in enumerator InterruptNum
1978 /// @param  pIntCb  \b IN: Exception callback function
1979 /// @param  dat  \b IN: Data
1980 /// @return TRUE : succeed
1981 //-------------------------------------------------------------------------------------------------
MsOS_CPU_AttachException(MHAL_EXCEPTION_TYPE expt_num,mhal_isr_t isr,MS_U32 dat)1982 MS_BOOL MsOS_CPU_AttachException (MHAL_EXCEPTION_TYPE expt_num, mhal_isr_t isr, MS_U32 dat)
1983 {
1984     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1985     return TRUE;
1986 }
1987 
1988 //-------------------------------------------------------------------------------------------------
1989 /// Detach the CPU exception callback function to interrupt #
1990 /// @param  eIntNum \b IN: Exception number in enumerator InterruptNum
1991 /// @param  pIntCb  \b IN: Exception callback function
1992 /// @param  dat  \b IN: Data
1993 /// @return TRUE : succeed
1994 //-------------------------------------------------------------------------------------------------
MsOS_CPU_DetachExceptiont(MHAL_EXCEPTION_TYPE expt_num)1995 MS_BOOL MsOS_CPU_DetachExceptiont (MHAL_EXCEPTION_TYPE expt_num)
1996 {
1997     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
1998     return TRUE;
1999 }
2000 //-------------------------------------------------------------------------------------------------
2001 /// Set EBASE
2002 /// @param  u32Addr \b IN: MIPS Code Start Address
2003 /// @return TRUE : succeed
2004 //-------------------------------------------------------------------------------------------------
MsOS_CPU_SetEBASE(MS_U32 u32Addr)2005 MS_BOOL MsOS_CPU_SetEBASE (MS_U32 u32Addr)
2006 {
2007     PRINTF("[%s][%d] %s is not supported\n", __FUNCTION__, __LINE__, __FUNCTION__);
2008     return TRUE;
2009 }
2010 //-------------------------------------------------------------------------------------------------
2011 /// Sync data in EC-Brigde
2012 /// @return TRUE : succeed
2013 /// @return FALSE : fail
2014 //-------------------------------------------------------------------------------------------------
MsOS_Sync(void)2015 void MsOS_Sync(void)
2016 {
2017     MAsm_CPU_Sync();
2018 }
2019 
2020 //-------------------------------------------------------------------------------------------------
2021 /// Flush non-cached data into dram
2022 //-------------------------------------------------------------------------------------------------
MsOS_FlushMemory(void)2023 void MsOS_FlushMemory(void)
2024 {
2025 }
2026 
2027 //-------------------------------------------------------------------------------------------------
2028 /// Flush non-cached data into dram
2029 //-------------------------------------------------------------------------------------------------
MsOS_ReadMemory(void)2030 void MsOS_ReadMemory(void)
2031 {
2032 }
2033 
MsOS_GetKattribute(char * pAttr)2034 MS_U32 MsOS_GetKattribute(char *pAttr)
2035 {
2036     // This is for linux only, do nothing here
2037     return 0;
2038 }
2039