cmsis_liteos2.c 42.5 KB
Newer Older
W
wenjun 已提交
1
/*
M
mamingshuai 已提交
2 3
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
W
wenjun 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

C
Caoruihong 已提交
32 33
#include "cmsis_os2.h"
#include "kal.h"
W
wenjun 已提交
34 35 36
#include "los_event.h"
#include "los_membox.h"
#include "los_memory.h"
C
Caoruihong 已提交
37 38 39 40 41 42
#include "los_interrupt.h"
#include "los_mux.h"
#include "los_queue.h"
#include "los_sem.h"
#include "los_swtmr.h"
#include "los_task.h"
43
#include "los_timer.h"
C
Caoruihong 已提交
44 45
#include "los_debug.h"

W
wenjun 已提交
46 47
#include "string.h"
#include "securec.h"
C
Caoruihong 已提交
48

W
wenjun 已提交
49 50 51 52 53
/* Kernel initialization state */
static osKernelState_t g_kernelState;

extern BOOL g_taskScheduled;

54 55 56 57 58 59
/* LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO <---> osPriorityNormal */
#define LOS_PRIORITY(cmsisPriority) (LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO - ((cmsisPriority) - osPriorityNormal))
#define CMSIS_PRIORITY(losPriority) (osPriorityNormal + (LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO - (losPriority)))

/* OS_TASK_PRIORITY_HIGHEST and OS_TASK_PRIORITY_LOWEST is reserved for internal TIMER and IDLE task use only. */
#define ISVALID_LOS_PRIORITY(losPrio) ((losPrio) > OS_TASK_PRIORITY_HIGHEST && (losPrio) < OS_TASK_PRIORITY_LOWEST)
W
wenjun 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73

const osVersion_t g_stLosVersion = { 001, 001 };

#define LITEOS_VERSION_MAJOR 1
#define LITEOS_VERSION_MINOR 0
#define LITEOS_VERSION_BUILD 0

/* Kernel version and identification string definition */
#define KERNEL_VERSION            (((UINT32)LITEOS_VERSION_MAJOR * 10000000UL) | \
                                   ((UINT32)LITEOS_VERSION_MINOR *    10000UL) | \
                                   ((UINT32)LITEOS_VERSION_BUILD *        1UL))

#define KERNEL_ID "HUAWEI-LiteOS"

L
LiteOS 已提交
74 75
#define KERNEL_UNLOCKED 0
#define KERNEL_LOCKED   1
W
wenjun 已提交
76

L
LiteOS 已提交
77
//  ==== Kernel Management Functions ====
W
wenjun 已提交
78 79 80 81 82 83 84 85 86 87
osStatus_t osKernelInitialize(void)
{
    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }

    if (g_kernelState != osKernelInactive) {
        return osError;
    }

L
LiteOS 已提交
88
    if (LOS_KernelInit() == LOS_OK) {
W
wenjun 已提交
89 90 91 92 93 94 95 96 97
        g_kernelState = osKernelReady;
        return osOK;
    } else {
        return osError;
    }
}

osStatus_t osKernelGetInfo(osVersion_t *version, char *id_buf, uint32_t id_size)
{
L
LiteOS 已提交
98
    errno_t ret;
W
wenjun 已提交
99

L
LiteOS 已提交
100 101
    if ((version == NULL) || (id_buf == NULL) || (id_size == 0)) {
        return osError;
W
wenjun 已提交
102 103
    }

L
LiteOS 已提交
104 105
    ret = memcpy_s(id_buf, id_size, KERNEL_ID, sizeof(KERNEL_ID));
    if (ret == EOK) {
W
wenjun 已提交
106 107
        version->api = g_stLosVersion.api;
        version->kernel = g_stLosVersion.kernel;
L
LiteOS 已提交
108 109 110 111
        return osOK;
    } else {
        PRINT_ERR("[%s] memcpy_s failed, error type = %d\n", __func__, ret);
        return osError;
W
wenjun 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    }
}

osKernelState_t osKernelGetState(void)
{
    if (!g_taskScheduled) {
        if (g_kernelState == osKernelReady) {
            return osKernelReady;
        } else {
            return osKernelInactive;
        }
    } else if (g_losTaskLock > 0) {
        return osKernelLocked;
    } else {
        return osKernelRunning;
    }
}

osStatus_t osKernelStart(void)
{
    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }
L
LiteOS 已提交
135 136 137
    if (g_kernelState != osKernelReady) {
        return osError;
    }
W
wenjun 已提交
138

L
LiteOS 已提交
139 140 141
    if (LOS_Start() == LOS_OK) {
        g_kernelState = osKernelRunning;
        return osOK;
W
wenjun 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    } else {
        return osError;
    }
}

int32_t osKernelLock(void)
{
    int32_t lock;

    if (OS_INT_ACTIVE) {
        return (int32_t)osErrorISR;
    }

    if (!g_taskScheduled) {
        return (int32_t)osError;
    }

    if (g_losTaskLock > 0) {
L
LiteOS 已提交
160
        lock = KERNEL_LOCKED;
W
wenjun 已提交
161 162
    } else {
        LOS_TaskLock();
L
LiteOS 已提交
163
        lock = KERNEL_UNLOCKED;
W
wenjun 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    }

    return lock;
}

int32_t osKernelUnlock(void)
{
    int32_t lock;

    if (OS_INT_ACTIVE) {
        return (int32_t)osErrorISR;
    }

    if (!g_taskScheduled) {
        return (int32_t)osError;
    }

    if (g_losTaskLock > 0) {
        LOS_TaskUnlock();
        if (g_losTaskLock != 0) {
            return (int32_t)osError;
        }
L
LiteOS 已提交
186
        lock = KERNEL_LOCKED;
W
wenjun 已提交
187
    } else {
L
LiteOS 已提交
188
        lock = KERNEL_UNLOCKED;
W
wenjun 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    }

    return lock;
}

int32_t osKernelRestoreLock(int32_t lock)
{
    if (OS_INT_ACTIVE) {
        return (int32_t)osErrorISR;
    }

    if (!g_taskScheduled) {
        return (int32_t)osError;
    }

    switch (lock) {
L
LiteOS 已提交
205
        case KERNEL_UNLOCKED:
W
wenjun 已提交
206 207 208 209
            LOS_TaskUnlock();
            if (g_losTaskLock != 0) {
                break;
            }
L
LiteOS 已提交
210 211
            return KERNEL_UNLOCKED;
        case KERNEL_LOCKED:
W
wenjun 已提交
212
            LOS_TaskLock();
L
LiteOS 已提交
213
            return KERNEL_LOCKED;
W
wenjun 已提交
214 215 216 217 218 219 220 221 222
        default:
            break;
    }

    return (int32_t)osError;
}

uint32_t osKernelGetTickCount(void)
{
星e雨's avatar
星e雨 已提交
223
    uint64_t ticks = LOS_TickCountGet();
W
wenjun 已提交
224 225 226 227 228
    return (uint32_t)ticks;
}

uint32_t osKernelGetTickFreq(void)
{
L
LiteOS 已提交
229
    return (uint32_t)LOSCFG_BASE_CORE_TICK_PER_SECOND;
W
wenjun 已提交
230 231 232 233
}

uint32_t osKernelGetSysTimerCount(void)
{
L
LiteOS 已提交
234
    return (uint32_t)LOS_SysCycleGet();
W
wenjun 已提交
235 236 237 238
}

uint32_t osKernelGetSysTimerFreq(void)
{
239
    return g_sysClock;
W
wenjun 已提交
240 241 242 243 244
}

//  ==== Thread Management Functions ====
osThreadId_t osThreadNew(osThreadFunc_t func, void *argument, const osThreadAttr_t *attr)
{
L
LiteOS 已提交
245 246
    UINT32 tid;
    UINT32 ret;
Z
zhushengle 已提交
247 248
    osThreadAttr_t attrTemp = {0};
    TSK_INIT_PARAM_S stTskInitParam = {0};
L
LiteOS 已提交
249
    UINT16 priority;
W
wenjun 已提交
250

251 252
    if (OS_INT_ACTIVE || (func == NULL)) {
        return (osThreadId_t)NULL;
W
wenjun 已提交
253 254
    }

Z
zhushengle 已提交
255 256 257 258 259 260
    if (attr == NULL) {
        attrTemp.priority = osPriorityNormal,
        attr = &attrTemp;
    }

    priority = LOS_PRIORITY(attr->priority);
L
LiteOS 已提交
261
    if (!ISVALID_LOS_PRIORITY(priority)) {
262
        /* unsupported priority */
W
wenjun 已提交
263 264 265 266
        return (osThreadId_t)NULL;
    }
    stTskInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)func;
    stTskInitParam.uwArg = (UINT32)argument;
