cmsis_os1.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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) && !defined(os1_Disable)
  29. // Thread
  30. #if !defined(os1_Disable_Thread)
  31. osThreadId osThreadCreate (const osThreadDef_t *thread_def, void *argument) {
  32. if (thread_def == NULL) {
  33. return NULL;
  34. }
  35. return osThreadNew((osThreadFunc_t)thread_def->pthread, argument, &thread_def->attr);
  36. }
  37. #endif
  38. // Signals
  39. #if !defined(os1_Disable_Signal)
  40. #define SignalMask ((1U<<osFeature_Signals)-1U)
  41. int32_t osSignalSet (osThreadId thread_id, int32_t signals) {
  42. uint32_t flags;
  43. flags = osThreadFlagsSet(thread_id, (uint32_t)signals);
  44. if ((flags & 0x80000000U) != 0U) {
  45. return ((int32_t)0x80000000U);
  46. }
  47. return ((int32_t)(flags & ~((uint32_t)signals)));
  48. }
  49. int32_t osSignalClear (osThreadId thread_id, int32_t signals) {
  50. uint32_t flags;
  51. if (thread_id != osThreadGetId()) {
  52. return ((int32_t)0x80000000U);
  53. }
  54. flags = osThreadFlagsClear((uint32_t)signals);
  55. if ((flags & 0x80000000U) != 0U) {
  56. return ((int32_t)0x80000000U);
  57. }
  58. return ((int32_t)flags);
  59. }
  60. os_InRegs osEvent osSignalWait (int32_t signals, uint32_t millisec) {
  61. osEvent event;
  62. uint32_t flags;
  63. if (signals != 0) {
  64. flags = osThreadFlagsWait((uint32_t)signals, osFlagsWaitAll, millisec);
  65. } else {
  66. flags = osThreadFlagsWait(SignalMask, osFlagsWaitAny, millisec);
  67. }
  68. if ((flags > 0U) && (flags < 0x80000000U)) {
  69. event.status = osEventSignal;
  70. event.value.signals = (int32_t)flags;
  71. } else {
  72. switch ((int32_t)flags) {
  73. case osErrorResource:
  74. event.status = osOK;
  75. break;
  76. case osErrorTimeout:
  77. event.status = osEventTimeout;
  78. break;
  79. case osErrorParameter:
  80. event.status = osErrorValue;
  81. break;
  82. default:
  83. event.status = (osStatus)flags;
  84. break;
  85. }
  86. }
  87. return event;
  88. }
  89. #endif // Signal
  90. // Timer
  91. #if !defined(os1_Disable_Timer)
  92. osTimerId osTimerCreate (const osTimerDef_t *timer_def, os_timer_type type, void *argument) {
  93. if (timer_def == NULL) {
  94. return NULL;
  95. }
  96. return osTimerNew((osTimerFunc_t)timer_def->ptimer, type, argument, &timer_def->attr);
  97. }
  98. #endif
  99. // Mutex
  100. #if !defined(os1_Disable_Mutex)
  101. osMutexId osMutexCreate (const osMutexDef_t *mutex_def) {
  102. if (mutex_def == NULL) {
  103. return NULL;
  104. }
  105. return osMutexNew(mutex_def);
  106. }
  107. #endif
  108. // Semaphore
  109. #if (defined (osFeature_Semaphore) && (osFeature_Semaphore != 0U)) && !defined(os1_Disable_Semaphore)
  110. osSemaphoreId osSemaphoreCreate (const osSemaphoreDef_t *semaphore_def, int32_t count) {
  111. if (semaphore_def == NULL) {
  112. return NULL;
  113. }
  114. return osSemaphoreNew((uint32_t)count, (uint32_t)count, semaphore_def);
  115. }
  116. int32_t osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec) {
  117. osStatus_t status;
  118. uint32_t count;
  119. status = osSemaphoreAcquire(semaphore_id, millisec);
  120. switch (status) {
  121. case osOK:
  122. count = osSemaphoreGetCount(semaphore_id);
  123. return ((int32_t)count + 1);
  124. case osErrorResource:
  125. case osErrorTimeout:
  126. return 0;
  127. default:
  128. break;
  129. }
  130. return -1;
  131. }
  132. #endif // Semaphore
  133. // Memory Pool
  134. #if (defined(osFeature_Pool) && (osFeature_Pool != 0))&& !defined(os1_Disable_Pool)
  135. osPoolId osPoolCreate (const osPoolDef_t *pool_def) {
  136. if (pool_def == NULL) {
  137. return NULL;
  138. }
  139. return osMemoryPoolNew(pool_def->pool_sz, pool_def->item_sz, &pool_def->attr);
  140. }
  141. void *osPoolAlloc (osPoolId pool_id) {
  142. return osMemoryPoolAlloc(pool_id, 0U);
  143. }
  144. void *osPoolCAlloc (osPoolId pool_id) {
  145. void *block;
  146. uint32_t block_size;
  147. block_size = osMemoryPoolGetBlockSize((osMemoryPoolId_t)pool_id);
  148. if (block_size == 0U) {
  149. return NULL;
  150. }
  151. block = osMemoryPoolAlloc(pool_id, 0U);
  152. if (block != NULL) {
  153. memset(block, 0, block_size);
  154. }
  155. return block;
  156. }
  157. osStatus osPoolFree (osPoolId pool_id, void *block) {
  158. return osMemoryPoolFree(pool_id, block);
  159. }
  160. #endif // Memory Pool
  161. // Message Queue
  162. #if (defined(osFeature_MessageQ) && (osFeature_MessageQ != 0)) && !defined(os1_Disable_MessageQ)
  163. osMessageQId osMessageCreate (const osMessageQDef_t *queue_def, osThreadId thread_id) {
  164. (void)thread_id;
  165. if (queue_def == NULL) {
  166. return NULL;
  167. }
  168. return osMessageQueueNew(queue_def->queue_sz, sizeof(uint32_t), &queue_def->attr);
  169. }
  170. osStatus osMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec) {
  171. return osMessageQueuePut(queue_id, &info, 0U, millisec);
  172. }
  173. os_InRegs osEvent osMessageGet (osMessageQId queue_id, uint32_t millisec) {
  174. osStatus_t status;
  175. osEvent event;
  176. uint32_t message;
  177. status = osMessageQueueGet(queue_id, &message, NULL, millisec);
  178. switch (status) {
  179. case osOK:
  180. event.status = osEventMessage;
  181. event.value.v = message;
  182. break;
  183. case osErrorResource:
  184. event.status = osOK;
  185. break;
  186. case osErrorTimeout:
  187. event.status = osEventTimeout;
  188. break;
  189. default:
  190. event.status = status;
  191. break;
  192. }
  193. return event;
  194. }
  195. #endif // Message Queue
  196. // Mail Queue
  197. #if (defined(osFeature_MailQ) && (osFeature_MailQ != 0)) && !defined(os1_Disable_MailQ)
  198. typedef struct os_mail_queue_s {
  199. osMemoryPoolId_t mp_id;
  200. osMessageQueueId_t mq_id;
  201. } os_mail_queue_t;
  202. osMailQId osMailCreate (const osMailQDef_t *queue_def, osThreadId thread_id) {
  203. os_mail_queue_t *ptr;
  204. (void)thread_id;
  205. if (queue_def == NULL) {
  206. return NULL;
  207. }
  208. ptr = queue_def->mail;
  209. if (ptr == NULL) {
  210. return NULL;
  211. }
  212. ptr->mp_id = osMemoryPoolNew (queue_def->queue_sz, queue_def->item_sz, &queue_def->mp_attr);
  213. ptr->mq_id = osMessageQueueNew(queue_def->queue_sz, sizeof(void *), &queue_def->mq_attr);
  214. if ((ptr->mp_id == NULL) || (ptr->mq_id == NULL)) {
  215. if (ptr->mp_id != NULL) {
  216. osMemoryPoolDelete(ptr->mp_id);
  217. }
  218. if (ptr->mq_id != NULL) {
  219. osMessageQueueDelete(ptr->mq_id);
  220. }
  221. return NULL;
  222. }
  223. return ptr;
  224. }
  225. void *osMailAlloc (osMailQId queue_id, uint32_t millisec) {
  226. os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
  227. if (ptr == NULL) {
  228. return NULL;
  229. }
  230. return osMemoryPoolAlloc(ptr->mp_id, millisec);
  231. }
  232. void *osMailCAlloc (osMailQId queue_id, uint32_t millisec) {
  233. os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
  234. void *block;
  235. uint32_t block_size;
  236. if (ptr == NULL) {
  237. return NULL;
  238. }
  239. block_size = osMemoryPoolGetBlockSize(ptr->mp_id);
  240. if (block_size == 0U) {
  241. return NULL;
  242. }
  243. block = osMemoryPoolAlloc(ptr->mp_id, millisec);
  244. if (block != NULL) {
  245. memset(block, 0, block_size);
  246. }
  247. return block;
  248. }
  249. osStatus osMailPut (osMailQId queue_id, const void *mail) {
  250. os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
  251. if (ptr == NULL) {
  252. return osErrorParameter;
  253. }
  254. if (mail == NULL) {
  255. return osErrorValue;
  256. }
  257. return osMessageQueuePut(ptr->mq_id, &mail, 0U, 0U);
  258. }
  259. os_InRegs osEvent osMailGet (osMailQId queue_id, uint32_t millisec) {
  260. os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
  261. osStatus_t status;
  262. osEvent event;
  263. void *mail;
  264. if (ptr == NULL) {
  265. event.status = osErrorParameter;
  266. return event;
  267. }
  268. status = osMessageQueueGet(ptr->mq_id, &mail, NULL, millisec);
  269. switch (status) {
  270. case osOK:
  271. event.status = osEventMail;
  272. event.value.p = mail;
  273. break;
  274. case osErrorResource:
  275. event.status = osOK;
  276. break;
  277. case osErrorTimeout:
  278. event.status = osEventTimeout;
  279. break;
  280. default:
  281. event.status = status;
  282. break;
  283. }
  284. return event;
  285. }
  286. osStatus osMailFree (osMailQId queue_id, void *mail) {
  287. os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
  288. if (ptr == NULL) {
  289. return osErrorParameter;
  290. }
  291. if (mail == NULL) {
  292. return osErrorValue;
  293. }
  294. return osMemoryPoolFree(ptr->mp_id, mail);
  295. }
  296. #endif // Mail Queue
  297. #endif // osCMSIS