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