Z
zhushengle 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280
    if ((attr->stack_mem != NULL) && (attr->stack_size != 0)) {
        stTskInitParam.stackAddr = (UINTPTR)attr->stack_mem;
        stTskInitParam.uwStackSize = attr->stack_size;
    } else if (attr->stack_size != 0) {
        stTskInitParam.uwStackSize = attr->stack_size;
    } else {
        stTskInitParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
    }
    if (attr->name != NULL) {
        stTskInitParam.pcName = (char *)attr->name;
    } else {
        stTskInitParam.pcName = "CmsisTask";
    }
    if (attr->attr_bits == osThreadJoinable) {
281 282
        stTskInitParam.uwResved = LOS_TASK_ATTR_JOINABLE;
    }
Z
zhushengle 已提交
283
    stTskInitParam.usTaskPrio = priority;
L
LiteOS 已提交
284 285
    ret = LOS_TaskCreate(&tid, &stTskInitParam);
    if (ret != LOS_OK) {
W
wenjun 已提交
286 287 288
        return (osThreadId_t)NULL;
    }

Z
zhushengle 已提交
289
    return (osThreadId_t)OS_TCB_FROM_TID(tid);
W
wenjun 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
}

const char *osThreadGetName(osThreadId_t thread_id)
{
    LosTaskCB *pstTaskCB = NULL;

    if (OS_INT_ACTIVE || thread_id == NULL) {
        return NULL;
    }

    pstTaskCB = (LosTaskCB *)thread_id;

    return pstTaskCB->taskName;
}

osThreadId_t osThreadGetId(void)
{
    return (osThreadId_t)(g_losTask.runTask);
}

void *osThreadGetArgument(void)
{
    if (OS_INT_ACTIVE) {
        return 0;
    }

    LosTaskCB *taskCb = (LosTaskCB *)osThreadGetId();
    if (taskCb == NULL) {
        return NULL;
    }
    return (void *)(taskCb->arg);
}

osThreadState_t osThreadGetState(osThreadId_t thread_id)
{
    UINT16 taskStatus;
    osThreadState_t stState;
    LosTaskCB *pstTaskCB = NULL;

    if (OS_INT_ACTIVE || thread_id == NULL) {
        return osThreadError;
    }

    pstTaskCB = (LosTaskCB *)thread_id;
    taskStatus = pstTaskCB->taskStatus;

    if (taskStatus & OS_TASK_STATUS_RUNNING) {
        stState = osThreadRunning;
    } else if (taskStatus & OS_TASK_STATUS_READY) {
        stState = osThreadReady;
    } else if (taskStatus &
L
LiteOS 已提交
341 342
        (OS_TASK_STATUS_DELAY | OS_TASK_STATUS_PEND |
         OS_TASK_STATUS_SUSPEND | OS_TASK_STATUS_PEND_TIME)) {
W
wenjun 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
        stState = osThreadBlocked;
    } else if (taskStatus & OS_TASK_STATUS_UNUSED) {
        stState = osThreadInactive;
    } else {
        stState = osThreadError;
    }

    return stState;
}

uint32_t osThreadGetStackSize(osThreadId_t thread_id)
{
    LosTaskCB *pstTaskCB = NULL;

    if (OS_INT_ACTIVE || thread_id == NULL) {
        return 0U;
    }

    pstTaskCB = (LosTaskCB *)thread_id;

    return pstTaskCB->stackSize;
}

uint32_t osTaskStackWaterMarkGet(UINT32 taskID)
{
L
LiteOS 已提交
368 369
    UINT32 count = 0;
    UINT32 *ptopOfStack = NULL;
370
    UINT32 intSave;
W
wenjun 已提交
371 372 373 374 375 376
    LosTaskCB *pstTaskCB = NULL;

    if (taskID > LOSCFG_BASE_CORE_TSK_LIMIT) {
        return 0;
    }

377
    intSave = LOS_IntLock();
W
wenjun 已提交
378 379 380

    pstTaskCB = OS_TCB_FROM_TID(taskID);
    if (OS_TASK_STATUS_UNUSED & (pstTaskCB->taskStatus)) {
381
        LOS_IntRestore(intSave);
W
wenjun 已提交
382 383 384 385 386 387 388 389
        return 0;
    }

    // first 4 bytes is OS_TASK_MAGIC_WORD, skip
    ptopOfStack = (UINT32 *)(UINTPTR)pstTaskCB->topOfStack + 1;

    while (*ptopOfStack == (UINT32)OS_TASK_STACK_INIT) {
        ++ptopOfStack;
L
LiteOS 已提交
390
        ++count;
W
wenjun 已提交
391 392
    }

L
LiteOS 已提交
393
    count *= sizeof(UINT32);
W
wenjun 已提交
394

395
    LOS_IntRestore(intSave);
L
LiteOS 已提交
396
    return count;
W
wenjun 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
}

uint32_t osThreadGetStackSpace(osThreadId_t thread_id)
{
    LosTaskCB *pstTaskCB = NULL;

    if (OS_INT_ACTIVE || thread_id == NULL) {
        return 0U;
    }

    pstTaskCB = (LosTaskCB *)thread_id;

    return osTaskStackWaterMarkGet(pstTaskCB->taskID);
}

osStatus_t osThreadSetPriority(osThreadId_t thread_id, osPriority_t priority)
{
L
LiteOS 已提交
414 415
    UINT32 ret;
    UINT16 prio;
W
wenjun 已提交
416 417 418 419 420 421 422 423 424 425
    LosTaskCB *pstTaskCB = NULL;

    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }

    if (thread_id == NULL) {
        return osErrorParameter;
    }

L
LiteOS 已提交
426 427
    prio = LOS_PRIORITY(priority);
    if (!ISVALID_LOS_PRIORITY(prio)) {
W
wenjun 已提交
428 429 430 431
        return osErrorParameter;
    }

    pstTaskCB = (LosTaskCB *)thread_id;
L
LiteOS 已提交
432 433
    ret = LOS_TaskPriSet(pstTaskCB->taskID, prio);
    switch (ret) {
W
wenjun 已提交
434
        case LOS_ERRNO_TSK_PRIOR_ERROR:
L
LiteOS 已提交
435
        case LOS_ERRNO_TSK_OPERATE_SYSTEM_TASK:
W
wenjun 已提交
436 437 438 439 440 441 442 443 444 445 446 447 448
        case LOS_ERRNO_TSK_ID_INVALID:
            return osErrorParameter;

        case LOS_ERRNO_TSK_NOT_CREATED:
            return osErrorResource;

        default:
            return osOK;
    }
}

osPriority_t osThreadGetPriority(osThreadId_t thread_id)
{
L
LiteOS 已提交
449
    UINT16 ret;
W
wenjun 已提交
450 451 452 453 454 455 456
    LosTaskCB *pstTaskCB = NULL;

    if (OS_INT_ACTIVE || thread_id == NULL) {
        return osPriorityError;
    }

    pstTaskCB = (LosTaskCB *)thread_id;
L
LiteOS 已提交
457
    ret = LOS_TaskPriGet(pstTaskCB->taskID);
W
wenjun 已提交
458

L
LiteOS 已提交
459
    if ((ret == (UINT16)OS_INVALID) || (ret > OS_TASK_PRIORITY_LOWEST)) {
W
wenjun 已提交
460 461 462
        return osPriorityError;
    }

L
LiteOS 已提交
463
    return (osPriority_t)CMSIS_PRIORITY(ret);
W
wenjun 已提交
464 465 466 467
}

osStatus_t osThreadYield(void)
{
L
LiteOS 已提交
468
    UINT32 ret;
W
wenjun 已提交
469 470 471 472 473

    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }

L
LiteOS 已提交
474
    ret = LOS_TaskYield();
W
wenjun 已提交
475

L
LiteOS 已提交
476
    if (ret == LOS_OK) {
W
wenjun 已提交
477 478 479 480 481 482 483 484
        return osOK;
    }

    return osError;
}

