cmsis_rtthread.c 8.0 KB

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