1 /* --------------------------------------------------------------------------
2 * Copyright (c) 2013-2017 ARM Limited. All rights reserved.
4 * SPDX-License-Identifier: Apache-2.0
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 * Purpose: CMSIS RTOS2 wrapper for FreeRTOS
21 *---------------------------------------------------------------------------*/
25 #include "RTE_Components.h" // Component selection
26 #include CMSIS_device_header
28 #include "cmsis_os2.h" // ::CMSIS:RTOS2
29 #include "cmsis_compiler.h"
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
36 /*---------------------------------------------------------------------------*/
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)))
43 #define IS_IRQ_MASKED() (__get_PRIMASK() != 0U)
46 #define IS_IRQ_MODE() (__get_IPSR() != 0U)
48 #define IS_IRQ() (IS_IRQ_MODE() || IS_IRQ_MASKED())
51 #define MAX_BITS_TASK_NOTIFY 31U
52 #define MAX_BITS_EVENT_GROUPS 24U
54 #define THREAD_FLAGS_INVALID_BITS (~((1UL << MAX_BITS_TASK_NOTIFY) - 1U))
55 #define EVENT_FLAGS_INVALID_BITS (~((1UL << MAX_BITS_EVENT_GROUPS) - 1U))
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))
62 #define KERNEL_ID "FreeRTOS V9.0.0"
64 /* Timer callback information structure definition */
70 /* Kernel initialization state */
71 static osKernelState_t KernelState;
73 /* Heap region definition used by heap_5 variant */
74 #if defined(RTE_RTOS_FreeRTOS_HEAP_5)
75 #if (configAPPLICATION_ALLOCATED_HEAP == 1)
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.
80 extern uint8_t ucHeap[configTOTAL_HEAP_SIZE];
82 static uint8_t ucHeap[configTOTAL_HEAP_SIZE];
83 #endif /* configAPPLICATION_ALLOCATED_HEAP */
85 static HeapRegion_t xHeapRegions[] = {
86 { ucHeap, configTOTAL_HEAP_SIZE },
89 #endif /* RTE_RTOS_FreeRTOS_HEAP_5 */
91 /*---------------------------------------------------------------------------*/
93 /* FreeRTOS SysTick handler prototypes */
94 extern void SysTick_Handler (void);
95 extern void xPortSysTickHandler (void);
97 void SysTick_Handler (void) {
98 /* Clear overflow flag */
101 /* Call tick handler */
102 xPortSysTickHandler();
105 /*---------------------------------------------------------------------------*/
107 osStatus_t osKernelInitialize (void) {
114 #if defined(RTE_RTOS_FreeRTOS_HEAP_5)
115 vPortDefineHeapRegions (xHeapRegions);
117 KernelState = osKernelReady;
124 osStatus_t osKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size) {
131 if (version != NULL) {
132 version->api = KERNEL_VERSION;
133 version->kernel = KERNEL_VERSION;
136 if ((id_buf != NULL) && (id_size != 0U)) {
137 if (id_size > sizeof(KERNEL_ID)) {
138 id_size = sizeof(KERNEL_ID);
140 memcpy(id_buf, KERNEL_ID, id_size);
148 osKernelState_t osKernelGetState (void) {
149 osKernelState_t state;
152 state = osKernelError;
155 switch (xTaskGetSchedulerState()) {
156 case taskSCHEDULER_RUNNING:
157 state = osKernelRunning;
160 case taskSCHEDULER_SUSPENDED:
161 state = osKernelLocked;
164 case taskSCHEDULER_NOT_STARTED:
166 if (KernelState == osKernelReady) {
167 state = osKernelReady;
169 state = osKernelInactive;
177 osStatus_t osKernelStart (void) {
184 KernelState = osKernelRunning;
185 vTaskStartScheduler();
192 int32_t osKernelLock (void) {
196 lock = (int32_t)osErrorISR;
199 switch (xTaskGetSchedulerState()) {
200 case taskSCHEDULER_SUSPENDED:
204 case taskSCHEDULER_RUNNING:
209 case taskSCHEDULER_NOT_STARTED:
211 lock = (int32_t)osError;
219 int32_t osKernelUnlock (void) {
223 lock = (int32_t)osErrorISR;
226 switch (xTaskGetSchedulerState()) {
227 case taskSCHEDULER_SUSPENDED:
228 if (xTaskResumeAll() == pdTRUE) {
231 lock = (int32_t)osError;
235 case taskSCHEDULER_RUNNING:
239 case taskSCHEDULER_NOT_STARTED:
241 lock = (int32_t)osError;
249 int32_t osKernelRestoreLock (int32_t lock) {
252 lock = (int32_t)osErrorISR;
255 switch (xTaskGetSchedulerState()) {
256 case taskSCHEDULER_SUSPENDED:
257 case taskSCHEDULER_RUNNING:
262 if ((lock != 0) || (xTaskResumeAll() != pdTRUE)) {
263 lock = (int32_t)osError;
268 case taskSCHEDULER_NOT_STARTED:
270 lock = (int32_t)osError;
278 uint64_t osKernelGetTickCount (void) {
282 ticks = xTaskGetTickCountFromISR();
284 ticks = xTaskGetTickCount();
287 return ((uint64_t)ticks);
290 uint32_t osKernelGetTickFreq (void) {
296 freq = configTICK_RATE_HZ;
302 uint32_t osKernelGetSysTimerCount (void) {
306 portDISABLE_INTERRUPTS();
308 ticks = xTaskGetTickCount();
310 val = SysTick->LOAD - SysTick->VAL;
312 if ((SysTick->CTRL >> 16) & 1U) {
314 val = SysTick->LOAD - SysTick->VAL;
317 val += ticks * (SysTick->LOAD + 1U);
319 portENABLE_INTERRUPTS();
324 uint32_t osKernelGetSysTimerFreq (void) {
325 return (configCPU_CLOCK_HZ);
328 /*---------------------------------------------------------------------------*/
330 osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) {
340 if (!IS_IRQ() && (func != NULL)) {
341 stack = configMINIMAL_STACK_SIZE;
342 prio = (UBaseType_t)osPriorityNormal;
349 if (attr->name != NULL) {
352 if (attr->priority != osPriorityNone) {
353 prio = (UBaseType_t)attr->priority;
356 if ((prio < osPriorityIdle) || (prio > osPriorityISR) || ((attr->attr_bits & osThreadJoinable) == osThreadJoinable)) {
360 if (attr->stack_size > 0U) {
361 stack = attr->stack_size;
364 if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticTask_t)) &&
365 (attr->stack_mem != NULL) && (attr->stack_size > 0U)) {
369 if ((attr->cb_mem == NULL) && (attr->cb_size == 0U) && (attr->stack_mem == NULL)) {
379 h = xTaskCreateStatic ((TaskFunction_t)func, name, stack, argument, prio, (StackType_t *)attr->stack_mem,
380 (StaticTask_t *)attr->cb_mem);
384 if (xTaskCreate ((TaskFunction_t)func, name, stack, argument, prio, &h) != pdPASS) {
391 return ((osThreadId_t)h);
394 const char *osThreadGetName (osThreadId_t thread_id) {
397 if (IS_IRQ() || (thread_id == NULL)) {
400 name = pcTaskGetName ((TaskHandle_t)thread_id);
406 osThreadId_t osThreadGetId (void) {
412 id = (osThreadId_t)xTaskGetCurrentTaskHandle();
418 osThreadState_t osThreadGetState (osThreadId_t thread_id) {
419 osThreadState_t state;
421 if (IS_IRQ() || (thread_id == NULL)) {
422 state = osThreadError;
425 switch (eTaskGetState ((TaskHandle_t)thread_id)) {
426 case eRunning: state = osThreadRunning; break;
427 case eReady: state = osThreadReady; break;
429 case eSuspended: state = osThreadBlocked; break;
430 case eDeleted: state = osThreadTerminated; break;
432 default: state = osThreadError; break;
439 uint32_t osThreadGetStackSpace (osThreadId_t thread_id) {
442 if (IS_IRQ() || (thread_id == NULL)) {
445 sz = (uint32_t)uxTaskGetStackHighWaterMark ((TaskHandle_t)thread_id);
451 osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
457 else if ((thread_id == NULL) || (priority < osPriorityIdle) || (priority > osPriorityISR)) {
458 stat = osErrorParameter;
462 vTaskPrioritySet ((TaskHandle_t)thread_id, (UBaseType_t)priority);
468 osPriority_t osThreadGetPriority (osThreadId_t thread_id) {
471 if (IS_IRQ() || (thread_id == NULL)) {
472 prio = osPriorityError;
474 prio = (osPriority_t)uxTaskPriorityGet ((TaskHandle_t)thread_id);
480 osStatus_t osThreadYield (void) {
493 osStatus_t osThreadSuspend (osThreadId_t thread_id) {
499 else if (thread_id == NULL) {
500 stat = osErrorParameter;
504 vTaskSuspend ((TaskHandle_t)thread_id);
510 osStatus_t osThreadResume (osThreadId_t thread_id) {
516 else if (thread_id == NULL) {
517 stat = osErrorParameter;
521 vTaskResume ((TaskHandle_t)thread_id);
527 __NO_RETURN void osThreadExit (void) {
528 #ifndef RTE_RTOS_FreeRTOS_HEAP_1
534 osStatus_t osThreadTerminate (osThreadId_t thread_id) {
536 #ifndef RTE_RTOS_FreeRTOS_HEAP_1
542 else if (thread_id == NULL) {
543 stat = osErrorParameter;
546 tstate = eTaskGetState ((TaskHandle_t)thread_id);
548 if (tstate != eDeleted) {
550 vTaskDelete ((TaskHandle_t)thread_id);
552 stat = osErrorResource;
562 uint32_t osThreadGetCount (void) {
568 count = uxTaskGetNumberOfTasks();
574 uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
578 if (IS_IRQ() || (thread_array == NULL) || (array_items == 0U)) {
583 count = uxTaskGetNumberOfTasks();
584 task = pvPortMalloc (count * sizeof(TaskStatus_t));
587 count = uxTaskGetSystemState (task, count, NULL);
589 for (i = 0U; (i < count) && (i < array_items); i++) {
590 thread_array[i] = (osThreadId_t)task[i].xHandle;
594 (void)xTaskResumeAll();
602 uint32_t osThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
603 TaskHandle_t thread = (TaskHandle_t)thread_id;
605 if ((thread == NULL) || ((flags & THREAD_FLAGS_INVALID_BITS) != 0U)) {
606 flags = (uint32_t)osErrorParameter;
609 if (xTaskNotifyFromISR (thread, flags, eSetBits, NULL) != pdPASS) {
610 flags = (uint32_t)osError;
614 if (xTaskNotify (thread, flags, eSetBits) != pdPASS) {
615 flags = (uint32_t)osError;
618 /* Return flags after setting */
622 uint32_t osThreadFlagsClear (uint32_t flags) {
624 uint32_t rflags, cflags;
627 rflags = (uint32_t)osErrorISR;
629 else if ((flags & THREAD_FLAGS_INVALID_BITS) != 0U) {
630 rflags = (uint32_t)osErrorParameter;
633 thread = xTaskGetCurrentTaskHandle();
635 if (xTaskNotifyAndQuery (thread, 0, eNoAction, &cflags) == pdPASS) {
639 if (xTaskNotify (thread, cflags, eSetValueWithOverwrite) != pdPASS) {
640 rflags = (uint32_t)osError;
644 rflags = (uint32_t)osError;
648 /* Return flags before clearing */
652 uint32_t osThreadFlagsGet (void) {
657 rflags = (uint32_t)osErrorISR;
660 thread = xTaskGetCurrentTaskHandle();
662 if (xTaskNotifyAndQuery (thread, 0, eNoAction, &rflags) != pdPASS) {
663 rflags = (uint32_t)osError;
670 uint32_t osThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout) {
671 uint32_t rflags, nval;
676 rflags = (uint32_t)osErrorISR;
678 else if ((flags & THREAD_FLAGS_INVALID_BITS) != 0U) {
679 rflags = (uint32_t)osErrorParameter;
682 if ((options & osFlagsNoClear) == osFlagsNoClear) {
690 t0 = xTaskGetTickCount();
692 if (xTaskNotifyWait (0, clear, &nval, timeout) == pdPASS) {
695 if ((options & osFlagsWaitAll) == osFlagsWaitAll) {
696 if ((flags & rflags) == flags) {
700 rflags = (uint32_t)osErrorResource;
705 if ((flags & rflags) != 0) {
709 rflags = (uint32_t)osErrorResource;
716 rflags = (uint32_t)osErrorResource;
718 rflags = (uint32_t)osErrorTimeout;
723 while ((xTaskGetTickCount() - t0) < timeout);
726 /* Return flags before clearing */
730 osStatus_t osDelay (uint32_t ticks) {
747 osStatus_t osDelayUntil (uint64_t ticks) {
756 tcnt = xTaskGetTickCount();
758 vTaskDelayUntil (&tcnt, (TickType_t)ticks);
764 /*---------------------------------------------------------------------------*/
766 static void TimerCallback (TimerHandle_t hTimer) {
767 TimerCallback_t *callb;
769 callb = (TimerCallback_t *)pvTimerGetTimerID (hTimer);
772 callb->func (callb->arg);
776 osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr) {
779 TimerCallback_t *callb;
785 if (!IS_IRQ() && (func != NULL)) {
786 /* Allocate memory to store callback function and argument */
787 callb = pvPortMalloc (sizeof(TimerCallback_t));
791 callb->arg = argument;
793 if (type == osTimerOnce) {
803 if (attr->name != NULL) {
807 if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticTimer_t))) {
811 if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
821 h = xTimerCreateStatic (name, 1, reload, callb, TimerCallback, (StaticTimer_t *)attr->cb_mem);
825 h = xTimerCreate (name, 1, reload, callb, TimerCallback);
831 return ((osTimerId_t)h);
834 const char *osTimerGetName (osTimerId_t timer_id) {
837 if (IS_IRQ() || (timer_id == NULL)) {
840 p = pcTimerGetName ((TimerHandle_t)timer_id);
846 osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks) {
852 else if (timer_id == NULL) {
853 stat = osErrorParameter;
856 if (xTimerChangePeriod ((TimerHandle_t)timer_id, ticks, 0) == pdPASS) {
859 stat = osErrorResource;
866 osStatus_t osTimerStop (osTimerId_t timer_id) {
872 else if (timer_id == NULL) {
873 stat = osErrorParameter;
876 if (xTimerIsTimerActive ((TimerHandle_t)timer_id) == pdFALSE) {
877 stat = osErrorResource;
880 if (xTimerStop ((TimerHandle_t)timer_id, 0) == pdPASS) {
891 uint32_t osTimerIsRunning (osTimerId_t timer_id) {
894 if (IS_IRQ() || (timer_id == NULL)) {
897 running = xTimerIsTimerActive ((TimerHandle_t)timer_id);
903 osStatus_t osTimerDelete (osTimerId_t timer_id) {
905 #ifndef RTE_RTOS_FreeRTOS_HEAP_1
906 TimerCallback_t *callb;
911 else if (timer_id == NULL) {
912 stat = osErrorParameter;
915 callb = (TimerCallback_t *)pvTimerGetTimerID ((TimerHandle_t)timer_id);
917 if (xTimerDelete ((TimerHandle_t)timer_id, 0) == pdPASS) {
921 stat = osErrorResource;
931 /*---------------------------------------------------------------------------*/
933 osEventFlagsId_t osEventFlagsNew (const osEventFlagsAttr_t *attr) {
934 EventGroupHandle_t h;
943 if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticEventGroup_t))) {
947 if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
957 h = xEventGroupCreateStatic (attr->cb_mem);
961 h = xEventGroupCreate();
966 return ((osEventFlagsId_t)h);
969 uint32_t osEventFlagsSet (osEventFlagsId_t ef_id, uint32_t flags) {
972 if ((ef_id == NULL) || ((flags & EVENT_FLAGS_INVALID_BITS) != 0U)) {
973 rflags = (uint32_t)osErrorParameter;
976 if (xEventGroupSetBitsFromISR ((EventGroupHandle_t)ef_id, (EventBits_t)flags, NULL) == pdPASS) {
979 rflags = (uint32_t)osErrorResource;
983 rflags = xEventGroupSetBits ((EventGroupHandle_t)ef_id, (EventBits_t)flags);
989 uint32_t osEventFlagsClear (osEventFlagsId_t ef_id, uint32_t flags) {
992 if ((ef_id == NULL) || ((flags & EVENT_FLAGS_INVALID_BITS) != 0U)) {
993 rflags = (uint32_t)osErrorParameter;
996 rflags = xEventGroupGetBitsFromISR ((EventGroupHandle_t)ef_id);
998 if (xEventGroupClearBitsFromISR ((EventGroupHandle_t)ef_id, (EventBits_t)flags) == pdFAIL) {
999 rflags = (uint32_t)osErrorResource;
1003 rflags = xEventGroupClearBits ((EventGroupHandle_t)ef_id, (EventBits_t)flags);
1009 uint32_t osEventFlagsGet (osEventFlagsId_t ef_id) {
1012 if (ef_id == NULL) {
1015 else if (IS_IRQ()) {
1016 rflags = xEventGroupGetBitsFromISR ((EventGroupHandle_t)ef_id);
1019 rflags = xEventGroupGetBits ((EventGroupHandle_t)ef_id);
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;
1030 if ((ef_id == NULL) || ((flags & EVENT_FLAGS_INVALID_BITS) != 0U)) {
1031 rflags = (uint32_t)osErrorParameter;
1033 else if (IS_IRQ()) {
1034 rflags = (uint32_t)osErrorISR;
1037 if (options & osFlagsWaitAll) {
1043 if (options & osFlagsNoClear) {
1049 rflags = xEventGroupWaitBits ((EventGroupHandle_t)ef_id, (EventBits_t)flags, exit_clr,
1051 (TickType_t)timeout);
1052 if (options & osFlagsWaitAll) {
1053 if (flags != rflags) {
1055 rflags = (uint32_t)osErrorTimeout;
1057 rflags = (uint32_t)osErrorResource;
1062 if ((flags & rflags) == 0U) {
1064 rflags = (uint32_t)osErrorTimeout;
1066 rflags = (uint32_t)osErrorResource;
1075 osStatus_t osEventFlagsDelete (osEventFlagsId_t ef_id) {
1078 #ifndef RTE_RTOS_FreeRTOS_HEAP_1
1082 else if (ef_id == NULL) {
1083 stat = osErrorParameter;
1087 vEventGroupDelete ((EventGroupHandle_t)ef_id);
1096 /*---------------------------------------------------------------------------*/
1098 osMutexId_t osMutexNew (const osMutexAttr_t *attr) {
1099 SemaphoreHandle_t mutex;
1108 type = attr->attr_bits;
1113 if ((type & osMutexRecursive) == osMutexRecursive) {
1119 if ((type & osMutexRobust) != osMutexRobust) {
1123 if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticSemaphore_t))) {
1127 if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
1138 mutex = xSemaphoreCreateRecursiveMutexStatic (attr->cb_mem);
1141 mutex = xSemaphoreCreateMutexStatic (attr->cb_mem);
1147 mutex = xSemaphoreCreateRecursiveMutex ();
1149 mutex = xSemaphoreCreateMutex ();
1154 if ((mutex != NULL) && (rmtx != 0U)) {
1155 mutex = (SemaphoreHandle_t)((uint32_t)mutex | 1U);
1160 return ((osMutexId_t)mutex);
1163 osStatus_t osMutexAcquire (osMutexId_t mutex_id, uint32_t timeout) {
1164 SemaphoreHandle_t mutex;
1168 mutex = (SemaphoreHandle_t)((uint32_t)mutex_id & ~1U);
1170 rmtx = (uint32_t)mutex_id & 1U;
1177 else if (mutex == NULL) {
1178 stat = osErrorParameter;
1182 if (xSemaphoreTakeRecursive (mutex, timeout) != pdPASS) {
1183 if (timeout != 0U) {
1184 stat = osErrorTimeout;
1186 stat = osErrorResource;
1191 if (xSemaphoreTake (mutex, timeout) != pdPASS) {
1192 if (timeout != 0U) {
1193 stat = osErrorTimeout;
1195 stat = osErrorResource;
1204 osStatus_t osMutexRelease (osMutexId_t mutex_id) {
1205 SemaphoreHandle_t mutex;
1209 mutex = (SemaphoreHandle_t)((uint32_t)mutex_id & ~1U);
1211 rmtx = (uint32_t)mutex_id & 1U;
1218 else if (mutex == NULL) {
1219 stat = osErrorParameter;
1223 if (xSemaphoreGiveRecursive (mutex) != pdPASS) {
1224 stat = osErrorResource;
1228 if (xSemaphoreGive (mutex) != pdPASS) {
1229 stat = osErrorResource;
1237 osThreadId_t osMutexGetOwner (osMutexId_t mutex_id) {
1238 SemaphoreHandle_t mutex;
1241 mutex = (SemaphoreHandle_t)((uint32_t)mutex_id & ~1U);
1243 if (IS_IRQ() || (mutex == NULL)) {
1246 owner = (osThreadId_t)xSemaphoreGetMutexHolder (mutex);
1252 osStatus_t osMutexDelete (osMutexId_t mutex_id) {
1254 #ifndef RTE_RTOS_FreeRTOS_HEAP_1
1255 SemaphoreHandle_t mutex;
1257 mutex = (SemaphoreHandle_t)((uint32_t)mutex_id & ~1U);
1262 else if (mutex == NULL) {
1263 stat = osErrorParameter;
1267 vSemaphoreDelete (mutex);
1276 /*---------------------------------------------------------------------------*/
1278 osSemaphoreId_t osSemaphoreNew (uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr) {
1279 SemaphoreHandle_t h;
1284 if (!IS_IRQ() && (max_count > 0U) && (initial_count <= max_count)) {
1288 if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticSemaphore_t))) {
1292 if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
1302 if (max_count == 1U) {
1304 h = xSemaphoreCreateBinaryStatic ((StaticSemaphore_t *)attr->cb_mem);
1307 h = xSemaphoreCreateBinary();
1310 if ((h != NULL) && (initial_count != 0U)) {
1311 if (xSemaphoreGive (h) != pdPASS) {
1312 vSemaphoreDelete (h);
1319 h = xSemaphoreCreateCountingStatic (max_count, initial_count, (StaticSemaphore_t *)attr->cb_mem);
1322 h = xSemaphoreCreateCounting (max_count, initial_count);
1331 osStatus_t osSemaphoreAcquire (osSemaphoreId_t semaphore_id, uint32_t timeout) {
1336 if (semaphore_id == NULL) {
1337 stat = osErrorParameter;
1339 else if (IS_IRQ()) {
1340 if (timeout != 0U) {
1341 stat = osErrorParameter;
1344 if (xSemaphoreTakeFromISR ((SemaphoreHandle_t)semaphore_id, NULL) != pdPASS) {
1345 stat = osErrorResource;
1350 if (xSemaphoreTake ((SemaphoreHandle_t)semaphore_id, (TickType_t)timeout) != pdPASS) {
1351 if (timeout != 0U) {
1352 stat = osErrorTimeout;
1354 stat = osErrorResource;
1362 osStatus_t osSemaphoreRelease (osSemaphoreId_t semaphore_id) {
1367 if (semaphore_id == NULL) {
1368 stat = osErrorParameter;
1370 else if (IS_IRQ()) {
1371 if (xSemaphoreGiveFromISR ((SemaphoreHandle_t)semaphore_id, NULL) != pdTRUE) {
1372 stat = osErrorResource;
1376 if (xSemaphoreGive ((SemaphoreHandle_t)semaphore_id) != pdPASS) {
1377 stat = osErrorResource;
1384 uint32_t osSemaphoreGetCount (osSemaphoreId_t semaphore_id) {
1387 if (semaphore_id == NULL) {
1390 else if (IS_IRQ()) {
1391 count = uxQueueMessagesWaitingFromISR ((QueueHandle_t)semaphore_id);
1393 count = (uint32_t)uxSemaphoreGetCount ((SemaphoreHandle_t)semaphore_id);
1399 osStatus_t osSemaphoreDelete (osSemaphoreId_t semaphore_id) {
1402 #ifndef RTE_RTOS_FreeRTOS_HEAP_1
1406 else if (semaphore_id == NULL) {
1407 stat = osErrorParameter;
1411 vSemaphoreDelete ((SemaphoreHandle_t)semaphore_id);
1420 /*---------------------------------------------------------------------------*/
1422 osMessageQueueId_t osMessageQueueNew (uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr) {
1428 if (!IS_IRQ() && (msg_count > 0U) && (msg_size > 0U)) {
1432 if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticQueue_t)) &&
1433 (attr->mq_mem != NULL) && (attr->mq_size >= (msg_count * msg_size))) {
1437 if ((attr->cb_mem == NULL) && (attr->cb_size == 0U) &&
1438 (attr->mq_mem == NULL) && (attr->mq_size == 0U)) {
1448 h = xQueueCreateStatic (msg_count, msg_size, attr->mq_mem, attr->cb_mem);
1452 h = xQueueCreate (msg_count, msg_size);
1457 return ((osMessageQueueId_t)h);
1460 osStatus_t osMessageQueuePut (osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout) {
1463 (void)msg_prio; /* Message priority is ignored */
1468 if ((mq_id == NULL) || (msg_ptr == NULL) || (timeout != 0U)) {
1469 stat = osErrorParameter;
1472 if (xQueueSendToBackFromISR ((QueueHandle_t)mq_id, msg_ptr, NULL) != pdTRUE) {
1473 stat = osErrorResource;
1478 if ((mq_id == NULL) || (msg_ptr == NULL)) {
1479 stat = osErrorParameter;
1482 if (xQueueSendToBack ((QueueHandle_t)mq_id, msg_ptr, (TickType_t)timeout) != pdPASS) {
1483 if (timeout != 0U) {
1484 stat = osErrorTimeout;
1486 stat = osErrorResource;
1495 osStatus_t osMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout) {
1498 (void)msg_prio; /* Message priority is ignored */
1503 if ((mq_id == NULL) || (msg_ptr == NULL) || (timeout != 0U)) {
1504 stat = osErrorParameter;
1507 if (xQueueReceiveFromISR ((QueueHandle_t)mq_id, msg_ptr, NULL) != pdPASS) {
1508 stat = osErrorResource;
1513 if ((mq_id == NULL) || (msg_ptr == NULL)) {
1514 stat = osErrorParameter;
1517 if (xQueueReceive ((QueueHandle_t)mq_id, msg_ptr, (TickType_t)timeout) != pdPASS) {
1518 if (timeout != 0U) {
1519 stat = osErrorTimeout;
1521 stat = osErrorResource;
1530 uint32_t osMessageQueueGetCapacity (osMessageQueueId_t mq_id) {
1531 StaticQueue_t *mq = (StaticQueue_t *)mq_id;
1537 /* capacity = pxQueue->uxLength */
1538 capacity = mq->uxDummy4[1];
1544 uint32_t osMessageQueueGetMsgSize (osMessageQueueId_t mq_id) {
1545 StaticQueue_t *mq = (StaticQueue_t *)mq_id;
1551 /* size = pxQueue->uxItemSize */
1552 size = mq->uxDummy4[2];
1558 uint32_t osMessageQueueGetCount (osMessageQueueId_t mq_id) {
1561 if (mq_id == NULL) {
1564 else if (IS_IRQ()) {
1565 count = uxQueueMessagesWaitingFromISR ((QueueHandle_t)mq_id);
1568 count = uxQueueMessagesWaiting ((QueueHandle_t)mq_id);
1571 return ((uint32_t)count);
1574 uint32_t osMessageQueueGetSpace (osMessageQueueId_t mq_id) {
1575 StaticQueue_t *mq = (StaticQueue_t *)mq_id;
1582 else if (IS_IRQ()) {
1583 isrm = taskENTER_CRITICAL_FROM_ISR();
1585 /* space = pxQueue->uxLength - pxQueue->uxMessagesWaiting; */
1586 space = mq->uxDummy4[1] - mq->uxDummy4[0];
1588 taskEXIT_CRITICAL_FROM_ISR(isrm);
1591 space = (uint32_t)uxQueueSpacesAvailable ((QueueHandle_t)mq);
1597 osStatus_t osMessageQueueReset (osMessageQueueId_t mq_id) {
1603 else if (mq_id == NULL) {
1604 stat = osErrorParameter;
1608 (void)xQueueReset ((QueueHandle_t)mq_id);
1614 osStatus_t osMessageQueueDelete (osMessageQueueId_t mq_id) {
1617 #ifndef RTE_RTOS_FreeRTOS_HEAP_1
1621 else if (mq_id == NULL) {
1622 stat = osErrorParameter;
1626 vQueueDelete ((QueueHandle_t)mq_id);
1635 /*---------------------------------------------------------------------------*/
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);
1645 Dummy implementation of the callback function vApplicationIdleHook().
1647 #if (configUSE_IDLE_HOOK == 1)
1648 __WEAK void vApplicationIdleHook (void){}
1652 Dummy implementation of the callback function vApplicationTickHook().
1654 #if (configUSE_TICK_HOOK == 1)
1655 __WEAK void vApplicationTickHook (void){}
1659 Dummy implementation of the callback function vApplicationMallocFailedHook().
1661 #if (configUSE_MALLOC_FAILED_HOOK == 1)
1662 __WEAK void vApplicationMallocFailedHook (void){}
1666 Dummy implementation of the callback function vApplicationDaemonTaskStartupHook().
1668 #if (configUSE_DAEMON_TASK_STARTUP_HOOK == 1)
1669 __WEAK void vApplicationDaemonTaskStartupHook (void){}
1673 Dummy implementation of the callback function vApplicationStackOverflowHook().
1675 #if (configCHECK_FOR_STACK_OVERFLOW > 0)
1676 __WEAK void vApplicationStackOverflowHook (TaskHandle_t xTask, signed char *pcTaskName) {
1682 /*---------------------------------------------------------------------------*/
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);
1688 /* Idle task control block and stack */
1689 static StaticTask_t Idle_TCB;
1690 static StackType_t Idle_Stack[configMINIMAL_STACK_SIZE];
1692 /* Timer task control block and stack */
1693 static StaticTask_t Timer_TCB;
1694 static StackType_t Timer_Stack[configTIMER_TASK_STACK_DEPTH];
1697 vApplicationGetIdleTaskMemory gets called when configSUPPORT_STATIC_ALLOCATION
1698 equals to 1 and is required for static memory allocation support.
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;
1707 vApplicationGetTimerTaskMemory gets called when configSUPPORT_STATIC_ALLOCATION
1708 equals to 1 and is required for static memory allocation support.
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;