osStatus_t osThreadSuspend(osThreadId_t thread_id)
{
L
LiteOS 已提交
485
    UINT32 ret;
W
wenjun 已提交
486 487 488 489 490 491 492 493 494 495 496
    LosTaskCB *pstTaskCB = NULL;

    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }

    if (thread_id == NULL) {
        return osErrorParameter;
    }

    pstTaskCB = (LosTaskCB *)thread_id;
L
LiteOS 已提交
497 498
    ret = LOS_TaskSuspend(pstTaskCB->taskID);
    switch (ret) {
W
wenjun 已提交
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
        case LOS_ERRNO_TSK_OPERATE_IDLE:
        case LOS_ERRNO_TSK_SUSPEND_SWTMR_NOT_ALLOWED:
        case LOS_ERRNO_TSK_ID_INVALID:
            return osErrorParameter;

        case LOS_ERRNO_TSK_NOT_CREATED:
        case LOS_ERRNO_TSK_ALREADY_SUSPENDED:
        case LOS_ERRNO_TSK_SUSPEND_LOCKED:
            return osErrorResource;

        default:
            return osOK;
    }
}

osStatus_t osThreadResume(osThreadId_t thread_id)
{
L
LiteOS 已提交
516
    UINT32 ret;
W
wenjun 已提交
517 518 519 520 521 522 523 524 525 526 527
    LosTaskCB *pstTaskCB = NULL;

    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }

    if (thread_id == NULL) {
        return osErrorParameter;
    }

    pstTaskCB = (LosTaskCB *)thread_id;
L
LiteOS 已提交
528
    ret = LOS_TaskResume(pstTaskCB->taskID);
W
wenjun 已提交
529

L
LiteOS 已提交
530
    switch (ret) {
W
wenjun 已提交
531 532 533 534 535 536 537 538 539 540 541 542
        case LOS_ERRNO_TSK_ID_INVALID:
            return osErrorParameter;

        case LOS_ERRNO_TSK_NOT_CREATED:
        case LOS_ERRNO_TSK_NOT_SUSPENDED:
            return osErrorResource;

        default:
            return osOK;
    }
}

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
osStatus_t osThreadDetach(osThreadId_t thread_id)
{
    UINT32 ret;
    LosTaskCB *taskCB = (LosTaskCB *)thread_id;

    if (thread_id == NULL) {
        return osErrorParameter;
    }

    ret = LOS_TaskDetach(taskCB->taskID);
    if (ret == LOS_ERRNO_TSK_NOT_ALLOW_IN_INT) {
        return osErrorISR;
    } else if (ret != LOS_OK) {
        return osErrorResource;
    }

    return osOK;
}

osStatus_t osThreadJoin(osThreadId_t thread_id)
{
    UINT32 ret;
    LosTaskCB *taskCB = (LosTaskCB *)thread_id;

    if (thread_id == NULL) {
        return osErrorParameter;
    }

    ret = LOS_TaskJoin(taskCB->taskID, NULL);
    if (ret == LOS_ERRNO_TSK_NOT_ALLOW_IN_INT) {
        return osErrorISR;
    } else if (ret != LOS_OK) {
        return osErrorResource;
    }

    return osOK;
}

W
wenjun 已提交
581 582
osStatus_t osThreadTerminate(osThreadId_t thread_id)
{
L
LiteOS 已提交
583
    UINT32 ret;
W
wenjun 已提交
584 585 586 587 588 589 590 591 592 593 594
    LosTaskCB *pstTaskCB = NULL;

    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }

    if (thread_id == NULL) {
        return osErrorParameter;
    }

    pstTaskCB = (LosTaskCB *)thread_id;
L
LiteOS 已提交
595
    ret = LOS_TaskDelete(pstTaskCB->taskID);
W
wenjun 已提交
596

L
LiteOS 已提交
597
    switch (ret) {
W
wenjun 已提交
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
        case LOS_ERRNO_TSK_OPERATE_IDLE:
        case LOS_ERRNO_TSK_SUSPEND_SWTMR_NOT_ALLOWED:
        case LOS_ERRNO_TSK_ID_INVALID:
            return osErrorParameter;

        case LOS_ERRNO_TSK_NOT_CREATED:
            return osErrorResource;

        default:
            return osOK;
    }
}

uint32_t osThreadGetCount(void)
{
L
LiteOS 已提交
613
    uint32_t count = 0;
W
wenjun 已提交
614 615 616 617 618 619 620

    if (OS_INT_ACTIVE) {
        return 0U;
    }

    for (uint32_t index = 0; index <= LOSCFG_BASE_CORE_TSK_LIMIT; index++) {
        if (!((g_taskCBArray + index)->taskStatus & OS_TASK_STATUS_UNUSED)) {
L
LiteOS 已提交
621
            count++;
W
wenjun 已提交
622 623 624
        }
    }

L
LiteOS 已提交
625
    return count;
W
wenjun 已提交
626 627
}

C
Caoruihong 已提交
628 629 630 631 632 633
void osThreadExit(void)
{
    (void)LOS_TaskDelete(LOS_CurTaskIDGet());
    UNREACHABLE;
}

W
wenjun 已提交
634 635
osStatus_t osDelay(uint32_t ticks)
{
L
LiteOS 已提交
636 637 638
    UINT32 ret;
    if (OS_INT_ACTIVE) {
        return osErrorISR;
W
wenjun 已提交
639
    }
L
LiteOS 已提交
640 641
    if (ticks == 0) {
        return osErrorParameter;
W
wenjun 已提交
642
    }
L
LiteOS 已提交
643 644
    ret = LOS_TaskDelay(ticks);
    if (ret == LOS_OK) {
W
wenjun 已提交
645 646 647 648 649 650 651 652
        return osOK;
    } else {
        return osError;
    }
}

osStatus_t osDelayUntil(uint32_t ticks)
{
L
LiteOS 已提交
653
    UINT32 ret;
W
wenjun 已提交
654 655 656
    UINT32 uwTicks;
    UINT32 tickCount = osKernelGetTickCount();

L
LiteOS 已提交
657 658 659 660
    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }

W
wenjun 已提交
661 662 663 664 665 666
    if (ticks < tickCount) {
        return osError;
    }

    uwTicks = (UINT32)(ticks - tickCount);

L
LiteOS 已提交
667 668
    ret = LOS_TaskDelay(uwTicks);
    if (ret == LOS_OK) {
W
wenjun 已提交
669 670 671 672 673 674 675
        return osOK;
    } else {
        return osError;
    }
}

//  ==== Timer Management Functions ====
C
Caoruihong 已提交
676
#if (LOSCFG_BASE_CORE_SWTMR == 1)
W
wenjun 已提交
677 678 679
osTimerId_t osTimerNew(osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)
{
    UNUSED(attr);
L
LiteOS 已提交
680
    UINT32 swtmrId;
W
wenjun 已提交
681 682
    UINT8 mode;

L
LiteOS 已提交
683 684 685 686 687 688 689 690 691 692
    if ((func == NULL) || OS_INT_ACTIVE) {
        return NULL;
    }

    if (type == osTimerOnce) {
        mode = LOS_SWTMR_MODE_NO_SELFDELETE;
    } else if (type == osTimerPeriodic) {
        mode = LOS_SWTMR_MODE_PERIOD;
    } else {
        return NULL;
W
wenjun 已提交
693 694
    }

C
Caoruihong 已提交
695
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
L
LiteOS 已提交
696 697
    if (LOS_SwtmrCreate(1, mode, (SWTMR_PROC_FUNC)func, &swtmrId, (UINT32)(UINTPTR)argument,
        osTimerRousesAllow, osTimerAlignIgnore) != LOS_OK) {
W
wenjun 已提交
698 699 700
        return (osTimerId_t)NULL;
    }
#else
L
LiteOS 已提交
701
    if (LOS_SwtmrCreate(1, mode, (SWTMR_PROC_FUNC)func, &swtmrId, (UINT32)(UINTPTR)argument) != LOS_OK) {
W
wenjun 已提交
702 703 704
        return (osTimerId_t)NULL;
    }
#endif
L
LiteOS 已提交
705
    return (osTimerId_t)OS_SWT_FROM_SID(swtmrId);
W
wenjun 已提交
706
}
C
Caoruihong 已提交
707

