cmsis_os1.c 8.6 KB

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