rtt_cmsis.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include "cmsis_os.h"
  2. // Kernel Control Public API
  3. /// Start the RTOS Kernel with executing the specified thread
  4. osStatus osKernelStart(osThreadDef_t *thread_def, void *argument)
  5. {
  6. osThreadCreate(thread_def, argument);
  7. rt_system_scheduler_start();
  8. return osOK;
  9. }
  10. /// Check if the RTOS kernel is already started
  11. int32_t osKernelRunning(void)
  12. {
  13. return (rt_thread_self() != RT_NULL) ? 1 : 0;
  14. }
  15. // Thread Public API
  16. /// Create a thread and add it to Active Threads and set it to state READY
  17. osThreadId osThreadCreate(osThreadDef_t *thread_def, void *argument)
  18. {
  19. osThreadId thread;
  20. thread = rt_thread_create(thread_def->name, thread_def->entry, argument, thread_def->stack_size, thread_def->priority, thread_def->tick);
  21. if (thread != RT_NULL)
  22. rt_thread_startup(thread);
  23. return thread;
  24. }
  25. /// Return the thread ID of the current running thread
  26. osThreadId osThreadGetId(void)
  27. {
  28. return rt_thread_self();
  29. }
  30. /// Terminate execution of a thread and remove it from ActiveThreads
  31. osStatus osThreadTerminate(osThreadId thread_id)
  32. {
  33. rt_err_t result;
  34. result = rt_thread_suspend(thread_id);
  35. if (result == RT_EOK)
  36. return osOK;
  37. else
  38. return osErrorOS;
  39. }
  40. /// Pass control to next thread that is in state READY
  41. osStatus osThreadYield(void)
  42. {
  43. rt_err_t result;
  44. result = rt_thread_yield();
  45. if (result == RT_EOK)
  46. return osOK;
  47. else
  48. return osErrorOS;
  49. }
  50. /// Change prority of an active thread
  51. osStatus osThreadSetPriority(osThreadId thread_id, osPriority priority)
  52. {
  53. if (thread_id == RT_NULL)
  54. return osErrorOS;
  55. if (priority < osPriorityIdle || priority > osPriorityRealtime)
  56. return osErrorOS;
  57. thread_id->current_priority = priority;
  58. return osOK;
  59. }
  60. /// Get current prority of an active thread
  61. osPriority osThreadGetPriority(osThreadId thread_id)
  62. {
  63. if (thread_id == RT_NULL)
  64. return osPriorityError;
  65. if (thread_id->current_priority < osPriorityIdle || thread_id->current_priority > osPriorityRealtime)
  66. return osPriorityError;
  67. return thread_id->current_priority;
  68. }
  69. // Generic Wait API
  70. /// Wait for Timeout (Time Delay)
  71. osStatus osDelay(uint32_t millisec)
  72. {
  73. rt_err_t result;
  74. rt_tick_t ticks;
  75. ticks = rt_tick_from_millisecond(millisec);
  76. result = rt_thread_delay(ticks);
  77. if (result == RT_EOK)
  78. return osOK;
  79. else
  80. return osErrorOS;
  81. }
  82. /// Wait for Signal, Message, Mail, or Timeout
  83. osEvent osWait(uint32_t millisec)
  84. {
  85. }
  86. // Timer Management Public API
  87. /// Create timer
  88. osTimerId osTimerCreate(osTimerDef_t *timer_def, os_timer_type type, void *argument)
  89. {
  90. return rt_timer_create(timer_def->name, timer_def->timeout, timer_def->parameter, timer_def->time, timer_def->flag);
  91. }
  92. /// Start or restart timer
  93. osStatus osTimerStart(osTimerId timer_id, uint32_t millisec)
  94. {
  95. rt_err_t result;
  96. result = rt_timer_start(timer_id);
  97. if (result == RT_EOK)
  98. return osOK;
  99. else
  100. return osErrorOS;
  101. }
  102. /// Stop timer
  103. osStatus osTimerStop(osTimerId timer_id)
  104. {
  105. rt_err_t result;
  106. result = rt_timer_stop(timer_id);
  107. if (result == RT_EOK)
  108. return osOK;
  109. else
  110. return osErrorOS;
  111. }
  112. // Mutex Public API
  113. /// Create and Initialize a Mutex object
  114. osMutexId osMutexCreate(osMutexDef_t *mutex_def)
  115. {
  116. return rt_mutex_create(mutex_def->name, mutex_def->flag);
  117. }
  118. /// Wait until a Mutex becomes available
  119. osStatus osMutexWait(osMutexId mutex_id, uint32_t millisec)
  120. {
  121. rt_err_t result;
  122. rt_tick_t ticks;
  123. ticks = rt_tick_from_millisecond(millisec);
  124. result = rt_mutex_take(mutex_id, ticks);
  125. if (result == RT_EOK)
  126. return osOK;
  127. else
  128. return osErrorOS;
  129. }
  130. /// Release a Mutex that was obtained with osMutexWait
  131. osStatus osMutexRelease(osMutexId mutex_id)
  132. {
  133. rt_err_t result;
  134. result = rt_mutex_release(mutex_id);
  135. if (result == RT_EOK)
  136. return osOK;
  137. else
  138. return osErrorOS;
  139. }
  140. // Semaphore Public API
  141. /// Create and Initialize a Semaphore object
  142. osSemaphoreId osSemaphoreCreate(osSemaphoreDef_t *semaphore_def, int32_t count)
  143. {
  144. return rt_sem_create(semaphore_def->name, count, semaphore_def->flag);
  145. }
  146. /// Wait until a Semaphore becomes available
  147. int32_t osSemaphoreWait(osSemaphoreId semaphore_id, uint32_t millisec)
  148. {
  149. rt_tick_t ticks;
  150. ticks = rt_tick_from_millisecond(millisec);
  151. rt_sem_take(semaphore_id, ticks);
  152. }
  153. /// Release a Semaphore
  154. osStatus osSemaphoreRelease(osSemaphoreId semaphore_id)
  155. {
  156. rt_err_t result;
  157. result = rt_sem_release(semaphore_id);
  158. if (result == RT_EOK)
  159. return osOK;
  160. else
  161. return osErrorOS;
  162. }
  163. // Memory Management Public API
  164. /// Create and Initialize memory pool
  165. osPoolId osPoolCreate(osPoolDef_t *pool_def)
  166. {
  167. return rt_mp_create(pool_def->name, pool_def->block_count, pool_def->block_size);
  168. }
  169. /// Allocate a memory block from a memory pool
  170. void *osPoolAlloc(osPoolId pool_id)
  171. {
  172. return rt_mp_alloc(pool_id, 0);
  173. }
  174. /// Allocate a memory block from a memory pool and set memory block to zero
  175. void *osPoolCAlloc(osPoolId pool_id)
  176. {
  177. }
  178. /// Return an allocated memory block back to a specific memory pool
  179. osStatus osPoolFree(osPoolId pool_id, void *block)
  180. {
  181. rt_mp_free(block);
  182. return osOK;
  183. }
  184. // Message Queue Management Public API
  185. /// Create and Initialize Message Queue
  186. osMessageQId osMessageCreate(osMessageQDef_t *queue_def, osThreadId thread_id)
  187. {
  188. return rt_mq_create(queue_def->name, queue_def->msg_size, queue_def->max_msgs, queue_def->flag);
  189. }
  190. /// Put a Message to a Queue
  191. osStatus osMessagePut(osMessageQId queue_id, uint32_t info, uint32_t millisec)
  192. {
  193. rt_err_t result;
  194. result = rt_mq_send(queue_id, &info, 1);
  195. if (result == RT_EOK)
  196. return osOK;
  197. else
  198. return osErrorOS;
  199. }
  200. /// Get a Message or Wait for a Message from a Queue
  201. osEvent osMessageGet(osMessageQId queue_id, uint32_t millisec)
  202. {
  203. }
  204. // Mail Queue Management Public API
  205. /// Create and Initialize mail queue
  206. osMailQId osMailCreate(osMailQDef_t *queue_def, osThreadId thread_id)
  207. {
  208. }
  209. /// Allocate a memory block from a mail
  210. void *osMailAlloc(osMailQId queue_id, uint32_t millisec)
  211. {
  212. }
  213. /// Allocate a memory block from a mail and set memory block to zero
  214. void *osMailCAlloc(osMailQId queue_id, uint32_t millisec)
  215. {
  216. }
  217. /// Free a memory block from a mail
  218. osStatus osMailFree(osMailQId queue_id, void *mail)
  219. {
  220. }
  221. /// Put a mail to a queue
  222. osStatus osMailPut(osMailQId queue_id, void *mail)
  223. {
  224. }
  225. /// Get a mail from a queue
  226. osEvent osMailGet(osMailQId queue_id, uint32_t millisec)
  227. {
  228. osEvent ret;
  229. if (queue_id == NULL) {
  230. ret.status = osErrorParameter;
  231. return ret;
  232. }
  233. ret = osMessageGet(*((void **)queue_id), millisec);
  234. if (ret.status == osEventMessage) ret.status = osEventMail;
  235. return ret;
  236. }