C
Caoruihong 已提交
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
osTimerId_t osTimerExtNew(osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr,
    osTimerRouses_t ucRouses, osTimerAlign_t ucSensitive)
{
    UNUSED(attr);
    UINT32 usSwTmrID;
    UINT8 mode;

    if ((OS_INT_ACTIVE) || (NULL == func) || ((osTimerOnce != type) && (osTimerPeriodic != type))) {
        return (osTimerId_t)NULL;
    }

    if (osTimerOnce == type) {
        mode = LOS_SWTMR_MODE_NO_SELFDELETE;
    } else {
        mode = LOS_SWTMR_MODE_PERIOD;
    }
    if (LOS_OK != LOS_SwtmrCreate(1, mode, (SWTMR_PROC_FUNC)func, &usSwTmrID,
        (UINT32)(UINTPTR)argument, ucRouses, ucSensitive)) {
        return (osTimerId_t)NULL;
    }

    return (osTimerId_t)OS_SWT_FROM_SID(usSwTmrID);
}
#endif

W
wenjun 已提交
734 735
osStatus_t osTimerStart(osTimerId_t timer_id, uint32_t ticks)
{
L
LiteOS 已提交
736 737 738 739 740 741
    UINT32 ret;
    SWTMR_CTRL_S *pstSwtmr = NULL;
    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }
    if ((ticks == 0) || (timer_id == NULL)) {
W
wenjun 已提交
742 743 744
        return osErrorParameter;
    }

745
    UINT32 intSave = LOS_IntLock();
W
wenjun 已提交
746 747
    pstSwtmr = (SWTMR_CTRL_S *)timer_id;
    pstSwtmr->uwInterval = ticks;
L
LiteOS 已提交
748
    ret = LOS_SwtmrStart(pstSwtmr->usTimerID);
C
Caoruihong 已提交
749
    LOS_IntRestore(intSave);
L
LiteOS 已提交
750
    if (ret == LOS_OK) {
W
wenjun 已提交
751
        return osOK;
L
LiteOS 已提交
752
    } else if (ret == LOS_ERRNO_SWTMR_ID_INVALID) {
W
wenjun 已提交
753 754 755 756 757 758 759 760 761 762 763 764 765 766
        return osErrorParameter;
    } else {
        return osErrorResource;
    }
}

const char *osTimerGetName(osTimerId_t timer_id)
{
    UNUSED(timer_id);
    return (const char *)NULL;
}

osStatus_t osTimerStop(osTimerId_t timer_id)
{
L
LiteOS 已提交
767
    UINT32 ret;
W
wenjun 已提交
768 769
    SWTMR_CTRL_S *pstSwtmr = (SWTMR_CTRL_S *)timer_id;

L
LiteOS 已提交
770 771 772 773 774
    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }

    if (pstSwtmr == NULL) {
W
wenjun 已提交
775 776 777
        return osErrorParameter;
    }

L
LiteOS 已提交
778 779
    ret = LOS_SwtmrStop(pstSwtmr->usTimerID);
    if (ret == LOS_OK) {
W
wenjun 已提交
780
        return osOK;
L
LiteOS 已提交
781
    } else if (ret == LOS_ERRNO_SWTMR_ID_INVALID) {
W
wenjun 已提交
782 783 784 785 786 787 788 789
        return osErrorParameter;
    } else {
        return osErrorResource;
    }
}

uint32_t osTimerIsRunning(osTimerId_t timer_id)
{
L
LiteOS 已提交
790
    if (OS_INT_ACTIVE) {
王liangliang's avatar
王liangliang 已提交
791
        return (uint32_t)osErrorISR;
L
LiteOS 已提交
792 793
    }
    if (timer_id == NULL) {
W
wenjun 已提交
794 795 796 797 798 799 800 801
        return 0;
    }

    return (OS_SWTMR_STATUS_TICKING == ((SWTMR_CTRL_S *)timer_id)->ucState);
}

osStatus_t osTimerDelete(osTimerId_t timer_id)
{
L
LiteOS 已提交
802
    UINT32 ret;
W
wenjun 已提交
803 804
    SWTMR_CTRL_S *pstSwtmr = (SWTMR_CTRL_S *)timer_id;

L
LiteOS 已提交
805 806 807 808
    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }
    if (pstSwtmr == NULL) {
W
wenjun 已提交
809 810
        return osErrorParameter;
    }
L
LiteOS 已提交
811 812
    ret = LOS_SwtmrDelete(pstSwtmr->usTimerID);
    if (ret == LOS_OK) {
W
wenjun 已提交
813
        return osOK;
L
LiteOS 已提交
814
    } else if (ret == LOS_ERRNO_SWTMR_ID_INVALID) {
W
wenjun 已提交
815 816 817 818 819 820 821 822 823 824
        return osErrorParameter;
    } else {
        return osErrorResource;
    }
}
#endif

osEventFlagsId_t osEventFlagsNew(const osEventFlagsAttr_t *attr)
{
    PEVENT_CB_S pstEventCB;
L
LiteOS 已提交
825
    UINT32 ret;
W
wenjun 已提交
826 827 828 829 830 831 832 833 834 835 836 837

    UNUSED(attr);

    if (OS_INT_ACTIVE) {
        return (osEventFlagsId_t)NULL;
    }

    pstEventCB = (PEVENT_CB_S)LOS_MemAlloc(m_aucSysMem0, sizeof(EVENT_CB_S));
    if (pstEventCB == NULL) {
        return (osEventFlagsId_t)NULL;
    }

L
LiteOS 已提交
838 839
    ret = LOS_EventInit(pstEventCB);
    if (ret == LOS_OK) {
W
wenjun 已提交
840
        return (osEventFlagsId_t)pstEventCB;
L
LiteOS 已提交
841 842 843 844 845
    } else {
        if (LOS_MemFree(m_aucSysMem0, pstEventCB) != LOS_OK) {
            PRINT_ERR("[%s] memory free fail!\n", __func__);
        }
        return NULL;
W
wenjun 已提交
846 847 848 849 850
    }
}

const char *osEventFlagsGetName(osEventFlagsId_t ef_id)
{
851
    (void)ef_id;
W
wenjun 已提交
852
    if (OS_INT_ACTIVE) {
L
LiteOS 已提交
853
        return NULL;
W
wenjun 已提交
854
    }
L
LiteOS 已提交
855
    return NULL;
W
wenjun 已提交
856 857 858 859 860
}

uint32_t osEventFlagsSet(osEventFlagsId_t ef_id, uint32_t flags)
{
    PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
L
LiteOS 已提交
861
    UINT32 ret;
W
wenjun 已提交
862
    uint32_t rflags;
L
LiteOS 已提交
863

W
wenjun 已提交
864 865 866
    if (pstEventCB == NULL) {
        return osFlagsErrorParameter;
    }
L
LiteOS 已提交
867 868 869

    ret = LOS_EventWrite(pstEventCB, (UINT32)flags);
    if (ret == LOS_OK) {
W
wenjun 已提交
870 871
        rflags = pstEventCB->uwEventID;
        return rflags;
L
LiteOS 已提交
872 873
    } else {
        return (uint32_t)osFlagsErrorResource;
W
wenjun 已提交
874 875 876 877 878 879
    }
}

uint32_t osEventFlagsClear(osEventFlagsId_t ef_id, uint32_t flags)
{
    PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
880
    UINT32 intSave;
W
wenjun 已提交
881
    uint32_t rflags;
L
LiteOS 已提交
882
    UINT32 ret;
W
wenjun 已提交
883 884 885 886 887

    if (pstEventCB == NULL) {
        return (uint32_t)osFlagsErrorParameter;
    }

888
    intSave = LOS_IntLock();
W
wenjun 已提交
889 890
    rflags = pstEventCB->uwEventID;

L
LiteOS 已提交
891
    ret = LOS_EventClear(pstEventCB, ~flags);
892
    LOS_IntRestore(intSave);
L
LiteOS 已提交
893
    if (ret == LOS_OK) {
W
wenjun 已提交
894
        return rflags;
L
LiteOS 已提交
895 896
    } else {
        return (uint32_t)osFlagsErrorResource;
W
wenjun 已提交
897 898 899 900 901 902
    }
}

uint32_t osEventFlagsGet(osEventFlagsId_t ef_id)
{
    PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
903
    UINT32 intSave;
W
wenjun 已提交
904 905 906
    uint32_t rflags;

    if (pstEventCB == NULL) {
C
Caoruihong 已提交
907
        return 0;
W
wenjun 已提交
908 909
    }

910
    intSave = LOS_IntLock();
W
wenjun 已提交
911
    rflags = pstEventCB->uwEventID;
912
    LOS_IntRestore(intSave);
W
wenjun 已提交
913 914 915 916 917 918 919

    return rflags;
}

