cmsis_os1.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright (c) 2013-2017 ARM Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. * ----------------------------------------------------------------------
  19. *
  20. * $Date: 10. January 2017
  21. * $Revision: V1.2
  22. *
  23. * Project: CMSIS-RTOS API V1
  24. * Title: cmsis_os_v1.c V1 module file
  25. *---------------------------------------------------------------------------*/
  26. #include <string.h>
  27. #include "cmsis_os.h"
  28. #if (osCMSIS >= 0x20000U)
  29. // Thread
  30. osThreadId osThreadCreate (const osThreadDef_t *thread_def, void *argument) {
  31. if (thread_def == NULL) {
  32. return (osThreadId)NULL;
  33. }
  34. return osThreadNew((osThreadFunc_t)thread_def->pthread, argument, &thread_def->attr);
  35. }
  36. // Signals
  37. #define SignalMask ((1U<<osFeature_Signals)-1U)
  38. int32_t osSignalSet (osThreadId thread_id, int32_t signals) {
  39. uint32_t flags;
  40. flags = osThreadFlagsSet(thread_id, (uint32_t)signals);
  41. if ((flags & 0x80000000U) != 0U) {
  42. return ((int32_t)0x80000000U);
  43. }
  44. return ((int32_t)(flags & ~((uint32_t)signals)));
  45. }
  46. int32_t osSignalClear (osThreadId thread_id, int32_t signals) {
  47. uint32_t flags;
  48. if (thread_id != osThreadGetId()) {
  49. return ((int32_t)0x80000000U);
  50. }
  51. flags = osThreadFlagsClear((uint32_t)signals);
  52. if ((flags & 0x80000000U) != 0U) {
  53. return ((int32_t)0x80000000U);
  54. }
  55. return ((int32_t)flags);
  56. }
  57. osEvent osSignalWait (int32_t signals, uint32_t millisec) {
  58. osEvent event;
  59. uint32_t flags;
  60. if (signals != 0) {
  61. flags = osThreadFlagsWait((uint32_t)signals, osFlagsWaitAll, millisec);
  62. } else {
  63. flags = osThreadFlagsWait(SignalMask, osFlagsWaitAny, millisec);
  64. }
  65. if ((flags > 0U) && (flags < 0x80000000U)) {
  66. event.status = osEventSignal;
  67. event.value.signals = (int32_t)flags;
  68. } else {
  69. switch ((int32_t)flags) {
  70. case osErrorResource:
  71. event.status = osOK;
  72. break;
  73. case osErrorTimeout:
  74. event.status = osEventTimeout;
  75. break;
  76. case osErrorParameter:
  77. event.status = osErrorValue;
  78. break;
  79. default:
  80. event.status = (osStatus)flags;
  81. break;
  82. }
  83. }
  84. return event;
  85. }
  86. // Timer
  87. osTimerId osTimerCreate (const osTimerDef_t *timer_def, os_timer_type type, void *argument) {
  88. if (timer_def == NULL) {
  89. return (osTimerId)NULL;
  90. }
  91. return osTimerNew((osTimerFunc_t)timer_def->ptimer, type, argument, &timer_def->attr);
  92. }
  93. // Mutex
  94. osMutexId osMutexCreate (const osMutexDef_t *mutex_def) {
  95. if (mutex_def == NULL) {
  96. return (osMutexId)NULL;
  97. }
  98. return osMutexNew(mutex_def);
  99. }
  100. // Semaphore
  101. #if (defined (osFeature_Semaphore) && (osFeature_Semaphore != 0U))
  102. osSemaphoreId osSemaphoreCreate (const osSemaphoreDef_t *semaphore_def, int32_t count) {
  103. if (semaphore_def == NULL) {
  104. return (osSemaphoreId)NULL;
  105. }
  106. return osSemaphoreNew((uint32_t)count, (uint32_t)count, semaphore_def);
  107. }
  108. int32_t osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec) {
  109. osStatus_t status;
  110. uint32_t count;
  111. status = osSemaphoreAcquire(semaphore_id, millisec);
  112. switch (status) {
  113. case osOK:
  114. count = osSemaphoreGetCount(semaphore_id);
  115. return ((int32_t)count + 1);
  116. case osErrorResource:
  117. case osErrorTimeout:
  118. return 0;
  119. default:
  120. break;
  121. }
  122. return -1;
  123. }
  124. #endif // Semaphore
  125. // Memory Pool
  126. #if (defined(osFeature_Pool) && (osFeature_Pool != 0))
  127. osPoolId osPoolCreate (const osPoolDef_t *pool_def) {
  128. if (pool_def == NULL) {
  129. return (osPoolId)NULL;
  130. }
  131. return ((osPoolId)(osMemoryPoolNew(pool_def->pool_sz, pool_def->item_sz, &pool_def->attr)));
  132. }
  133. void *osPoolAlloc (osPoolId pool_id) {
  134. return osMemoryPoolAlloc((osMemoryPoolId_t)pool_id, 0U);
  135. }
  136. void *osPoolCAlloc (osPoolId pool_id) {
  137. void *block;
  138. uint32_t block_size;
  139. block_size = osMemoryPoolGetBlockSize((osMemoryPoolId_t)pool_id);
  140. if (block_size == 0U) {
  141. return NULL;
  142. }
  143. block = osMemoryPoolAlloc((osMemoryPoolId_t)pool_id, 0U);
  144. if (block != NULL) {
  145. memset(block, 0, block_size);
  146. }
  147. return block;
  148. }
  149. osStatus osPoolFree (osPoolId pool_id, void *block) {
  150. return osMemoryPoolFree((osMemoryPoolId_t)pool_id, block);
  151. }
  152. #endif // Memory Pool
  153. // Message Queue
  154. #if (defined(osFeature_MessageQ) && (osFeature_MessageQ != 0))
  155. osMessageQId osMessageCreate (const osMessageQDef_t *queue_def, osThreadId thread_id) {
  156. (void)thread_id;
  157. if (queue_def == NULL) {
  158. return (osMessageQId)NULL;
  159. }
  160. return ((osMessageQId)(osMessageQueueNew(queue_def->queue_sz, sizeof(uint32_t), &queue_def->attr)));
  161. }
  162. osStatus osMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec) {
  163. return osMessageQueuePut((osMessageQueueId_t)queue_id, &info, 0U, millisec);
  164. }
  165. osEvent osMessageGet (osMessageQId queue_id, uint32_t millisec) {
  166. osStatus_t status;
  167. osEvent event;
  168. uint32_t message;
  169. status = osMessageQueueGet((osMessageQueueId_t)queue_id, &message, NULL, millisec);
  170. switch (status) {
  171. case osOK:
  172. event.status = osEventMessage;
  173. event.value.v = message;
  174. break;
  175. case osErrorResource:
  176. event.status = osOK;
  177. break;
  178. case osErrorTimeout:
  179. event.status = osEventTimeout;
  180. break;
  181. default:
  182. event.status = status;
  183. break;
  184. }
  185. return event;
  186. }
  187. #endif // Message Queue
  188. // Mail Queue
  189. #if (defined(osFeature_MailQ) && (osFeature_MailQ != 0))
  190. typedef struct os_mail_queue_s {
  191. osMemoryPoolId_t mp_id;
  192. osMessageQueueId_t mq_id;
  193. } os_mail_queue_t;
  194. osMailQId osMailCreate (const osMailQDef_t *queue_def, osThreadId thread_id) {
  195. os_mail_queue_t *ptr;
  196. (void)thread_id;
  197. if (queue_def == NULL) {
  198. return (osMailQId)NULL;
  199. }
  200. ptr = queue_def->mail;
  201. if (ptr == NULL) {
  202. return (osMailQId)NULL;
  203. }
  204. ptr->mp_id = osMemoryPoolNew (queue_def->queue_sz, queue_def->item_sz, &queue_def->mp_attr);
  205. ptr->mq_id = osMessageQueueNew(queue_def->queue_sz, sizeof(void *), &queue_def->mq_attr);
  206. if ((ptr->mp_id == (osMemoryPoolId_t)NULL) || (ptr->mq_id == (osMessageQueueId_t)NULL)) {
  207. if (ptr->mp_id != (osMemoryPoolId_t)NULL) {
  208. osMemoryPoolDelete(ptr->mp_id);
  209. }
  210. if (ptr->mq_id != (osMessageQueueId_t)NULL) {
  211. osMessageQueueDelete(ptr->mq_id);
  212. }
  213. return (osMailQId)NULL;
  214. }
  215. return (osMailQId)ptr;
  216. }
  217. void *osMailAlloc (osMailQId queue_id, uint32_t millisec) {
  218. os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
  219. if (ptr == NULL) {
  220. return NULL;
  221. }
  222. return osMemoryPoolAlloc(ptr->mp_id, millisec);
  223. }
  224. void *osMailCAlloc (osMailQId queue_id, uint32_t millisec) {
  225. os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
  226. void *block;
  227. uint32_t block_size;
  228. if (ptr == NULL) {
  229. return NULL;
  230. }
  231. block_size = osMemoryPoolGetBlockSize(ptr->mp_id);
  232. if (block_size == 0U) {
  233. return NULL;
  234. }
  235. block = osMemoryPoolAlloc(ptr->mp_id, millisec);
  236. if (block != NULL) {
  237. memset(block, 0, block_size);
  238. }
  239. return block;
  240. }
  241. osStatus osMailPut (osMailQId queue_id, const void *mail) {
  242. os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
  243. if (ptr == NULL) {
  244. return osErrorParameter;
  245. }
  246. if (mail == NULL) {
  247. return osErrorValue;
  248. }
  249. return osMessageQueuePut(ptr->mq_id, &mail, 0U, 0U);
  250. }
  251. osEvent osMailGet (osMailQId queue_id, uint32_t millisec) {
  252. os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
  253. osStatus_t status;
  254. osEvent event;
  255. void *mail;
  256. if (ptr == NULL) {
  257. event.status = osErrorParameter;
  258. return event;
  259. }
  260. status = osMessageQueueGet(ptr->mq_id, &mail, NULL, millisec);
  261. switch (status) {
  262. case osOK:
  263. event.status = osEventMail;
  264. event.value.p = mail;
  265. break;
  266. case osErrorResource:
  267. event.status = osOK;
  268. break;
  269. case osErrorTimeout:
  270. event.status = osEventTimeout;
  271. break;
  272. default:
  273. event.status = status;
  274. break;
  275. }
  276. return event;
  277. }
  278. osStatus osMailFree (osMailQId queue_id, void *mail) {
  279. os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
  280. if (ptr == NULL) {
  281. return osErrorParameter;
  282. }
  283. if (mail == NULL) {
  284. return osErrorValue;
  285. }
  286. return osMemoryPoolFree(ptr->mp_id, mail);
  287. }
  288. #endif // Mail Queue
  289. #endif // osCMSIS