12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871 |
- /* --------------------------------------------------------------------------
- * Portions Copyright © 2017 STMicroelectronics International N.V. All rights reserved.
- * Portions Copyright (c) 2013-2017 ARM Limited. All rights reserved.
- * --------------------------------------------------------------------------
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Licensed under the Apache License, Version 2.0 (the License); you may
- * not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an AS IS BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * Name: cmsis_os2.c
- * Purpose: CMSIS RTOS2 wrapper for FreeRTOS
- *
- *---------------------------------------------------------------------------*/
- #include <string.h>
- #include "cmsis_os2.h" // ::CMSIS:RTOS2
- #include "cmsis_compiler.h"
- #include "FreeRTOS.h" // ARM.FreeRTOS::RTOS:Core
- #include "task.h" // ARM.FreeRTOS::RTOS:Core
- #include "event_groups.h" // ARM.FreeRTOS::RTOS:Event Groups
- #include "semphr.h" // ARM.FreeRTOS::RTOS:Core
- /*---------------------------------------------------------------------------*/
- #ifndef __ARM_ARCH_6M__
- #define __ARM_ARCH_6M__ 0
- #endif
- #ifndef __ARM_ARCH_7M__
- #define __ARM_ARCH_7M__ 0
- #endif
- #ifndef __ARM_ARCH_7EM__
- #define __ARM_ARCH_7EM__ 0
- #endif
- #ifndef __ARM_ARCH_8M_MAIN__
- #define __ARM_ARCH_8M_MAIN__ 0
- #endif
- #ifndef __ARM_ARCH_7A__
- #define __ARM_ARCH_7A__ 0
- #endif
- #if ((__ARM_ARCH_7M__ == 1U) || \
- (__ARM_ARCH_7EM__ == 1U) || \
- (__ARM_ARCH_8M_MAIN__ == 1U))
- #define IS_IRQ_MASKED() ((__get_PRIMASK() != 0U) || ((KernelState == osKernelRunning) && (__get_BASEPRI() != 0U)))
- #elif (__ARM_ARCH_6M__ == 1U)
- #define IS_IRQ_MASKED() ((__get_PRIMASK() != 0U) && (KernelState == osKernelRunning))
- #elif (__ARM_ARCH_7A__ == 1)
- #define IS_IRQ_MASKED() (0U)
- #else
- #define IS_IRQ_MASKED() (__get_PRIMASK() != 0U)
- #endif
- #if (__ARM_ARCH_7A__ == 1U)
- /* CPSR mode bitmasks */
- #define CPSR_MODE_USER 0x10U
- #define CPSR_MODE_SYSTEM 0x1FU
- #define IS_IRQ_MODE() ((__get_mode() != CPSR_MODE_USER) && (__get_mode() != CPSR_MODE_SYSTEM))
- #else
- #define IS_IRQ_MODE() (__get_IPSR() != 0U)
- #endif
- #define IS_IRQ() (IS_IRQ_MODE() || IS_IRQ_MASKED())
- /* Limits */
- #define MAX_BITS_TASK_NOTIFY 31U
- #define MAX_BITS_EVENT_GROUPS 24U
- #define THREAD_FLAGS_INVALID_BITS (~((1UL << MAX_BITS_TASK_NOTIFY) - 1U))
- #define EVENT_FLAGS_INVALID_BITS (~((1UL << MAX_BITS_EVENT_GROUPS) - 1U))
- /* Kernel version and identification string definition */
- #define KERNEL_VERSION (((uint32_t)tskKERNEL_VERSION_MAJOR * 10000000UL) | \
- ((uint32_t)tskKERNEL_VERSION_MINOR * 10000UL) | \
- ((uint32_t)tskKERNEL_VERSION_BUILD * 1UL))
- #define KERNEL_ID "FreeRTOS V10.0.1"
- /* Timer callback information structure definition */
- typedef struct {
- osTimerFunc_t func;
- void *arg;
- } TimerCallback_t;
- /* Kernel initialization state */
- static osKernelState_t KernelState;
- /* Heap region definition used by heap_5 variant */
- #if defined(USE_FreeRTOS_HEAP_5)
- #if (configAPPLICATION_ALLOCATED_HEAP == 1)
- /*
- The application writer has already defined the array used for the RTOS
- heap - probably so it can be placed in a special segment or address.
- */
- extern uint8_t ucHeap[configTOTAL_HEAP_SIZE];
- #else
- static uint8_t ucHeap[configTOTAL_HEAP_SIZE];
- #endif /* configAPPLICATION_ALLOCATED_HEAP */
- static HeapRegion_t xHeapRegions[] = {
- { ucHeap, configTOTAL_HEAP_SIZE },
- { NULL, 0 }
- };
- #endif /* USE_FreeRTOS_HEAP_5 */
- #if defined(SysTick)
- /* FreeRTOS tick timer interrupt handler prototype */
- extern void xPortSysTickHandler (void);
- /*
- SysTick handler implementation that also clears overflow flag.
- */
- void SysTick_Handler (void) {
- /* Clear overflow flag */
- SysTick->CTRL;
- /* Call tick handler */
- xPortSysTickHandler();
- }
- #endif /* SysTick */
- /*---------------------------------------------------------------------------*/
- osStatus_t osKernelInitialize (void) {
- osStatus_t stat;
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else {
- if (KernelState == osKernelInactive) {
- #if defined(USE_FreeRTOS_HEAP_5)
- vPortDefineHeapRegions (xHeapRegions);
- #endif
- KernelState = osKernelReady;
- stat = osOK;
- } else {
- stat = osError;
- }
- }
- return (stat);
- }
- osStatus_t osKernelGetInfo (osVersion_t *version, char *id_buf, uint32_t id_size) {
- if (version != NULL) {
- version->api = KERNEL_VERSION;
- version->kernel = KERNEL_VERSION;
- }
- if ((id_buf != NULL) && (id_size != 0U)) {
- if (id_size > sizeof(KERNEL_ID)) {
- id_size = sizeof(KERNEL_ID);
- }
- memcpy(id_buf, KERNEL_ID, id_size);
- }
- return (osOK);
- }
- osKernelState_t osKernelGetState (void) {
- osKernelState_t state;
- switch (xTaskGetSchedulerState()) {
- case taskSCHEDULER_RUNNING:
- state = osKernelRunning;
- break;
- case taskSCHEDULER_SUSPENDED:
- state = osKernelLocked;
- break;
- case taskSCHEDULER_NOT_STARTED:
- default:
- if (KernelState == osKernelReady) {
- state = osKernelReady;
- } else {
- state = osKernelInactive;
- }
- break;
- }
- return (state);
- }
- osStatus_t osKernelStart (void) {
- osStatus_t stat;
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else {
- if (KernelState == osKernelReady) {
- KernelState = osKernelRunning;
- vTaskStartScheduler();
- stat = osOK;
- } else {
- stat = osError;
- }
- }
- return (stat);
- }
- int32_t osKernelLock (void) {
- int32_t lock;
- if (IS_IRQ()) {
- lock = (int32_t)osErrorISR;
- }
- else {
- switch (xTaskGetSchedulerState()) {
- case taskSCHEDULER_SUSPENDED:
- lock = 1;
- break;
- case taskSCHEDULER_RUNNING:
- vTaskSuspendAll();
- lock = 0;
- break;
- case taskSCHEDULER_NOT_STARTED:
- default:
- lock = (int32_t)osError;
- break;
- }
- }
- return (lock);
- }
- int32_t osKernelUnlock (void) {
- int32_t lock;
- if (IS_IRQ()) {
- lock = (int32_t)osErrorISR;
- }
- else {
- switch (xTaskGetSchedulerState()) {
- case taskSCHEDULER_SUSPENDED:
- lock = 1;
- if (xTaskResumeAll() != pdTRUE) {
- if (xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED) {
- lock = (int32_t)osError;
- }
- }
- break;
- case taskSCHEDULER_RUNNING:
- lock = 0;
- break;
- case taskSCHEDULER_NOT_STARTED:
- default:
- lock = (int32_t)osError;
- break;
- }
- }
- return (lock);
- }
- int32_t osKernelRestoreLock (int32_t lock) {
- if (IS_IRQ()) {
- lock = (int32_t)osErrorISR;
- }
- else {
- switch (xTaskGetSchedulerState()) {
- case taskSCHEDULER_SUSPENDED:
- case taskSCHEDULER_RUNNING:
- if (lock == 1) {
- vTaskSuspendAll();
- }
- else {
- if (lock != 0) {
- lock = (int32_t)osError;
- }
- else {
- if (xTaskResumeAll() != pdTRUE) {
- if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
- lock = (int32_t)osError;
- }
- }
- }
- }
- break;
- case taskSCHEDULER_NOT_STARTED:
- default:
- lock = (int32_t)osError;
- break;
- }
- }
- return (lock);
- }
- uint32_t osKernelGetTickCount (void) {
- TickType_t ticks;
- if (IS_IRQ()) {
- ticks = xTaskGetTickCountFromISR();
- } else {
- ticks = xTaskGetTickCount();
- }
- return (ticks);
- }
- uint32_t osKernelGetTickFreq (void) {
- return (configTICK_RATE_HZ);
- }
- uint32_t osKernelGetSysTimerCount (void) {
- TickType_t ticks;
- uint32_t val;
- portDISABLE_INTERRUPTS();
- ticks = xTaskGetTickCount();
- val = ticks * ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );
- portENABLE_INTERRUPTS();
- return (val);
- }
- uint32_t osKernelGetSysTimerFreq (void) {
- return (configCPU_CLOCK_HZ);
- }
- /*---------------------------------------------------------------------------*/
- osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr) {
- char empty;
- const char *name;
- uint32_t stack;
- TaskHandle_t hTask;
- UBaseType_t prio;
- int32_t mem;
- hTask = NULL;
- if (!IS_IRQ() && (func != NULL)) {
- stack = configMINIMAL_STACK_SIZE;
- prio = (UBaseType_t)osPriorityNormal;
- empty = '\0';
- name = ∅
- mem = -1;
- if (attr != NULL) {
- if (attr->name != NULL) {
- name = attr->name;
- }
- if (attr->priority != osPriorityNone) {
- prio = (UBaseType_t)attr->priority;
- }
- if ((prio < osPriorityIdle) || (prio > osPriorityISR) || ((attr->attr_bits & osThreadJoinable) == osThreadJoinable)) {
- return (NULL);
- }
- if (attr->stack_size > 0U) {
- /* In FreeRTOS stack is not in bytes, but in sizeof(StackType_t) which is 4 on ARM ports. */
- /* Stack size should be therefore 4 byte aligned in order to avoid division caused side effects */
- stack = attr->stack_size / sizeof(StackType_t);
- }
- if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticTask_t)) &&
- (attr->stack_mem != NULL) && (attr->stack_size > 0U)) {
- mem = 1;
- }
- else {
- if ((attr->cb_mem == NULL) && (attr->cb_size == 0U) && (attr->stack_mem == NULL)) {
- mem = 0;
- }
- }
- }
- else {
- mem = 0;
- }
- if (mem == 1) {
- hTask = xTaskCreateStatic ((TaskFunction_t)func, name, stack, argument, prio, (StackType_t *)attr->stack_mem,
- (StaticTask_t *)attr->cb_mem);
- }
- else {
- if (mem == 0) {
- if (xTaskCreate ((TaskFunction_t)func, name, (uint16_t)stack, argument, prio, &hTask) != pdPASS) {
- hTask = NULL;
- }
- }
- }
- }
- return ((osThreadId_t)hTask);
- }
- const char *osThreadGetName (osThreadId_t thread_id) {
- TaskHandle_t hTask = (TaskHandle_t)thread_id;
- const char *name;
- if (IS_IRQ() || (hTask == NULL)) {
- name = NULL;
- } else {
- name = pcTaskGetName (hTask);
- }
- return (name);
- }
- osThreadId_t osThreadGetId (void) {
- osThreadId_t id;
- if (IS_IRQ()) {
- id = NULL;
- } else {
- id = (osThreadId_t)xTaskGetCurrentTaskHandle();
- }
- return (id);
- }
- osThreadState_t osThreadGetState (osThreadId_t thread_id) {
- TaskHandle_t hTask = (TaskHandle_t)thread_id;
- osThreadState_t state;
- if (IS_IRQ() || (hTask == NULL)) {
- state = osThreadError;
- }
- else {
- switch (eTaskGetState (hTask)) {
- case eRunning: state = osThreadRunning; break;
- case eReady: state = osThreadReady; break;
- case eBlocked:
- case eSuspended: state = osThreadBlocked; break;
- case eDeleted: state = osThreadTerminated; break;
- case eInvalid:
- default: state = osThreadError; break;
- }
- }
- return (state);
- }
- uint32_t osThreadGetStackSpace (osThreadId_t thread_id) {
- TaskHandle_t hTask = (TaskHandle_t)thread_id;
- uint32_t sz;
- if (IS_IRQ() || (hTask == NULL)) {
- sz = 0U;
- } else {
- sz = (uint32_t)uxTaskGetStackHighWaterMark (hTask);
- }
- return (sz);
- }
- uint32_t osThreadGetStackSize (osThreadId_t thread_id) {
- /*
- * this implmentation is not correct.
- * this function is implmented to avoid link errors (undefined reference)
- * Bug reported : https://github.com/ARM-software/CMSIS-FreeRTOS/issues/14
- */
- (void) thread_id;
- return 0;
- }
- osStatus_t osThreadSetPriority (osThreadId_t thread_id, osPriority_t priority) {
- TaskHandle_t hTask = (TaskHandle_t)thread_id;
- osStatus_t stat;
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else if ((hTask == NULL) || (priority < osPriorityIdle) || (priority > osPriorityISR)) {
- stat = osErrorParameter;
- }
- else {
- stat = osOK;
- vTaskPrioritySet (hTask, (UBaseType_t)priority);
- }
- return (stat);
- }
- osPriority_t osThreadGetPriority (osThreadId_t thread_id) {
- TaskHandle_t hTask = (TaskHandle_t)thread_id;
- osPriority_t prio;
- if (IS_IRQ() || (hTask == NULL)) {
- prio = osPriorityError;
- } else {
- prio = (osPriority_t)uxTaskPriorityGet (hTask);
- }
- return (prio);
- }
- osStatus_t osThreadYield (void) {
- osStatus_t stat;
- if (IS_IRQ()) {
- stat = osErrorISR;
- } else {
- stat = osOK;
- taskYIELD();
- }
- return (stat);
- }
- osStatus_t osThreadSuspend (osThreadId_t thread_id) {
- TaskHandle_t hTask = (TaskHandle_t)thread_id;
- osStatus_t stat;
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else if (hTask == NULL) {
- stat = osErrorParameter;
- }
- else {
- stat = osOK;
- vTaskSuspend (hTask);
- }
- return (stat);
- }
- osStatus_t osThreadResume (osThreadId_t thread_id) {
- TaskHandle_t hTask = (TaskHandle_t)thread_id;
- osStatus_t stat;
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else if (hTask == NULL) {
- stat = osErrorParameter;
- }
- else {
- stat = osOK;
- vTaskResume (hTask);
- }
- return (stat);
- }
- __NO_RETURN void osThreadExit (void) {
- #ifndef USE_FreeRTOS_HEAP_1
- vTaskDelete (NULL);
- #endif
- for (;;);
- }
- osStatus_t osThreadTerminate (osThreadId_t thread_id) {
- TaskHandle_t hTask = (TaskHandle_t)thread_id;
- osStatus_t stat;
- #ifndef USE_FreeRTOS_HEAP_1
- eTaskState tstate;
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else if (hTask == NULL) {
- stat = osErrorParameter;
- }
- else {
- tstate = eTaskGetState (hTask);
- if (tstate != eDeleted) {
- stat = osOK;
- vTaskDelete (hTask);
- } else {
- stat = osErrorResource;
- }
- }
- #else
- stat = osError;
- #endif
- return (stat);
- }
- uint32_t osThreadGetCount (void) {
- uint32_t count;
- if (IS_IRQ()) {
- count = 0U;
- } else {
- count = uxTaskGetNumberOfTasks();
- }
- return (count);
- }
- uint32_t osThreadEnumerate (osThreadId_t *thread_array, uint32_t array_items) {
- uint32_t i, count;
- TaskStatus_t *task;
- if (IS_IRQ() || (thread_array == NULL) || (array_items == 0U)) {
- count = 0U;
- } else {
- vTaskSuspendAll();
- count = uxTaskGetNumberOfTasks();
- task = pvPortMalloc (count * sizeof(TaskStatus_t));
- if (task != NULL) {
- count = uxTaskGetSystemState (task, count, NULL);
- for (i = 0U; (i < count) && (i < array_items); i++) {
- thread_array[i] = (osThreadId_t)task[i].xHandle;
- }
- count = i;
- }
- (void)xTaskResumeAll();
- vPortFree (task);
- }
- return (count);
- }
- uint32_t osThreadFlagsSet (osThreadId_t thread_id, uint32_t flags) {
- TaskHandle_t hTask = (TaskHandle_t)thread_id;
- uint32_t rflags;
- BaseType_t yield;
- if ((hTask == NULL) || ((flags & THREAD_FLAGS_INVALID_BITS) != 0U)) {
- rflags = (uint32_t)osErrorParameter;
- }
- else {
- rflags = (uint32_t)osError;
- if (IS_IRQ()) {
- yield = pdFALSE;
- (void)xTaskNotifyFromISR (hTask, flags, eSetBits, &yield);
- (void)xTaskNotifyAndQueryFromISR (hTask, 0, eNoAction, &rflags, NULL);
- portYIELD_FROM_ISR (yield);
- }
- else {
- (void)xTaskNotify (hTask, flags, eSetBits);
- (void)xTaskNotifyAndQuery (hTask, 0, eNoAction, &rflags);
- }
- }
- /* Return flags after setting */
- return (rflags);
- }
- uint32_t osThreadFlagsClear (uint32_t flags) {
- TaskHandle_t hTask;
- uint32_t rflags, cflags;
- if (IS_IRQ()) {
- rflags = (uint32_t)osErrorISR;
- }
- else if ((flags & THREAD_FLAGS_INVALID_BITS) != 0U) {
- rflags = (uint32_t)osErrorParameter;
- }
- else {
- hTask = xTaskGetCurrentTaskHandle();
- if (xTaskNotifyAndQuery (hTask, 0, eNoAction, &cflags) == pdPASS) {
- rflags = cflags;
- cflags &= ~flags;
- if (xTaskNotify (hTask, cflags, eSetValueWithOverwrite) != pdPASS) {
- rflags = (uint32_t)osError;
- }
- }
- else {
- rflags = (uint32_t)osError;
- }
- }
- /* Return flags before clearing */
- return (rflags);
- }
- uint32_t osThreadFlagsGet (void) {
- TaskHandle_t hTask;
- uint32_t rflags;
- if (IS_IRQ()) {
- rflags = (uint32_t)osErrorISR;
- }
- else {
- hTask = xTaskGetCurrentTaskHandle();
- if (xTaskNotifyAndQuery (hTask, 0, eNoAction, &rflags) != pdPASS) {
- rflags = (uint32_t)osError;
- }
- }
- return (rflags);
- }
- uint32_t osThreadFlagsWait (uint32_t flags, uint32_t options, uint32_t timeout) {
- uint32_t rflags, nval;
- uint32_t clear;
- TickType_t t0, td, tout;
- BaseType_t rval;
- if (IS_IRQ()) {
- rflags = (uint32_t)osErrorISR;
- }
- else if ((flags & THREAD_FLAGS_INVALID_BITS) != 0U) {
- rflags = (uint32_t)osErrorParameter;
- }
- else {
- if ((options & osFlagsNoClear) == osFlagsNoClear) {
- clear = 0U;
- } else {
- clear = flags;
- }
- rflags = 0U;
- tout = timeout;
- t0 = xTaskGetTickCount();
- do {
- rval = xTaskNotifyWait (0, clear, &nval, tout);
- if (rval == pdPASS) {
- rflags &= flags;
- rflags |= nval;
- if ((options & osFlagsWaitAll) == osFlagsWaitAll) {
- if ((flags & rflags) == flags) {
- break;
- } else {
- if (timeout == 0U) {
- rflags = (uint32_t)osErrorResource;
- break;
- }
- }
- }
- else {
- if ((flags & rflags) != 0) {
- break;
- } else {
- if (timeout == 0U) {
- rflags = (uint32_t)osErrorResource;
- break;
- }
- }
- }
- /* Update timeout */
- td = xTaskGetTickCount() - t0;
- if (td > tout) {
- tout = 0;
- } else {
- tout -= td;
- }
- }
- else {
- if (timeout == 0) {
- rflags = (uint32_t)osErrorResource;
- } else {
- rflags = (uint32_t)osErrorTimeout;
- }
- }
- }
- while (rval != pdFAIL);
- }
- /* Return flags before clearing */
- return (rflags);
- }
- osStatus_t osDelay (uint32_t ticks) {
- osStatus_t stat;
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else {
- stat = osOK;
- if (ticks != 0U) {
- vTaskDelay(ticks);
- }
- }
- return (stat);
- }
- osStatus_t osDelayUntil (uint32_t ticks) {
- TickType_t tcnt;
- osStatus_t stat;
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else {
- stat = osOK;
- tcnt = xTaskGetTickCount();
- vTaskDelayUntil (&tcnt, (TickType_t)(ticks - tcnt));
- }
- return (stat);
- }
- /*---------------------------------------------------------------------------*/
- static void TimerCallback (TimerHandle_t hTimer) {
- TimerCallback_t *callb;
- callb = (TimerCallback_t *)pvTimerGetTimerID (hTimer);
- if (callb != NULL) {
- callb->func (callb->arg);
- }
- }
- osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr) {
- const char *name;
- TimerHandle_t hTimer;
- TimerCallback_t *callb;
- UBaseType_t reload;
- int32_t mem;
- hTimer = NULL;
- if (!IS_IRQ() && (func != NULL)) {
- /* Allocate memory to store callback function and argument */
- callb = pvPortMalloc (sizeof(TimerCallback_t));
- if (callb != NULL) {
- callb->func = func;
- callb->arg = argument;
- if (type == osTimerOnce) {
- reload = pdFALSE;
- } else {
- reload = pdTRUE;
- }
- mem = -1;
- name = NULL;
- if (attr != NULL) {
- if (attr->name != NULL) {
- name = attr->name;
- }
- if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticTimer_t))) {
- mem = 1;
- }
- else {
- if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
- mem = 0;
- }
- }
- }
- else {
- mem = 0;
- }
- if (mem == 1) {
- hTimer = xTimerCreateStatic (name, 1, reload, callb, TimerCallback, (StaticTimer_t *)attr->cb_mem);
- }
- else {
- if (mem == 0) {
- hTimer = xTimerCreate (name, 1, reload, callb, TimerCallback);
- }
- }
- }
- }
- return ((osTimerId_t)hTimer);
- }
- const char *osTimerGetName (osTimerId_t timer_id) {
- TimerHandle_t hTimer = (TimerHandle_t)timer_id;
- const char *p;
- if (IS_IRQ() || (hTimer == NULL)) {
- p = NULL;
- } else {
- p = pcTimerGetName (hTimer);
- }
- return (p);
- }
- osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks) {
- TimerHandle_t hTimer = (TimerHandle_t)timer_id;
- osStatus_t stat;
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else if (hTimer == NULL) {
- stat = osErrorParameter;
- }
- else {
- if (xTimerChangePeriod (hTimer, ticks, 0) == pdPASS) {
- stat = osOK;
- } else {
- stat = osErrorResource;
- }
- }
- return (stat);
- }
- osStatus_t osTimerStop (osTimerId_t timer_id) {
- TimerHandle_t hTimer = (TimerHandle_t)timer_id;
- osStatus_t stat;
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else if (hTimer == NULL) {
- stat = osErrorParameter;
- }
- else {
- if (xTimerIsTimerActive (hTimer) == pdFALSE) {
- stat = osErrorResource;
- }
- else {
- if (xTimerStop (hTimer, 0) == pdPASS) {
- stat = osOK;
- } else {
- stat = osError;
- }
- }
- }
- return (stat);
- }
- uint32_t osTimerIsRunning (osTimerId_t timer_id) {
- TimerHandle_t hTimer = (TimerHandle_t)timer_id;
- uint32_t running;
- if (IS_IRQ() || (hTimer == NULL)) {
- running = 0U;
- } else {
- running = (uint32_t)xTimerIsTimerActive (hTimer);
- }
- return (running);
- }
- osStatus_t osTimerDelete (osTimerId_t timer_id) {
- TimerHandle_t hTimer = (TimerHandle_t)timer_id;
- osStatus_t stat;
- #ifndef USE_FreeRTOS_HEAP_1
- TimerCallback_t *callb;
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else if (hTimer == NULL) {
- stat = osErrorParameter;
- }
- else {
- callb = (TimerCallback_t *)pvTimerGetTimerID (hTimer);
- if (xTimerDelete (hTimer, 0) == pdPASS) {
- vPortFree (callb);
- stat = osOK;
- } else {
- stat = osErrorResource;
- }
- }
- #else
- stat = osError;
- #endif
- return (stat);
- }
- /*---------------------------------------------------------------------------*/
- osEventFlagsId_t osEventFlagsNew (const osEventFlagsAttr_t *attr) {
- EventGroupHandle_t hEventGroup;
- int32_t mem;
- hEventGroup = NULL;
- if (!IS_IRQ()) {
- mem = -1;
- if (attr != NULL) {
- if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticEventGroup_t))) {
- mem = 1;
- }
- else {
- if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
- mem = 0;
- }
- }
- }
- else {
- mem = 0;
- }
- if (mem == 1) {
- hEventGroup = xEventGroupCreateStatic (attr->cb_mem);
- }
- else {
- if (mem == 0) {
- hEventGroup = xEventGroupCreate();
- }
- }
- }
- return ((osEventFlagsId_t)hEventGroup);
- }
- uint32_t osEventFlagsSet (osEventFlagsId_t ef_id, uint32_t flags) {
- EventGroupHandle_t hEventGroup = (EventGroupHandle_t)ef_id;
- uint32_t rflags;
- BaseType_t yield;
- if ((hEventGroup == NULL) || ((flags & EVENT_FLAGS_INVALID_BITS) != 0U)) {
- rflags = (uint32_t)osErrorParameter;
- }
- else if (IS_IRQ()) {
- yield = pdFALSE;
- if (xEventGroupSetBitsFromISR (hEventGroup, (EventBits_t)flags, &yield) != pdFAIL) {
- rflags = (uint32_t)osErrorResource;
- } else {
- rflags = flags;
- portYIELD_FROM_ISR (yield);
- }
- }
- else {
- rflags = xEventGroupSetBits (hEventGroup, (EventBits_t)flags);
- }
- return (rflags);
- }
- uint32_t osEventFlagsClear (osEventFlagsId_t ef_id, uint32_t flags) {
- EventGroupHandle_t hEventGroup = (EventGroupHandle_t)ef_id;
- uint32_t rflags;
- if ((hEventGroup == NULL) || ((flags & EVENT_FLAGS_INVALID_BITS) != 0U)) {
- rflags = (uint32_t)osErrorParameter;
- }
- else if (IS_IRQ()) {
- rflags = xEventGroupGetBitsFromISR (hEventGroup);
- if (xEventGroupClearBitsFromISR (hEventGroup, (EventBits_t)flags) == pdFAIL) {
- rflags = (uint32_t)osErrorResource;
- }
- }
- else {
- rflags = xEventGroupClearBits (hEventGroup, (EventBits_t)flags);
- }
- return (rflags);
- }
- uint32_t osEventFlagsGet (osEventFlagsId_t ef_id) {
- EventGroupHandle_t hEventGroup = (EventGroupHandle_t)ef_id;
- uint32_t rflags;
- if (ef_id == NULL) {
- rflags = 0U;
- }
- else if (IS_IRQ()) {
- rflags = xEventGroupGetBitsFromISR (hEventGroup);
- }
- else {
- rflags = xEventGroupGetBits (hEventGroup);
- }
- return (rflags);
- }
- uint32_t osEventFlagsWait (osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout) {
- EventGroupHandle_t hEventGroup = (EventGroupHandle_t)ef_id;
- BaseType_t wait_all;
- BaseType_t exit_clr;
- uint32_t rflags;
- if ((hEventGroup == NULL) || ((flags & EVENT_FLAGS_INVALID_BITS) != 0U)) {
- rflags = (uint32_t)osErrorParameter;
- }
- else if (IS_IRQ()) {
- rflags = (uint32_t)osErrorISR;
- }
- else {
- if (options & osFlagsWaitAll) {
- wait_all = pdTRUE;
- } else {
- wait_all = pdFAIL;
- }
- if (options & osFlagsNoClear) {
- exit_clr = pdFAIL;
- } else {
- exit_clr = pdTRUE;
- }
- rflags = xEventGroupWaitBits (hEventGroup, (EventBits_t)flags, exit_clr, wait_all, (TickType_t)timeout);
- if (options & osFlagsWaitAll) {
- if (flags != rflags) {
- if (timeout > 0U) {
- rflags = (uint32_t)osErrorTimeout;
- } else {
- rflags = (uint32_t)osErrorResource;
- }
- }
- }
- else {
- if ((flags & rflags) == 0U) {
- if (timeout > 0U) {
- rflags = (uint32_t)osErrorTimeout;
- } else {
- rflags = (uint32_t)osErrorResource;
- }
- }
- }
- }
- return (rflags);
- }
- osStatus_t osEventFlagsDelete (osEventFlagsId_t ef_id) {
- EventGroupHandle_t hEventGroup = (EventGroupHandle_t)ef_id;
- osStatus_t stat;
- #ifndef USE_FreeRTOS_HEAP_1
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else if (hEventGroup == NULL) {
- stat = osErrorParameter;
- }
- else {
- stat = osOK;
- vEventGroupDelete (hEventGroup);
- }
- #else
- stat = osError;
- #endif
- return (stat);
- }
- /*---------------------------------------------------------------------------*/
- osMutexId_t osMutexNew (const osMutexAttr_t *attr) {
- SemaphoreHandle_t hMutex;
- uint32_t type;
- uint32_t rmtx;
- int32_t mem;
- #if (configQUEUE_REGISTRY_SIZE > 0)
- const char *name;
- #endif
- hMutex = NULL;
- if (!IS_IRQ()) {
- if (attr != NULL) {
- type = attr->attr_bits;
- } else {
- type = 0U;
- }
- if ((type & osMutexRecursive) == osMutexRecursive) {
- rmtx = 1U;
- } else {
- rmtx = 0U;
- }
- if ((type & osMutexRobust) != osMutexRobust) {
- mem = -1;
- if (attr != NULL) {
- if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticSemaphore_t))) {
- mem = 1;
- }
- else {
- if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
- mem = 0;
- }
- }
- }
- else {
- mem = 0;
- }
- if (mem == 1) {
- if (rmtx != 0U) {
- hMutex = xSemaphoreCreateRecursiveMutexStatic (attr->cb_mem);
- }
- else {
- hMutex = xSemaphoreCreateMutexStatic (attr->cb_mem);
- }
- }
- else {
- if (mem == 0) {
- if (rmtx != 0U) {
- hMutex = xSemaphoreCreateRecursiveMutex ();
- } else {
- hMutex = xSemaphoreCreateMutex ();
- }
- }
- }
- #if (configQUEUE_REGISTRY_SIZE > 0)
- if (hMutex != NULL) {
- if (attr != NULL) {
- name = attr->name;
- } else {
- name = NULL;
- }
- vQueueAddToRegistry (hMutex, name);
- }
- #endif
- if ((hMutex != NULL) && (rmtx != 0U)) {
- hMutex = (SemaphoreHandle_t)((uint32_t)hMutex | 1U);
- }
- }
- }
- return ((osMutexId_t)hMutex);
- }
- osStatus_t osMutexAcquire (osMutexId_t mutex_id, uint32_t timeout) {
- SemaphoreHandle_t hMutex;
- osStatus_t stat;
- uint32_t rmtx;
- hMutex = (SemaphoreHandle_t)((uint32_t)mutex_id & ~1U);
- rmtx = (uint32_t)mutex_id & 1U;
- stat = osOK;
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else if (hMutex == NULL) {
- stat = osErrorParameter;
- }
- else {
- if (rmtx != 0U) {
- if (xSemaphoreTakeRecursive (hMutex, timeout) != pdPASS) {
- if (timeout != 0U) {
- stat = osErrorTimeout;
- } else {
- stat = osErrorResource;
- }
- }
- }
- else {
- if (xSemaphoreTake (hMutex, timeout) != pdPASS) {
- if (timeout != 0U) {
- stat = osErrorTimeout;
- } else {
- stat = osErrorResource;
- }
- }
- }
- }
- return (stat);
- }
- osStatus_t osMutexRelease (osMutexId_t mutex_id) {
- SemaphoreHandle_t hMutex;
- osStatus_t stat;
- uint32_t rmtx;
- hMutex = (SemaphoreHandle_t)((uint32_t)mutex_id & ~1U);
- rmtx = (uint32_t)mutex_id & 1U;
- stat = osOK;
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else if (hMutex == NULL) {
- stat = osErrorParameter;
- }
- else {
- if (rmtx != 0U) {
- if (xSemaphoreGiveRecursive (hMutex) != pdPASS) {
- stat = osErrorResource;
- }
- }
- else {
- if (xSemaphoreGive (hMutex) != pdPASS) {
- stat = osErrorResource;
- }
- }
- }
- return (stat);
- }
- osThreadId_t osMutexGetOwner (osMutexId_t mutex_id) {
- SemaphoreHandle_t hMutex;
- osThreadId_t owner;
- hMutex = (SemaphoreHandle_t)((uint32_t)mutex_id & ~1U);
- if (IS_IRQ() || (hMutex == NULL)) {
- owner = NULL;
- } else {
- owner = (osThreadId_t)xSemaphoreGetMutexHolder (hMutex);
- }
- return (owner);
- }
- osStatus_t osMutexDelete (osMutexId_t mutex_id) {
- osStatus_t stat;
- #ifndef USE_FreeRTOS_HEAP_1
- SemaphoreHandle_t hMutex;
- hMutex = (SemaphoreHandle_t)((uint32_t)mutex_id & ~1U);
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else if (hMutex == NULL) {
- stat = osErrorParameter;
- }
- else {
- #if (configQUEUE_REGISTRY_SIZE > 0)
- vQueueUnregisterQueue (hMutex);
- #endif
- stat = osOK;
- vSemaphoreDelete (hMutex);
- }
- #else
- stat = osError;
- #endif
- return (stat);
- }
- /*---------------------------------------------------------------------------*/
- osSemaphoreId_t osSemaphoreNew (uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr) {
- SemaphoreHandle_t hSemaphore;
- int32_t mem;
- #if (configQUEUE_REGISTRY_SIZE > 0)
- const char *name;
- #endif
- hSemaphore = NULL;
- if (!IS_IRQ() && (max_count > 0U) && (initial_count <= max_count)) {
- mem = -1;
- if (attr != NULL) {
- if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticSemaphore_t))) {
- mem = 1;
- }
- else {
- if ((attr->cb_mem == NULL) && (attr->cb_size == 0U)) {
- mem = 0;
- }
- }
- }
- else {
- mem = 0;
- }
- if (mem != -1) {
- if (max_count == 1U) {
- if (mem == 1) {
- hSemaphore = xSemaphoreCreateBinaryStatic ((StaticSemaphore_t *)attr->cb_mem);
- }
- else {
- hSemaphore = xSemaphoreCreateBinary();
- }
- if ((hSemaphore != NULL) && (initial_count != 0U)) {
- if (xSemaphoreGive (hSemaphore) != pdPASS) {
- vSemaphoreDelete (hSemaphore);
- hSemaphore = NULL;
- }
- }
- }
- else {
- if (mem == 1) {
- hSemaphore = xSemaphoreCreateCountingStatic (max_count, initial_count, (StaticSemaphore_t *)attr->cb_mem);
- }
- else {
- hSemaphore = xSemaphoreCreateCounting (max_count, initial_count);
- }
- }
- #if (configQUEUE_REGISTRY_SIZE > 0)
- if (hSemaphore != NULL) {
- if (attr != NULL) {
- name = attr->name;
- } else {
- name = NULL;
- }
- vQueueAddToRegistry (hSemaphore, name);
- }
- #endif
- }
- }
- return ((osSemaphoreId_t)hSemaphore);
- }
- osStatus_t osSemaphoreAcquire (osSemaphoreId_t semaphore_id, uint32_t timeout) {
- SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)semaphore_id;
- osStatus_t stat;
- BaseType_t yield;
- stat = osOK;
- if (hSemaphore == NULL) {
- stat = osErrorParameter;
- }
- else if (IS_IRQ()) {
- if (timeout != 0U) {
- stat = osErrorParameter;
- }
- else {
- yield = pdFALSE;
- if (xSemaphoreTakeFromISR (hSemaphore, &yield) != pdPASS) {
- stat = osErrorResource;
- } else {
- portYIELD_FROM_ISR (yield);
- }
- }
- }
- else {
- if (xSemaphoreTake (hSemaphore, (TickType_t)timeout) != pdPASS) {
- if (timeout != 0U) {
- stat = osErrorTimeout;
- } else {
- stat = osErrorResource;
- }
- }
- }
- return (stat);
- }
- osStatus_t osSemaphoreRelease (osSemaphoreId_t semaphore_id) {
- SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)semaphore_id;
- osStatus_t stat;
- BaseType_t yield;
- stat = osOK;
- if (hSemaphore == NULL) {
- stat = osErrorParameter;
- }
- else if (IS_IRQ()) {
- yield = pdFALSE;
- if (xSemaphoreGiveFromISR (hSemaphore, &yield) != pdTRUE) {
- stat = osErrorResource;
- } else {
- portYIELD_FROM_ISR (yield);
- }
- }
- else {
- if (xSemaphoreGive (hSemaphore) != pdPASS) {
- stat = osErrorResource;
- }
- }
- return (stat);
- }
- uint32_t osSemaphoreGetCount (osSemaphoreId_t semaphore_id) {
- SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)semaphore_id;
- uint32_t count;
- if (hSemaphore == NULL) {
- count = 0U;
- }
- else if (IS_IRQ()) {
- count = uxQueueMessagesWaitingFromISR (hSemaphore);
- } else {
- count = (uint32_t)uxSemaphoreGetCount (hSemaphore);
- }
- return (count);
- }
- osStatus_t osSemaphoreDelete (osSemaphoreId_t semaphore_id) {
- SemaphoreHandle_t hSemaphore = (SemaphoreHandle_t)semaphore_id;
- osStatus_t stat;
- #ifndef USE_FreeRTOS_HEAP_1
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else if (hSemaphore == NULL) {
- stat = osErrorParameter;
- }
- else {
- #if (configQUEUE_REGISTRY_SIZE > 0)
- vQueueUnregisterQueue (hSemaphore);
- #endif
- stat = osOK;
- vSemaphoreDelete (hSemaphore);
- }
- #else
- stat = osError;
- #endif
- return (stat);
- }
- /*---------------------------------------------------------------------------*/
- osMessageQueueId_t osMessageQueueNew (uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr) {
- QueueHandle_t hQueue;
- int32_t mem;
- #if (configQUEUE_REGISTRY_SIZE > 0)
- const char *name;
- #endif
- hQueue = NULL;
- if (!IS_IRQ() && (msg_count > 0U) && (msg_size > 0U)) {
- mem = -1;
- if (attr != NULL) {
- if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(StaticQueue_t)) &&
- (attr->mq_mem != NULL) && (attr->mq_size >= (msg_count * msg_size))) {
- mem = 1;
- }
- else {
- if ((attr->cb_mem == NULL) && (attr->cb_size == 0U) &&
- (attr->mq_mem == NULL) && (attr->mq_size == 0U)) {
- mem = 0;
- }
- }
- }
- else {
- mem = 0;
- }
- if (mem == 1) {
- hQueue = xQueueCreateStatic (msg_count, msg_size, attr->mq_mem, attr->cb_mem);
- }
- else {
- if (mem == 0) {
- hQueue = xQueueCreate (msg_count, msg_size);
- }
- }
- #if (configQUEUE_REGISTRY_SIZE > 0)
- if (hQueue != NULL) {
- if (attr != NULL) {
- name = attr->name;
- } else {
- name = NULL;
- }
- vQueueAddToRegistry (hQueue, name);
- }
- #endif
- }
- return ((osMessageQueueId_t)hQueue);
- }
- osStatus_t osMessageQueuePut (osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout) {
- QueueHandle_t hQueue = (QueueHandle_t)mq_id;
- osStatus_t stat;
- BaseType_t yield;
- (void)msg_prio; /* Message priority is ignored */
- stat = osOK;
- if (IS_IRQ()) {
- if ((hQueue == NULL) || (msg_ptr == NULL) || (timeout != 0U)) {
- stat = osErrorParameter;
- }
- else {
- yield = pdFALSE;
- if (xQueueSendToBackFromISR (hQueue, msg_ptr, &yield) != pdTRUE) {
- stat = osErrorResource;
- } else {
- portYIELD_FROM_ISR (yield);
- }
- }
- }
- else {
- if ((hQueue == NULL) || (msg_ptr == NULL)) {
- stat = osErrorParameter;
- }
- else {
- if (xQueueSendToBack (hQueue, msg_ptr, (TickType_t)timeout) != pdPASS) {
- if (timeout != 0U) {
- stat = osErrorTimeout;
- } else {
- stat = osErrorResource;
- }
- }
- }
- }
- return (stat);
- }
- osStatus_t osMessageQueueGet (osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout) {
- QueueHandle_t hQueue = (QueueHandle_t)mq_id;
- osStatus_t stat;
- BaseType_t yield;
- (void)msg_prio; /* Message priority is ignored */
- stat = osOK;
- if (IS_IRQ()) {
- if ((hQueue == NULL) || (msg_ptr == NULL) || (timeout != 0U)) {
- stat = osErrorParameter;
- }
- else {
- yield = pdFALSE;
- if (xQueueReceiveFromISR (hQueue, msg_ptr, &yield) != pdPASS) {
- stat = osErrorResource;
- } else {
- portYIELD_FROM_ISR (yield);
- }
- }
- }
- else {
- if ((hQueue == NULL) || (msg_ptr == NULL)) {
- stat = osErrorParameter;
- }
- else {
- if (xQueueReceive (hQueue, msg_ptr, (TickType_t)timeout) != pdPASS) {
- if (timeout != 0U) {
- stat = osErrorTimeout;
- } else {
- stat = osErrorResource;
- }
- }
- }
- }
- return (stat);
- }
- uint32_t osMessageQueueGetCapacity (osMessageQueueId_t mq_id) {
- StaticQueue_t *mq = (StaticQueue_t *)mq_id;
- uint32_t capacity;
- if (mq == NULL) {
- capacity = 0U;
- } else {
- /* capacity = pxQueue->uxLength */
- capacity = mq->uxDummy4[1];
- }
- return (capacity);
- }
- uint32_t osMessageQueueGetMsgSize (osMessageQueueId_t mq_id) {
- StaticQueue_t *mq = (StaticQueue_t *)mq_id;
- uint32_t size;
- if (mq == NULL) {
- size = 0U;
- } else {
- /* size = pxQueue->uxItemSize */
- size = mq->uxDummy4[2];
- }
- return (size);
- }
- uint32_t osMessageQueueGetCount (osMessageQueueId_t mq_id) {
- QueueHandle_t hQueue = (QueueHandle_t)mq_id;
- UBaseType_t count;
- if (hQueue == NULL) {
- count = 0U;
- }
- else if (IS_IRQ()) {
- count = uxQueueMessagesWaitingFromISR (hQueue);
- }
- else {
- count = uxQueueMessagesWaiting (hQueue);
- }
- return ((uint32_t)count);
- }
- uint32_t osMessageQueueGetSpace (osMessageQueueId_t mq_id) {
- StaticQueue_t *mq = (StaticQueue_t *)mq_id;
- uint32_t space;
- uint32_t isrm;
- if (mq == NULL) {
- space = 0U;
- }
- else if (IS_IRQ()) {
- isrm = taskENTER_CRITICAL_FROM_ISR();
- /* space = pxQueue->uxLength - pxQueue->uxMessagesWaiting; */
- space = mq->uxDummy4[1] - mq->uxDummy4[0];
- taskEXIT_CRITICAL_FROM_ISR(isrm);
- }
- else {
- space = (uint32_t)uxQueueSpacesAvailable ((QueueHandle_t)mq);
- }
- return (space);
- }
- osStatus_t osMessageQueueReset (osMessageQueueId_t mq_id) {
- QueueHandle_t hQueue = (QueueHandle_t)mq_id;
- osStatus_t stat;
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else if (hQueue == NULL) {
- stat = osErrorParameter;
- }
- else {
- stat = osOK;
- (void)xQueueReset (hQueue);
- }
- return (stat);
- }
- osStatus_t osMessageQueueDelete (osMessageQueueId_t mq_id) {
- QueueHandle_t hQueue = (QueueHandle_t)mq_id;
- osStatus_t stat;
- #ifndef USE_FreeRTOS_HEAP_1
- if (IS_IRQ()) {
- stat = osErrorISR;
- }
- else if (hQueue == NULL) {
- stat = osErrorParameter;
- }
- else {
- #if (configQUEUE_REGISTRY_SIZE > 0)
- vQueueUnregisterQueue (hQueue);
- #endif
- stat = osOK;
- vQueueDelete (hQueue);
- }
- #else
- stat = osError;
- #endif
- return (stat);
- }
- /*---------------------------------------------------------------------------*/
- /* Callback function prototypes */
- extern void vApplicationIdleHook (void);
- extern void vApplicationTickHook (void);
- extern void vApplicationMallocFailedHook (void);
- extern void vApplicationDaemonTaskStartupHook (void);
- extern void vApplicationStackOverflowHook (TaskHandle_t xTask, signed char *pcTaskName);
- /**
- Dummy implementation of the callback function vApplicationIdleHook().
- */
- #if (configUSE_IDLE_HOOK == 1)
- __WEAK void vApplicationIdleHook (void){}
- #endif
- /**
- Dummy implementation of the callback function vApplicationTickHook().
- */
- #if (configUSE_TICK_HOOK == 1)
- __WEAK void vApplicationTickHook (void){}
- #endif
- /**
- Dummy implementation of the callback function vApplicationMallocFailedHook().
- */
- #if (configUSE_MALLOC_FAILED_HOOK == 1)
- __WEAK void vApplicationMallocFailedHook (void){}
- #endif
- /**
- Dummy implementation of the callback function vApplicationDaemonTaskStartupHook().
- */
- #if (configUSE_DAEMON_TASK_STARTUP_HOOK == 1)
- __WEAK void vApplicationDaemonTaskStartupHook (void){}
- #endif
- /**
- Dummy implementation of the callback function vApplicationStackOverflowHook().
- */
- #if (configCHECK_FOR_STACK_OVERFLOW > 0)
- __WEAK void vApplicationStackOverflowHook (TaskHandle_t xTask, signed char *pcTaskName) {
- (void)xTask;
- (void)pcTaskName;
- }
- #endif
- /*---------------------------------------------------------------------------*/
- /* External Idle and Timer task static memory allocation functions */
- extern void vApplicationGetIdleTaskMemory (StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize);
- extern void vApplicationGetTimerTaskMemory (StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize);
- /* Idle task control block and stack */
- static StaticTask_t Idle_TCB;
- static StackType_t Idle_Stack[configMINIMAL_STACK_SIZE];
- /* Timer task control block and stack */
- static StaticTask_t Timer_TCB;
- static StackType_t Timer_Stack[configTIMER_TASK_STACK_DEPTH];
- /*
- vApplicationGetIdleTaskMemory gets called when configSUPPORT_STATIC_ALLOCATION
- equals to 1 and is required for static memory allocation support.
- */
- void vApplicationGetIdleTaskMemory (StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize) {
- *ppxIdleTaskTCBBuffer = &Idle_TCB;
- *ppxIdleTaskStackBuffer = &Idle_Stack[0];
- *pulIdleTaskStackSize = (uint32_t)configMINIMAL_STACK_SIZE;
- }
- /*
- vApplicationGetTimerTaskMemory gets called when configSUPPORT_STATIC_ALLOCATION
- equals to 1 and is required for static memory allocation support.
- */
- void vApplicationGetTimerTaskMemory (StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize) {
- *ppxTimerTaskTCBBuffer = &Timer_TCB;
- *ppxTimerTaskStackBuffer = &Timer_Stack[0];
- *pulTimerTaskStackSize = (uint32_t)configTIMER_TASK_STACK_DEPTH;
- }
|