uint32_t osEventFlagsWait(osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout)
{
    PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
L
LiteOS 已提交
920 921
    UINT32 mode = 0;
    UINT32 ret;
W
wenjun 已提交
922 923
    uint32_t rflags;

L
LiteOS 已提交
924 925 926 927
    if (OS_INT_ACTIVE && (timeout != 0)) {
        return (uint32_t)osFlagsErrorParameter;
    }

W
wenjun 已提交
928 929 930 931 932
    if (options > (osFlagsWaitAny | osFlagsWaitAll | osFlagsNoClear)) {
        return (uint32_t)osFlagsErrorParameter;
    }

    if ((options & osFlagsWaitAll) == osFlagsWaitAll) {
L
LiteOS 已提交
933
        mode |= LOS_WAITMODE_AND;
W
wenjun 已提交
934
    } else {
L
LiteOS 已提交
935
        mode |= LOS_WAITMODE_OR;
W
wenjun 已提交
936 937 938
    }

    if ((options & osFlagsNoClear) == osFlagsNoClear) {
L
LiteOS 已提交
939
        mode &= ~LOS_WAITMODE_CLR;
W
wenjun 已提交
940
    } else {
L
LiteOS 已提交
941
        mode |= LOS_WAITMODE_CLR;
W
wenjun 已提交
942 943
    }

L
LiteOS 已提交
944 945
    ret = LOS_EventRead(pstEventCB, (UINT32)flags, mode, (UINT32)timeout);
    switch (ret) {
W
wenjun 已提交
946 947
        case LOS_ERRNO_EVENT_PTR_NULL:
        case LOS_ERRNO_EVENT_EVENTMASK_INVALID:
L
LiteOS 已提交
948
        case LOS_ERRNO_EVENT_FLAGS_INVALID:
W
wenjun 已提交
949 950 951 952 953 954 955 956 957 958 959
        case LOS_ERRNO_EVENT_SETBIT_INVALID:
            return (uint32_t)osFlagsErrorParameter;

        case LOS_ERRNO_EVENT_READ_IN_INTERRUPT:
        case LOS_ERRNO_EVENT_READ_IN_LOCK:
            return (uint32_t)osFlagsErrorResource;

        case LOS_ERRNO_EVENT_READ_TIMEOUT:
            return (uint32_t)osFlagsErrorTimeout;

        default:
L
LiteOS 已提交
960
            rflags = (uint32_t)ret;
W
wenjun 已提交
961 962 963 964 965 966 967
            return rflags;
    }
}

osStatus_t osEventFlagsDelete(osEventFlagsId_t ef_id)
{
    PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
968
    UINT32 intSave;
L
LiteOS 已提交
969 970 971 972
    osStatus_t ret;
    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }
973
    intSave = LOS_IntLock();
W
wenjun 已提交
974
    if (LOS_EventDestroy(pstEventCB) == LOS_OK) {
L
LiteOS 已提交
975
        ret = osOK;
W
wenjun 已提交
976
    } else {
L
LiteOS 已提交
977
        ret = osErrorParameter;
W
wenjun 已提交
978
    }
979
    LOS_IntRestore(intSave);
W
wenjun 已提交
980 981

    if (LOS_MemFree(m_aucSysMem0, (void *)pstEventCB) == LOS_OK) {
L
LiteOS 已提交
982
        ret = osOK;
W
wenjun 已提交
983
    } else {
L
LiteOS 已提交
984
        ret = osErrorParameter;
W
wenjun 已提交
985 986
    }

L
LiteOS 已提交
987
    return ret;
W
wenjun 已提交
988 989 990
}

//  ==== Mutex Management Functions ====
C
Caoruihong 已提交
991
#if (LOSCFG_BASE_IPC_MUX == 1)
W
wenjun 已提交
992 993
osMutexId_t osMutexNew(const osMutexAttr_t *attr)
{
L
LiteOS 已提交
994 995
    UINT32 ret;
    UINT32 muxId;
W
wenjun 已提交
996 997 998 999 1000 1001 1002

    UNUSED(attr);

    if (OS_INT_ACTIVE) {
        return NULL;
    }

L
LiteOS 已提交
1003 1004 1005
    ret = LOS_MuxCreate(&muxId);
    if (ret == LOS_OK) {
        return (osMutexId_t)(GET_MUX(muxId));
W
wenjun 已提交
1006 1007 1008 1009 1010 1011 1012
    } else {
        return (osMutexId_t)NULL;
    }
}

osStatus_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout)
{
L
LiteOS 已提交
1013 1014
    LosMuxCB *muxCB = (LosMuxCB *)mutex_id;
    UINT32 ret;
W
wenjun 已提交
1015

L
LiteOS 已提交
1016
    if (muxCB == NULL) {
W
wenjun 已提交
1017 1018
        return osErrorParameter;
    }
L
LiteOS 已提交
1019 1020
    if (OS_INT_ACTIVE) {
        return osErrorISR;
W
wenjun 已提交
1021 1022
    }

L
LiteOS 已提交
1023 1024
    ret = LOS_MuxPend(muxCB->muxID, timeout);
    if (ret == LOS_OK) {
W
wenjun 已提交
1025
        return osOK;
L
LiteOS 已提交
1026
    } else if (ret == LOS_ERRNO_MUX_INVALID) {
W
wenjun 已提交
1027
        return osErrorParameter;
L
LiteOS 已提交
1028 1029
    } else if (ret == LOS_ERRNO_MUX_TIMEOUT) {
        return osErrorTimeout;
W
wenjun 已提交
1030 1031 1032 1033 1034 1035 1036
    } else {
        return osErrorResource;
    }
}

osStatus_t osMutexRelease(osMutexId_t mutex_id)
{
L
LiteOS 已提交
1037 1038
    LosMuxCB *muxCB = (LosMuxCB *)mutex_id;
    UINT32 ret;
W
wenjun 已提交
1039

L
LiteOS 已提交
1040
    if (muxCB == NULL) {
W
wenjun 已提交
1041 1042
        return osErrorParameter;
    }
L
LiteOS 已提交
1043 1044 1045
    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }
W
wenjun 已提交
1046

L
LiteOS 已提交
1047 1048
    ret = LOS_MuxPost(muxCB->muxID);
    if (ret == LOS_OK) {
W
wenjun 已提交
1049
        return osOK;
L
LiteOS 已提交
1050 1051
    } else if (ret == LOS_ERRNO_MUX_INVALID) {
        return osErrorParameter;
W
wenjun 已提交
1052 1053 1054 1055 1056 1057 1058
    } else {
        return osErrorResource;
    }
}

osThreadId_t osMutexGetOwner(osMutexId_t mutex_id)
{
1059
    UINT32 intSave;
L
LiteOS 已提交
1060
    LosTaskCB *pstTaskCB = NULL;
W
wenjun 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069

    if (OS_INT_ACTIVE) {
        return NULL;
    }

    if (mutex_id == NULL) {
        return NULL;
    }

1070
    intSave = LOS_IntLock();
W
wenjun 已提交
1071
    pstTaskCB = ((LosMuxCB *)mutex_id)->owner;
1072
    LOS_IntRestore(intSave);
W
wenjun 已提交
1073 1074 1075 1076 1077 1078

    return (osThreadId_t)pstTaskCB;
}

osStatus_t osMutexDelete(osMutexId_t mutex_id)
{
L
LiteOS 已提交
1079
    UINT32 ret;
W
wenjun 已提交
1080 1081 1082 1083 1084 1085 1086 1087 1088

    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }

    if (mutex_id == NULL) {
        return osErrorParameter;
    }

L
LiteOS 已提交
1089 1090
    ret = LOS_MuxDelete(((LosMuxCB *)mutex_id)->muxID);
    if (ret == LOS_OK) {
W
wenjun 已提交
1091
        return osOK;
L
LiteOS 已提交
1092
    } else if (ret == LOS_ERRNO_MUX_INVALID) {
W
wenjun 已提交
1093 1094 1095 1096 1097 1098 1099 1100
        return osErrorParameter;
    } else {
        return osErrorResource;
    }
}
#endif

