2 * Copyright (c) 2013-2017 ARM Limited. All rights reserved.
4 * SPDX-License-Identifier: Apache-2.0
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
10 * www.apache.org/licenses/LICENSE-2.0
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.
18 * ----------------------------------------------------------------------
20 * $Date: 10. January 2017
23 * Project: CMSIS-RTOS API V1
24 * Title: cmsis_os_v1.c V1 module file
25 *---------------------------------------------------------------------------*/
30 #if (osCMSIS >= 0x20000U) && !defined(os1_Disable)
34 #if !defined(os1_Disable_Thread)
35 osThreadId osThreadCreate (const osThreadDef_t *thread_def, void *argument) {
37 if (thread_def == NULL) {
40 return osThreadNew((osThreadFunc_t)thread_def->pthread, argument, &thread_def->attr);
47 #if !defined(os1_Disable_Signal)
49 #define SignalMask ((1U<<osFeature_Signals)-1U)
51 int32_t osSignalSet (osThreadId thread_id, int32_t signals) {
54 flags = osThreadFlagsSet(thread_id, (uint32_t)signals);
55 if ((flags & 0x80000000U) != 0U) {
56 return ((int32_t)0x80000000U);
58 return ((int32_t)(flags & ~((uint32_t)signals)));
61 int32_t osSignalClear (osThreadId thread_id, int32_t signals) {
64 if (thread_id != osThreadGetId()) {
65 return ((int32_t)0x80000000U);
67 flags = osThreadFlagsClear((uint32_t)signals);
68 if ((flags & 0x80000000U) != 0U) {
69 return ((int32_t)0x80000000U);
71 return ((int32_t)flags);
74 os_InRegs osEvent osSignalWait (int32_t signals, uint32_t millisec) {
79 flags = osThreadFlagsWait((uint32_t)signals, osFlagsWaitAll, millisec);
81 flags = osThreadFlagsWait(SignalMask, osFlagsWaitAny, millisec);
83 if ((flags > 0U) && (flags < 0x80000000U)) {
84 event.status = osEventSignal;
85 event.value.signals = (int32_t)flags;
87 switch ((int32_t)flags) {
92 event.status = osEventTimeout;
94 case osErrorParameter:
95 event.status = osErrorValue;
98 event.status = (osStatus)flags;
109 #if !defined(os1_Disable_Timer)
110 osTimerId osTimerCreate (const osTimerDef_t *timer_def, os_timer_type type, void *argument) {
112 if (timer_def == NULL) {
115 return osTimerNew((osTimerFunc_t)timer_def->ptimer, type, argument, &timer_def->attr);
121 #if !defined(os1_Disable_Mutex)
122 osMutexId osMutexCreate (const osMutexDef_t *mutex_def) {
124 if (mutex_def == NULL) {
127 return osMutexNew(mutex_def);
134 #if (defined (osFeature_Semaphore) && (osFeature_Semaphore != 0U)) && !defined(os1_Disable_Semaphore)
136 osSemaphoreId osSemaphoreCreate (const osSemaphoreDef_t *semaphore_def, int32_t count) {
138 if (semaphore_def == NULL) {
141 return osSemaphoreNew((uint32_t)count, (uint32_t)count, semaphore_def);
144 int32_t osSemaphoreWait (osSemaphoreId semaphore_id, uint32_t millisec) {
148 status = osSemaphoreAcquire(semaphore_id, millisec);
151 count = osSemaphoreGetCount(semaphore_id);
152 return ((int32_t)count + 1);
153 case osErrorResource:
167 #if (defined(osFeature_Pool) && (osFeature_Pool != 0))&& !defined(os1_Disable_Pool)
169 osPoolId osPoolCreate (const osPoolDef_t *pool_def) {
171 if (pool_def == NULL) {
174 return osMemoryPoolNew(pool_def->pool_sz, pool_def->item_sz, &pool_def->attr);
177 void *osPoolAlloc (osPoolId pool_id) {
178 return osMemoryPoolAlloc(pool_id, 0U);
181 void *osPoolCAlloc (osPoolId pool_id) {
185 block_size = osMemoryPoolGetBlockSize((osMemoryPoolId_t)pool_id);
186 if (block_size == 0U) {
189 block = osMemoryPoolAlloc(pool_id, 0U);
191 memset(block, 0, block_size);
196 osStatus osPoolFree (osPoolId pool_id, void *block) {
197 return osMemoryPoolFree(pool_id, block);
200 #endif // Memory Pool
205 #if (defined(osFeature_MessageQ) && (osFeature_MessageQ != 0)) && !defined(os1_Disable_MessageQ)
207 osMessageQId osMessageCreate (const osMessageQDef_t *queue_def, osThreadId thread_id) {
210 if (queue_def == NULL) {
213 return osMessageQueueNew(queue_def->queue_sz, sizeof(uint32_t), &queue_def->attr);
216 osStatus osMessagePut (osMessageQId queue_id, uint32_t info, uint32_t millisec) {
217 return osMessageQueuePut(queue_id, &info, 0U, millisec);
220 os_InRegs osEvent osMessageGet (osMessageQId queue_id, uint32_t millisec) {
225 status = osMessageQueueGet(queue_id, &message, NULL, millisec);
228 event.status = osEventMessage;
229 event.value.v = message;
231 case osErrorResource:
235 event.status = osEventTimeout;
238 event.status = status;
244 #endif // Message Queue
249 #if (defined(osFeature_MailQ) && (osFeature_MailQ != 0)) && !defined(os1_Disable_MailQ)
251 typedef struct os_mail_queue_s {
252 osMemoryPoolId_t mp_id;
253 osMessageQueueId_t mq_id;
256 osMailQId osMailCreate (const osMailQDef_t *queue_def, osThreadId thread_id) {
257 os_mail_queue_t *ptr;
260 if (queue_def == NULL) {
264 ptr = queue_def->mail;
269 ptr->mp_id = osMemoryPoolNew (queue_def->queue_sz, queue_def->item_sz, &queue_def->mp_attr);
270 ptr->mq_id = osMessageQueueNew(queue_def->queue_sz, sizeof(void *), &queue_def->mq_attr);
271 if ((ptr->mp_id == NULL) || (ptr->mq_id == NULL)) {
272 if (ptr->mp_id != NULL) {
273 osMemoryPoolDelete(ptr->mp_id);
275 if (ptr->mq_id != NULL) {
276 osMessageQueueDelete(ptr->mq_id);
284 void *osMailAlloc (osMailQId queue_id, uint32_t millisec) {
285 os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
290 return osMemoryPoolAlloc(ptr->mp_id, millisec);
293 void *osMailCAlloc (osMailQId queue_id, uint32_t millisec) {
294 os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
301 block_size = osMemoryPoolGetBlockSize(ptr->mp_id);
302 if (block_size == 0U) {
305 block = osMemoryPoolAlloc(ptr->mp_id, millisec);
307 memset(block, 0, block_size);
314 osStatus osMailPut (osMailQId queue_id, const void *mail) {
315 os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
318 return osErrorParameter;
323 return osMessageQueuePut(ptr->mq_id, &mail, 0U, 0U);
326 os_InRegs osEvent osMailGet (osMailQId queue_id, uint32_t millisec) {
327 os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
333 event.status = osErrorParameter;
337 status = osMessageQueueGet(ptr->mq_id, &mail, NULL, millisec);
340 event.status = osEventMail;
341 event.value.p = mail;
343 case osErrorResource:
347 event.status = osEventTimeout;
350 event.status = status;
356 osStatus osMailFree (osMailQId queue_id, void *mail) {
357 os_mail_queue_t *ptr = (os_mail_queue_t *)queue_id;
360 return osErrorParameter;
365 return osMemoryPoolFree(ptr->mp_id, mail);