]> begriffs open source - cmsis-freertos/blob - CMSIS/RTOS2/FreeRTOS/Source/cmsis_os2.c
Merge branch 'develop' of https://github.com/ARM-software/CMSIS-FreeRTOS into feature...
[cmsis-freertos] / CMSIS / RTOS2 / FreeRTOS / Source / cmsis_os2.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  *      Name:    cmsis_os2.c
19  *      Purpose: CMSIS RTOS2 wrapper for FreeRTOS
20  *
21  *---------------------------------------------------------------------------*/
22
23 #include <string.h>
24
25 #include "RTE_Components.h"             // Component selection
26 #include CMSIS_device_header
27
28 #include "cmsis_os2.h"                  // ::CMSIS:RTOS2
29 #include "cmsis_compiler.h"
30
31 #include "FreeRTOS.h"                   // ARM.FreeRTOS::RTOS:Core
32 #include "task.h"                       // ARM.FreeRTOS::RTOS:Core
33 #include "event_groups.h"               // ARM.FreeRTOS::RTOS:Event Groups
34 #include "semphr.h"                     // ARM.FreeRTOS::RTOS:Core
35
36 /*---------------------------------------------------------------------------*/
37
38 #if   ((__ARM_ARCH_7M__      == 1U) || \
39        (__ARM_ARCH_7EM__     == 1U) || \
40        (__ARM_ARCH_8M_MAIN__ == 1U))
41 #define IS_IRQ_MASKED()           ((__get_PRIMASK() != 0U) || ((KernelState == osKernelRunning) && (__get_BASEPRI() != 0U)))
42 #else
43 #define IS_IRQ_MASKED()           (__get_PRIMASK() != 0U)
44 #endif
45
46 #if    (__ARM_ARCH_7A__      == 1U)
47 #define IS_IRQ() (__get_mode() == 0x12U)
48 #else
49 #define IS_IRQ_MODE()             (__get_IPSR() != 0U)
50 #define IS_IRQ()                  (IS_IRQ_MODE() || IS_IRQ_MASKED())
51 #endif
52
53 /* Limits */
54 #define MAX_BITS_TASK_NOTIFY      31U
55 #define MAX_BITS_EVENT_GROUPS     24U
56
57 #define THREAD_FLAGS_INVALID_BITS (~((1UL << MAX_BITS_TASK_NOTIFY)  - 1U))
58 #define EVENT_FLAGS_INVALID_BITS  (~((1UL << MAX_BITS_EVENT_GROUPS) - 1U))
59
60 /* Kernel version and identification string definition */
61 #define KERNEL_VERSION            (((uint32_t)tskKERNEL_VERSION_MAJOR * 10000000UL) | \
62                                    ((uint32_t)tskKERNEL_VERSION_MINOR *    10000UL) | \
63                                    ((uint32_t)tskKERNEL_VERSION_BUILD *        1UL))
64
65 #define KERNEL_ID                 "FreeRTOS V9.0.0"
66
67 /* Timer callback information structure definition */
68 typedef struct {
69   osTimerFunc_t func;
70   void         *arg;
71 } TimerCallback_t;
72
73 /* Kernel initialization state */
74 static osKernelState_t KernelState;
75
76 /* Heap region definition used by heap_5 variant */
77 #if defined(RTE_RTOS_FreeRTOS_HEAP_5)
78 #if (configAPPLICATION_ALLOCATED_HEAP == 1)
79 /*
80   The application writer has already defined the array used for the RTOS
81   heap - probably so it can be placed in a special segment or address.
82 */
83   extern uint8_t ucHeap[configTOTAL_HEAP_SIZE];
84 #else
85   static uint8_t ucHeap[configTOTAL_HEAP_SIZE];
86 #endif /* configAPPLICATION_ALLOCATED_HEAP */
87
88 static HeapRegion_t xHeapRegions[] = {
89   { ucHeap, configTOTAL_HEAP_SIZE },
90   { NULL,   0                     }
91 };
92 #endif /* RTE_RTOS_FreeRTOS_HEAP_5 */
93
94 /*---------------------------------------------------------------------------*/
95
96 #if (__ARM_ARCH_7A__ == 0U)
97 /* FreeRTOS SysTick handler prototypes */
98 extern void SysTick_Handler     (void);
99 extern void xPortSysTickHandler (void);
100
101 void SysTick_Handler (void) {
102   /* Clear overflow flag */
103   SysTick->CTRL;
104
105   /* Call tick handler */
106   xPortSysTickHandler();
107 }
108 #endif
109
110 /*---------------------------------------------------------------------------*/
111
112 osStatus_t osKernelInitialize (void) {
113   osStatus_t stat;
114
115   if (IS_IRQ()) {
116     stat = osErrorISR;
117   }
118   else {
119     #if defined(RTE_RTOS_FreeRTOS_HEAP_5)
120       vPortDefineHeapRegions (xHeapRegions);
121     #endif
122     KernelState = osKernelReady;
123     stat = osOK;
124   }
125
126   return (stat);
127 }
128
129 osStatus_t osKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size) {
130   osStatus_t stat;
131
132   if (IS_IRQ()) {
133     stat = osErrorISR;
134   }
135   else {
136     if (version != NULL) {
137       version->api    = KERNEL_VERSION;
138       version->kernel = KERNEL_VERSION;
139     }
140
141     if ((id_buf != NULL) && (id_size != 0U)) {
142       if (id_size > sizeof(KERNEL_ID)) {
143         id_size = sizeof(KERNEL_ID);
144       }
145       memcpy(id_buf, KERNEL_ID, id_size);
146     }
147     stat = osOK;
148   }
149
150   return (stat);
151 }
152
153 osKernelState_t osKernelGetState (void) {
154   osKernelState_t state;
155
156   if (IS_IRQ()) {
157     state = osKernelError;
158   }
159   else {
160     switch (xTaskGetSchedulerState()) {
161       case taskSCHEDULER_RUNNING:
162         state = osKernelRunning;
163         break;
164
165       case taskSCHEDULER_SUSPENDED:
166         state = osKernelLocked;
167         break;
168
169       case taskSCHEDULER_NOT_STARTED:
170       default:
171         if (KernelState == osKernelReady) {
172           state = osKernelReady;
173         } else {
174           state = osKernelInactive;
175         }
176         break;
177     }
178   }
179   return (state);
180 }
181
182 osStatus_t osKernelStart (void) {
183   osStatus_t stat;
184
185   if (IS_IRQ()) {
186     stat = osErrorISR;
187   }
188   else {
189     KernelState = osKernelRunning;
190     vTaskStartScheduler();
191     stat = osOK;
192   }
193
194   return (stat);
195 }
196
197 int32_t osKernelLock (void) {
198   int32_t lock;
199
200   if (IS_IRQ()) {
201     lock = (int32_t)osErrorISR;
202   }
203   else {
204     switch (xTaskGetSchedulerState()) {
205       case taskSCHEDULER_SUSPENDED:
206         lock = 1;
207         break;
208
209       case taskSCHEDULER_RUNNING:
210         vTaskSuspendAll();
211         lock = 0;
212         break;
213
214       case taskSCHEDULER_NOT_STARTED:
215       default:
216         lock = (int32_t)osError;
217         break;
218     }
219   }
220
221   return (lock);
222 }
223
224 int32_t osKernelUnlock (void) {
225   int32_t lock;
226
227   if (IS_IRQ()) {
228     lock = (int32_t)osErrorISR;
229   }
230   else {
231     switch (xTaskGetSchedulerState()) {
232       case taskSCHEDULER_SUSPENDED:
233         if (xTaskResumeAll() == pdTRUE) {
234           lock = 1;
235         } else {
236           lock = (int32_t)osError;
237         }
238         break;
239
240       case taskSCHEDULER_RUNNING:
241         lock = 0;
242         break;
243
244       case taskSCHEDULER_NOT_STARTED:
245       default:
246         lock = (int32_t)osError;
247         break;
248     }
249   }
250
251   return (lock);
252 }
253
254 int32_t osKernelRestoreLock (int32_t lock) {
255
256   if (IS_IRQ()) {
257     lock = (int32_t)osErrorISR;
258   }
259   else {
260     switch (xTaskGetSchedulerState()) {
261       case taskSCHEDULER_SUSPENDED:
262       case taskSCHEDULER_RUNNING:
263         if (lock == 1) {
264           vTaskSuspendAll();
265         }
266         else {
267           if ((lock != 0) || (xTaskResumeAll() != pdTRUE)) {
268             lock = (int32_t)osError;
269           }
270         }
271         break;
272
273       case taskSCHEDULER_NOT_STARTED:
274       default:
275         lock = (int32_t)osError;
276         break;
277     }
278   }
279
280   return (lock);
281 }
282
283 uint64_t osKernelGetTickCount (void) {
284   TickType_t ticks;
285
286   if (IS_IRQ()) {
287     ticks = xTaskGetTickCountFromISR();
288   } else {
289     ticks = xTaskGetTickCount();
290   }
291
292   return ((uint64_t)ticks);
293 }
294
295 uint32_t osKernelGetTickFreq (void) {
296   uint32_t freq;
297
298   if (IS_IRQ()) {
299     freq = 0U;
300   } else {
301     freq = configTICK_RATE_HZ;
302   }
303
304   return (freq);
305 }
306
307 uint32_t osKernelGetSysTimerCount (void) {
308   TickType_t ticks;
309   uint32_t val;
310
311   portDISABLE_INTERRUPTS();
312
313   ticks = xTaskGetTickCount();
314
315 #if (__ARM_ARCH_7A__ == 1U)
316 #if (__CORTEX_A == 9U)
317   /* Cortex-A9 uses Private Timer */
318   val = PTIM_GetLoadValue() - PTIM_GetCurrentValue();
319   val += ticks * (PTIM_GetLoadValue() + 1U);
320 #endif
321 #else
322   val = SysTick->LOAD - SysTick->VAL;
323
324   if ((SysTick->CTRL >> 16) & 1U) {
325     /* Overflow */
326     val = SysTick->LOAD - SysTick->VAL;
327     ticks++;
328   }
329   val += ticks * (SysTick->LOAD + 1U);
330 #endif
331
332   portENABLE_INTERRUPTS();
333
334   return (val);
335 }
336
337 uint32_t osKernelGetSysTimerFreq (void) {
338   return (configCPU_CLOCK_HZ);
339 }
340
341 /*---------------------------------------------------------------------------*/
342
343 osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) {
344   char empty;
345   const char *name;
346   uint32_t stack;
347   TaskHandle_t h;
348   UBaseType_t prio;
349   int32_t mem;
350
351   h = NULL;
352
353   if (!IS_IRQ() && (func != NULL)) {
354     stack = configMINIMAL_STACK_SIZE;
355     prio  = (UBaseType_t)osPriorityNormal;
356
357     empty = '\0';
358     name  = &empty;
359     mem   = -1;
360
361     if (attr != NULL) {
362       if (attr->name != NULL) {
363         name = attr->name;
364       }
365       if (attr->priority != osPriorityNone) {
366         prio = (UBaseType_t)attr->priority;
367       }
368
369       if ((prio < osPriorityIdle) || (prio > osPriorityISR) || ((attr->attr_bits & osThreadJoinable) == osThreadJoinable)) {
370         return (NULL);
371       }
372
373       if (attr->stack_size > 0U) {
374         stack = attr->stack_size;
375       }
376
377       if ((attr->cb_mem    != NULL) && (attr->cb_size    >= sizeof(StaticTask_t)) &&
378           (attr->stack_mem != NULL) && (attr->stack_size >  0U)) {
379         mem = 1;
380       }
381       else {
382         if ((attr->cb_mem == NULL) && (attr->cb_size == 0U) && (attr->stack_mem == NULL)) {
383           mem = 0;
384         }
385       }
386     }
387     else {
388       mem = 0;
389     }
390
391     if (mem == 1) {
392       h = xTaskCreateStatic ((TaskFunction_t)func, name, stack, argument, prio, (StackType_t  *)attr->stack_mem,
393                                                                                 (StaticTask_t *)attr->cb_mem);
394     }
395     else {
396       if (mem == 0) {
397         if (xTaskCreate ((TaskFunction_t)func, name, stack, argument, prio, &h) != pdPASS) {
398           h = NULL;
399         }
400       }
401     }
402   }
403
404   return ((osThreadId_t)h);
405 }
406
407 const char *osThreadGetName (osThreadId_t thread_id) {
408   const char *name;
409
410   if (IS_IRQ() || (thread_id == NULL)) {
411     name = NULL;
412   } else {
413     name = pcTaskGetName ((TaskHandle_t)thread_id);
414   }
415
416   return (name);
417 }
418
419 osThreadId_t osThreadGetId (void) {
420   osThreadId_t id;
421
422   if (IS_IRQ()) {
423     id = NULL;
424   } else {
425     id = (osThreadId_t)xTaskGetCurrentTaskHandle();
426   }
427
428   return (id);
429 }
430
431 osThreadState_t osThreadGetState (osThreadId_t thread_id) {
432   osThreadState_t state;
433
434   if (IS_IRQ() || (thread_id == NULL)) {
435     state = osThreadError;
436   }
437   else {
438     switch (eTaskGetState ((TaskHandle_t)thread_id)) {
439       case eRunning:   state = osThreadRunning;    break;
440       case eReady:     state = osThreadReady;      break;
441       case eBlocked:
442       case eSuspended: state = osThreadBlocked;    break;
443       case eDeleted:   state = osThreadTerminated; break;
444       case eInvalid:
445       default:         state = osThreadError;      break;
446     }
447   }
448
449   return (state);
450 }
451
452 uint32_t osThreadGetStackSpace (osThreadId_t thread_id) {
453   uint32_t sz;
454
455   if (IS_IRQ() || (thread_id == NULL)) {
456     sz = 0U;
457   } else {
458     sz = (uint32_t)uxTaskGetStackHighWaterMark ((TaskHandle_t)thread_id);
459   }
460
461   return (sz);
462 }
463
464 osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
465   osStatus_t stat;
466
467   if (IS_IRQ()) {
468     stat = osErrorISR;
469   }
470   else if ((thread_id == NULL) || (priority < osPriorityIdle) || (priority > osPriorityISR)) {
471     stat = osErrorParameter;
472   }
473   else {
474     stat = osOK;
475     vTaskPrioritySet ((TaskHandle_t)thread_id, (UBaseType_t)priority);
476   }
477
478   return (stat);
479 }
480
481 osPriority_t osThreadGetPriority (osThreadId_t thread_id) {
482   osPriority_t prio;
483
484   if (IS_IRQ() || (thread_id == NULL)) {
485     prio = osPriorityError;
486   } else {
487     prio = (osPriority_t)uxTaskPriorityGet ((TaskHandle_t)thread_id);
488   }
489
490   return (prio);
491 }
492
493 osStatus_t osThreadYield (void) {
494   osStatus_t stat;
495
496   if (IS_IRQ()) {
497     stat = osErrorISR;
498   } else {
499     stat = osOK;
500     taskYIELD();
501   }
502
503   return (stat);
504 }
505
506 osStatus_t osThreadSuspend (osThreadId_t thread_id) {
507   osStatus_t stat;
508
509   if (IS_IRQ()) {
510     stat = osErrorISR;
511   }
512   else if (thread_id == NULL) {
513     stat = osErrorParameter;
514   }
515   else {
516     stat = osOK;
517     vTaskSuspend ((TaskHandle_t)thread_id);
518   }
519
520   return (stat);
521 }
522
523 osStatus_t osThreadResume (osThreadId_t thread_id) {
524   osStatus_t stat;
525
526   if (IS_IRQ()) {
527     stat = osErrorISR;
528   }
529   else if (thread_id == NULL) {
530     stat = osErrorParameter;
531   }
532   else {
533     stat = osOK;
534     vTaskResume ((TaskHandle_t)thread_id);
535   }
536
537   return (stat);
538 }
539
540 __NO_RETURN void osThreadExit (void) {
541 #ifndef RTE_RTOS_FreeRTOS_HEAP_1
542   vTaskDelete (NULL);
543 #endif
544   for (;;);
545 }
546
547 osStatus_t osThreadTerminate (osThreadId_t thread_id) {
548   osStatus_t stat;
549 #ifndef RTE_RTOS_FreeRTOS_HEAP_1
550   eTaskState tstate;
551
552   if (IS_IRQ()) {
553     stat = osErrorISR;
554   }
555   else if (thread_id == NULL) {
556     stat = osErrorParameter;
557   }
558   else {
559     tstate = eTaskGetState ((TaskHandle_t)thread_id);
560
561     if (tstate != eDeleted) {
562       stat = osOK;
563       vTaskDelete ((TaskHandle_t)thread_id);
564     } else {
565       stat = osErrorResource;
566     }
567   }
568 #else
569   stat = osError;
570 #endif
571
572   return (stat);
573 }
574
575 uint32_t osThreadGetCount (void) {
576   uint32_t count;
577
578   if (IS_IRQ()) {
579     count = 0U;
580   } else {
581     count = uxTaskGetNumberOfTasks();
582   }
583
584   return (count);
585 }
586
587 uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
588   uint32_t i, count;
589   TaskStatus_t *task;
590
591   if (IS_IRQ() || (thread_array == NULL) || (array_items == 0U)) {
592     count = 0U;
593   } else {
594     vTaskSuspendAll();
595
596     count = uxTaskGetNumberOfTasks();
597     task  = pvPortMalloc (count * sizeof(TaskStatus_t));
598
599     if (task != NULL) {
600       count = uxTaskGetSystemState (task, count, NULL);
601
602       for (i = 0U; (i < count) && (i < array_items); i++) {
603         thread_array[i] = (osThreadId_t)task[i].xHandle;
604       }
605       count = i;
606     }
607     (void)xTaskResumeAll();
608
609     vPortFree (task);
610   }
611
612   return (count);
613 }
614
615 uint32_t osThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
616   TaskHandle_t thread = (TaskHandle_t)thread_id;
617
618   if ((thread == NULL) || ((flags & THREAD_FLAGS_INVALID_BITS) != 0U)) {
619     flags = (uint32_t)osErrorParameter;
620   }
621   else if (IS_IRQ()) {
622     if (xTaskNotifyFromISR (thread, flags, eSetBits, NULL) != pdPASS) {
623       flags = (uint32_t)osError;
624     }
625   }
626   else {
627     if (xTaskNotify (thread, flags, eSetBits) != pdPASS) {
628       flags = (uint32_t)osError;
629     }
630   }
631   /* Return flags after setting */
632   return (flags);
633 }
634
635 uint32_t osThreadFlagsClear (uint32_t flags) {
636   TaskHandle_t thread;
637   uint32_t rflags, cflags;
638
639   if (IS_IRQ()) {
640     rflags = (uint32_t)osErrorISR;
641   }
642   else if ((flags & THREAD_FLAGS_INVALID_BITS) != 0U) {
643     rflags = (uint32_t)osErrorParameter;
644   }
645   else {
646     thread = xTaskGetCurrentTaskHandle();
647
648     if (xTaskNotifyAndQuery (thread, 0, eNoAction, &cflags) == pdPASS) {
649       rflags = cflags;
650       cflags &= ~flags;
651
652       if (xTaskNotify (thread, cflags, eSetValueWithOverwrite) != pdPASS) {
653         rflags = (uint32_t)osError;
654       }
655     }
656     else {
657       rflags = (uint32_t)osError;
658     }
659   }
660
661   /* Return flags before clearing */
662   return (rflags);
663 }
664
665 uint32_t osThreadFlagsGet (void) {
666   TaskHandle_t thread;
667   uint32_t rflags;
668
669   if (IS_IRQ()) {
670     rflags = (uint32_t)osErrorISR;
671   }
672   else {
673     thread = xTaskGetCurrentTaskHandle();
674
675     if (xTaskNotifyAndQuery (thread, 0, eNoAction, &rflags) != pdPASS) {
676       rflags = (uint32_t)osError;
677     }
678   }
679
680   return (rflags);
681 }
682
683 uint32_t osThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout) {
684   uint32_t rflags, nval;
685   uint32_t clear;
686   TickType_t t0;
687
688   if (IS_IRQ()) {
689     rflags = (uint32_t)osErrorISR;
690   }
691   else if ((flags & THREAD_FLAGS_INVALID_BITS) != 0U) {
692     rflags = (uint32_t)osErrorParameter;
693   }
694   else {
695     if ((options & osFlagsNoClear) == osFlagsNoClear) {
696       clear = 0U;
697     } else {
698       clear = flags;
699     }
700
701     rflags = 0U;
702
703     t0 = xTaskGetTickCount();
704     do {
705       if (xTaskNotifyWait (0, clear, &nval, timeout) == pdPASS) {
706         rflags |= nval;
707
708         if ((options & osFlagsWaitAll) == osFlagsWaitAll) {
709           if ((flags & rflags) == flags) {
710             break;
711           } else {
712             if (timeout == 0U) {
713               rflags = (uint32_t)osErrorResource;
714             }
715           }
716         }
717         else {
718           if ((flags & rflags) != 0) {
719             break;
720           } else {
721             if (timeout == 0U) {
722               rflags = (uint32_t)osErrorResource;
723             }
724           }
725         }
726       }
727       else {
728         if (timeout == 0) {
729           rflags = (uint32_t)osErrorResource;
730         } else {
731           rflags = (uint32_t)osErrorTimeout;
732         }
733         break;
734       }
735     }
736     while ((xTaskGetTickCount() - t0) < timeout);
737   }
738
739   /* Return flags before clearing */
740   return (rflags);
741 }
742
743 osStatus_t osDelay (uint32_t ticks) {
744   osStatus_t stat;
745
746   if (IS_IRQ()) {
747     stat = osErrorISR;
748   }
749   else {
750     stat = osOK;
751
752     if (ticks != 0U) {
753       vTaskDelay(ticks);
754     }
755   }
756
757   return (stat);
758 }
759
760 osStatus_t osDelayUntil (uint64_t ticks) {
761   TickType_t tcnt;
762   osStatus_t stat;
763
764   if (IS_IRQ()) {
765     stat = osErrorISR;
766   }
767   else {
768     stat = osOK;
769     tcnt = xTaskGetTickCount();
770
771     vTaskDelayUntil (&tcnt, (TickType_t)ticks);
772   }
773
774   return (stat);
775 }
776
777 /*---------------------------------------------------------------------------*/
778
779 static void TimerCallback (TimerHandle_t hTimer) {
780   TimerCallback_t *callb;
781
782   callb = (TimerCallback_t *)pvTimerGetTimerID (hTimer);
783
784   if (callb != NULL) {
785     callb->func (callb->arg);
786   }
787 }
788
789 osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr) {
790   const char *name;
791   TimerHandle_t h;
792   TimerCallback_t *callb;
793   UBaseType_t reload;
794   int32_t mem;
795
796   h = NULL;
797
798   if (!IS_IRQ() && (func != NULL)) {
799     /* Allocate memory to store callback function and argument */
800     callb = pvPortMalloc (sizeof(TimerCallback_t));
801
802     if (callb != NULL) {
803       callb->func = func;
804       callb->arg  = argument;
805
806       if (type == osTimerOnce) {
807         reload = pdFALSE;
808       } else {
809         reload = pdTRUE;
810       }
811
812       mem  = -1;
813       name = NULL;
814
815       if (attr != NULL) {
816         if (attr->name != NULL) {
817           name = attr->name;
818         }
819
820         if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticTimer_t))) {
821           mem = 1;
822         }
823         else {
824           if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
825             mem = 0;
826           }
827         }
828       }
829       else {
830         mem = 0;
831       }
832
833       if (mem == 1) {
834         h = xTimerCreateStatic (name, 1, reload, callb, TimerCallback, (StaticTimer_t *)attr->cb_mem);
835       }
836       else {
837         if (mem == 0) {
838           h = xTimerCreate (name, 1, reload, callb, TimerCallback);
839         }
840       }
841     }
842   }
843
844   return ((osTimerId_t)h);
845 }
846
847 const char *osTimerGetName (osTimerId_t timer_id) {
848   const char *p;
849
850   if (IS_IRQ() || (timer_id == NULL)) {
851     p = NULL;
852   } else {
853     p = pcTimerGetName ((TimerHandle_t)timer_id);
854   }
855
856   return (p);
857 }
858
859 osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks) {
860   osStatus_t stat;
861
862   if (IS_IRQ()) {
863     stat = osErrorISR;
864   }
865   else if (timer_id == NULL) {
866     stat = osErrorParameter;
867   }
868   else {
869     if (xTimerChangePeriod ((TimerHandle_t)timer_id, ticks, 0) == pdPASS) {
870       stat = osOK;
871     } else {
872       stat = osErrorResource;
873     }
874   }
875
876   return (stat);
877 }
878
879 osStatus_t osTimerStop (osTimerId_t timer_id) {
880   osStatus_t stat;
881
882   if (IS_IRQ()) {
883     stat = osErrorISR;
884   }
885   else if (timer_id == NULL) {
886     stat = osErrorParameter;
887   }
888   else {
889     if (xTimerIsTimerActive ((TimerHandle_t)timer_id) == pdFALSE) {
890       stat = osErrorResource;
891     }
892     else {
893       if (xTimerStop ((TimerHandle_t)timer_id, 0) == pdPASS) {
894         stat = osOK;
895       } else {
896         stat = osError;
897       }
898     }
899   }
900
901   return (stat);
902 }
903
904 uint32_t osTimerIsRunning (osTimerId_t timer_id) {
905   uint32_t running;
906
907   if (IS_IRQ() || (timer_id == NULL)) {
908     running = 0U;
909   } else {
910     running = xTimerIsTimerActive ((TimerHandle_t)timer_id);
911   }
912
913   return (running);
914 }
915
916 osStatus_t osTimerDelete (osTimerId_t timer_id) {
917   osStatus_t stat;
918 #ifndef RTE_RTOS_FreeRTOS_HEAP_1
919   TimerCallback_t *callb;
920
921   if (IS_IRQ()) {
922     stat = osErrorISR;
923   }
924   else if (timer_id == NULL) {
925     stat = osErrorParameter;
926   }
927   else {
928     callb = (TimerCallback_t *)pvTimerGetTimerID ((TimerHandle_t)timer_id);
929
930     if (xTimerDelete ((TimerHandle_t)timer_id, 0) == pdPASS) {
931       vPortFree (callb);
932       stat = osOK;
933     } else {
934       stat = osErrorResource;
935     }
936   }
937 #else
938   stat = osError;
939 #endif
940
941   return (stat);
942 }
943
944 /*---------------------------------------------------------------------------*/
945
946 osEventFlagsId_t osEventFlagsNew (const osEventFlagsAttr_t *attr) {
947   EventGroupHandle_t h;
948   int32_t mem;
949
950   h = NULL;
951
952   if (!IS_IRQ()) {
953     mem = -1;
954
955     if (attr != NULL) {
956       if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticEventGroup_t))) {
957         mem = 1;
958       }
959       else {
960         if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
961           mem = 0;
962         }
963       }
964     }
965     else {
966       mem = 0;
967     }
968
969     if (mem == 1) {
970       h = xEventGroupCreateStatic (attr->cb_mem);
971     }
972     else {
973       if (mem == 0) {
974         h = xEventGroupCreate();
975       }
976     }
977   }
978
979   return ((osEventFlagsId_t)h);
980 }
981
982 uint32_t osEventFlagsSet (osEventFlagsId_t ef_id, uint32_t flags) {
983   uint32_t rflags;
984
985   if ((ef_id == NULL) || ((flags & EVENT_FLAGS_INVALID_BITS) != 0U)) {
986     rflags = (uint32_t)osErrorParameter;
987   }
988   else if (IS_IRQ()) {
989     if (xEventGroupSetBitsFromISR ((EventGroupHandle_t)ef_id, (EventBits_t)flags, NULL) == pdPASS) {
990       rflags = flags;
991     } else {
992       rflags = (uint32_t)osErrorResource;
993     }
994   }
995   else {
996     rflags = xEventGroupSetBits ((EventGroupHandle_t)ef_id, (EventBits_t)flags);
997   }
998
999   return (rflags);
1000 }
1001
1002 uint32_t osEventFlagsClear (osEventFlagsId_t ef_id, uint32_t flags) {
1003   uint32_t rflags;
1004
1005   if ((ef_id == NULL) || ((flags & EVENT_FLAGS_INVALID_BITS) != 0U)) {
1006     rflags = (uint32_t)osErrorParameter;
1007   }
1008   else if (IS_IRQ()) {
1009     rflags = xEventGroupGetBitsFromISR ((EventGroupHandle_t)ef_id);
1010
1011     if (xEventGroupClearBitsFromISR ((EventGroupHandle_t)ef_id, (EventBits_t)flags) == pdFAIL) {
1012       rflags = (uint32_t)osErrorResource;
1013     }
1014   }
1015   else {
1016     rflags = xEventGroupClearBits ((EventGroupHandle_t)ef_id, (EventBits_t)flags);
1017   }
1018
1019   return (rflags);
1020 }
1021
1022 uint32_t osEventFlagsGet (osEventFlagsId_t ef_id) {
1023   uint32_t rflags;
1024
1025   if (ef_id == NULL) {
1026     rflags = 0U;
1027   }
1028   else if (IS_IRQ()) {
1029     rflags = xEventGroupGetBitsFromISR ((EventGroupHandle_t)ef_id);
1030   }
1031   else {
1032     rflags = xEventGroupGetBits ((EventGroupHandle_t)ef_id);
1033   }
1034
1035   return (rflags);
1036 }
1037
1038 uint32_t osEventFlagsWait (osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout) {
1039   BaseType_t wait_all;
1040   BaseType_t exit_clr;
1041   uint32_t rflags;
1042
1043   if ((ef_id == NULL) || ((flags & EVENT_FLAGS_INVALID_BITS) != 0U)) {
1044     rflags = (uint32_t)osErrorParameter;
1045   }
1046   else if (IS_IRQ()) {
1047     rflags = (uint32_t)osErrorISR;
1048   }
1049   else {
1050     if (options & osFlagsWaitAll) {
1051       wait_all = pdTRUE;
1052     } else {
1053       wait_all = pdFAIL;
1054     }
1055
1056     if (options & osFlagsNoClear) {
1057       exit_clr = pdFAIL;
1058     } else {
1059       exit_clr = pdTRUE;
1060     }
1061
1062     rflags = xEventGroupWaitBits ((EventGroupHandle_t)ef_id, (EventBits_t)flags, exit_clr,
1063                                                                                  wait_all,
1064                                                                                  (TickType_t)timeout);
1065     if (options & osFlagsWaitAll) {
1066       if (flags != rflags) {
1067         if (timeout > 0U) {
1068           rflags = (uint32_t)osErrorTimeout;
1069         } else {
1070           rflags = (uint32_t)osErrorResource;
1071         }
1072       }
1073     }
1074     else {
1075       if ((flags & rflags) == 0U) {
1076         if (timeout > 0U) {
1077           rflags = (uint32_t)osErrorTimeout;
1078         } else {
1079           rflags = (uint32_t)osErrorResource;
1080         }
1081       }
1082     }
1083   }
1084
1085   return (rflags);
1086 }
1087
1088 osStatus_t osEventFlagsDelete (osEventFlagsId_t ef_id) {
1089   osStatus_t stat;
1090
1091 #ifndef RTE_RTOS_FreeRTOS_HEAP_1
1092   if (IS_IRQ()) {
1093     stat = osErrorISR;
1094   }
1095   else if (ef_id == NULL) {
1096     stat = osErrorParameter;
1097   }
1098   else {
1099     stat = osOK;
1100     vEventGroupDelete ((EventGroupHandle_t)ef_id);
1101   }
1102 #else
1103   stat = osError;
1104 #endif
1105
1106   return (stat);
1107 }
1108
1109 /*---------------------------------------------------------------------------*/
1110
1111 osMutexId_t osMutexNew (const osMutexAttr_t *attr) {
1112   SemaphoreHandle_t mutex;
1113   uint32_t type;
1114   uint32_t rmtx;
1115   int32_t  mem;
1116
1117   mutex = NULL;
1118
1119   if (!IS_IRQ()) {
1120     if (attr != NULL) {
1121       type = attr->attr_bits;
1122     } else {
1123       type = 0U;
1124     }
1125
1126     if ((type & osMutexRecursive) == osMutexRecursive) {
1127       rmtx = 1U;
1128     } else {
1129       rmtx = 0U;
1130     }
1131
1132     if ((type & osMutexRobust) != osMutexRobust) {
1133       mem = -1;
1134
1135       if (attr != NULL) {
1136         if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticSemaphore_t))) {
1137           mem = 1;
1138         }
1139         else {
1140           if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
1141             mem = 0;
1142           }
1143         }
1144       }
1145       else {
1146         mem = 0;
1147       }
1148
1149       if (mem == 1) {
1150         if (rmtx != 0U) {
1151           mutex = xSemaphoreCreateRecursiveMutexStatic (attr->cb_mem);
1152         }
1153         else {
1154           mutex = xSemaphoreCreateMutexStatic (attr->cb_mem);
1155         }
1156       }
1157       else {
1158         if (mem == 0) {
1159           if (rmtx != 0U) {
1160             mutex = xSemaphoreCreateRecursiveMutex ();
1161           } else {
1162             mutex = xSemaphoreCreateMutex ();
1163           }
1164         }
1165       }
1166
1167       if ((mutex != NULL) && (rmtx != 0U)) {
1168         mutex = (SemaphoreHandle_t)((uint32_t)mutex | 1U);
1169       }
1170     }
1171   }
1172
1173   return ((osMutexId_t)mutex);
1174 }
1175
1176 osStatus_t osMutexAcquire (osMutexId_t mutex_id, uint32_t timeout) {
1177   SemaphoreHandle_t mutex;
1178   osStatus_t stat;
1179   uint32_t rmtx;
1180
1181   mutex = (SemaphoreHandle_t)((uint32_t)mutex_id & ~1U);
1182
1183   rmtx = (uint32_t)mutex_id & 1U;
1184
1185   stat = osOK;
1186
1187   if (IS_IRQ()) {
1188     stat = osErrorISR;
1189   }
1190   else if (mutex == NULL) {
1191     stat = osErrorParameter;
1192   }
1193   else {
1194     if (rmtx != 0U) {
1195       if (xSemaphoreTakeRecursive (mutex, timeout) != pdPASS) {
1196         if (timeout != 0U) {
1197           stat = osErrorTimeout;
1198         } else {
1199           stat = osErrorResource;
1200         }
1201       }
1202     }
1203     else {
1204       if (xSemaphoreTake (mutex, timeout) != pdPASS) {
1205         if (timeout != 0U) {
1206           stat = osErrorTimeout;
1207         } else {
1208           stat = osErrorResource;
1209         }
1210       }
1211     }
1212   }
1213
1214   return (stat);
1215 }
1216
1217 osStatus_t osMutexRelease (osMutexId_t mutex_id) {
1218   SemaphoreHandle_t mutex;
1219   osStatus_t stat;
1220   uint32_t rmtx;
1221
1222   mutex = (SemaphoreHandle_t)((uint32_t)mutex_id & ~1U);
1223
1224   rmtx = (uint32_t)mutex_id & 1U;
1225
1226   stat = osOK;
1227
1228   if (IS_IRQ()) {
1229     stat = osErrorISR;
1230   }
1231   else if (mutex == NULL) {
1232     stat = osErrorParameter;
1233   }
1234   else {
1235     if (rmtx != 0U) {
1236       if (xSemaphoreGiveRecursive (mutex) != pdPASS) {
1237         stat = osErrorResource;
1238       }
1239     }
1240     else {
1241       if (xSemaphoreGive (mutex) != pdPASS) {
1242         stat = osErrorResource;
1243       }
1244     }
1245   }
1246
1247   return (stat);
1248 }
1249
1250 osThreadId_t osMutexGetOwner (osMutexId_t mutex_id) {
1251   SemaphoreHandle_t mutex;
1252   osThreadId_t owner;
1253
1254   mutex = (SemaphoreHandle_t)((uint32_t)mutex_id & ~1U);
1255
1256   if (IS_IRQ() || (mutex == NULL)) {
1257     owner = NULL;
1258   } else {
1259     owner = (osThreadId_t)xSemaphoreGetMutexHolder (mutex);
1260   }
1261
1262   return (owner);
1263 }
1264
1265 osStatus_t osMutexDelete (osMutexId_t mutex_id) {
1266   osStatus_t stat;
1267 #ifndef RTE_RTOS_FreeRTOS_HEAP_1
1268   SemaphoreHandle_t mutex;
1269
1270   mutex = (SemaphoreHandle_t)((uint32_t)mutex_id & ~1U);
1271
1272   if (IS_IRQ()) {
1273     stat = osErrorISR;
1274   }
1275   else if (mutex == NULL) {
1276     stat = osErrorParameter;
1277   }
1278   else {
1279     stat = osOK;
1280     vSemaphoreDelete (mutex);
1281   }
1282 #else
1283   stat = osError;
1284 #endif
1285
1286   return (stat);
1287 }
1288
1289 /*---------------------------------------------------------------------------*/
1290
1291 osSemaphoreId_t osSemaphoreNew (uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr) {
1292   SemaphoreHandle_t h;
1293   int32_t mem;
1294
1295   h = NULL;
1296
1297   if (!IS_IRQ() && (max_count > 0U) && (initial_count <= max_count)) {
1298     mem = -1;
1299
1300     if (attr != NULL) {
1301       if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticSemaphore_t))) {
1302         mem = 1;
1303       }
1304       else {
1305         if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
1306           mem = 0;
1307         }
1308       }
1309     }
1310     else {
1311       mem = 0;
1312     }
1313
1314     if (mem != -1) {
1315       if (max_count == 1U) {
1316         if (mem == 1) {
1317           h = xSemaphoreCreateBinaryStatic ((StaticSemaphore_t *)attr->cb_mem);
1318         }
1319         else {
1320           h = xSemaphoreCreateBinary();
1321         }
1322
1323         if ((h != NULL) && (initial_count != 0U)) {
1324           if (xSemaphoreGive (h) != pdPASS) {
1325             vSemaphoreDelete (h);
1326             h = NULL;
1327           }
1328         }
1329       }
1330       else {
1331         if (mem == 1) {
1332           h = xSemaphoreCreateCountingStatic (max_count, initial_count, (StaticSemaphore_t *)attr->cb_mem);
1333         }
1334         else {
1335           h = xSemaphoreCreateCounting (max_count, initial_count);
1336         }
1337       }
1338     }
1339   }
1340
1341   return (h);
1342 }
1343
1344 osStatus_t osSemaphoreAcquire (osSemaphoreId_t semaphore_id, uint32_t timeout) {
1345   osStatus_t stat;
1346
1347   stat = osOK;
1348
1349   if (semaphore_id == NULL) {
1350     stat = osErrorParameter;
1351   }
1352   else if (IS_IRQ()) {
1353     if (timeout != 0U) {
1354       stat = osErrorParameter;
1355     }
1356     else {
1357       if (xSemaphoreTakeFromISR ((SemaphoreHandle_t)semaphore_id, NULL) != pdPASS) {
1358         stat = osErrorResource;
1359       }
1360     }
1361   }
1362   else {
1363     if (xSemaphoreTake ((SemaphoreHandle_t)semaphore_id, (TickType_t)timeout) != pdPASS) {
1364       if (timeout != 0U) {
1365         stat = osErrorTimeout;
1366       } else {
1367         stat = osErrorResource;
1368       }
1369     }
1370   }
1371
1372   return (stat);
1373 }
1374
1375 osStatus_t osSemaphoreRelease (osSemaphoreId_t semaphore_id) {
1376   osStatus_t stat;
1377
1378   stat = osOK;
1379
1380   if (semaphore_id == NULL) {
1381     stat = osErrorParameter;
1382   }
1383   else if (IS_IRQ()) {
1384     if (xSemaphoreGiveFromISR ((SemaphoreHandle_t)semaphore_id, NULL) != pdTRUE) {
1385       stat = osErrorResource;
1386     }
1387   }
1388   else {
1389     if (xSemaphoreGive ((SemaphoreHandle_t)semaphore_id) != pdPASS) {
1390       stat = osErrorResource;
1391     }
1392   }
1393
1394   return (stat);
1395 }
1396
1397 uint32_t osSemaphoreGetCount (osSemaphoreId_t semaphore_id) {
1398   uint32_t count;
1399
1400   if (semaphore_id == NULL) {
1401     count = 0U;
1402   }
1403   else if (IS_IRQ()) {
1404     count = uxQueueMessagesWaitingFromISR ((QueueHandle_t)semaphore_id);
1405   } else {
1406     count = (uint32_t)uxSemaphoreGetCount ((SemaphoreHandle_t)semaphore_id);
1407   }
1408
1409   return (count);
1410 }
1411
1412 osStatus_t osSemaphoreDelete (osSemaphoreId_t semaphore_id) {
1413   osStatus_t stat;
1414
1415 #ifndef RTE_RTOS_FreeRTOS_HEAP_1
1416   if (IS_IRQ()) {
1417     stat = osErrorISR;
1418   }
1419   else if (semaphore_id == NULL) {
1420     stat = osErrorParameter;
1421   }
1422   else {
1423     stat = osOK;
1424     vSemaphoreDelete ((SemaphoreHandle_t)semaphore_id);
1425   }
1426 #else
1427   stat = osError;
1428 #endif
1429
1430   return (stat);
1431 }
1432
1433 /*---------------------------------------------------------------------------*/
1434
1435 osMessageQueueId_t osMessageQueueNew (uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr) {
1436   QueueHandle_t h;
1437   int32_t mem;
1438
1439   h = NULL;
1440
1441   if (!IS_IRQ() && (msg_count > 0U) && (msg_size > 0U)) {
1442     mem = -1;
1443
1444     if (attr != NULL) {
1445       if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticQueue_t)) &&
1446           (attr->mq_mem != NULL) && (attr->mq_size >= (msg_count * msg_size))) {
1447         mem = 1;
1448       }
1449       else {
1450         if ((attr->cb_mem == NULL) && (attr->cb_size == 0U) &&
1451             (attr->mq_mem == NULL) && (attr->mq_size == 0U)) {
1452           mem = 0;
1453         }
1454       }
1455     }
1456     else {
1457       mem = 0;
1458     }
1459
1460     if (mem == 1) {
1461       h = xQueueCreateStatic (msg_count, msg_size, attr->mq_mem, attr->cb_mem);
1462     }
1463     else {
1464       if (mem == 0) {
1465         h = xQueueCreate (msg_count, msg_size);
1466       }
1467     }
1468   }
1469
1470   return ((osMessageQueueId_t)h);
1471 }
1472
1473 osStatus_t osMessageQueuePut (osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout) {
1474   osStatus_t stat;
1475
1476   (void)msg_prio; /* Message priority is ignored */
1477
1478   stat = osOK;
1479
1480   if (IS_IRQ()) {
1481     if ((mq_id == NULL) || (msg_ptr == NULL) || (timeout != 0U)) {
1482       stat = osErrorParameter;
1483     }
1484     else {
1485       if (xQueueSendToBackFromISR ((QueueHandle_t)mq_id, msg_ptr, NULL) != pdTRUE) {
1486         stat = osErrorResource;
1487       }
1488     }
1489   }
1490   else {
1491     if ((mq_id == NULL) || (msg_ptr == NULL)) {
1492       stat = osErrorParameter;
1493     }
1494     else {
1495       if (xQueueSendToBack ((QueueHandle_t)mq_id, msg_ptr, (TickType_t)timeout) != pdPASS) {
1496         if (timeout != 0U) {
1497           stat = osErrorTimeout;
1498         } else {
1499           stat = osErrorResource;
1500         }
1501       }
1502     }
1503   }
1504
1505   return (stat);
1506 }
1507
1508 osStatus_t osMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout) {
1509   osStatus_t stat;
1510
1511   (void)msg_prio; /* Message priority is ignored */
1512
1513   stat = osOK;
1514
1515   if (IS_IRQ()) {
1516     if ((mq_id == NULL) || (msg_ptr == NULL) || (timeout != 0U)) {
1517       stat = osErrorParameter;
1518     }
1519     else {
1520       if (xQueueReceiveFromISR ((QueueHandle_t)mq_id, msg_ptr, NULL) != pdPASS) {
1521         stat = osErrorResource;
1522       }
1523     }
1524   }
1525   else {
1526     if ((mq_id == NULL) || (msg_ptr == NULL)) {
1527       stat = osErrorParameter;
1528     }
1529     else {
1530       if (xQueueReceive ((QueueHandle_t)mq_id, msg_ptr, (TickType_t)timeout) != pdPASS) {
1531         if (timeout != 0U) {
1532           stat = osErrorTimeout;
1533         } else {
1534           stat = osErrorResource;
1535         }
1536       }
1537     }
1538   }
1539
1540   return (stat);
1541 }
1542
1543 uint32_t osMessageQueueGetCapacity (osMessageQueueId_t mq_id) {
1544   StaticQueue_t *mq = (StaticQueue_t *)mq_id;
1545   uint32_t capacity;
1546
1547   if (mq == NULL) {
1548     capacity = 0U;
1549   } else {
1550     /* capacity = pxQueue->uxLength */
1551     capacity = mq->uxDummy4[1];
1552   }
1553
1554   return (capacity);
1555 }
1556
1557 uint32_t osMessageQueueGetMsgSize (osMessageQueueId_t mq_id) {
1558   StaticQueue_t *mq = (StaticQueue_t *)mq_id;
1559   uint32_t size;
1560
1561   if (mq == NULL) {
1562     size = 0U;
1563   } else {
1564     /* size = pxQueue->uxItemSize */
1565     size = mq->uxDummy4[2];
1566   }
1567
1568   return (size);
1569 }
1570
1571 uint32_t osMessageQueueGetCount (osMessageQueueId_t mq_id) {
1572   UBaseType_t count;
1573
1574   if (mq_id == NULL) {
1575     count = 0U;
1576   }
1577   else if (IS_IRQ()) {
1578     count = uxQueueMessagesWaitingFromISR ((QueueHandle_t)mq_id);
1579   }
1580   else {
1581     count = uxQueueMessagesWaiting ((QueueHandle_t)mq_id);
1582   }
1583
1584   return ((uint32_t)count);
1585 }
1586
1587 uint32_t osMessageQueueGetSpace (osMessageQueueId_t mq_id) {
1588   StaticQueue_t *mq = (StaticQueue_t *)mq_id;
1589   uint32_t space;
1590   uint32_t isrm;
1591
1592   if (mq == NULL) {
1593     space = 0U;
1594   }
1595   else if (IS_IRQ()) {
1596     isrm = taskENTER_CRITICAL_FROM_ISR();
1597
1598     /* space = pxQueue->uxLength - pxQueue->uxMessagesWaiting; */
1599     space = mq->uxDummy4[1] - mq->uxDummy4[0];
1600
1601     taskEXIT_CRITICAL_FROM_ISR(isrm);
1602   }
1603   else {
1604     space = (uint32_t)uxQueueSpacesAvailable ((QueueHandle_t)mq);
1605   }
1606
1607   return (space);
1608 }
1609
1610 osStatus_t osMessageQueueReset (osMessageQueueId_t mq_id) {
1611   osStatus_t stat;
1612
1613   if (IS_IRQ()) {
1614     stat = osErrorISR;
1615   }
1616   else if (mq_id == NULL) {
1617     stat = osErrorParameter;
1618   }
1619   else {
1620     stat = osOK;
1621     (void)xQueueReset ((QueueHandle_t)mq_id);
1622   }
1623
1624   return (stat);
1625 }
1626
1627 osStatus_t osMessageQueueDelete (osMessageQueueId_t mq_id) {
1628   osStatus_t stat;
1629
1630 #ifndef RTE_RTOS_FreeRTOS_HEAP_1
1631   if (IS_IRQ()) {
1632     stat = osErrorISR;
1633   }
1634   else if (mq_id == NULL) {
1635     stat = osErrorParameter;
1636   }
1637   else {
1638     stat = osOK;
1639     vQueueDelete ((QueueHandle_t)mq_id);
1640   }
1641 #else
1642   stat = osError;
1643 #endif
1644
1645   return (stat);
1646 }
1647
1648 /*---------------------------------------------------------------------------*/
1649
1650 /* Callback function prototypes */
1651 extern void vApplicationIdleHook (void);
1652 extern void vApplicationTickHook (void);
1653 extern void vApplicationMallocFailedHook (void);
1654 extern void vApplicationDaemonTaskStartupHook (void);
1655 extern void vApplicationStackOverflowHook (TaskHandle_t xTask, signed char *pcTaskName);
1656
1657 /**
1658   Dummy implementation of the callback function vApplicationIdleHook().
1659 */
1660 #if (configUSE_IDLE_HOOK == 1)
1661 __WEAK void vApplicationIdleHook (void){}
1662 #endif
1663
1664 /**
1665   Dummy implementation of the callback function vApplicationTickHook().
1666 */
1667 #if (configUSE_TICK_HOOK == 1)
1668  __WEAK void vApplicationTickHook (void){}
1669 #endif
1670
1671 /**
1672   Dummy implementation of the callback function vApplicationMallocFailedHook().
1673 */
1674 #if (configUSE_MALLOC_FAILED_HOOK == 1)
1675 __WEAK void vApplicationMallocFailedHook (void){}
1676 #endif
1677
1678 /**
1679   Dummy implementation of the callback function vApplicationDaemonTaskStartupHook().
1680 */
1681 #if (configUSE_DAEMON_TASK_STARTUP_HOOK == 1)
1682 __WEAK void vApplicationDaemonTaskStartupHook (void){}
1683 #endif
1684
1685 /**
1686   Dummy implementation of the callback function vApplicationStackOverflowHook().
1687 */
1688 #if (configCHECK_FOR_STACK_OVERFLOW > 0)
1689 __WEAK void vApplicationStackOverflowHook (TaskHandle_t xTask, signed char *pcTaskName) {
1690   (void)xTask;
1691   (void)pcTaskName;
1692 }
1693 #endif
1694
1695 /*---------------------------------------------------------------------------*/
1696
1697 /* External Idle and Timer task static memory allocation functions */
1698 extern void vApplicationGetIdleTaskMemory  (StaticTask_t **ppxIdleTaskTCBBuffer,  StackType_t **ppxIdleTaskStackBuffer,  uint32_t *pulIdleTaskStackSize);
1699 extern void vApplicationGetTimerTaskMemory (StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize);
1700
1701 /* Idle task control block and stack */
1702 static StaticTask_t Idle_TCB;
1703 static StackType_t  Idle_Stack[configMINIMAL_STACK_SIZE];
1704
1705 /* Timer task control block and stack */
1706 static StaticTask_t Timer_TCB;
1707 static StackType_t  Timer_Stack[configTIMER_TASK_STACK_DEPTH];
1708
1709 /*
1710   vApplicationGetIdleTaskMemory gets called when configSUPPORT_STATIC_ALLOCATION
1711   equals to 1 and is required for static memory allocation support.
1712 */
1713 void vApplicationGetIdleTaskMemory (StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize) {
1714   *ppxIdleTaskTCBBuffer   = &Idle_TCB;
1715   *ppxIdleTaskStackBuffer = &Idle_Stack[0];
1716   *pulIdleTaskStackSize   = (uint32_t)configMINIMAL_STACK_SIZE;
1717 }
1718
1719 /*
1720   vApplicationGetTimerTaskMemory gets called when configSUPPORT_STATIC_ALLOCATION
1721   equals to 1 and is required for static memory allocation support.
1722 */
1723 void vApplicationGetTimerTaskMemory (StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize) {
1724   *ppxTimerTaskTCBBuffer   = &Timer_TCB;
1725   *ppxTimerTaskStackBuffer = &Timer_Stack[0];
1726   *pulTimerTaskStackSize   = (uint32_t)configTIMER_TASK_STACK_DEPTH;
1727 }