//  ==== Semaphore Management Functions ====
C
Caoruihong 已提交
1101
#if (LOSCFG_BASE_IPC_SEM == 1)
W
wenjun 已提交
1102 1103
osSemaphoreId_t osSemaphoreNew(uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr)
{
L
LiteOS 已提交
1104 1105
    UINT32 ret;
    UINT32 semId;
W
wenjun 已提交
1106 1107 1108

    UNUSED(attr);

L
LiteOS 已提交
1109 1110
    if ((initial_count > max_count) || (max_count > OS_SEM_COUNTING_MAX_COUNT) || (max_count == 0) || OS_INT_ACTIVE) {
        return NULL;
W
wenjun 已提交
1111 1112
    }

L
LiteOS 已提交
1113 1114 1115 1116
    if (max_count == 1) {
        ret = LOS_BinarySemCreate((UINT16)initial_count, &semId);
    } else {
        ret = LOS_SemCreate((UINT16)initial_count, &semId);
W
wenjun 已提交
1117 1118
    }

L
LiteOS 已提交
1119 1120
    if (ret == LOS_OK) {
        return (osSemaphoreId_t)(GET_SEM(semId));
W
wenjun 已提交
1121 1122 1123 1124 1125
    } else {
        return (osSemaphoreId_t)NULL;
    }
}

L
LiteOS 已提交
1126 1127 1128 1129 1130
const char *osSemaphoreGetName(osSemaphoreId_t semaphore_id)
{
    UNUSED(semaphore_id);
    return NULL;
}
W
wenjun 已提交
1131 1132 1133

osStatus_t osSemaphoreAcquire(osSemaphoreId_t semaphore_id, uint32_t timeout)
{
L
LiteOS 已提交
1134 1135
    LosSemCB *semCB = (LosSemCB *)semaphore_id;
    UINT32 ret;
W
wenjun 已提交
1136

L
LiteOS 已提交
1137
    if ((semCB == NULL) || (OS_INT_ACTIVE && (timeout != 0))) {
W
wenjun 已提交
1138 1139 1140
        return osErrorParameter;
    }

L
LiteOS 已提交
1141 1142
    ret = LOS_SemPend(semCB->semID, timeout);
    if (ret == LOS_OK) {
W
wenjun 已提交
1143
        return osOK;
L
LiteOS 已提交
1144
    } else if (ret == LOS_ERRNO_SEM_INVALID) {
W
wenjun 已提交
1145
        return osErrorParameter;
L
LiteOS 已提交
1146 1147
    } else if (ret == LOS_ERRNO_SEM_TIMEOUT) {
        return osErrorTimeout;
W
wenjun 已提交
1148 1149 1150 1151 1152 1153 1154
    } else {
        return osErrorResource;
    }
}

osStatus_t osSemaphoreRelease(osSemaphoreId_t semaphore_id)
{
L
LiteOS 已提交
1155
    UINT32 ret;
W
wenjun 已提交
1156 1157 1158 1159 1160

    if (semaphore_id == NULL) {
        return osErrorParameter;
    }

L
LiteOS 已提交
1161 1162
    ret = LOS_SemPost(((LosSemCB *)semaphore_id)->semID);
    if (ret == LOS_OK) {
W
wenjun 已提交
1163
        return osOK;
L
LiteOS 已提交
1164
    } else if (ret == LOS_ERRNO_SEM_INVALID) {
W
wenjun 已提交
1165 1166 1167 1168 1169 1170 1171 1172
        return osErrorParameter;
    } else {
        return osErrorResource;
    }
}

uint32_t osSemaphoreGetCount(osSemaphoreId_t semaphore_id)
{
L
LiteOS 已提交
1173
    LosSemCB *semCB = (LosSemCB *)semaphore_id;
1174
    UINT32 intSave;
L
LiteOS 已提交
1175
    UINT16 count;
W
wenjun 已提交
1176

L
LiteOS 已提交
1177
    if (semCB == NULL) {
W
wenjun 已提交
1178 1179 1180
        return 0;
    }

L
LiteOS 已提交
1181 1182 1183
    intSave = LOS_IntLock();
    if (semCB->semStat == 0) {
        LOS_IntRestore(intSave);
W
wenjun 已提交
1184 1185 1186
        return 0;
    }

L
LiteOS 已提交
1187
    count = semCB->semCount;
1188
    LOS_IntRestore(intSave);
W
wenjun 已提交
1189

L
LiteOS 已提交
1190
    return (uint32_t)count;
W
wenjun 已提交
1191 1192 1193 1194
}

osStatus_t osSemaphoreDelete(osSemaphoreId_t semaphore_id)
{
L
LiteOS 已提交
1195
    UINT32 ret;
W
wenjun 已提交
1196 1197 1198 1199 1200 1201 1202 1203 1204

    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }

    if (semaphore_id == NULL) {
        return osErrorParameter;
    }

L
LiteOS 已提交
1205 1206
    ret = LOS_SemDelete(((LosSemCB *)semaphore_id)->semID);
    if (ret == LOS_OK) {
W
wenjun 已提交
1207
        return osOK;
L
LiteOS 已提交
1208
    } else if (ret == LOS_ERRNO_SEM_INVALID) {
W
wenjun 已提交
1209 1210 1211 1212 1213 1214 1215 1216
        return osErrorParameter;
    } else {
        return osErrorResource;
    }
}
#endif

//  ==== Message Queue Management Functions ====
C
Caoruihong 已提交
1217
#if (LOSCFG_BASE_IPC_QUEUE == 1)
L
LiteOS 已提交
1218 1219 1220 1221 1222 1223 1224
typedef enum {
    ATTR_CAPACITY = 0,
    ATTR_MSGSIZE = 1,
    ATTR_COUNT = 2,
    ATTR_SPACE = 3
} QueueAttribute;

W
wenjun 已提交
1225 1226
osMessageQueueId_t osMessageQueueNew(uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr)
{
L
LiteOS 已提交
1227 1228
    UINT32 queueId;
    UINT32 ret;
W
wenjun 已提交
1229 1230 1231
    UNUSED(attr);
    osMessageQueueId_t handle;

L
LiteOS 已提交
1232
    if ((msg_count == 0) || (msg_size == 0) || OS_INT_ACTIVE) {
W
wenjun 已提交
1233 1234 1235
        return (osMessageQueueId_t)NULL;
    }

L
LiteOS 已提交
1236 1237 1238
    ret = LOS_QueueCreate((char *)NULL, (UINT16)msg_count, &queueId, 0, (UINT16)msg_size);
    if (ret == LOS_OK) {
        handle = (osMessageQueueId_t)(GET_QUEUE_HANDLE(queueId));
W
wenjun 已提交
1239 1240 1241 1242 1243 1244 1245
    } else {
        handle = (osMessageQueueId_t)NULL;
    }

    return handle;
}

L
LiteOS 已提交
1246
STATIC osStatus_t osMessageQueueOp(osMessageQueueId_t mq_id, VOID *msg_ptr, UINT32 timeout, QueueReadWrite rw)
W
wenjun 已提交
1247
{
L
LiteOS 已提交
1248 1249 1250
    LosQueueCB *queueCB = (LosQueueCB *)mq_id;
    UINT32 ret;
    UINT32 bufferSize;
W
wenjun 已提交
1251

L
LiteOS 已提交
1252
    if ((queueCB == NULL) || (msg_ptr == NULL) || (OS_INT_ACTIVE && (timeout != 0))) {
W
wenjun 已提交
1253 1254
        return osErrorParameter;
    }
L
LiteOS 已提交
1255 1256 1257 1258 1259 1260

    bufferSize = (UINT32)(queueCB->queueSize - sizeof(UINT32));
    if (rw == OS_QUEUE_WRITE) {
        ret = LOS_QueueWriteCopy(queueCB->queueID, msg_ptr, bufferSize, timeout);
    } else {
        ret = LOS_QueueReadCopy(queueCB->queueID, msg_ptr, &bufferSize, timeout);
W
wenjun 已提交
1261
    }
L
LiteOS 已提交
1262 1263

    if (ret == LOS_OK) {
W
wenjun 已提交
1264
        return osOK;
L
LiteOS 已提交
1265
    } else if ((ret == LOS_ERRNO_QUEUE_INVALID) || (ret == LOS_ERRNO_QUEUE_NOT_CREATE)) {
W
wenjun 已提交
1266
        return osErrorParameter;
L
LiteOS 已提交
1267
    } else if (ret == LOS_ERRNO_QUEUE_TIMEOUT) {
W
wenjun 已提交
1268 1269 1270 1271 1272 1273
        return osErrorTimeout;
    } else {
        return osErrorResource;
    }
}

L
LiteOS 已提交
1274 1275 1276 1277 1278
osStatus_t osMessageQueuePut(osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout)
{
    UNUSED(msg_prio);
    return osMessageQueueOp(mq_id, (VOID *)msg_ptr, (UINT32)timeout, OS_QUEUE_WRITE);
}
W
wenjun 已提交
1279 1280 1281 1282

