rtos_lib.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #ifndef RTOS_LIB_H
  2. #define RTOS_LIB_H
  3. #include <limits.h>
  4. #include <string.h>
  5. #include "rthw.h"
  6. #include "rtthread.h"
  7. #include "gtypes.h" /* global type definitions */
  8. #include "gd_int.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. typedef void(*RTOS_ThreadFunctionT)(void*);
  13. typedef void(*RTOS_HookFunctionT)(void*);
  14. typedef void (*RTOS_TimerFunctionT)(void *);
  15. typedef void* RTOS_ThreadT;
  16. typedef void* RTOS_EventT;
  17. typedef void* RTOS_SemaphoreT;
  18. typedef void* RTOS_MailboxT;
  19. typedef void* RTOS_MailqueueT;
  20. typedef void* RTOS_MutexT;
  21. typedef void* RTOS_TimerT;
  22. typedef void* RTOS_CondT;
  23. typedef U32 RTOS_SYS_SemaphoreT;
  24. typedef void* RTOS_SYS_MailqueueT;
  25. //typedef struct rt_thread *RTOS_ThreadT;
  26. //typedef struct rt_semaphore * RTOS_SemaphoreT;
  27. //typedef struct rt_messagequeue *RTOS_MailqueueT;
  28. //typedef struct rt_mailbox *RTOS_MailqueueT;
  29. //typedef struct rt_mutex *RTOS_MutexT;
  30. /*
  31. ********************************************************************************
  32. ********************************************************************************
  33. **
  34. ** macros to define various suspend modes
  35. **
  36. ********************************************************************************
  37. ********************************************************************************
  38. */
  39. #define RTOS_OK (RTOS_Status)0 //!< The OK status
  40. #define RTOS_FAILURE (RTOS_Status)-1 //!< The general FAILURE status
  41. #define RTOS_NO_SUSPEND 0x00000000UL //!< do not suspend, return immediately
  42. #define RTOS_SUSPEND 0xFFFFFFFFUL //!< suspend forever, wait for valid event without timeout
  43. #define RTOS_MAX_MAILQUEUE_ENTRIES 4096
  44. #define RTOS_MSG_IS_POINTER 0xFFFFFFFFUL //!< the message to transport is a generic pointer only
  45. typedef GBOOL RTOS_Flag; //!< the boolean flag type
  46. typedef S32 RTOS_Status; //!< the result status type
  47. typedef S32 RTOS_Size; //!< the generic size type
  48. typedef U32 RTOS_Time; //!< the generic time type
  49. typedef RTOS_TimerT RTOS_Timer; //!< the generic time type
  50. typedef RTOS_ThreadT RTOS_Thread; //!< the thread identifier type
  51. typedef S32 RTOS_Priority; //!< the generic priority type
  52. typedef RTOS_SemaphoreT RTOS_Semaphore; //!< the semaphore identifier type
  53. typedef RTOS_MutexT RTOS_Mutex; //!< the mutex semaphore identifier type
  54. typedef RTOS_MailboxT RTOS_Mailbox; //!< the mailbox identifier type
  55. typedef RTOS_MailqueueT RTOS_Mailqueue; //!< the mailqueue identifier type
  56. typedef void* RTOS_Message; //!< the generic message identifier type
  57. typedef void* RTOS_Memory; //!< the generic memory pointer type
  58. typedef U32 RTOS_Segment; //!< the memory heap segment type
  59. typedef RTOS_ThreadFunctionT RTOS_ThreadFunction; //!< the thread function definition type
  60. typedef RTOS_HookFunctionT RTOS_HookFunction; //!< the hook function definition type
  61. typedef RTOS_ThreadT RTOS_ThreadData; //!< the optional thread data structure
  62. typedef RTOS_CondT RTOS_Cond; //!< the cond semaphore identifier type
  63. #define GKOS_TIMER_HOOK_TABLE_SIZE 8 //!< number of timer-tick hook function entries
  64. #define GKOS_SWITCH_HOOK_TABLE_SIZE 4 //!< number of context-switch hook function entries
  65. #define RTOS_ThreadPriorityHighest (RTOS_Priority)127 //!< The highest possible priority
  66. #define RTOS_ThreadPriorityLowest (RTOS_Priority)1 //!< The lowest possible priority
  67. /*
  68. ********************************************************************************
  69. ********************************************************************************
  70. **
  71. ** rtos type, gkos:0, rtthread:1
  72. **
  73. ********************************************************************************
  74. ********************************************************************************
  75. */
  76. #define RTOS_GKOS 0
  77. #define RTOS_RTTHREAD 1
  78. #define RTOS_TIMER_FLAG_DEACTIVATED 0x0 /**< timer is deactive */
  79. #define RTOS_TIMER_FLAG_ACTIVATED 0x1 /**< timer is active */
  80. #define RTOS_TIMER_FLAG_ONE_SHOT 0x0 /**< one shot timer */
  81. #define RTOS_TIMER_FLAG_PERIODIC 0x2 /**< periodic timer */
  82. #define RTOS_TIMER_FLAG_HARD_TIMER 0x0 /**< hard timer,the timer's callback function will be called in tick isr. */
  83. #define RTOS_TIMER_FLAG_SOFT_TIMER 0x4 /**< soft timer,the timer's callback function will be called in timer thread. */
  84. #define RTOS_TIMER_CTRL_SET_TIME 0x0 /**< set timer control command */
  85. #define RTOS_TIMER_CTRL_GET_TIME 0x1 /**< get timer control command */
  86. #define RTOS_TIMER_CTRL_SET_ONESHOT 0x2 /**< change timer to one shot */
  87. #define RTOS_TIMER_CTRL_SET_PERIODIC 0x3 /**< change timer to periodic */
  88. typedef struct
  89. {
  90. RTOS_HookFunctionT timerFunctionArray[GKOS_TIMER_HOOK_TABLE_SIZE];
  91. U32 timerFunctionCount;
  92. RTOS_HookFunctionT switchFunctionArray[GKOS_SWITCH_HOOK_TABLE_SIZE];
  93. U32 switchFunctionCount;
  94. }gkosHookDataT;
  95. gkosHookDataT* gkosHookData;
  96. #define THREAD_TIMESLICE 10
  97. #define RTOS_ThreadCreate(function,arg,priority,stackBuffer,stackSize) \
  98. RTOS_CreateThread(stackBuffer, stackSize, priority, function, arg, 0, 0)
  99. U32 RTOS_DestroyThread( RTOS_ThreadT threadHandle );
  100. #define RTOS_ThreadSleep(msecs) rt_thread_delay(msecs)
  101. U32 RTOS_ThreadSetName( RTOS_ThreadT threadHandle, const char* optName);
  102. U32 RTOS_SetThreadName( RTOS_ThreadT threadHandle, const char* optName);
  103. char * RTOS_ThreadGetName();
  104. RTOS_Status RTOS_MemoryRelease( RTOS_Memory memory );
  105. RTOS_Memory RTOS_MemoryAllocate( RTOS_Size bytes, RTOS_Flag shared );
  106. RTOS_Memory RTOS_MemorySet( RTOS_Memory mem, U8 value, RTOS_Size bytes );
  107. RTOS_Memory RTOS_MemoryCopy( RTOS_Memory dst, RTOS_Memory src, RTOS_Size bytes );
  108. RTOS_Memory RTOS_KernelMemoryAllocate( RTOS_Size bytes);
  109. RTOS_Status RTOS_KernelMemoryRelease( RTOS_Memory memory );
  110. #define RTOS_ATOM_PRINTF(format, ...) do {\
  111. U32 pval = 0;\
  112. RTOS_EnterCritical(pval);\
  113. printf("\033[0;31m[@%s.%d]->"format"\033[0;0m\n", __FUNCTION__, __LINE__, ##__VA_ARGS__);\
  114. RTOS_LeaveCritical(pval);\
  115. }while(0)
  116. #define RTOS_ASSERT(EX) \
  117. if (!(EX)) \
  118. { \
  119. volatile char dummy = 0; \
  120. U32 pval = 0;\
  121. RTOS_EnterCritical(pval);\
  122. printf("(%s) assert failed at %s:%d thread name:%s\n", #EX, __FUNCTION__, __LINE__, RTOS_ThreadGetSelfName());\
  123. while (dummy == 0); \
  124. }
  125. //U32 GKOS_EnterCriticalSection( void );
  126. //void GKOS_LeaveCriticalSection( U32 cpuStatus );
  127. void RTOS_LockScheduler( void );
  128. void RTOS_UnlockScheduler( void );
  129. void gkosFinishThread( void );
  130. U32 RTOS_SleepThread( U32 msecs );
  131. RTOS_SemaphoreT RTOS_CreateSemaphore( U32 initCount );
  132. U32 RTOS_GetSemaphore( RTOS_SemaphoreT semaphoreHandle, U32 msecsTimeout );
  133. U32 RTOS_SetSemaphore( RTOS_SemaphoreT semaphoreHandle, U32 msecsTimeout );
  134. S32 RTOS_SemaphoreQuery( RTOS_Semaphore semaphoreHandle );
  135. RTOS_Status RTOS_SemaphoreDestroy( RTOS_Semaphore semaphoreHandle );
  136. #define RTOS_SemaphoreWait(semaphoreHandle, suspend) RTOS_WaitSemaphore(semaphoreHandle, suspend)
  137. #define RTOS_SemaphoreWaitTimeout(semaphoreHandle, timeout) RTOS_GetSemaphore((RTOS_Semaphore)semaphoreHandle, timeout)
  138. #define RTOS_SemaphoreCreate(initCount) RTOS_CreateSemaphore(initCount)
  139. #define RTOS_SemaphoreRelease(semaphoreHandle) RTOS_SetSemaphore((RTOS_Semaphore)semaphoreHandle,RTOS_SUSPEND)
  140. void RTOS_HwTickInit(void);
  141. void RTOS_KernelTimerSetHook(RTOS_HookFunction function);
  142. RTOS_MailqueueT RTOS_CreateMailqueue( U32 queueElements, U32 elementBytes );
  143. #define RTOS_MailqueueCreate(elements) RTOS_CreateMailqueue(elements, RTOS_MSG_IS_POINTER)
  144. #define RTOS_EnterCritical(cpusr) {cpusr=rt_hw_interrupt_disable();}
  145. #define RTOS_LeaveCritical(cpusr) {rt_hw_interrupt_enable(cpusr);}
  146. extern U32 cpu_cpsr;
  147. #define RTOS_EnterCriticalEx() cpu_cpsr = RTOS_EnterCriticalSection()
  148. #define RTOS_LeaveCriticalEx() RTOS_LeaveCriticalSection(cpu_cpsr)
  149. #define RTOS_ThreadSuspend(threadHandle) RTOS_SuspendThread(threadHandle)
  150. #define RTOS_ThreadWakeup(threadHandle) RTOS_WakeupThread(threadHandle)
  151. RTOS_Status RTOS_MailqueueSend(RTOS_Mailqueue mailqueue, RTOS_Message data);
  152. #define isb() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c5, 4" \
  153. : : "r" (0) : "memory")
  154. #define dsb() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 4" \
  155. : : "r" (0) : "memory")
  156. #define dmb() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" \
  157. : : "r" (0) : "memory")
  158. void rtos_cache_inv_range(void *addr, unsigned int size);
  159. void rtos_cache_clean_range(void *addr, unsigned int size);
  160. U32 RTOS_Initialize(U32 Heap_size);
  161. #define RTOS_Initialize RTOS_InitKernel
  162. #define RTOS_GetIdentity RTOS_GetIdentity
  163. #ifdef __cplusplus
  164. }
  165. #endif
  166. /*
  167. ********************************************************************************
  168. ** end of file
  169. ********************************************************************************
  170. */
  171. #endif /* RTOS_LIB_H */