1 /*
2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "cmsis_os2.h"
16 #include "kal.h"
17 #include "los_event.h"
18 #include "los_membox.h"
19 #include "los_memory.h"
20 #include "los_interrupt.h"
21 #include "los_mux.h"
22 #include "los_queue.h"
23 #include "los_sem.h"
24 #include "los_swtmr.h"
25 #include "los_task.h"
26 #include "los_timer.h"
27 #include "los_debug.h"
28
29 #include "string.h"
30 #include "securec.h"
31
32
33 /* Kernel initialization state */
34 static osKernelState_t g_kernelState;
35
36 extern BOOL g_taskScheduled;
37
38 /* LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO <---> osPriorityNormal */
39 #define LOS_PRIORITY(cmsisPriority) (LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO - ((cmsisPriority) - osPriorityNormal))
40 #define CMSIS_PRIORITY(losPriority) (osPriorityNormal + (LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO - (losPriority)))
41
42 /* OS_TASK_PRIORITY_HIGHEST and OS_TASK_PRIORITY_LOWEST is reserved for internal TIMER and IDLE task use only. */
43 #define ISVALID_LOS_PRIORITY(losPrio) ((losPrio) > OS_TASK_PRIORITY_HIGHEST && (losPrio) < OS_TASK_PRIORITY_LOWEST)
44
45 const osVersion_t g_stLosVersion = { 001, 001 };
46
47 #define LITEOS_VERSION_MAJOR 1
48 #define LITEOS_VERSION_MINOR 0
49 #define LITEOS_VERSION_BUILD 0
50
51 /* Kernel version and identification string definition */
52 #define KERNEL_VERSION (((UINT32)LITEOS_VERSION_MAJOR * 10000000UL) | \
53 ((UINT32)LITEOS_VERSION_MINOR * 10000UL) | \
54 ((UINT32)LITEOS_VERSION_BUILD * 1UL))
55
56 #define KERNEL_ID "HUAWEI-LiteOS"
57
58 // ==== Kernel Management Functions ====
59
osKernelInitialize(void)60 osStatus_t osKernelInitialize(void)
61 {
62 if (OS_INT_ACTIVE) {
63 return osErrorISR;
64 }
65
66 if (g_kernelState != osKernelInactive) {
67 return osError;
68 }
69
70 if (LOS_OK == LOS_KernelInit()) {
71 g_kernelState = osKernelReady;
72 return osOK;
73 } else {
74 return osError;
75 }
76 }
77
78
osKernelGetInfo(osVersion_t * version,char * id_buf,uint32_t id_size)79 osStatus_t osKernelGetInfo(osVersion_t *version, char *id_buf, uint32_t id_size)
80 {
81 uint32_t uwRet;
82
83 if (OS_INT_ACTIVE) {
84 return osErrorISR;
85 }
86
87 if (version != NULL) {
88 version->api = g_stLosVersion.api;
89 version->kernel = g_stLosVersion.kernel;
90 }
91
92 if ((id_buf != NULL) && (id_size != 0U)) {
93 if (id_size > sizeof(KERNEL_ID)) {
94 id_size = sizeof(KERNEL_ID);
95 }
96 uwRet = memcpy_s(id_buf, id_size, KERNEL_ID, id_size);
97 if (uwRet != EOK) {
98 PRINT_ERR("%s[%d] memcpy failed, error type = %u\n", __FUNCTION__, __LINE__, uwRet);
99 return osError;
100 }
101 }
102
103 return osOK;
104 }
105
106
osKernelGetState(void)107 osKernelState_t osKernelGetState(void)
108 {
109 if (OS_INT_ACTIVE) {
110 return osKernelError;
111 }
112
113 if (!g_taskScheduled) {
114 if (g_kernelState == osKernelReady) {
115 return osKernelReady;
116 } else {
117 return osKernelInactive;
118 }
119 } else if (g_losTaskLock > 0) {
120 return osKernelLocked;
121 } else {
122 return osKernelRunning;
123 }
124 }
125
126
osKernelStart(void)127 osStatus_t osKernelStart(void)
128 {
129 if (OS_INT_ACTIVE) {
130 return osErrorISR;
131 }
132
133 if (g_kernelState == osKernelReady) {
134 if (LOS_OK == LOS_Start()) {
135 g_kernelState = osKernelRunning;
136 return osOK;
137 } else {
138 return osError;
139 }
140 } else {
141 return osError;
142 }
143 }
144
145
osKernelLock(void)146 int32_t osKernelLock(void)
147 {
148 int32_t lock;
149
150 if (OS_INT_ACTIVE) {
151 return (int32_t)osErrorISR;
152 }
153
154 if (!g_taskScheduled) {
155 return (int32_t)osError;
156 }
157
158 if (g_losTaskLock > 0) {
159 lock = 1;
160 } else {
161 LOS_TaskLock();
162 lock = 0;
163 }
164
165 return lock;
166 }
167
168
osKernelUnlock(void)169 int32_t osKernelUnlock(void)
170 {
171 int32_t lock;
172
173 if (OS_INT_ACTIVE) {
174 return (int32_t)osErrorISR;
175 }
176
177 if (!g_taskScheduled) {
178 return (int32_t)osError;
179 }
180
181 if (g_losTaskLock > 0) {
182 LOS_TaskUnlock();
183 if (g_losTaskLock != 0) {
184 return (int32_t)osError;
185 }
186 lock = 1;
187 } else {
188 lock = 0;
189 }
190
191 return lock;
192 }
193
194
osKernelRestoreLock(int32_t lock)195 int32_t osKernelRestoreLock(int32_t lock)
196 {
197 if (OS_INT_ACTIVE) {
198 return (int32_t)osErrorISR;
199 }
200
201 if (!g_taskScheduled) {
202 return (int32_t)osError;
203 }
204
205 switch (lock) {
206 case 0:
207 LOS_TaskUnlock();
208 if (g_losTaskLock != 0) {
209 break;
210 }
211 return 0;
212 case 1:
213 LOS_TaskLock();
214 return 1;
215 default:
216 break;
217 }
218
219 return (int32_t)osError;
220 }
221
222
osKernelGetTickCount(void)223 uint32_t osKernelGetTickCount(void)
224 {
225 uint64_t ticks = LOS_TickCountGet();
226 return (uint32_t)ticks;
227 }
228
osKernelGetTickFreq(void)229 uint32_t osKernelGetTickFreq(void)
230 {
231 uint32_t freq;
232
233 if (OS_INT_ACTIVE) {
234 freq = 0U;
235 } else {
236 freq = LOSCFG_BASE_CORE_TICK_PER_SECOND;
237 }
238
239 return (freq);
240 }
241
osKernelGetSysTimerCount(void)242 uint32_t osKernelGetSysTimerCount(void)
243 {
244 uint32_t countLow;
245 if (OS_INT_ACTIVE) {
246 countLow = 0U;
247 } else {
248 countLow = (UINT32)LOS_SysCycleGet();
249 }
250 return countLow;
251 }
252
253
osKernelGetSysTimerFreq(void)254 uint32_t osKernelGetSysTimerFreq(void)
255 {
256 return OS_SYS_CLOCK;
257 }
258
259
260 // ==== Thread Management Functions ====
osThreadCb(void * arg)261 void osThreadCb(void *arg)
262 {
263 osThreadId_t thread_id = arg;
264 if (!thread_id)
265 return;
266
267 ((TSK_ENTRY_FUNC)thread_id->func)((UINT32)(thread_id->arg));
268 }
269
osThreadNew(osThreadFunc_t func,void * argument,const osThreadAttr_t * attr)270 osThreadId_t osThreadNew(osThreadFunc_t func, void *argument, const osThreadAttr_t *attr)
271 {
272 UINT32 uwTid;
273 UINT32 uwRet;
274 LosTaskCB *pstTaskCB = NULL;
275 TSK_INIT_PARAM_S stTskInitParam = {NULL};
276 UINT16 usPriority;
277 osThreadId_t thread_id = NULL;
278
279 if (OS_INT_ACTIVE || (func == NULL)) {
280 return (osThreadId_t)NULL;
281 }
282
283 usPriority = attr ? LOS_PRIORITY(attr->priority) : LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO;
284 if (!ISVALID_LOS_PRIORITY(usPriority)) {
285 /* unsupported priority */
286 return (osThreadId_t)NULL;
287 }
288
289 thread_id = (osThreadId_t)LOS_MemAlloc(m_aucSysMem0, sizeof(*thread_id));
290 if (thread_id)
291 thread_id->ef_id = (void *)osEventFlagsNew(NULL);
292 if (!thread_id->ef_id) {
293 LOS_MemFree(m_aucSysMem0, thread_id);
294 return (osThreadId_t)NULL;
295 }
296
297 thread_id ->func = func;
298 thread_id ->arg = argument;
299 (void)memset_s(&stTskInitParam, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
300 stTskInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)osThreadCb;
301 stTskInitParam.uwArg = (UINT32)thread_id;
302 stTskInitParam.uwStackSize = attr ? attr->stack_size : LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
303 stTskInitParam.pcName = (CHAR *)(attr ? attr->name : "[NULL]");
304 stTskInitParam.usTaskPrio = usPriority;
305
306 uwRet = LOS_TaskCreate(&uwTid, &stTskInitParam);
307
308 if (LOS_OK != uwRet) {
309 osEventFlagsDelete(thread_id->ef_id);
310 LOS_MemFree(m_aucSysMem0, thread_id);
311 return (osThreadId_t)NULL;
312 }
313
314 pstTaskCB = OS_TCB_FROM_TID(uwTid);
315 thread_id->tid = uwTid;
316 thread_id->pstTaskCB = (void *)pstTaskCB;
317 return thread_id;
318 }
319
320
osThreadGetName(osThreadId_t thread_id)321 const char *osThreadGetName(osThreadId_t thread_id)
322 {
323 LosTaskCB *pstTaskCB = NULL;
324
325 if (OS_INT_ACTIVE || thread_id == NULL) {
326 return NULL;
327 }
328
329 pstTaskCB = (LosTaskCB *)(thread_id->pstTaskCB);
330
331 return pstTaskCB->taskName;
332 }
333
osThreadGetId(void)334 osThreadId_t osThreadGetId(void)
335 {
336 if (OS_INT_ACTIVE) {
337 return NULL;
338 }
339
340 return (osThreadId_t)(g_losTask.runTask->arg);
341 }
342
osThreadGetArgument(void)343 void *osThreadGetArgument(void)
344 {
345 osThreadId_t thread_id;
346
347 if (OS_INT_ACTIVE) {
348 return 0;
349 }
350
351 thread_id = osThreadGetId();
352 if (thread_id == NULL) {
353 return NULL;
354 }
355
356 return thread_id->arg;
357 }
358
osThreadGetState(osThreadId_t thread_id)359 osThreadState_t osThreadGetState(osThreadId_t thread_id)
360 {
361 UINT16 taskStatus;
362 osThreadState_t stState;
363 LosTaskCB *pstTaskCB = NULL;
364
365 if (OS_INT_ACTIVE || thread_id == NULL) {
366 return osThreadError;
367 }
368
369 pstTaskCB = (LosTaskCB *)(thread_id->pstTaskCB);
370 taskStatus = pstTaskCB->taskStatus;
371
372 if (taskStatus & OS_TASK_STATUS_RUNNING) {
373 stState = osThreadRunning;
374 } else if (taskStatus & OS_TASK_STATUS_READY) {
375 stState = osThreadReady;
376 } else if (taskStatus &
377 (OS_TASK_STATUS_DELAY | OS_TASK_STATUS_PEND | OS_TASK_STATUS_SUSPEND)) {
378 stState = osThreadBlocked;
379 } else if (taskStatus & OS_TASK_STATUS_UNUSED) {
380 stState = osThreadInactive;
381 } else {
382 stState = osThreadError;
383 }
384
385 return stState;
386 }
387
388
osThreadGetStackSize(osThreadId_t thread_id)389 uint32_t osThreadGetStackSize(osThreadId_t thread_id)
390 {
391 LosTaskCB *pstTaskCB = NULL;
392
393 if (OS_INT_ACTIVE || thread_id == NULL) {
394 return 0U;
395 }
396
397 pstTaskCB = (LosTaskCB *)(thread_id->pstTaskCB);
398
399 return pstTaskCB->stackSize;
400 }
401
402
osTaskStackWaterMarkGet(UINT32 taskID)403 uint32_t osTaskStackWaterMarkGet(UINT32 taskID)
404 {
405 UINT32 uwCount = 0;
406 UINT32 *ptopOfStack;
407 UINT32 intSave;
408 LosTaskCB *pstTaskCB = NULL;
409
410 if (taskID > LOSCFG_BASE_CORE_TSK_LIMIT) {
411 return 0;
412 }
413
414 intSave = LOS_IntLock();
415
416 pstTaskCB = OS_TCB_FROM_TID(taskID);
417 if (OS_TASK_STATUS_UNUSED & (pstTaskCB->taskStatus)) {
418 LOS_IntRestore(intSave);
419 return 0;
420 }
421
422 // first 4 bytes is OS_TASK_MAGIC_WORD, skip
423 ptopOfStack = (UINT32 *)(UINTPTR)pstTaskCB->topOfStack + 1;
424
425 while (*ptopOfStack == (UINT32)OS_TASK_STACK_INIT) {
426 ++ptopOfStack;
427 ++uwCount;
428 }
429
430 uwCount *= sizeof(UINT32);
431
432 LOS_IntRestore(intSave);
433 return uwCount;
434 }
435
436
osThreadGetStackSpace(osThreadId_t thread_id)437 uint32_t osThreadGetStackSpace(osThreadId_t thread_id)
438 {
439 LosTaskCB *pstTaskCB = NULL;
440
441 if (OS_INT_ACTIVE || thread_id == NULL) {
442 return 0U;
443 }
444
445 pstTaskCB = (LosTaskCB *)(thread_id->pstTaskCB);
446
447 return osTaskStackWaterMarkGet(pstTaskCB->taskID);
448 }
449
450
osThreadSetPriority(osThreadId_t thread_id,osPriority_t priority)451 osStatus_t osThreadSetPriority(osThreadId_t thread_id, osPriority_t priority)
452 {
453 UINT32 uwRet;
454 UINT16 usPriority;
455 LosTaskCB *pstTaskCB = NULL;
456
457 if (OS_INT_ACTIVE) {
458 return osErrorISR;
459 }
460
461 if (thread_id == NULL) {
462 return osErrorParameter;
463 }
464
465 usPriority = LOS_PRIORITY(priority);
466 if (!ISVALID_LOS_PRIORITY(usPriority)) {
467 return osErrorParameter;
468 }
469
470 pstTaskCB = (LosTaskCB *)(thread_id->pstTaskCB);
471 uwRet = LOS_TaskPriSet(pstTaskCB->taskID, usPriority);
472 switch (uwRet) {
473 case LOS_ERRNO_TSK_PRIOR_ERROR:
474 case LOS_ERRNO_TSK_OPERATE_IDLE:
475 case LOS_ERRNO_TSK_ID_INVALID:
476 return osErrorParameter;
477
478 case LOS_ERRNO_TSK_NOT_CREATED:
479 return osErrorResource;
480
481 default:
482 return osOK;
483 }
484 }
485
486
osThreadGetPriority(osThreadId_t thread_id)487 osPriority_t osThreadGetPriority(osThreadId_t thread_id)
488 {
489 UINT16 usRet;
490 LosTaskCB *pstTaskCB = NULL;
491
492 if (OS_INT_ACTIVE || thread_id == NULL) {
493 return osPriorityError;
494 }
495
496 pstTaskCB = (LosTaskCB *)(thread_id->pstTaskCB);
497 usRet = LOS_TaskPriGet(pstTaskCB->taskID);
498
499 if (usRet == (UINT16)OS_INVALID) {
500 return osPriorityError;
501 }
502
503 return (osPriority_t)CMSIS_PRIORITY(usRet);
504 }
505
506
osThreadYield(void)507 osStatus_t osThreadYield(void)
508 {
509 UINT32 uwRet;
510
511 if (OS_INT_ACTIVE) {
512 return osErrorISR;
513 }
514
515 uwRet = LOS_TaskYield();
516
517 if (uwRet == LOS_OK) {
518 return osOK;
519 }
520
521 return osError;
522 }
523
524
osThreadSuspend(osThreadId_t thread_id)525 osStatus_t osThreadSuspend(osThreadId_t thread_id)
526 {
527 UINT32 uwRet;
528 LosTaskCB *pstTaskCB = NULL;
529
530 if (OS_INT_ACTIVE) {
531 return osErrorISR;
532 }
533
534 if (thread_id == NULL) {
535 return osErrorParameter;
536 }
537
538 pstTaskCB = (LosTaskCB *)(thread_id->pstTaskCB);
539 uwRet = LOS_TaskSuspend(pstTaskCB->taskID);
540 switch (uwRet) {
541 case LOS_ERRNO_TSK_OPERATE_IDLE:
542 case LOS_ERRNO_TSK_SUSPEND_SWTMR_NOT_ALLOWED:
543 case LOS_ERRNO_TSK_ID_INVALID:
544 return osErrorParameter;
545
546 case LOS_ERRNO_TSK_NOT_CREATED:
547 case LOS_ERRNO_TSK_ALREADY_SUSPENDED:
548 case LOS_ERRNO_TSK_SUSPEND_LOCKED:
549 return osErrorResource;
550
551 default:
552 return osOK;
553 }
554 }
555
556
osThreadResume(osThreadId_t thread_id)557 osStatus_t osThreadResume(osThreadId_t thread_id)
558 {
559 UINT32 uwRet;
560 LosTaskCB *pstTaskCB = NULL;
561
562 if (OS_INT_ACTIVE) {
563 return osErrorISR;
564 }
565
566 if (thread_id == NULL) {
567 return osErrorParameter;
568 }
569
570 pstTaskCB = (LosTaskCB *)(thread_id->pstTaskCB);
571 uwRet = LOS_TaskResume(pstTaskCB->taskID);
572
573 switch (uwRet) {
574 case LOS_ERRNO_TSK_ID_INVALID:
575 return osErrorParameter;
576
577 case LOS_ERRNO_TSK_NOT_CREATED:
578 case LOS_ERRNO_TSK_NOT_SUSPENDED:
579 return osErrorResource;
580
581 default:
582 return osOK;
583 }
584 }
585
586
osThreadTerminate(osThreadId_t thread_id)587 osStatus_t osThreadTerminate(osThreadId_t thread_id)
588 {
589 UINT32 uwRet;
590 LosTaskCB *pstTaskCB = NULL;
591
592 if (OS_INT_ACTIVE) {
593 return osErrorISR;
594 }
595
596 if (thread_id == NULL) {
597 return osErrorParameter;
598 }
599
600 pstTaskCB = (LosTaskCB *)(thread_id->pstTaskCB);
601 uwRet = LOS_TaskDelete(pstTaskCB->taskID);
602 osEventFlagsDelete(thread_id->ef_id);
603 LOS_MemFree(m_aucSysMem0, thread_id);
604
605 switch (uwRet) {
606 case LOS_ERRNO_TSK_OPERATE_IDLE:
607 case LOS_ERRNO_TSK_SUSPEND_SWTMR_NOT_ALLOWED:
608 case LOS_ERRNO_TSK_ID_INVALID:
609 return osErrorParameter;
610
611 case LOS_ERRNO_TSK_NOT_CREATED:
612 return osErrorResource;
613
614 default:
615 return osOK;
616 }
617 }
618
619
osThreadGetCount(void)620 uint32_t osThreadGetCount(void)
621 {
622 uint32_t uwCount = 0;
623
624 if (OS_INT_ACTIVE) {
625 return 0U;
626 }
627
628 for (uint32_t index = 0; index <= LOSCFG_BASE_CORE_TSK_LIMIT; index++) {
629 if (!((g_taskCBArray + index)->taskStatus & OS_TASK_STATUS_UNUSED)) {
630 uwCount++;
631 }
632 }
633
634 return uwCount;
635 }
636
637
osThreadExit(void)638 void osThreadExit(void)
639 {
640 osThreadId_t thread_id = osThreadGetId();
641 if (!thread_id)
642 return;
643
644 osEventFlagsDelete(thread_id->ef_id);
645 LOS_MemFree(m_aucSysMem0, thread_id);
646 (void)LOS_TaskDelete(LOS_CurTaskIDGet());
647 UNREACHABLE;
648 }
649
650
osDelay(uint32_t ticks)651 osStatus_t osDelay(uint32_t ticks)
652 {
653 UINT32 uwRet = LOS_OK;
654 if (ticks == 0) {
655 return osOK;
656 }
657
658 if (__get_IPSR() != 0 || __get_PRIMASK() != 0 || __get_BASEPRI() != 0)
659 return osError;
660
661 if (osKernelGetState() != osKernelRunning) {
662 LOS_UDelay(ticks * OS_US_PER_TICK);
663 } else {
664 uwRet = LOS_TaskDelay(ticks);
665 }
666 if (uwRet == LOS_OK) {
667 return osOK;
668 } else {
669 return osError;
670 }
671 }
672
673
osDelayUntil(uint32_t ticks)674 osStatus_t osDelayUntil(uint32_t ticks)
675 {
676 UINT32 uwRet;
677 UINT32 uwTicks;
678 UINT32 tickCount = osKernelGetTickCount();
679
680 if (ticks < tickCount) {
681 return osError;
682 }
683
684 uwTicks = (UINT32)(ticks - tickCount);
685
686 uwRet = LOS_TaskDelay(uwTicks);
687 if (uwRet == LOS_OK) {
688 return osOK;
689 } else {
690 return osError;
691 }
692 }
693
694 // ==== Timer Management Functions ====
695 #if (LOSCFG_BASE_CORE_SWTMR == 1)
osTimerNew(osTimerFunc_t func,osTimerType_t type,void * argument,const osTimerAttr_t * attr)696 osTimerId_t osTimerNew(osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)
697 {
698 UNUSED(attr);
699 UINT32 usSwTmrID;
700 UINT8 mode;
701
702 if ((NULL == func) || ((osTimerOnce != type) && (osTimerPeriodic != type))) {
703 return (osTimerId_t)NULL;
704 }
705
706 if (osTimerOnce == type) {
707 mode = LOS_SWTMR_MODE_NO_SELFDELETE;
708 } else {
709 mode = LOS_SWTMR_MODE_PERIOD;
710 }
711 #if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
712 if (LOS_OK != LOS_SwtmrCreate(1, mode, (SWTMR_PROC_FUNC)func, &usSwTmrID, (UINT32)(UINTPTR)argument,
713 osTimerRousesAllow, osTimerAlignIgnore)) {
714 return (osTimerId_t)NULL;
715 }
716 #else
717 if (LOS_OK != LOS_SwtmrCreate(1, mode, (SWTMR_PROC_FUNC)func, &usSwTmrID, (UINT32)(UINTPTR)argument)) {
718 return (osTimerId_t)NULL;
719 }
720 #endif
721
722 return (osTimerId_t)OS_SWT_FROM_SID(usSwTmrID);
723 }
724
725
726 #if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
osTimerExtNew(osTimerFunc_t func,osTimerType_t type,void * argument,const osTimerAttr_t * attr,osTimerRouses_t ucRouses,osTimerAlign_t ucSensitive)727 osTimerId_t osTimerExtNew(osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr,
728 osTimerRouses_t ucRouses, osTimerAlign_t ucSensitive)
729 {
730 UNUSED(attr);
731 UINT32 usSwTmrID;
732 UINT8 mode;
733
734 if ((OS_INT_ACTIVE) || (NULL == func) || ((osTimerOnce != type) && (osTimerPeriodic != type))) {
735 return (osTimerId_t)NULL;
736 }
737
738 if (osTimerOnce == type) {
739 mode = LOS_SWTMR_MODE_NO_SELFDELETE;
740 } else {
741 mode = LOS_SWTMR_MODE_PERIOD;
742 }
743 if (LOS_OK != LOS_SwtmrCreate(1, mode, (SWTMR_PROC_FUNC)func, &usSwTmrID,
744 (UINT32)(UINTPTR)argument, ucRouses, ucSensitive)) {
745 return (osTimerId_t)NULL;
746 }
747
748 return (osTimerId_t)OS_SWT_FROM_SID(usSwTmrID);
749 }
750 #endif
751
752
osTimerStart(osTimerId_t timer_id,uint32_t ticks)753 osStatus_t osTimerStart(osTimerId_t timer_id, uint32_t ticks)
754 {
755 UINT32 uwRet;
756 SWTMR_CTRL_S *pstSwtmr;
757
758 if ((0 == ticks) || (NULL == timer_id)) {
759 return osErrorParameter;
760 }
761
762 UINT32 intSave = LOS_IntLock();
763 pstSwtmr = (SWTMR_CTRL_S *)timer_id;
764 pstSwtmr->uwInterval = ticks;
765 uwRet = LOS_SwtmrStart(pstSwtmr->usTimerID);
766 LOS_IntRestore(intSave);
767 if (LOS_OK == uwRet) {
768 return osOK;
769 } else if (LOS_ERRNO_SWTMR_ID_INVALID == uwRet) {
770 return osErrorParameter;
771 } else {
772 return osErrorResource;
773 }
774 }
775
776
osTimerGetName(osTimerId_t timer_id)777 const char *osTimerGetName(osTimerId_t timer_id)
778 {
779 UNUSED(timer_id);
780 return (const char *)NULL;
781 }
782
783
osTimerStop(osTimerId_t timer_id)784 osStatus_t osTimerStop(osTimerId_t timer_id)
785 {
786 UINT32 uwRet;
787 SWTMR_CTRL_S *pstSwtmr = (SWTMR_CTRL_S *)timer_id;
788
789 if (NULL == pstSwtmr) {
790 return osErrorParameter;
791 }
792
793 uwRet = LOS_SwtmrStop(pstSwtmr->usTimerID);
794 if (LOS_OK == uwRet) {
795 return osOK;
796 } else if (LOS_ERRNO_SWTMR_ID_INVALID == uwRet) {
797 return osErrorParameter;
798 } else {
799 return osErrorResource;
800 }
801 }
802
803
osTimerIsRunning(osTimerId_t timer_id)804 uint32_t osTimerIsRunning(osTimerId_t timer_id)
805 {
806 if (NULL == timer_id) {
807 return 0;
808 }
809
810 return (OS_SWTMR_STATUS_TICKING == ((SWTMR_CTRL_S *)timer_id)->ucState);
811 }
812
813
osTimerDelete(osTimerId_t timer_id)814 osStatus_t osTimerDelete(osTimerId_t timer_id)
815 {
816 UINT32 uwRet;
817 SWTMR_CTRL_S *pstSwtmr = (SWTMR_CTRL_S *)timer_id;
818
819 if (NULL == pstSwtmr) {
820 return osErrorParameter;
821 }
822
823 uwRet = LOS_SwtmrDelete(pstSwtmr->usTimerID);
824 if (LOS_OK == uwRet) {
825 return osOK;
826 } else if (LOS_ERRNO_SWTMR_ID_INVALID == uwRet) {
827 return osErrorParameter;
828 } else {
829 return osErrorResource;
830 }
831 }
832 #endif
833
osEventFlagsNew(const osEventFlagsAttr_t * attr)834 osEventFlagsId_t osEventFlagsNew(const osEventFlagsAttr_t *attr)
835 {
836 PEVENT_CB_S pstEventCB;
837 UINT32 uwRet;
838
839 UNUSED(attr);
840
841 if (OS_INT_ACTIVE) {
842 return (osEventFlagsId_t)NULL;
843 }
844
845 pstEventCB = (PEVENT_CB_S)LOS_MemAlloc(m_aucSysMem0, sizeof(EVENT_CB_S));
846 if (pstEventCB == NULL) {
847 return (osEventFlagsId_t)NULL;
848 }
849
850 uwRet = LOS_EventInit(pstEventCB);
851 if (uwRet == LOS_ERRNO_EVENT_PTR_NULL) {
852 return (osEventFlagsId_t)NULL;
853 } else {
854 return (osEventFlagsId_t)pstEventCB;
855 }
856 }
857
858
osEventFlagsGetName(osEventFlagsId_t ef_id)859 const char *osEventFlagsGetName(osEventFlagsId_t ef_id)
860 {
861 UNUSED(ef_id);
862
863 if (OS_INT_ACTIVE) {
864 return (const char *)NULL;
865 }
866
867 return (const char *)NULL;
868 }
869
870
osEventFlagsSet(osEventFlagsId_t ef_id,uint32_t flags)871 uint32_t osEventFlagsSet(osEventFlagsId_t ef_id, uint32_t flags)
872 {
873 PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
874 UINT32 uwRet;
875 uint32_t rflags;
876 if (pstEventCB == NULL) {
877 return osFlagsErrorParameter;
878 }
879 uwRet = LOS_EventWrite(pstEventCB, (UINT32)flags);
880 if (uwRet != LOS_OK) {
881 return (uint32_t)osFlagsErrorParameter;
882 } else {
883 rflags = pstEventCB->uwEventID;
884 return rflags;
885 }
886 }
887
888
osEventFlagsClear(osEventFlagsId_t ef_id,uint32_t flags)889 uint32_t osEventFlagsClear(osEventFlagsId_t ef_id, uint32_t flags)
890 {
891 PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
892 UINT32 intSave;
893 uint32_t rflags;
894 UINT32 uwRet;
895
896 if (pstEventCB == NULL) {
897 return (uint32_t)osFlagsErrorParameter;
898 }
899
900 intSave = LOS_IntLock();
901 rflags = pstEventCB->uwEventID;
902
903 uwRet = LOS_EventClear(pstEventCB, ~flags);
904 LOS_IntRestore(intSave);
905 if (uwRet != LOS_OK) {
906 return (uint32_t)osFlagsErrorParameter;
907 } else {
908 return rflags;
909 }
910 }
911
912
osEventFlagsGet(osEventFlagsId_t ef_id)913 uint32_t osEventFlagsGet(osEventFlagsId_t ef_id)
914 {
915 PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
916 UINT32 intSave;
917 uint32_t rflags;
918
919 if (pstEventCB == NULL) {
920 return 0;
921 }
922
923 intSave = LOS_IntLock();
924 rflags = pstEventCB->uwEventID;
925 LOS_IntRestore(intSave);
926
927 return rflags;
928 }
929
osEventFlagsWait(osEventFlagsId_t ef_id,uint32_t flags,uint32_t options,uint32_t timeout)930 uint32_t osEventFlagsWait(osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout)
931 {
932 PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
933 UINT32 uwMode = 0;
934 UINT32 uwRet;
935 uint32_t rflags;
936
937 if (options > (osFlagsWaitAny | osFlagsWaitAll | osFlagsNoClear)) {
938 return (uint32_t)osFlagsErrorParameter;
939 }
940
941 if ((options & osFlagsWaitAll) == osFlagsWaitAll) {
942 uwMode |= LOS_WAITMODE_AND;
943 } else {
944 uwMode |= LOS_WAITMODE_OR;
945 }
946
947 if ((options & osFlagsNoClear) == osFlagsNoClear) {
948 uwMode &= ~LOS_WAITMODE_CLR;
949 } else {
950 uwMode |= LOS_WAITMODE_CLR;
951 }
952
953 uwRet = LOS_EventRead(pstEventCB, (UINT32)flags, uwMode, (UINT32)timeout);
954 switch (uwRet) {
955 case LOS_ERRNO_EVENT_PTR_NULL:
956 case LOS_ERRNO_EVENT_EVENTMASK_INVALID:
957 case LOS_ERRNO_EVENT_SETBIT_INVALID:
958 return (uint32_t)osFlagsErrorParameter;
959
960 case LOS_ERRNO_EVENT_READ_IN_INTERRUPT:
961 case LOS_ERRNO_EVENT_FLAGS_INVALID:
962 case LOS_ERRNO_EVENT_READ_IN_LOCK:
963 return (uint32_t)osFlagsErrorResource;
964
965 case LOS_ERRNO_EVENT_READ_TIMEOUT:
966 return (uint32_t)osFlagsErrorTimeout;
967
968 default:
969 rflags = (uint32_t)uwRet;
970 return rflags;
971 }
972 }
973
osEventFlagsDelete(osEventFlagsId_t ef_id)974 osStatus_t osEventFlagsDelete(osEventFlagsId_t ef_id)
975 {
976 PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
977 UINT32 intSave;
978 osStatus_t uwRet;
979
980 intSave = LOS_IntLock();
981 if (LOS_EventDestroy(pstEventCB) == LOS_OK) {
982 uwRet = osOK;
983 } else {
984 uwRet = osErrorParameter;
985 }
986 LOS_IntRestore(intSave);
987
988 if (LOS_MemFree(m_aucSysMem0, (void *)pstEventCB) == LOS_OK) {
989 uwRet = osOK;
990 } else {
991 uwRet = osErrorParameter;
992 }
993
994 return uwRet;
995 }
996
997 // ==== Mutex Management Functions ====
998 #if (LOSCFG_BASE_IPC_MUX == 1)
osMutexNew(const osMutexAttr_t * attr)999 osMutexId_t osMutexNew(const osMutexAttr_t *attr)
1000 {
1001 UINT32 uwRet;
1002 UINT32 uwMuxId;
1003
1004 UNUSED(attr);
1005
1006 if (OS_INT_ACTIVE) {
1007 return NULL;
1008 }
1009
1010 uwRet = LOS_MuxCreate(&uwMuxId);
1011 if (uwRet == LOS_OK) {
1012 return (osMutexId_t)(GET_MUX(uwMuxId));
1013 } else {
1014 return (osMutexId_t)NULL;
1015 }
1016 }
1017
1018 #include "hal_trace.h"
osMutexAcquire(osMutexId_t mutex_id,uint32_t timeout)1019 osStatus_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout)
1020 {
1021 UINT32 uwRet;
1022
1023 if (mutex_id == NULL) {
1024 return osErrorParameter;
1025 }
1026
1027 if (OS_INT_ACTIVE && (timeout != LOS_NO_WAIT)) {
1028 timeout = 0;
1029 }
1030
1031 uwRet = LOS_MuxPend(((LosMuxCB *)mutex_id)->muxID, timeout);
1032 if (uwRet == LOS_OK) {
1033 return osOK;
1034 } else if (uwRet == LOS_ERRNO_MUX_TIMEOUT) {
1035 return osErrorTimeout;
1036 } else if (uwRet == LOS_ERRNO_MUX_INVALID) {
1037 return osErrorParameter;
1038 } else {
1039 TRACE(0, "LOS_MuxPend fail 0x%x", uwRet);
1040 return osErrorResource;
1041 }
1042 }
1043
1044
osMutexRelease(osMutexId_t mutex_id)1045 osStatus_t osMutexRelease(osMutexId_t mutex_id)
1046 {
1047 UINT32 uwRet;
1048
1049 if (mutex_id == NULL) {
1050 return osErrorParameter;
1051 }
1052
1053 uwRet = LOS_MuxPost(((LosMuxCB *)mutex_id)->muxID);
1054 if (uwRet == LOS_OK) {
1055 return osOK;
1056 } else {
1057 return osErrorResource;
1058 }
1059 }
1060
1061
osMutexGetOwner(osMutexId_t mutex_id)1062 osThreadId_t osMutexGetOwner(osMutexId_t mutex_id)
1063 {
1064 UINT32 intSave;
1065 LosTaskCB *pstTaskCB;
1066
1067 if (OS_INT_ACTIVE) {
1068 return NULL;
1069 }
1070
1071 if (mutex_id == NULL) {
1072 return NULL;
1073 }
1074
1075 intSave = LOS_IntLock();
1076 pstTaskCB = ((LosMuxCB *)mutex_id)->owner;
1077 LOS_IntRestore(intSave);
1078
1079 return (osThreadId_t)pstTaskCB;
1080 }
1081
1082
osMutexDelete(osMutexId_t mutex_id)1083 osStatus_t osMutexDelete(osMutexId_t mutex_id)
1084 {
1085 UINT32 uwRet;
1086
1087 if (OS_INT_ACTIVE) {
1088 return osErrorISR;
1089 }
1090
1091 if (mutex_id == NULL) {
1092 return osErrorParameter;
1093 }
1094
1095 uwRet = LOS_MuxDelete(((LosMuxCB *)mutex_id)->muxID);
1096 if (uwRet == LOS_OK) {
1097 return osOK;
1098 } else if (uwRet == LOS_ERRNO_MUX_INVALID) {
1099 return osErrorParameter;
1100 } else {
1101 return osErrorResource;
1102 }
1103 }
1104 #endif
1105
1106 // ==== Semaphore Management Functions ====
1107 #if (LOSCFG_BASE_IPC_SEM == 1)
1108
osSemaphoreNew(uint32_t max_count,uint32_t initial_count,const osSemaphoreAttr_t * attr)1109 osSemaphoreId_t osSemaphoreNew(uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr)
1110 {
1111 UINT32 uwRet;
1112 UINT32 uwSemId;
1113
1114 UNUSED(attr);
1115
1116 if (OS_INT_ACTIVE) {
1117 return (osSemaphoreId_t)NULL;
1118 }
1119
1120 if (1 == max_count) {
1121 uwRet = LOS_BinarySemCreate((UINT16)initial_count, &uwSemId);
1122 } else {
1123 uwRet = LOS_SemCreate((UINT16)initial_count, &uwSemId);
1124 }
1125
1126 if (uwRet == LOS_OK) {
1127 return (osSemaphoreId_t)(GET_SEM(uwSemId));
1128 } else {
1129 return (osSemaphoreId_t)NULL;
1130 }
1131 }
1132
1133
osSemaphoreAcquire(osSemaphoreId_t semaphore_id,uint32_t timeout)1134 osStatus_t osSemaphoreAcquire(osSemaphoreId_t semaphore_id, uint32_t timeout)
1135 {
1136 UINT32 uwRet;
1137
1138 if (semaphore_id == NULL) {
1139 return osErrorParameter;
1140 }
1141
1142 if (OS_INT_ACTIVE && (timeout != LOS_NO_WAIT)) {
1143 return osErrorISR;
1144 }
1145
1146 uwRet = LOS_SemPend(((LosSemCB *)semaphore_id)->semID, timeout);
1147 if (uwRet == LOS_OK) {
1148 return osOK;
1149 } else if (uwRet == LOS_ERRNO_SEM_TIMEOUT) {
1150 return osErrorTimeout;
1151 } else if (uwRet == LOS_ERRNO_SEM_INVALID) {
1152 return osErrorParameter;
1153 } else if (uwRet == LOS_ERRNO_SEM_PEND_INTERR) {
1154 return osErrorISR;
1155 } else {
1156 return osErrorResource;
1157 }
1158 }
1159
1160
osSemaphoreRelease(osSemaphoreId_t semaphore_id)1161 osStatus_t osSemaphoreRelease(osSemaphoreId_t semaphore_id)
1162 {
1163 UINT32 uwRet;
1164
1165 if (semaphore_id == NULL) {
1166 return osErrorParameter;
1167 }
1168
1169 uwRet = LOS_SemPost(((LosSemCB *)semaphore_id)->semID);
1170 if (uwRet == LOS_OK) {
1171 return osOK;
1172 } else if (uwRet == LOS_ERRNO_SEM_INVALID) {
1173 return osErrorParameter;
1174 } else {
1175 return osErrorResource;
1176 }
1177 }
1178
1179
osSemaphoreGetCount(osSemaphoreId_t semaphore_id)1180 uint32_t osSemaphoreGetCount(osSemaphoreId_t semaphore_id)
1181 {
1182 UINT32 intSave;
1183 UINT32 uwCount;
1184
1185 if (OS_INT_ACTIVE) {
1186 return 0;
1187 }
1188
1189 if (semaphore_id == NULL) {
1190 return 0;
1191 }
1192
1193 intSave = LOS_IntLock();
1194 uwCount = ((LosSemCB *)semaphore_id)->semCount;
1195 LOS_IntRestore(intSave);
1196
1197 return uwCount;
1198 }
1199
1200
osSemaphoreDelete(osSemaphoreId_t semaphore_id)1201 osStatus_t osSemaphoreDelete(osSemaphoreId_t semaphore_id)
1202 {
1203 UINT32 uwRet;
1204
1205 if (OS_INT_ACTIVE) {
1206 return osErrorISR;
1207 }
1208
1209 if (semaphore_id == NULL) {
1210 return osErrorParameter;
1211 }
1212
1213 uwRet = LOS_SemDelete(((LosSemCB *)semaphore_id)->semID);
1214 if (uwRet == LOS_OK) {
1215 return osOK;
1216 } else if (uwRet == LOS_ERRNO_SEM_INVALID) {
1217 return osErrorParameter;
1218 } else {
1219 return osErrorResource;
1220 }
1221 }
1222 #endif
1223
1224
1225 // ==== Message Queue Management Functions ====
1226 #if (LOSCFG_BASE_IPC_QUEUE == 1)
osMessageQueueNew(uint32_t msg_count,uint32_t msg_size,const osMessageQueueAttr_t * attr)1227 osMessageQueueId_t osMessageQueueNew(uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr)
1228 {
1229 UINT32 uwQueueID;
1230 UINT32 uwRet;
1231 UNUSED(attr);
1232 osMessageQueueId_t handle;
1233
1234 if (0 == msg_count || 0 == msg_size || OS_INT_ACTIVE) {
1235 return (osMessageQueueId_t)NULL;
1236 }
1237
1238 uwRet = LOS_QueueCreate((char *)NULL, (UINT16)msg_count, &uwQueueID, 0, (UINT16)msg_size);
1239 if (uwRet == LOS_OK) {
1240 handle = (osMessageQueueId_t)(GET_QUEUE_HANDLE(uwQueueID));
1241 } else {
1242 handle = (osMessageQueueId_t)NULL;
1243 }
1244
1245 return handle;
1246 }
1247
1248
osMessageQueuePut(osMessageQueueId_t mq_id,const void * msg_ptr,uint8_t msg_prio,uint32_t timeout)1249 osStatus_t osMessageQueuePut(osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout)
1250 {
1251 UNUSED(msg_prio);
1252 UINT32 uwRet;
1253 UINT32 uwBufferSize;
1254 LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
1255
1256 if (pstQueue == NULL || msg_ptr == NULL || ((OS_INT_ACTIVE) && (0 != timeout))) {
1257 return osErrorParameter;
1258 }
1259 if (pstQueue->queueSize < sizeof(UINT32)) {
1260 return osErrorParameter;
1261 }
1262 uwBufferSize = (UINT32)(pstQueue->queueSize - sizeof(UINT32));
1263 uwRet = LOS_QueueWriteCopy((UINT32)pstQueue->queueID, (void *)msg_ptr, uwBufferSize, timeout);
1264 if (uwRet == LOS_OK) {
1265 return osOK;
1266 } else if (uwRet == LOS_ERRNO_QUEUE_INVALID || uwRet == LOS_ERRNO_QUEUE_NOT_CREATE) {
1267 return osErrorParameter;
1268 } else if (uwRet == LOS_ERRNO_QUEUE_TIMEOUT) {
1269 return osErrorTimeout;
1270 } else {
1271 return osErrorResource;
1272 }
1273 }
1274
1275
osMessageQueueGet(osMessageQueueId_t mq_id,void * msg_ptr,uint8_t * msg_prio,uint32_t timeout)1276 osStatus_t osMessageQueueGet(osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout)
1277 {
1278 UNUSED(msg_prio);
1279 UINT32 uwRet;
1280 UINT32 uwBufferSize;
1281 LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
1282
1283 if (pstQueue == NULL || msg_ptr == NULL || ((OS_INT_ACTIVE) && (0 != timeout))) {
1284 return osErrorParameter;
1285 }
1286
1287 uwBufferSize = (UINT32)(pstQueue->queueSize - sizeof(UINT32));
1288 uwRet = LOS_QueueReadCopy((UINT32)pstQueue->queueID, msg_ptr, &uwBufferSize, timeout);
1289 if (uwRet == LOS_OK) {
1290 return osOK;
1291 } else if (uwRet == LOS_ERRNO_QUEUE_INVALID || uwRet == LOS_ERRNO_QUEUE_NOT_CREATE) {
1292 return osErrorParameter;
1293 } else if (uwRet == LOS_ERRNO_QUEUE_TIMEOUT) {
1294 return osErrorTimeout;
1295 } else {
1296 return osErrorResource;
1297 }
1298 }
1299
osMessageQueueGetCapacity(osMessageQueueId_t mq_id)1300 uint32_t osMessageQueueGetCapacity(osMessageQueueId_t mq_id)
1301 {
1302 uint32_t capacity;
1303 LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
1304
1305 if (pstQueue == NULL) {
1306 capacity = 0U;
1307 } else {
1308 capacity = pstQueue->queueLen;
1309 }
1310
1311 return (capacity);
1312 }
1313
osMessageQueueGetMsgSize(osMessageQueueId_t mq_id)1314 uint32_t osMessageQueueGetMsgSize(osMessageQueueId_t mq_id)
1315 {
1316 uint32_t size;
1317 LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
1318
1319 if (pstQueue == NULL) {
1320 size = 0U;
1321 } else {
1322 size = pstQueue->queueSize - sizeof(UINT32);
1323 }
1324
1325 return (size);
1326 }
1327
1328
osMessageQueueGetCount(osMessageQueueId_t mq_id)1329 uint32_t osMessageQueueGetCount(osMessageQueueId_t mq_id)
1330 {
1331 uint32_t count;
1332 UINT32 intSave;
1333 LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
1334
1335 if (pstQueue == NULL) {
1336 count = 0U;
1337 } else {
1338 intSave = LOS_IntLock();
1339 count = (uint32_t)(pstQueue->readWriteableCnt[OS_QUEUE_READ]);
1340 LOS_IntRestore(intSave);
1341 }
1342 return count;
1343 }
1344
1345
osMessageQueueGetSpace(osMessageQueueId_t mq_id)1346 uint32_t osMessageQueueGetSpace(osMessageQueueId_t mq_id)
1347 {
1348 uint32_t space;
1349 UINT32 intSave;
1350 LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
1351
1352 if (pstQueue == NULL) {
1353 space = 0U;
1354 } else {
1355 intSave = LOS_IntLock();
1356 space = (uint32_t)pstQueue->readWriteableCnt[OS_QUEUE_WRITE];
1357 LOS_IntRestore(intSave);
1358 }
1359 return space;
1360 }
1361
osMessageQueueDelete(osMessageQueueId_t mq_id)1362 osStatus_t osMessageQueueDelete(osMessageQueueId_t mq_id)
1363 {
1364 LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
1365 UINT32 uwRet;
1366
1367 if (pstQueue == NULL) {
1368 return osErrorParameter;
1369 }
1370
1371 if (OS_INT_ACTIVE) {
1372 return osErrorISR;
1373 }
1374
1375 uwRet = LOS_QueueDelete((UINT32)pstQueue->queueID);
1376 if (uwRet == LOS_OK) {
1377 return osOK;
1378 } else if (uwRet == LOS_ERRNO_QUEUE_NOT_FOUND || uwRet == LOS_ERRNO_QUEUE_NOT_CREATE) {
1379 return osErrorParameter;
1380 } else {
1381 return osErrorResource;
1382 }
1383 }
1384 #endif
1385
1386
1387 #define MP_ALLOC 1U
1388 #define MD_ALLOC 2U
1389 #define MEM_POOL_VALID 0xFFEEFF00
1390
1391 typedef struct {
1392 LOS_MEMBOX_INFO poolInfo;
1393 void *poolBase;
1394 uint32_t poolSize;
1395 uint32_t status;
1396 const char *name;
1397 } MemPoolCB;
1398
osMemoryPoolNew(uint32_t block_count,uint32_t block_size,const osMemoryPoolAttr_t * attr)1399 osMemoryPoolId_t osMemoryPoolNew(uint32_t block_count, uint32_t block_size, const osMemoryPoolAttr_t *attr)
1400 {
1401 MemPoolCB *mp = NULL;
1402 const char *name = NULL;
1403 LOS_MEMBOX_NODE *node = NULL;
1404 uint32_t memCB = 0;
1405 uint32_t memMP = 0;
1406 uint32_t size;
1407 uint32_t index;
1408
1409 if (OS_INT_ACTIVE) {
1410 return NULL;
1411 }
1412
1413 if ((block_count == 0) || (block_size == 0)) {
1414 return NULL;
1415 }
1416
1417 size = block_count * block_size;
1418
1419 if (attr != NULL) {
1420 if ((attr->cb_mem != NULL) && (attr->cb_size >= sizeof(MemPoolCB))) {
1421 memCB = 1;
1422 }
1423
1424 if ((attr->mp_mem != NULL) &&
1425 (((UINTPTR)attr->mp_mem & 0x3) == 0) && /* 0x3: Check if array is 4-byte aligned. */
1426 (attr->mp_size >= size)) {
1427 memMP = 1;
1428 }
1429 name = attr->name;
1430 }
1431
1432 if (memCB == 0) {
1433 mp = LOS_MemAlloc(OS_SYS_MEM_ADDR, sizeof(MemPoolCB));
1434 if (mp == NULL) {
1435 return NULL;
1436 }
1437 mp->status = MP_ALLOC;
1438 } else {
1439 mp = attr->cb_mem;
1440 mp->status = 0;
1441 }
1442
1443 if (memMP == 0) {
1444 mp->poolBase = LOS_MemAlloc(OS_SYS_MEM_ADDR, size);
1445 if (mp->poolBase == NULL) {
1446 (void)LOS_MemFree(OS_SYS_MEM_ADDR, mp);
1447 return NULL;
1448 }
1449 mp->status |= MD_ALLOC;
1450 } else {
1451 mp->poolBase = attr->mp_mem;
1452 }
1453 mp->poolSize = size;
1454 mp->name = name;
1455 mp->poolInfo.uwBlkCnt = 0;
1456 mp->poolInfo.uwBlkNum = block_count;
1457 mp->poolInfo.uwBlkSize = block_size;
1458
1459 node = (LOS_MEMBOX_NODE *)mp->poolBase;
1460 mp->poolInfo.stFreeList.pstNext = node;
1461 for (index = 0; index < block_count - 1; ++index) {
1462 node->pstNext = OS_MEMBOX_NEXT(node, block_size);
1463 node = node->pstNext;
1464 }
1465 node->pstNext = NULL;
1466
1467 mp->status |= MEM_POOL_VALID;
1468
1469 return mp;
1470 }
1471
osMemoryPoolAlloc(osMemoryPoolId_t mp_id,uint32_t timeout)1472 void *osMemoryPoolAlloc(osMemoryPoolId_t mp_id, uint32_t timeout)
1473 {
1474 MemPoolCB *mp = (MemPoolCB *)mp_id;
1475 LOS_MEMBOX_NODE *node = NULL;
1476 UINT32 intSave;
1477
1478 UNUSED(timeout);
1479
1480 if (mp_id == NULL) {
1481 return NULL;
1482 }
1483
1484 intSave = LOS_IntLock();
1485 if ((mp->status & MEM_POOL_VALID) == MEM_POOL_VALID) {
1486 node = mp->poolInfo.stFreeList.pstNext;
1487 if (node != NULL) {
1488 mp->poolInfo.stFreeList.pstNext = node->pstNext;
1489 mp->poolInfo.uwBlkCnt++;
1490 }
1491 }
1492 LOS_IntRestore(intSave);
1493
1494 return node;
1495 }
1496
osMemoryPoolFree(osMemoryPoolId_t mp_id,void * block)1497 osStatus_t osMemoryPoolFree(osMemoryPoolId_t mp_id, void *block)
1498 {
1499
1500 MemPoolCB *mp = (MemPoolCB *)mp_id;
1501 LOS_MEMBOX_NODE *node = NULL;
1502 LOS_MEMBOX_NODE *nodeTmp = NULL;
1503 UINT32 intSave;
1504
1505 if ((mp_id == NULL) || (block == NULL)) {
1506 return osErrorParameter;
1507 }
1508
1509 intSave = LOS_IntLock();
1510 if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
1511 LOS_IntRestore(intSave);
1512 return osErrorResource;
1513 }
1514
1515 if (((UINTPTR)block < (UINTPTR)mp->poolBase) ||
1516 ((UINTPTR)block >= ((UINTPTR)mp->poolBase + (UINTPTR)mp->poolSize))) {
1517 LOS_IntRestore(intSave);
1518 return osErrorParameter;
1519 }
1520
1521 node = (LOS_MEMBOX_NODE *)block;
1522 nodeTmp = mp->poolInfo.stFreeList.pstNext;
1523 mp->poolInfo.stFreeList.pstNext = node;
1524 node->pstNext = nodeTmp;
1525 mp->poolInfo.uwBlkCnt--;
1526 LOS_IntRestore(intSave);
1527
1528 return osOK;
1529 }
1530
osMemoryPoolDelete(osMemoryPoolId_t mp_id)1531 osStatus_t osMemoryPoolDelete(osMemoryPoolId_t mp_id)
1532 {
1533 MemPoolCB *mp = (MemPoolCB *)mp_id;
1534 UINT32 intSave;
1535
1536 if (OS_INT_ACTIVE) {
1537 return osErrorISR;
1538 }
1539
1540 if (mp_id == NULL) {
1541 return osErrorParameter;
1542 }
1543
1544 intSave = LOS_IntLock();
1545 if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
1546 LOS_IntRestore(intSave);
1547 return osErrorResource;
1548 }
1549
1550 if (mp->status & MD_ALLOC) {
1551 (void)LOS_MemFree(OS_SYS_MEM_ADDR, mp->poolBase);
1552 mp->poolBase = NULL;
1553 }
1554
1555 mp->name = NULL;
1556 mp->status &= ~MEM_POOL_VALID;
1557
1558 if (mp->status & MP_ALLOC) {
1559 (void)LOS_MemFree(OS_SYS_MEM_ADDR, mp);
1560 }
1561 LOS_IntRestore(intSave);
1562
1563 return osOK;
1564 }
1565
osMemoryPoolGetCapacity(osMemoryPoolId_t mp_id)1566 uint32_t osMemoryPoolGetCapacity(osMemoryPoolId_t mp_id)
1567 {
1568 MemPoolCB *mp = (MemPoolCB *)mp_id;
1569 UINT32 intSave;
1570 uint32_t num;
1571
1572 if (mp_id == NULL) {
1573 return 0;
1574 }
1575
1576 intSave = LOS_IntLock();
1577 if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
1578 num = 0;
1579 } else {
1580 num = mp->poolInfo.uwBlkNum;
1581 }
1582 LOS_IntRestore(intSave);
1583
1584 return num;
1585 }
1586
osMemoryPoolGetBlockSize(osMemoryPoolId_t mp_id)1587 uint32_t osMemoryPoolGetBlockSize(osMemoryPoolId_t mp_id)
1588 {
1589 MemPoolCB *mp = (MemPoolCB *)mp_id;
1590 UINT32 intSave;
1591 uint32_t size;
1592
1593 if (mp_id == NULL) {
1594 return 0;
1595 }
1596
1597 intSave = LOS_IntLock();
1598 if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
1599 size = 0;
1600 } else {
1601 size = mp->poolInfo.uwBlkSize;
1602 }
1603 LOS_IntRestore(intSave);
1604
1605 return size;
1606 }
1607
osMemoryPoolGetCount(osMemoryPoolId_t mp_id)1608 uint32_t osMemoryPoolGetCount(osMemoryPoolId_t mp_id)
1609 {
1610 MemPoolCB *mp = (MemPoolCB *)mp_id;
1611 UINT32 intSave;
1612 uint32_t count;
1613
1614 if (mp_id == NULL) {
1615 return 0;
1616 }
1617
1618 intSave = LOS_IntLock();
1619 if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
1620 count = 0;
1621 } else {
1622 count = mp->poolInfo.uwBlkCnt;
1623 }
1624 LOS_IntRestore(intSave);
1625
1626 return count;
1627 }
1628
osMemoryPoolGetSpace(osMemoryPoolId_t mp_id)1629 uint32_t osMemoryPoolGetSpace(osMemoryPoolId_t mp_id)
1630 {
1631 MemPoolCB *mp = (MemPoolCB *)mp_id;
1632 UINT32 intSave;
1633 uint32_t space;
1634
1635 if (mp_id == NULL) {
1636 return 0;
1637 }
1638
1639 intSave = LOS_IntLock();
1640 if ((mp->status & MEM_POOL_VALID) != MEM_POOL_VALID) {
1641 space = 0;
1642 } else {
1643 space = mp->poolInfo.uwBlkCnt - mp->poolInfo.uwBlkCnt;
1644 }
1645 LOS_IntRestore(intSave);
1646
1647 return space;
1648
1649 }
1650
osMemoryPoolGetName(osMemoryPoolId_t mp_id)1651 const char *osMemoryPoolGetName(osMemoryPoolId_t mp_id)
1652 {
1653 MemPoolCB *mp = (MemPoolCB *)mp_id;
1654 const char *p = NULL;
1655 UINT32 intSave;
1656
1657 if (mp_id == NULL) {
1658 return NULL;
1659 }
1660
1661 if (OS_INT_ACTIVE) {
1662 return NULL;
1663 }
1664
1665 intSave = LOS_IntLock();
1666 if ((mp->status & MEM_POOL_VALID) == MEM_POOL_VALID) {
1667 p = mp->name;
1668 }
1669 LOS_IntRestore(intSave);
1670
1671 return p;
1672 }
1673
1674 // ==== Thread Flags Functions ====
osThreadFlagsSet(osThreadId_t thread_id,uint32_t flags)1675 uint32_t osThreadFlagsSet(osThreadId_t thread_id, uint32_t flags)
1676 {
1677 UINT32 ret;
1678 LosTaskCB *taskCB = (LosTaskCB *)thread_id;
1679 EVENT_CB_S *eventCB = NULL;
1680 UINT32 curFlags;
1681
1682 if (taskCB == NULL) {
1683 return (uint32_t)osFlagsErrorParameter;
1684 }
1685
1686 eventCB = &(taskCB->event);
1687 curFlags = eventCB->uwEventID | flags;
1688
1689 ret = LOS_EventWrite(eventCB, (UINT32)flags);
1690 if (ret == LOS_ERRNO_EVENT_SETBIT_INVALID) {
1691 return (uint32_t)osFlagsErrorParameter;
1692 }
1693
1694 if (ret != LOS_OK) {
1695 return (uint32_t)osFlagsErrorResource;
1696 }
1697
1698 if (curFlags & taskCB->eventMask) {
1699 return curFlags & (~taskCB->eventMask);
1700 }
1701
1702 return curFlags;
1703 }
1704
osThreadFlagsClear(uint32_t flags)1705 uint32_t osThreadFlagsClear(uint32_t flags)
1706 {
1707 UINT32 ret;
1708 UINT32 saveFlags;
1709 LosTaskCB *runTask = NULL;
1710 EVENT_CB_S *eventCB = NULL;
1711
1712 if (OS_INT_ACTIVE) {
1713 return (uint32_t)osFlagsErrorUnknown;
1714 }
1715
1716 runTask = g_losTask.runTask;
1717 eventCB = &(runTask->event);
1718 saveFlags = eventCB->uwEventID;
1719
1720 ret = LOS_EventClear(eventCB, ~(UINT32)flags);
1721 if (ret == LOS_OK) {
1722 return (uint32_t)saveFlags;
1723 }
1724
1725 return (uint32_t)osFlagsErrorResource;
1726 }
1727
osThreadFlagsGet(void)1728 uint32_t osThreadFlagsGet(void)
1729 {
1730 LosTaskCB *runTask = NULL;
1731 EVENT_CB_S *eventCB = NULL;
1732
1733 if (OS_INT_ACTIVE) {
1734 return (uint32_t)osFlagsErrorUnknown;
1735 }
1736
1737 runTask = g_losTask.runTask;
1738 eventCB = &(runTask->event);
1739
1740 return (uint32_t)(eventCB->uwEventID);
1741 }
1742
osThreadFlagsWait(uint32_t flags,uint32_t options,uint32_t timeout)1743 uint32_t osThreadFlagsWait(uint32_t flags, uint32_t options, uint32_t timeout)
1744 {
1745 UINT32 ret;
1746 UINT32 mode = 0;
1747 LosTaskCB *runTask = NULL;
1748 EVENT_CB_S *eventCB = NULL;
1749
1750 if (OS_INT_ACTIVE) {
1751 return (uint32_t)osFlagsErrorUnknown;
1752 }
1753
1754 if (options > (osFlagsWaitAny | osFlagsWaitAll | osFlagsNoClear)) {
1755 return (uint32_t)osFlagsErrorParameter;
1756 }
1757
1758 if ((options & osFlagsWaitAll) == osFlagsWaitAll) {
1759 mode |= LOS_WAITMODE_AND;
1760 } else {
1761 mode |= LOS_WAITMODE_OR;
1762 }
1763
1764 if ((options & osFlagsNoClear) == osFlagsNoClear) {
1765 mode &= ~LOS_WAITMODE_CLR;
1766 } else {
1767 mode |= LOS_WAITMODE_CLR;
1768 }
1769
1770 runTask = g_losTask.runTask;
1771 eventCB = &(runTask->event);
1772
1773 ret = LOS_EventRead(eventCB, (UINT32)flags, mode, (UINT32)timeout);
1774 if (!(ret & LOS_ERRTYPE_ERROR)) {
1775 return (uint32_t)eventCB->uwEventID | ret;
1776 }
1777
1778 switch (ret) {
1779 case LOS_ERRNO_EVENT_PTR_NULL:
1780 case LOS_ERRNO_EVENT_SETBIT_INVALID:
1781 case LOS_ERRNO_EVENT_EVENTMASK_INVALID:
1782 case LOS_ERRNO_EVENT_FLAGS_INVALID:
1783 return (uint32_t)osFlagsErrorParameter;
1784 case LOS_ERRNO_EVENT_READ_TIMEOUT:
1785 return (uint32_t)osFlagsErrorTimeout;
1786 default:
1787 return (uint32_t)osFlagsErrorResource;
1788 }
1789 }
1790