osStatus_t osMessageQueueGet(osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout)
{
    UNUSED(msg_prio);
L
LiteOS 已提交
1283 1284
    return osMessageQueueOp(mq_id, (VOID *)msg_ptr, (UINT32)timeout, OS_QUEUE_READ);
}
W
wenjun 已提交
1285

L
LiteOS 已提交
1286 1287 1288 1289 1290 1291 1292
STATIC UINT16 osMessageQueueGetAttr(osMessageQueueId_t mq_id, QueueAttribute attr)
{
    LosQueueCB *queueCB = (LosQueueCB *)mq_id;
    UINT16 attrVal = 0;

    if (queueCB == NULL) {
        return 0;
W
wenjun 已提交
1293 1294
    }

L
LiteOS 已提交
1295 1296
    if (queueCB->queueState == OS_QUEUE_UNUSED) {
        return 0;
W
wenjun 已提交
1297
    }
L
LiteOS 已提交
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316

    switch (attr) {
        case ATTR_CAPACITY:
            attrVal = queueCB->queueLen;
            break;
        case ATTR_MSGSIZE:
            attrVal = queueCB->queueSize - sizeof(UINT32);
            break;
        case ATTR_COUNT:
            attrVal = queueCB->readWriteableCnt[OS_QUEUE_READ];
            break;
        case ATTR_SPACE:
            attrVal = queueCB->readWriteableCnt[OS_QUEUE_WRITE];
            break;
        default:
            break;
    }

    return attrVal;
W
wenjun 已提交
1317 1318 1319 1320
}

uint32_t osMessageQueueGetCapacity(osMessageQueueId_t mq_id)
{
L
LiteOS 已提交
1321
    return (uint32_t)osMessageQueueGetAttr(mq_id, ATTR_CAPACITY);
W
wenjun 已提交
1322 1323 1324 1325
}

uint32_t osMessageQueueGetMsgSize(osMessageQueueId_t mq_id)
{
L
LiteOS 已提交
1326
    return (uint32_t)osMessageQueueGetAttr(mq_id, ATTR_MSGSIZE);
W
wenjun 已提交
1327 1328 1329 1330
}

uint32_t osMessageQueueGetCount(osMessageQueueId_t mq_id)
{
L
LiteOS 已提交
1331
    return (uint32_t)osMessageQueueGetAttr(mq_id, ATTR_COUNT);
W
wenjun 已提交
1332 1333 1334 1335
}

uint32_t osMessageQueueGetSpace(osMessageQueueId_t mq_id)
{
L
LiteOS 已提交
1336
    return (uint32_t)osMessageQueueGetAttr(mq_id, ATTR_SPACE);
W
wenjun 已提交
1337 1338 1339 1340 1341
}

osStatus_t osMessageQueueDelete(osMessageQueueId_t mq_id)
{
    LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
L
LiteOS 已提交
1342
    UINT32 ret;
W
wenjun 已提交
1343 1344 1345 1346 1347 1348 1349 1350 1351

    if (pstQueue == NULL) {
        return osErrorParameter;
    }

    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }

L
LiteOS 已提交
1352 1353
    ret = LOS_QueueDelete((UINT32)pstQueue->queueID);
    if (ret == LOS_OK) {
W
wenjun 已提交
1354
        return osOK;
L
LiteOS 已提交
1355
    } else if (ret == LOS_ERRNO_QUEUE_NOT_FOUND || ret == LOS_ERRNO_QUEUE_NOT_CREATE) {
W
wenjun 已提交
1356 1357 1358 1359 1360 1361
        return osErrorParameter;
    } else {
        return osErrorResource;
    }
}

L
LiteOS 已提交
1362 1363 1364 1365 1366 1367
const char *osMessageQueueGetName(osMessageQueueId_t mq_id)
{
    UNUSED(mq_id);
    return NULL;
}
#endif
C
Caoruihong 已提交
1368

1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
#define MP_ALLOC        1U
#define MD_ALLOC        2U
#define MEM_POOL_VALID  0xFFEEFF00

typedef struct {
    LOS_MEMBOX_INFO poolInfo;
    void            *poolBase;
    uint32_t        poolSize;
    uint32_t        status;
    const char      *name;
} MemPoolCB;

osMemoryPoolId_t osMemoryPoolNew(uint32_t block_count, uint32_t block_size, const osMemoryPoolAttr_t *attr)
{
    MemPoolCB *mp = NULL;
    const char *name = NULL;
    LOS_MEMBOX_NODE *node = NULL;
    uint32_t memCB = 0;
    uint32_t memMP = 0;
    uint32_t size;
    uint32_t index;

    if (OS_INT_ACTIVE) {
        return NULL;
    }

    if ((block_count == 0) || (block_size == 0)) {
        return NULL;
    }

    size = block_count * block_size;

    if (attr != NULL) {
        if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(MemPoolCB))) {
            memCB = 1;
        }

        if ((attr->mp_mem != NULL) &&
            (((UINTPTR)attr->mp_mem & 0x3) == 0) &&  /* 0x3: Check if array is 4-byte aligned. */
            (attr->mp_size >= size)) {
            memMP = 1;
        }
        name = attr->name;
    }

    if (memCB == 0) {
        mp = LOS_MemAlloc(OS_SYS_MEM_ADDR, sizeof(MemPoolCB));
        if (mp == NULL) {
            return NULL;
        }
        mp->status = MP_ALLOC;
L
LiteOS 已提交
1420
    } else if ((attr != NULL) && (attr->cb_mem != NULL)) {
1421 1422
        mp = attr->cb_mem;
        mp->status = 0;
L
LiteOS 已提交
1423 1424
    } else {
        return NULL;
1425 1426 1427 1428
    }

    if (memMP == 0) {
        mp->poolBase = LOS_MemAlloc(OS_SYS_MEM_ADDR, size);
L
LiteOS 已提交
1429
        if ((mp->poolBase == NULL) && (mp->status & MP_ALLOC)) {
1430 1431 1432 1433
            (void)LOS_MemFree(OS_SYS_MEM_ADDR, mp);
            return NULL;
        }
        mp->status |= MD_ALLOC;
L
LiteOS 已提交
1434
    } else if ((attr != NULL) && (attr->mp_mem != NULL)) {
1435
        mp->poolBase = attr->mp_mem;
L
LiteOS 已提交
1436 1437 1438 1439 1440
    } else {
        if (mp->status & MP_ALLOC) {
            (void)LOS_MemFree(OS_SYS_MEM_ADDR, mp);
        }
        return NULL;
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
    }
    mp->poolSize = size;
    mp->name = name;
    mp->poolInfo.uwBlkCnt = 0;
    mp->poolInfo.uwBlkNum = block_count;
    mp->poolInfo.uwBlkSize = block_size;

    node = (LOS_MEMBOX_NODE *)mp->poolBase;
    mp->poolInfo.stFreeList.pstNext = node;
    for (index = 0; index < block_count - 1; ++index) {
        node->pstNext = OS_MEMBOX_NEXT(node, block_size);
        node = node->pstNext;
    }
    node->pstNext = NULL;

    mp->status |= MEM_POOL_VALID;

    return mp;
}

void *osMemoryPoolAlloc(osMemoryPoolId_t mp_id, uint32_t timeout)
{
    MemPoolCB *mp = (MemPoolCB *)mp_id;
    LOS_MEMBOX_NODE *node = NULL;
1465
    UINT32 intSave;
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491

    UNUSED(timeout);

    if (mp_id == NULL) {
        return NULL;
    }

    intSave = LOS_IntLock();
    if ((mp->status & MEM_POOL_VALID) == MEM_POOL_VALID) {
        node = mp->poolInfo.stFreeList.pstNext;
        if (node != NULL) {
            mp->poolInfo.stFreeList.pstNext = node->pstNext;
            mp->poolInfo.uwBlkCnt++;
        }
    }
    LOS_IntRestore(intSave);

    return node;
}

