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