cmsis_rtthread.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_delete(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. rt_err_t result;
  54. if (thread_id == RT_NULL)
  55. return osErrorOS;
  56. if (priority < osPriorityIdle || priority > osPriorityRealtime)
  57. return osErrorPriority;
  58. result = rt_thread_control(thread_id, RT_THREAD_CTRL_CHANGE_PRIORITY, &priority);
  59. if (result == RT_EOK)
  60. return osOK;
  61. else
  62. return osErrorOS;
  63. }
  64. /// Get current prority of an active thread
  65. osPriority osThreadGetPriority(osThreadId thread_id)
  66. {
  67. if (thread_id == RT_NULL)
  68. return osErrorOS;
  69. if (thread_id->current_priority < osPriorityIdle || thread_id->current_priority > osPriorityRealtime)
  70. return osPriorityError;
  71. return thread_id->current_priority;
  72. }
  73. // Generic Wait API
  74. /// Wait for Timeout (Time Delay)
  75. osStatus osDelay(uint32_t millisec)
  76. {
  77. rt_err_t result;
  78. rt_tick_t ticks;
  79. ticks = rt_tick_from_millisecond(millisec);
  80. result = rt_thread_delay(ticks);
  81. if (result == RT_EOK)
  82. return osOK;
  83. else
  84. return osErrorOS;
  85. }
  86. /// Wait for Signal, Message, Mail, or Timeout
  87. osEvent osWait(uint32_t millisec)
  88. {
  89. rt_err_t result;
  90. rt_tick_t ticks;
  91. ticks = rt_tick_from_millisecond(millisec);
  92. result = rt_thread_delay(ticks);
  93. /*
  94. if (result == RT_EOK)
  95. return osOK;
  96. else
  97. return osErrorOS;
  98. */
  99. }
  100. // Timer Management Public API
  101. /// Create timer
  102. osTimerId osTimerCreate(osTimerDef_t *timer_def, os_timer_type type, void *argument)
  103. {
  104. return rt_timer_create(timer_def->name, timer_def->timeout, timer_def->parameter, timer_def->time, timer_def->flag);
  105. }
  106. /// Start or restart timer
  107. osStatus osTimerStart(osTimerId timer_id, uint32_t millisec)
  108. {
  109. rt_err_t result;
  110. result = rt_timer_start(timer_id);
  111. if (result == RT_EOK)
  112. return osOK;
  113. else
  114. return osErrorOS;
  115. }
  116. /// Stop timer
  117. osStatus osTimerStop(osTimerId timer_id)
  118. {
  119. rt_err_t result;
  120. result = rt_timer_stop(timer_id);
  121. if (result == RT_EOK)
  122. return osOK;
  123. else
  124. return osErrorOS;
  125. }
  126. // Mutex Public API
  127. /// Create and Initialize a Mutex object
  128. osMutexId osMutexCreate(osMutexDef_t *mutex_def)
  129. {
  130. return rt_mutex_create(mutex_def->name, mutex_def->flag);
  131. }
  132. /// Wait until a Mutex becomes available
  133. osStatus osMutexWait(osMutexId mutex_id, uint32_t millisec)
  134. {
  135. rt_err_t result;
  136. rt_tick_t ticks;
  137. ticks = rt_tick_from_millisecond(millisec);
  138. result = rt_mutex_take(mutex_id, ticks);
  139. if (result == RT_EOK)
  140. return osOK;
  141. else
  142. return osErrorOS;
  143. }
  144. /// Release a Mutex that was obtained with osMutexWait
  145. osStatus osMutexRelease(osMutexId mutex_id)
  146. {
  147. rt_err_t result;
  148. result = rt_mutex_release(mutex_id);
  149. if (result == RT_EOK)
  150. return osOK;
  151. else
  152. return osErrorOS;
  153. }
  154. // Semaphore Public API
  155. /// Create and Initialize a Semaphore object
  156. osSemaphoreId osSemaphoreCreate(osSemaphoreDef_t *semaphore_def, int32_t count)
  157. {
  158. return rt_sem_create(semaphore_def->name, count, semaphore_def->flag);
  159. }
  160. /// Wait until a Semaphore becomes available
  161. int32_t osSemaphoreWait(osSemaphoreId semaphore_id, uint32_t millisec)
  162. {
  163. rt_tick_t ticks;
  164. if (semaphore_id == RT_NULL)
  165. return -1;
  166. ticks = rt_tick_from_millisecond(millisec);
  167. rt_sem_take(semaphore_id, ticks);
  168. return semaphore_id->value;
  169. }
  170. /// Release a Semaphore
  171. osStatus osSemaphoreRelease(osSemaphoreId semaphore_id)
  172. {
  173. rt_err_t result;
  174. result = rt_sem_release(semaphore_id);
  175. if (result == RT_EOK)
  176. return osOK;
  177. else
  178. return osErrorOS;
  179. }
  180. // Memory Management Public API
  181. /// Create and Initialize memory pool
  182. osPoolId osPoolCreate(osPoolDef_t *pool_def)
  183. {
  184. return rt_mp_create(pool_def->name, pool_def->block_count, pool_def->block_size);
  185. }
  186. /// Allocate a memory block from a memory pool
  187. void *osPoolAlloc(osPoolId pool_id)
  188. {
  189. return rt_mp_alloc(pool_id, 0);
  190. }
  191. /// Allocate a memory block from a memory pool and set memory block to zero
  192. void *osPoolCAlloc(osPoolId pool_id)
  193. {
  194. }
  195. /// Return an allocated memory block back to a specific memory pool
  196. osStatus osPoolFree(osPoolId pool_id, void *block)
  197. {
  198. rt_mp_free(block);
  199. return osOK;
  200. }
  201. // Message Queue Management Public API
  202. /// Create and Initialize Message Queue
  203. osMessageQId osMessageCreate(osMessageQDef_t *queue_def, osThreadId thread_id)
  204. {
  205. return rt_mq_create(queue_def->name, queue_def->msg_size, queue_def->max_msgs, queue_def->flag);
  206. }
  207. /// Put a Message to a Queue
  208. osStatus osMessagePut(osMessageQId queue_id, uint32_t info, uint32_t millisec)
  209. {
  210. rt_err_t result;
  211. result = rt_mq_send(queue_id, &info, 1);
  212. if (result == RT_EOK)
  213. return osOK;
  214. else
  215. return osErrorOS;
  216. }
  217. /// Get a Message or Wait for a Message from a Queue
  218. osEvent osMessageGet(osMessageQId queue_id, uint32_t millisec)
  219. {
  220. }
  221. // Mail Queue Management Public API
  222. /// Create and Initialize mail queue
  223. osMailQId osMailCreate(osMailQDef_t *queue_def, osThreadId thread_id)
  224. {
  225. }
  226. /// Allocate a memory block from a mail
  227. void *osMailAlloc(osMailQId queue_id, uint32_t millisec)
  228. {
  229. }
  230. /// Allocate a memory block from a mail and set memory block to zero
  231. void *osMailCAlloc(osMailQId queue_id, uint32_t millisec)
  232. {
  233. }
  234. /// Free a memory block from a mail
  235. osStatus osMailFree(osMailQId queue_id, void *mail)
  236. {
  237. }
  238. /// Put a mail to a queue
  239. osStatus osMailPut(osMailQId queue_id, void *mail)
  240. {
  241. }
  242. /// Get a mail from a queue
  243. osEvent osMailGet(osMailQId queue_id, uint32_t millisec)
  244. {
  245. osEvent ret;
  246. if (queue_id == NULL) {
  247. ret.status = osErrorParameter;
  248. return ret;
  249. }
  250. ret = osMessageGet(*((void **)queue_id), millisec);
  251. if (ret.status == osEventMessage) ret.status = osEventMail;
  252. return ret;
  253. }