osStatus_t osMemoryPoolFree(osMemoryPoolId_t mp_id, void *block)
{

    MemPoolCB *mp = (MemPoolCB *)mp_id;
    LOS_MEMBOX_NODE *node = NULL;
    LOS_MEMBOX_NODE *nodeTmp = NULL;
1492
    UINT32 intSave;
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522

    if ((mp_id == NULL) || (block == NULL)) {
        return osErrorParameter;
    }

    intSave = LOS_IntLock();
    if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
        LOS_IntRestore(intSave);
        return osErrorResource;
    }

    if (((UINTPTR)block < (UINTPTR)mp->poolBase) ||
        ((UINTPTR)block >= ((UINTPTR)mp->poolBase + (UINTPTR)mp->poolSize))) {
        LOS_IntRestore(intSave);
        return osErrorParameter;
    }

    node = (LOS_MEMBOX_NODE *)block;
    nodeTmp = mp->poolInfo.stFreeList.pstNext;
    mp->poolInfo.stFreeList.pstNext = node;
    node->pstNext = nodeTmp;
    mp->poolInfo.uwBlkCnt--;
    LOS_IntRestore(intSave);

    return osOK;
}

osStatus_t osMemoryPoolDelete(osMemoryPoolId_t mp_id)
{
    MemPoolCB *mp = (MemPoolCB *)mp_id;
1523
    UINT32 intSave;
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557

    if (OS_INT_ACTIVE) {
        return osErrorISR;
    }

    if (mp_id == NULL) {
        return osErrorParameter;
    }

    intSave = LOS_IntLock();
    if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
        LOS_IntRestore(intSave);
        return osErrorResource;
    }

    if (mp->status & MD_ALLOC) {
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, mp->poolBase);
        mp->poolBase = NULL;
    }

    mp->name = NULL;
    mp->status &= ~MEM_POOL_VALID;

    if (mp->status & MP_ALLOC) {
        (void)LOS_MemFree(OS_SYS_MEM_ADDR, mp);
    }
    LOS_IntRestore(intSave);

    return osOK;
}

uint32_t osMemoryPoolGetCapacity(osMemoryPoolId_t mp_id)
{
    MemPoolCB *mp = (MemPoolCB *)mp_id;
1558
    UINT32 intSave;
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
    uint32_t num;

    if (mp_id == NULL) {
        return 0;
    }

    intSave = LOS_IntLock();
    if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
        num = 0;
    } else {
        num = mp->poolInfo.uwBlkNum;
    }
    LOS_IntRestore(intSave);

    return num;
}

uint32_t osMemoryPoolGetBlockSize(osMemoryPoolId_t mp_id)
{
    MemPoolCB *mp = (MemPoolCB *)mp_id;
1579
    UINT32 intSave;
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
    uint32_t size;

    if (mp_id == NULL) {
        return 0;
    }

    intSave = LOS_IntLock();
    if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
        size = 0;
    } else {
        size = mp->poolInfo.uwBlkSize;
    }
    LOS_IntRestore(intSave);

    return size;
}

uint32_t osMemoryPoolGetCount(osMemoryPoolId_t mp_id)
{
    MemPoolCB *mp = (MemPoolCB *)mp_id;
1600
    UINT32 intSave;
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
    uint32_t count;

    if (mp_id == NULL) {
        return 0;
    }

    intSave = LOS_IntLock();
    if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
        count = 0;
    } else {
        count = mp->poolInfo.uwBlkCnt;
    }
    LOS_IntRestore(intSave);

    return count;
}

uint32_t osMemoryPoolGetSpace(osMemoryPoolId_t mp_id)
{
    MemPoolCB *mp = (MemPoolCB *)mp_id;
1621
    UINT32 intSave;
1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
    uint32_t space;

    if (mp_id == NULL) {
        return 0;
    }

    intSave = LOS_IntLock();
    if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
        space = 0;
    } else {
W
wangchen 已提交
1632
        space = mp->poolInfo.uwBlkNum - mp->poolInfo.uwBlkCnt;
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
    }
    LOS_IntRestore(intSave);

    return space;

}

const char *osMemoryPoolGetName(osMemoryPoolId_t mp_id)
{
    MemPoolCB *mp = (MemPoolCB *)mp_id;
    const char *p = NULL;
1644
    UINT32 intSave;
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661

    if (mp_id == NULL) {
        return NULL;
    }

    if (OS_INT_ACTIVE) {
        return NULL;
    }

    intSave = LOS_IntLock();
    if ((mp->status & MEM_POOL_VALID) == MEM_POOL_VALID) {
        p = mp->name;
    }
    LOS_IntRestore(intSave);

    return p;
}
M
mucor 已提交
1662 1663 1664 1665 1666

//  ==== Thread Flags Functions ====
uint32_t osThreadFlagsSet(osThreadId_t thread_id, uint32_t flags)
{
    LosTaskCB *taskCB = (LosTaskCB *)thread_id;
L
LiteOS 已提交
1667
    UINT32 ret;
M
mucor 已提交
1668
    EVENT_CB_S *eventCB = NULL;
L
LiteOS 已提交
1669
    UINT32 eventSave;
M
mucor 已提交
1670 1671 1672 1673 1674 1675

    if (taskCB == NULL) {
        return (uint32_t)osFlagsErrorParameter;
    }

    eventCB = &(taskCB->event);
L
LiteOS 已提交
1676
    eventSave = eventCB->uwEventID;
M
mucor 已提交
1677
    ret = LOS_EventWrite(eventCB, (UINT32)flags);
L
LiteOS 已提交
1678 1679 1680
    if (ret == LOS_OK) {
        return ((uint32_t)eventSave | flags);
    } else if (ret == LOS_ERRNO_EVENT_SETBIT_INVALID) {
M
mucor 已提交
1681
        return (uint32_t)osFlagsErrorParameter;
L
LiteOS 已提交
1682
    } else {
M
mucor 已提交
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771
        return (uint32_t)osFlagsErrorResource;
    }
}

uint32_t osThreadFlagsClear(uint32_t flags)
{
    UINT32 ret;
    UINT32 saveFlags;
    LosTaskCB *runTask = NULL;
    EVENT_CB_S *eventCB = NULL;

    if (OS_INT_ACTIVE) {
        return (uint32_t)osFlagsErrorUnknown;
    }

    runTask = g_losTask.runTask;
    eventCB = &(runTask->event);
    saveFlags = eventCB->uwEventID;

    ret = LOS_EventClear(eventCB, ~(UINT32)flags);
    if (ret == LOS_OK) {
        return (uint32_t)saveFlags;
    }

    return (uint32_t)osFlagsErrorResource;
}

uint32_t osThreadFlagsGet(void)
{
    LosTaskCB *runTask = NULL;
    EVENT_CB_S *eventCB = NULL;

    if (OS_INT_ACTIVE) {
        return (uint32_t)osFlagsErrorUnknown;
    }

    runTask = g_losTask.runTask;
    eventCB = &(runTask->event);

    return (uint32_t)(eventCB->uwEventID);
}

uint32_t osThreadFlagsWait(uint32_t flags, uint32_t options, uint32_t timeout)
{
    UINT32 ret;
    UINT32 mode = 0;
    LosTaskCB *runTask = NULL;
    EVENT_CB_S *eventCB = NULL;

    if (OS_INT_ACTIVE) {
        return (uint32_t)osFlagsErrorUnknown;
    }

    if (options > (osFlagsWaitAny | osFlagsWaitAll | osFlagsNoClear)) {
        return (uint32_t)osFlagsErrorParameter;
    }

    if ((options & osFlagsWaitAll) == osFlagsWaitAll) {
        mode |= LOS_WAITMODE_AND;
    } else {
        mode |= LOS_WAITMODE_OR;
    }

    if ((options & osFlagsNoClear) == osFlagsNoClear) {
        mode &= ~LOS_WAITMODE_CLR;
    } else {
        mode |= LOS_WAITMODE_CLR;
    }

    runTask = g_losTask.runTask;
    eventCB = &(runTask->event);

    ret = LOS_EventRead(eventCB, (UINT32)flags, mode, (UINT32)timeout);
    if (!(ret & LOS_ERRTYPE_ERROR)) {
        return (uint32_t)eventCB->uwEventID | ret;
    }

    switch (ret) {
        case LOS_ERRNO_EVENT_PTR_NULL:
        case LOS_ERRNO_EVENT_SETBIT_INVALID:
        case LOS_ERRNO_EVENT_EVENTMASK_INVALID:
        case LOS_ERRNO_EVENT_FLAGS_INVALID:
            return (uint32_t)osFlagsErrorParameter;
        case LOS_ERRNO_EVENT_READ_TIMEOUT:
            return (uint32_t)osFlagsErrorTimeout;
        default:
            return (uint32_t)osFlagsErrorResource;
    }
}