1 /*
2 # Copyright (C) 2024 HiHope Open Source Organization .
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 */
16
17 #include "cmsis_os.h"
18 #include "los_typedef.h"
19 #include "los_printf.h"
20
21 #include "los_event.h"
22 #include "los_membox.h"
23 #include "los_memory.h"
24 #include "los_hwi.h"
25
26 #include "los_mux_pri.h"
27 #include "los_queue_pri.h"
28 #include "los_sem_pri.h"
29 #include "los_swtmr_pri.h"
30 #include "los_sys_pri.h"
31 #include "los_task_pri.h"
32 #include "los_tick_pri.h"
33 #include "string.h"
34 #include "securec.h"
35
36 #ifdef __cplusplus
37 #if __cplusplus
38 extern "C" {
39 #endif /* __cplusplus */
40 #endif /* __cplusplus */
41 #if (CMSIS_OS_VER == 2)
42
43 /* Kernel initialization state */
44 static osKernelState_t g_kernelState;
45
46 extern BOOL g_taskScheduled;
47
48 #define LOS_PRIORITY_WIN 8
49
50 const osVersion_t g_stLosVersion = { 001, 001 };
51
52 #define LITEOS_VERSION_MAJOR 1
53 #define LITEOS_VERSION_MINOR 0
54 #define LITEOS_VERSION_BUILD 0
55
56 /* Kernel version and identification string definition */
57 #define KERNEL_VERSION (((UINT32)LITEOS_VERSION_MAJOR * 10000000UL) | \
58 ((UINT32)LITEOS_VERSION_MINOR * 10000UL) | \
59 ((UINT32)LITEOS_VERSION_BUILD * 1UL))
60
61 #define KERNEL_ID "HUAWEI-LiteOS"
62 #define UNUSED(var) do { (void)var; } while (0)
63
64 // ==== Kernel Management Functions ====
65 uint32_t osTaskStackWaterMarkGet(UINT32 taskID);
66
67
osKernelInitialize(void)68 osStatus_t osKernelInitialize(void)
69 {
70 if (OS_INT_ACTIVE) {
71 return osErrorISR;
72 }
73
74 if (g_kernelState != osKernelInactive) {
75 return osError;
76 }
77
78 if (LOS_OK == LOS_KernelInit()) {
79 g_kernelState = osKernelReady;
80 return osOK;
81 } else {
82 return osError;
83 }
84 }
85
86
osKernelGetInfo(osVersion_t * version,char * id_buf,uint32_t id_size)87 osStatus_t osKernelGetInfo(osVersion_t *version, char *id_buf, uint32_t id_size)
88 {
89 uint32_t uwRet;
90
91 if (OS_INT_ACTIVE) {
92 return osErrorISR;
93 }
94
95 if (version != NULL) {
96 version->api = g_stLosVersion.api;
97 version->kernel = g_stLosVersion.kernel;
98 }
99
100 if ((id_buf != NULL) && (id_size != 0U)) {
101 if (id_size > sizeof(KERNEL_ID)) {
102 id_size = sizeof(KERNEL_ID);
103 }
104 uwRet = memcpy_s(id_buf, id_size, KERNEL_ID, id_size);
105 if (uwRet != EOK) {
106 PRINT_ERR("%s[%d] memcpy failed, error type = %u\n", __FUNCTION__, __LINE__, uwRet);
107 return osError;
108 }
109 }
110
111 return osOK;
112 }
113
114
osKernelGetState(void)115 osKernelState_t osKernelGetState(void)
116 {
117 if (OS_INT_ACTIVE) {
118 return osKernelError;
119 }
120
121 if (!g_taskScheduled) {
122 if (g_kernelState == osKernelReady) {
123 return osKernelReady;
124 } else {
125 return osKernelInactive;
126 }
127 } else if (g_losTaskLock > 0) {
128 return osKernelLocked;
129 } else {
130 return osKernelRunning;
131 }
132 }
133
134
osKernelStart(void)135 osStatus_t osKernelStart(void)
136 {
137 if (OS_INT_ACTIVE) {
138 return osErrorISR;
139 }
140
141 if (g_kernelState == osKernelReady) {
142 if (LOS_OK == LOS_Start()) {
143 g_kernelState = osKernelRunning;
144 return osOK;
145 } else {
146 return osError;
147 }
148 } else {
149 return osError;
150 }
151 }
152
153
osKernelLock(void)154 int32_t osKernelLock(void)
155 {
156 int32_t lock;
157
158 if (OS_INT_ACTIVE) {
159 return (int32_t)osErrorISR;
160 }
161
162 if (!g_taskScheduled) {
163 return (int32_t)osError;
164 }
165
166 if (g_losTaskLock > 0) {
167 lock = 1;
168 } else {
169 LOS_TaskLock();
170 lock = 0;
171 }
172
173 return lock;
174 }
175
176
osKernelUnlock(void)177 int32_t osKernelUnlock(void)
178 {
179 int32_t lock;
180
181 if (OS_INT_ACTIVE) {
182 return (int32_t)osErrorISR;
183 }
184
185 if (!g_taskScheduled) {
186 return (int32_t)osError;
187 }
188
189 if (g_losTaskLock > 0) {
190 LOS_TaskUnlock();
191 if (g_losTaskLock != 0) {
192 return (int32_t)osError;
193 }
194 lock = 1;
195 } else {
196 lock = 0;
197 }
198
199 return lock;
200 }
201
202
osKernelRestoreLock(int32_t lock)203 int32_t osKernelRestoreLock(int32_t lock)
204 {
205 if (OS_INT_ACTIVE) {
206 return (int32_t)osErrorISR;
207 }
208
209 if (!g_taskScheduled) {
210 return (int32_t)osError;
211 }
212
213 switch (lock) {
214 case 0:
215 LOS_TaskUnlock();
216 if (g_losTaskLock != 0) {
217 break;
218 }
219 return 0;
220 case 1:
221 LOS_TaskLock();
222 return 1;
223 default:
224 break;
225 }
226
227 return (int32_t)osError;
228 }
229
230
osKernelGetTickCount(void)231 uint32_t osKernelGetTickCount(void)
232 {
233 uint64_t ticks;
234 UINTPTR uvIntSave;
235
236 if (OS_INT_ACTIVE) {
237 #ifndef LITEOS_WIFI_IOT_VERSION
238 ticks = g_ullTickCount;
239 #else
240 ticks = g_tickCount;
241 #endif
242 } else {
243 uvIntSave = LOS_IntLock();
244 #ifndef LITEOS_WIFI_IOT_VERSION
245 ticks = g_ullTickCount;
246 #else
247 ticks = g_tickCount;
248 #endif
249 LOS_IntRestore(uvIntSave);
250 }
251
252 return (uint32_t)ticks;
253 }
254
osKernelGetTickFreq(void)255 uint32_t osKernelGetTickFreq(void)
256 {
257 uint32_t freq;
258
259 if (OS_INT_ACTIVE) {
260 freq = 0U;
261 } else {
262 freq = LOSCFG_BASE_CORE_TICK_PER_SECOND;
263 }
264
265 return (freq);
266 }
267
268 extern VOID LOS_GetCpuCycle(UINT32 *puwCntHi, UINT32 *puwCntLo);
osKernelGetSysTimerCount(void)269 uint32_t osKernelGetSysTimerCount(void)
270 {
271 uint32_t countHigh = 0;
272 uint32_t countLow = 0;
273 if (OS_INT_ACTIVE) {
274 countLow = 0U;
275 } else {
276 LOS_GetCpuCycle((UINT32 *)&countHigh, (UINT32 *)&countLow);
277 }
278 return countLow;
279 }
280
281
osKernelGetSysTimerFreq(void)282 uint32_t osKernelGetSysTimerFreq(void)
283 {
284 return OS_SYS_CLOCK;
285 }
286
287
288 // ==== Thread Management Functions ====
289
osThreadNew(osThreadFunc_t func,void * argument,const osThreadAttr_t * attr)290 osThreadId_t osThreadNew(osThreadFunc_t func, void *argument, const osThreadAttr_t *attr)
291 {
292 UINT32 uwTid;
293 UINT32 uwRet;
294 LosTaskCB *pstTaskCB = NULL;
295 TSK_INIT_PARAM_S stTskInitParam;
296
297 if (OS_INT_ACTIVE) {
298 return NULL;
299 }
300
301 if ((attr == NULL) || (func == NULL) || (attr->priority < osPriorityLow1) ||
302 (attr->priority > osPriorityAboveNormal6)) {
303 return (osThreadId_t)NULL;
304 }
305
306 (void)memset_s(&stTskInitParam, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
307 stTskInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)func;
308 #ifndef LITEOS_WIFI_IOT_VERSION
309 stTskInitParam.uwArg = (UINT32)argument;
310 #else
311 stTskInitParam.auwArgs[0] = (UINT32)argument;
312 #endif
313 stTskInitParam.uwStackSize = attr->stack_size;
314 stTskInitParam.pcName = (CHAR *)attr->name;
315 stTskInitParam.usTaskPrio = OS_TASK_PRIORITY_LOWEST - ((UINT16)(attr->priority) - LOS_PRIORITY_WIN); /* 0~31 */
316
317 uwRet = LOS_TaskCreate(&uwTid, &stTskInitParam);
318
319 if (LOS_OK != uwRet) {
320 return (osThreadId_t)NULL;
321 }
322
323 pstTaskCB = OS_TCB_FROM_TID(uwTid);
324
325 return (osThreadId_t)pstTaskCB;
326 }
327
328
osThreadGetName(osThreadId_t thread_id)329 const char *osThreadGetName(osThreadId_t thread_id)
330 {
331 LosTaskCB *pstTaskCB = NULL;
332
333 if (OS_INT_ACTIVE || thread_id == NULL) {
334 return NULL;
335 }
336
337 pstTaskCB = (LosTaskCB *)thread_id;
338
339 return pstTaskCB->taskName;
340 }
341
342
osThreadGetId(void)343 osThreadId_t osThreadGetId(void)
344 {
345 if (OS_INT_ACTIVE) {
346 return NULL;
347 }
348
349 return (osThreadId_t)(g_losTask.runTask);
350 }
351
osThreadGetArgument(void)352 void *osThreadGetArgument(void)
353 {
354 if (OS_INT_ACTIVE) {
355 return 0;
356 }
357
358 LosTaskCB *taskCb = (LosTaskCB *)osThreadGetId();
359 if (taskCb == NULL) {
360 return NULL;
361 }
362 #ifndef LITEOS_WIFI_IOT_VERSION
363 return (void *)(taskCb->arg);
364 #else
365 return (void *)(taskCb->args[0]);
366 #endif
367 }
368
osThreadGetState(osThreadId_t thread_id)369 osThreadState_t osThreadGetState(osThreadId_t thread_id)
370 {
371 UINT16 taskStatus;
372 osThreadState_t stState;
373 LosTaskCB *pstTaskCB = NULL;
374
375 if (OS_INT_ACTIVE || thread_id == NULL) {
376 return osThreadError;
377 }
378
379 pstTaskCB = (LosTaskCB *)thread_id;
380 taskStatus = pstTaskCB->taskStatus;
381
382 if (taskStatus & OS_TASK_STATUS_RUNNING) {
383 stState = osThreadRunning;
384 } else if (taskStatus & OS_TASK_STATUS_READY) {
385 stState = osThreadReady;
386 } else if (taskStatus &
387 (OS_TASK_STATUS_DELAY | OS_TASK_STATUS_PEND | OS_TASK_STATUS_SUSPEND | OS_TASK_STATUS_PEND_QUEUE)) {
388 stState = osThreadBlocked;
389 } else if (taskStatus & OS_TASK_STATUS_UNUSED) {
390 stState = osThreadInactive;
391 } else {
392 stState = osThreadError;
393 }
394
395 return stState;
396 }
397
398
osThreadGetStackSize(osThreadId_t thread_id)399 uint32_t osThreadGetStackSize(osThreadId_t thread_id)
400 {
401 LosTaskCB *pstTaskCB = NULL;
402
403 if (OS_INT_ACTIVE || thread_id == NULL) {
404 return 0U;
405 }
406
407 pstTaskCB = (LosTaskCB *)thread_id;
408
409 return pstTaskCB->stackSize;
410 }
411
412
osTaskStackWaterMarkGet(UINT32 taskID)413 uint32_t osTaskStackWaterMarkGet(UINT32 taskID)
414 {
415 UINT32 uwCount = 0;
416 UINT32 *ptopOfStack;
417 UINTPTR uvIntSave;
418 LosTaskCB *pstTaskCB = NULL;
419
420 if (taskID > LOSCFG_BASE_CORE_TSK_LIMIT) {
421 return 0;
422 }
423
424 uvIntSave = LOS_IntLock();
425
426 pstTaskCB = OS_TCB_FROM_TID(taskID);
427 if (OS_TASK_STATUS_UNUSED & (pstTaskCB->taskStatus)) {
428 LOS_IntRestore(uvIntSave);
429 return 0;
430 }
431
432 // first 4 bytes is OS_TASK_MAGIC_WORD, skip
433 ptopOfStack = (UINT32 *)(UINTPTR)pstTaskCB->topOfStack + 1;
434
435 while (*ptopOfStack == (UINT32)OS_TASK_STACK_INIT) {
436 ++ptopOfStack;
437 ++uwCount;
438 }
439
440 uwCount *= sizeof(UINT32);
441
442 LOS_IntRestore(uvIntSave);
443 return uwCount;
444 }
445
446
osThreadGetStackSpace(osThreadId_t thread_id)447 uint32_t osThreadGetStackSpace(osThreadId_t thread_id)
448 {
449 LosTaskCB *pstTaskCB = NULL;
450
451 if (OS_INT_ACTIVE || thread_id == NULL) {
452 return 0U;
453 }
454
455 pstTaskCB = (LosTaskCB *)thread_id;
456
457 return osTaskStackWaterMarkGet(pstTaskCB->taskID);
458 }
459
460
osThreadSetPriority(osThreadId_t thread_id,osPriority_t priority)461 osStatus_t osThreadSetPriority(osThreadId_t thread_id, osPriority_t priority)
462 {
463 UINT32 uwRet;
464 UINT16 usPriority;
465 LosTaskCB *pstTaskCB = NULL;
466
467 if (OS_INT_ACTIVE) {
468 return osErrorISR;
469 }
470
471 if (thread_id == NULL) {
472 return osErrorParameter;
473 }
474
475 if (priority < osPriorityLow1 || priority > osPriorityAboveNormal6) {
476 return osErrorParameter;
477 }
478
479 pstTaskCB = (LosTaskCB *)thread_id;
480 usPriority = OS_TASK_PRIORITY_LOWEST - ((UINT16)priority - LOS_PRIORITY_WIN);
481 uwRet = LOS_TaskPriSet(pstTaskCB->taskID, usPriority);
482 switch (uwRet) {
483 case LOS_ERRNO_TSK_PRIOR_ERROR:
484 case LOS_ERRNO_TSK_OPERATE_IDLE:
485 case LOS_ERRNO_TSK_ID_INVALID:
486 return osErrorParameter;
487
488 case LOS_ERRNO_TSK_NOT_CREATED:
489 return osErrorResource;
490
491 default:
492 return osOK;
493 }
494 }
495
496
osThreadGetPriority(osThreadId_t thread_id)497 osPriority_t osThreadGetPriority(osThreadId_t thread_id)
498 {
499 UINT16 usRet;
500 LosTaskCB *pstTaskCB = NULL;
501
502 if (OS_INT_ACTIVE || thread_id == NULL) {
503 return osPriorityError;
504 }
505
506 pstTaskCB = (LosTaskCB *)thread_id;
507 usRet = LOS_TaskPriGet(pstTaskCB->taskID);
508
509 if (usRet == (UINT16)OS_INVALID) {
510 return osPriorityError;
511 }
512
513 return (osPriority_t)(OS_TASK_PRIORITY_LOWEST - (usRet - LOS_PRIORITY_WIN));
514 }
515
516
osThreadYield(void)517 osStatus_t osThreadYield(void)
518 {
519 UINT32 uwRet;
520
521 if (OS_INT_ACTIVE) {
522 return osErrorISR;
523 }
524
525 uwRet = LOS_TaskYield();
526
527 if (uwRet == LOS_OK) {
528 return osOK;
529 }
530
531 return osError;
532 }
533
534
osThreadSuspend(osThreadId_t thread_id)535 osStatus_t osThreadSuspend(osThreadId_t thread_id)
536 {
537 UINT32 uwRet;
538 LosTaskCB *pstTaskCB = NULL;
539
540 if (OS_INT_ACTIVE) {
541 return osErrorISR;
542 }
543
544 if (thread_id == NULL) {
545 return osErrorParameter;
546 }
547
548 pstTaskCB = (LosTaskCB *)thread_id;
549 uwRet = LOS_TaskSuspend(pstTaskCB->taskID);
550 switch (uwRet) {
551 case LOS_ERRNO_TSK_OPERATE_IDLE:
552 case LOS_ERRNO_TSK_SUSPEND_SWTMR_NOT_ALLOWED:
553 case LOS_ERRNO_TSK_ID_INVALID:
554 return osErrorParameter;
555
556 case LOS_ERRNO_TSK_NOT_CREATED:
557 case LOS_ERRNO_TSK_ALREADY_SUSPENDED:
558 case LOS_ERRNO_TSK_SUSPEND_LOCKED:
559 return osErrorResource;
560
561 default:
562 return osOK;
563 }
564 }
565
566
osThreadResume(osThreadId_t thread_id)567 osStatus_t osThreadResume(osThreadId_t thread_id)
568 {
569 UINT32 uwRet;
570 LosTaskCB *pstTaskCB = NULL;
571
572 if (OS_INT_ACTIVE) {
573 return osErrorISR;
574 }
575
576 if (thread_id == NULL) {
577 return osErrorParameter;
578 }
579
580 pstTaskCB = (LosTaskCB *)thread_id;
581 uwRet = LOS_TaskResume(pstTaskCB->taskID);
582
583 switch (uwRet) {
584 case LOS_ERRNO_TSK_ID_INVALID:
585 return osErrorParameter;
586
587 case LOS_ERRNO_TSK_NOT_CREATED:
588 case LOS_ERRNO_TSK_NOT_SUSPENDED:
589 return osErrorResource;
590
591 default:
592 return osOK;
593 }
594 }
595
596
osThreadTerminate(osThreadId_t thread_id)597 osStatus_t osThreadTerminate(osThreadId_t thread_id)
598 {
599 UINT32 uwRet;
600 LosTaskCB *pstTaskCB = NULL;
601
602 if (OS_INT_ACTIVE) {
603 return osErrorISR;
604 }
605
606 if (thread_id == NULL) {
607 return osErrorParameter;
608 }
609
610 pstTaskCB = (LosTaskCB *)thread_id;
611 uwRet = LOS_TaskDelete(pstTaskCB->taskID);
612
613 switch (uwRet) {
614 case LOS_ERRNO_TSK_OPERATE_IDLE:
615 case LOS_ERRNO_TSK_SUSPEND_SWTMR_NOT_ALLOWED:
616 case LOS_ERRNO_TSK_ID_INVALID:
617 return osErrorParameter;
618
619 case LOS_ERRNO_TSK_NOT_CREATED:
620 return osErrorResource;
621
622 default:
623 return osOK;
624 }
625 }
626
627
osThreadGetCount(void)628 uint32_t osThreadGetCount(void)
629 {
630 uint32_t uwCount = 0;
631
632 if (OS_INT_ACTIVE) {
633 return 0U;
634 }
635
636 for (uint32_t index = 0; index <= LOSCFG_BASE_CORE_TSK_LIMIT; index++) {
637 if (!((g_taskCBArray + index)->taskStatus & OS_TASK_STATUS_UNUSED)) {
638 uwCount++;
639 }
640 }
641
642 return uwCount;
643 }
644
645
646 // ==== Generic Wait Functions ====
LOS_HalDelay(UINT32 ticks)647 WEAK UINT32 LOS_HalDelay(UINT32 ticks)
648 {
649 UNUSED(ticks);
650 return LOS_ERRNO_TSK_DELAY_IN_INT;
651 }
652
653
osDelay(uint32_t ticks)654 osStatus_t osDelay(uint32_t ticks)
655 {
656 UINT32 uwRet;
657 if (ticks == 0) {
658 return osOK;
659 }
660 if (osKernelGetState() != osKernelRunning) {
661 uwRet = LOS_HalDelay(ticks);
662 } else {
663 uwRet = LOS_TaskDelay(ticks);
664 }
665 if (uwRet == LOS_OK) {
666 return osOK;
667 } else {
668 return osError;
669 }
670 }
671
672
osDelayUntil(uint32_t ticks)673 osStatus_t osDelayUntil(uint32_t ticks)
674 {
675 UINT32 uwRet;
676 UINT32 uwTicks;
677 UINT32 tickCount = osKernelGetTickCount();
678
679 if (ticks < tickCount) {
680 return osError;
681 }
682
683 uwTicks = (UINT32)(ticks - tickCount);
684
685 uwRet = LOS_TaskDelay(uwTicks);
686 if (uwRet == LOS_OK) {
687 return osOK;
688 } else {
689 return osError;
690 }
691 }
692
693 // ==== Timer Management Functions ====
694 #if (LOSCFG_BASE_CORE_SWTMR == 1)
osTimerNew(osTimerFunc_t func,osTimerType_t type,void * argument,const osTimerAttr_t * attr)695 osTimerId_t osTimerNew(osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)
696 {
697 UNUSED(attr);
698 UINT16 usSwTmrID;
699 UINT8 mode;
700
701 if ((OS_INT_ACTIVE) || (NULL == func) || ((osTimerOnce != type) && (osTimerPeriodic != type))) {
702 return (osTimerId_t)NULL;
703 }
704
705 if (osTimerOnce == type) {
706 mode = LOS_SWTMR_MODE_NO_SELFDELETE;
707 } else {
708 mode = LOS_SWTMR_MODE_PERIOD;
709 }
710 #if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
711 if (LOS_OK != LOS_SwtmrCreate(1, mode, (SWTMR_PROC_FUNC)func, &usSwTmrID, (UINT32)(UINTPTR)argument,
712 osTimerRousesAllow, osTimerAlignIgnore)) {
713 return (osTimerId_t)NULL;
714 }
715 #else
716 if (LOS_OK != LOS_SwtmrCreate(1, mode, (SWTMR_PROC_FUNC)func, &usSwTmrID, (UINT32)(UINTPTR)argument)) {
717 return (osTimerId_t)NULL;
718 }
719 #endif
720
721 return (osTimerId_t)OS_SWT_FROM_SID(usSwTmrID);
722 }
723
osTimerStart(osTimerId_t timer_id,uint32_t ticks)724 osStatus_t osTimerStart(osTimerId_t timer_id, uint32_t ticks)
725 {
726 UINT32 uwRet;
727 SWTMR_CTRL_S *pstSwtmr;
728
729 if ((0 == ticks) || (NULL == timer_id)) {
730 return osErrorParameter;
731 }
732
733 pstSwtmr = (SWTMR_CTRL_S *)timer_id;
734 pstSwtmr->uwInterval = ticks;
735 uwRet = LOS_SwtmrStart(pstSwtmr->usTimerID);
736 if (LOS_OK == uwRet) {
737 return osOK;
738 } else if (LOS_ERRNO_SWTMR_ID_INVALID == uwRet) {
739 return osErrorParameter;
740 } else {
741 return osErrorResource;
742 }
743 }
744
745
osTimerGetName(osTimerId_t timer_id)746 const char *osTimerGetName(osTimerId_t timer_id)
747 {
748 UNUSED(timer_id);
749 return (const char *)NULL;
750 }
751
752
osTimerStop(osTimerId_t timer_id)753 osStatus_t osTimerStop(osTimerId_t timer_id)
754 {
755 UINT32 uwRet;
756 SWTMR_CTRL_S *pstSwtmr = (SWTMR_CTRL_S *)timer_id;
757
758 if (OS_INT_ACTIVE) {
759 return osErrorISR;
760 }
761
762 if (NULL == pstSwtmr) {
763 return osErrorParameter;
764 }
765
766 uwRet = LOS_SwtmrStop(pstSwtmr->usTimerID);
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
osTimerIsRunning(osTimerId_t timer_id)777 uint32_t osTimerIsRunning(osTimerId_t timer_id)
778 {
779 if ((OS_INT_ACTIVE) || (NULL == timer_id)) {
780 return 0;
781 }
782
783 return (OS_SWTMR_STATUS_TICKING == ((SWTMR_CTRL_S *)timer_id)->ucState);
784 }
785
786
osTimerDelete(osTimerId_t timer_id)787 osStatus_t osTimerDelete(osTimerId_t timer_id)
788 {
789 UINT32 uwRet;
790 SWTMR_CTRL_S *pstSwtmr = (SWTMR_CTRL_S *)timer_id;
791
792 if (OS_INT_ACTIVE) {
793 return osErrorISR;
794 }
795
796 if (NULL == pstSwtmr) {
797 return osErrorParameter;
798 }
799
800 uwRet = LOS_SwtmrDelete(pstSwtmr->usTimerID);
801 if (LOS_OK == uwRet) {
802 return osOK;
803 } else if (LOS_ERRNO_SWTMR_ID_INVALID == uwRet) {
804 return osErrorParameter;
805 } else {
806 return osErrorResource;
807 }
808 }
809 #endif
810
osEventFlagsNew(const osEventFlagsAttr_t * attr)811 osEventFlagsId_t osEventFlagsNew(const osEventFlagsAttr_t *attr)
812 {
813 PEVENT_CB_S pstEventCB;
814 UINT32 uwRet;
815
816 UNUSED(attr);
817
818 if (OS_INT_ACTIVE) {
819 return (osEventFlagsId_t)NULL;
820 }
821
822 pstEventCB = (PEVENT_CB_S)LOS_MemAlloc(m_aucSysMem0, sizeof(EVENT_CB_S));
823 if (pstEventCB == NULL) {
824 return (osEventFlagsId_t)NULL;
825 }
826
827 uwRet = LOS_EventInit(pstEventCB);
828 if (uwRet == LOS_ERRNO_EVENT_PTR_NULL) {
829 return (osEventFlagsId_t)NULL;
830 } else {
831 return (osEventFlagsId_t)pstEventCB;
832 }
833 }
834
835
osEventFlagsGetName(osEventFlagsId_t ef_id)836 const char *osEventFlagsGetName(osEventFlagsId_t ef_id)
837 {
838 UNUSED(ef_id);
839
840 if (OS_INT_ACTIVE) {
841 return (const char *)NULL;
842 }
843
844 return (const char *)NULL;
845 }
846
847
osEventFlagsSet(osEventFlagsId_t ef_id,uint32_t flags)848 uint32_t osEventFlagsSet(osEventFlagsId_t ef_id, uint32_t flags)
849 {
850 PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
851 UINT32 uwRet;
852 uint32_t rflags;
853 if (pstEventCB == NULL) {
854 return osFlagsErrorParameter;
855 }
856 uwRet = LOS_EventWrite(pstEventCB, (UINT32)flags);
857 if (uwRet != LOS_OK) {
858 return (uint32_t)osFlagsErrorParameter;
859 } else {
860 rflags = pstEventCB->uwEventID;
861 return rflags;
862 }
863 }
864
865
osEventFlagsClear(osEventFlagsId_t ef_id,uint32_t flags)866 uint32_t osEventFlagsClear(osEventFlagsId_t ef_id, uint32_t flags)
867 {
868 PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
869 UINTPTR uwIntSave;
870 uint32_t rflags;
871 UINT32 uwRet;
872
873 if (pstEventCB == NULL) {
874 return (uint32_t)osFlagsErrorParameter;
875 }
876
877 uwIntSave = LOS_IntLock();
878 rflags = pstEventCB->uwEventID;
879
880 uwRet = LOS_EventClear(pstEventCB, ~flags);
881 LOS_IntRestore(uwIntSave);
882 if (uwRet != LOS_OK) {
883 return (uint32_t)osFlagsErrorParameter;
884 } else {
885 return rflags;
886 }
887 }
888
889
osEventFlagsGet(osEventFlagsId_t ef_id)890 uint32_t osEventFlagsGet(osEventFlagsId_t ef_id)
891 {
892 PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
893 UINTPTR uwIntSave;
894 uint32_t rflags;
895
896 if (pstEventCB == NULL) {
897 return (uint32_t)osFlagsErrorParameter;
898 }
899
900 uwIntSave = LOS_IntLock();
901 rflags = pstEventCB->uwEventID;
902 LOS_IntRestore(uwIntSave);
903
904 return rflags;
905 }
906
osEventFlagsWait(osEventFlagsId_t ef_id,uint32_t flags,uint32_t options,uint32_t timeout)907 uint32_t osEventFlagsWait(osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout)
908 {
909 PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
910 UINT32 uwMode = 0;
911 UINT32 uwRet;
912 uint32_t rflags;
913
914 if (options > (osFlagsWaitAny | osFlagsWaitAll | osFlagsNoClear)) {
915 return (uint32_t)osFlagsErrorParameter;
916 }
917
918 if ((options & osFlagsWaitAll) == osFlagsWaitAll) {
919 uwMode |= LOS_WAITMODE_AND;
920 } else {
921 uwMode |= LOS_WAITMODE_OR;
922 }
923
924 if ((options & osFlagsNoClear) == osFlagsNoClear) {
925 uwMode &= ~LOS_WAITMODE_CLR;
926 } else {
927 uwMode |= LOS_WAITMODE_CLR;
928 }
929
930 uwRet = LOS_EventRead(pstEventCB, (UINT32)flags, uwMode, (UINT32)timeout);
931 switch (uwRet) {
932 case LOS_ERRNO_EVENT_PTR_NULL:
933 case LOS_ERRNO_EVENT_EVENTMASK_INVALID:
934 case LOS_ERRNO_EVENT_SETBIT_INVALID:
935 return (uint32_t)osFlagsErrorParameter;
936
937 case LOS_ERRNO_EVENT_READ_IN_INTERRUPT:
938 case LOS_ERRNO_EVENT_FLAGS_INVALID:
939 case LOS_ERRNO_EVENT_READ_IN_LOCK:
940 return (uint32_t)osFlagsErrorResource;
941
942 case LOS_ERRNO_EVENT_READ_TIMEOUT:
943 return (uint32_t)osFlagsErrorTimeout;
944
945 default:
946 rflags = (uint32_t)uwRet;
947 return rflags;
948 }
949 }
950
osEventFlagsDelete(osEventFlagsId_t ef_id)951 osStatus_t osEventFlagsDelete(osEventFlagsId_t ef_id)
952 {
953 PEVENT_CB_S pstEventCB = (PEVENT_CB_S)ef_id;
954 UINTPTR uwIntSave;
955 osStatus_t uwRet;
956
957 uwIntSave = LOS_IntLock();
958 if (LOS_EventDestroy(pstEventCB) == LOS_OK) {
959 uwRet = osOK;
960 } else {
961 uwRet = osErrorParameter;
962 }
963 LOS_IntRestore(uwIntSave);
964
965 if (LOS_MemFree(m_aucSysMem0, (void *)pstEventCB) == LOS_OK) {
966 uwRet = osOK;
967 } else {
968 uwRet = osErrorParameter;
969 }
970
971 return uwRet;
972 }
973
974 // ==== Mutex Management Functions ====
975 #if (LOSCFG_BASE_IPC_MUX == 1)
osMutexNew(const osMutexAttr_t * attr)976 osMutexId_t osMutexNew(const osMutexAttr_t *attr)
977 {
978 UINT32 uwRet;
979 UINT32 uwMuxId;
980
981 UNUSED(attr);
982
983 if (OS_INT_ACTIVE) {
984 return NULL;
985 }
986
987 uwRet = LOS_MuxCreate(&uwMuxId);
988 if (uwRet == LOS_OK) {
989 return (osMutexId_t)(GET_MUX(uwMuxId));
990 } else {
991 return (osMutexId_t)NULL;
992 }
993 }
994
995
osMutexAcquire(osMutexId_t mutex_id,uint32_t timeout)996 osStatus_t osMutexAcquire(osMutexId_t mutex_id, uint32_t timeout)
997 {
998 UINT32 uwRet;
999
1000 if (mutex_id == NULL) {
1001 return osErrorParameter;
1002 }
1003
1004 if (OS_INT_ACTIVE && (timeout != LOS_NO_WAIT)) {
1005 timeout = 0;
1006 }
1007
1008 uwRet = LOS_MuxPend(((LosMuxCB *)mutex_id)->muxID, timeout);
1009 if (uwRet == LOS_OK) {
1010 return osOK;
1011 } else if (uwRet == LOS_ERRNO_MUX_TIMEOUT) {
1012 return osErrorTimeout;
1013 } else if (uwRet == LOS_ERRNO_MUX_INVALID) {
1014 return osErrorParameter;
1015 } else {
1016 return osErrorResource;
1017 }
1018 }
1019
1020
osMutexRelease(osMutexId_t mutex_id)1021 osStatus_t osMutexRelease(osMutexId_t mutex_id)
1022 {
1023 UINT32 uwRet;
1024
1025 if (mutex_id == NULL) {
1026 return osErrorParameter;
1027 }
1028
1029 uwRet = LOS_MuxPost(((LosMuxCB *)mutex_id)->muxID);
1030 if (uwRet == LOS_OK) {
1031 return osOK;
1032 } else {
1033 return osErrorResource;
1034 }
1035 }
1036
1037
osMutexGetOwner(osMutexId_t mutex_id)1038 osThreadId_t osMutexGetOwner(osMutexId_t mutex_id)
1039 {
1040 UINT32 uwIntSave;
1041 LosTaskCB *pstTaskCB;
1042
1043 if (OS_INT_ACTIVE) {
1044 return NULL;
1045 }
1046
1047 if (mutex_id == NULL) {
1048 return NULL;
1049 }
1050
1051 uwIntSave = LOS_IntLock();
1052 pstTaskCB = ((LosMuxCB *)mutex_id)->owner;
1053 LOS_IntRestore(uwIntSave);
1054
1055 return (osThreadId_t)pstTaskCB;
1056 }
1057
1058
osMutexDelete(osMutexId_t mutex_id)1059 osStatus_t osMutexDelete(osMutexId_t mutex_id)
1060 {
1061 UINT32 uwRet;
1062
1063 if (OS_INT_ACTIVE) {
1064 return osErrorISR;
1065 }
1066
1067 if (mutex_id == NULL) {
1068 return osErrorParameter;
1069 }
1070
1071 uwRet = LOS_MuxDelete(((LosMuxCB *)mutex_id)->muxID);
1072 if (uwRet == LOS_OK) {
1073 return osOK;
1074 } else if (uwRet == LOS_ERRNO_MUX_INVALID) {
1075 return osErrorParameter;
1076 } else {
1077 return osErrorResource;
1078 }
1079 }
1080 #endif
1081
1082 // ==== Semaphore Management Functions ====
1083 #if (LOSCFG_BASE_IPC_SEM == 1)
1084
osSemaphoreNew(uint32_t max_count,uint32_t initial_count,const osSemaphoreAttr_t * attr)1085 osSemaphoreId_t osSemaphoreNew(uint32_t max_count, uint32_t initial_count, const osSemaphoreAttr_t *attr)
1086 {
1087 UINT32 uwRet;
1088 UINT32 uwSemId;
1089
1090 UNUSED(attr);
1091
1092 if (OS_INT_ACTIVE) {
1093 return (osSemaphoreId_t)NULL;
1094 }
1095
1096 if (1 == max_count) {
1097 uwRet = LOS_BinarySemCreate((UINT16)initial_count, &uwSemId);
1098 } else {
1099 uwRet = LOS_SemCreate((UINT16)initial_count, &uwSemId);
1100 }
1101
1102 if (uwRet == LOS_OK) {
1103 return (osSemaphoreId_t)(GET_SEM(uwSemId));
1104 } else {
1105 return (osSemaphoreId_t)NULL;
1106 }
1107 }
1108
1109
osSemaphoreAcquire(osSemaphoreId_t semaphore_id,uint32_t timeout)1110 osStatus_t osSemaphoreAcquire(osSemaphoreId_t semaphore_id, uint32_t timeout)
1111 {
1112 UINT32 uwRet;
1113
1114 if (semaphore_id == NULL) {
1115 return osErrorParameter;
1116 }
1117
1118 if (OS_INT_ACTIVE && (timeout != LOS_NO_WAIT)) {
1119 return osErrorISR;
1120 }
1121
1122 uwRet = LOS_SemPend(((LosSemCB *)semaphore_id)->semID, timeout);
1123 if (uwRet == LOS_OK) {
1124 return osOK;
1125 } else if (uwRet == LOS_ERRNO_SEM_TIMEOUT) {
1126 return osErrorTimeout;
1127 } else if (uwRet == LOS_ERRNO_SEM_INVALID) {
1128 return osErrorParameter;
1129 } else if (uwRet == LOS_ERRNO_SEM_PEND_INTERR) {
1130 return osErrorISR;
1131 } else {
1132 return osErrorResource;
1133 }
1134 }
1135
1136
osSemaphoreRelease(osSemaphoreId_t semaphore_id)1137 osStatus_t osSemaphoreRelease(osSemaphoreId_t semaphore_id)
1138 {
1139 UINT32 uwRet;
1140
1141 if (semaphore_id == NULL) {
1142 return osErrorParameter;
1143 }
1144
1145 uwRet = LOS_SemPost(((LosSemCB *)semaphore_id)->semID);
1146 if (uwRet == LOS_OK) {
1147 return osOK;
1148 } else if (uwRet == LOS_ERRNO_SEM_INVALID) {
1149 return osErrorParameter;
1150 } else {
1151 return osErrorResource;
1152 }
1153 }
1154
1155
osSemaphoreGetCount(osSemaphoreId_t semaphore_id)1156 uint32_t osSemaphoreGetCount(osSemaphoreId_t semaphore_id)
1157 {
1158 UINT32 uwIntSave;
1159 UINT32 uwCount;
1160
1161 if (OS_INT_ACTIVE) {
1162 return 0;
1163 }
1164
1165 if (semaphore_id == NULL) {
1166 return 0;
1167 }
1168
1169 uwIntSave = LOS_IntLock();
1170 uwCount = ((LosSemCB *)semaphore_id)->semCount;
1171 LOS_IntRestore(uwIntSave);
1172
1173 return uwCount;
1174 }
1175
1176
osSemaphoreDelete(osSemaphoreId_t semaphore_id)1177 osStatus_t osSemaphoreDelete(osSemaphoreId_t semaphore_id)
1178 {
1179 UINT32 uwRet;
1180
1181 if (OS_INT_ACTIVE) {
1182 return osErrorISR;
1183 }
1184
1185 if (semaphore_id == NULL) {
1186 return osErrorParameter;
1187 }
1188
1189 uwRet = LOS_SemDelete(((LosSemCB *)semaphore_id)->semID);
1190 if (uwRet == LOS_OK) {
1191 return osOK;
1192 } else if (uwRet == LOS_ERRNO_SEM_INVALID) {
1193 return osErrorParameter;
1194 } else {
1195 return osErrorResource;
1196 }
1197 }
1198 #endif
1199
1200
1201 // ==== Message Queue Management Functions ====
1202 #if (LOSCFG_BASE_IPC_QUEUE == 1)
osMessageQueueNew(uint32_t msg_count,uint32_t msg_size,const osMessageQueueAttr_t * attr)1203 osMessageQueueId_t osMessageQueueNew(uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr)
1204 {
1205 UINT32 uwQueueID;
1206 UINT32 uwRet;
1207 UNUSED(attr);
1208 osMessageQueueId_t handle;
1209
1210 if (0 == msg_count || 0 == msg_size || OS_INT_ACTIVE) {
1211 return (osMessageQueueId_t)NULL;
1212 }
1213
1214 uwRet = LOS_QueueCreate((char *)NULL, (UINT16)msg_count, &uwQueueID, 0, (UINT16)msg_size);
1215 if (uwRet == LOS_OK) {
1216 handle = (osMessageQueueId_t)(GET_QUEUE_HANDLE(uwQueueID));
1217 } else {
1218 handle = (osMessageQueueId_t)NULL;
1219 }
1220
1221 return handle;
1222 }
1223
1224
osMessageQueuePut(osMessageQueueId_t mq_id,const void * msg_ptr,uint8_t msg_prio,uint32_t timeout)1225 osStatus_t osMessageQueuePut(osMessageQueueId_t mq_id, const void *msg_ptr, uint8_t msg_prio, uint32_t timeout)
1226 {
1227 UNUSED(msg_prio);
1228 UINT32 uwRet;
1229 UINT32 uwBufferSize;
1230 LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
1231
1232 if (pstQueue == NULL || msg_ptr == NULL || ((OS_INT_ACTIVE) && (0 != timeout))) {
1233 return osErrorParameter;
1234 }
1235 if (pstQueue->queueSize < sizeof(UINT32)) {
1236 return osErrorParameter;
1237 }
1238 uwBufferSize = (UINT32)(pstQueue->queueSize - sizeof(UINT32));
1239 uwRet = LOS_QueueWriteCopy((UINT32)pstQueue->queueID, (void *)msg_ptr, uwBufferSize, timeout);
1240 if (uwRet == LOS_OK) {
1241 return osOK;
1242 } else if (uwRet == LOS_ERRNO_QUEUE_INVALID || uwRet == LOS_ERRNO_QUEUE_NOT_CREATE) {
1243 return osErrorParameter;
1244 } else if (uwRet == LOS_ERRNO_QUEUE_TIMEOUT) {
1245 return osErrorTimeout;
1246 } else {
1247 return osErrorResource;
1248 }
1249 }
1250
1251
osMessageQueueGet(osMessageQueueId_t mq_id,void * msg_ptr,uint8_t * msg_prio,uint32_t timeout)1252 osStatus_t osMessageQueueGet(osMessageQueueId_t mq_id, void *msg_ptr, uint8_t *msg_prio, uint32_t timeout)
1253 {
1254 UNUSED(msg_prio);
1255 UINT32 uwRet;
1256 UINT32 uwBufferSize;
1257 LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
1258
1259 if (pstQueue == NULL || msg_ptr == NULL || ((OS_INT_ACTIVE) && (0 != timeout))) {
1260 return osErrorParameter;
1261 }
1262
1263 uwBufferSize = (UINT32)(pstQueue->queueSize - sizeof(UINT32));
1264 uwRet = LOS_QueueReadCopy((UINT32)pstQueue->queueID, msg_ptr, &uwBufferSize, timeout);
1265 if (uwRet == LOS_OK) {
1266 return osOK;
1267 } else if (uwRet == LOS_ERRNO_QUEUE_INVALID || uwRet == LOS_ERRNO_QUEUE_NOT_CREATE) {
1268 return osErrorParameter;
1269 } else if (uwRet == LOS_ERRNO_QUEUE_TIMEOUT) {
1270 return osErrorTimeout;
1271 } else {
1272 return osErrorResource;
1273 }
1274 }
1275
osMessageQueueGetCapacity(osMessageQueueId_t mq_id)1276 uint32_t osMessageQueueGetCapacity(osMessageQueueId_t mq_id)
1277 {
1278 uint32_t capacity;
1279 LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
1280
1281 if (pstQueue == NULL) {
1282 capacity = 0U;
1283 } else {
1284 capacity = pstQueue->queueLen;
1285 }
1286
1287 return (capacity);
1288 }
1289
osMessageQueueGetMsgSize(osMessageQueueId_t mq_id)1290 uint32_t osMessageQueueGetMsgSize(osMessageQueueId_t mq_id)
1291 {
1292 uint32_t size;
1293 LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
1294
1295 if (pstQueue == NULL) {
1296 size = 0U;
1297 } else {
1298 size = pstQueue->queueSize - sizeof(UINT32);
1299 }
1300
1301 return (size);
1302 }
1303
1304
osMessageQueueGetCount(osMessageQueueId_t mq_id)1305 uint32_t osMessageQueueGetCount(osMessageQueueId_t mq_id)
1306 {
1307 uint32_t count;
1308 UINTPTR uwIntSave;
1309 LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
1310
1311 if (pstQueue == NULL) {
1312 count = 0U;
1313 } else {
1314 uwIntSave = LOS_IntLock();
1315 count = (uint32_t)(pstQueue->readWriteableCnt[OS_QUEUE_READ]);
1316 LOS_IntRestore(uwIntSave);
1317 }
1318 return count;
1319 }
1320
1321
osMessageQueueGetSpace(osMessageQueueId_t mq_id)1322 uint32_t osMessageQueueGetSpace(osMessageQueueId_t mq_id)
1323 {
1324 uint32_t space;
1325 UINTPTR uwIntSave;
1326 LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
1327
1328 if (pstQueue == NULL) {
1329 space = 0U;
1330 } else {
1331 uwIntSave = LOS_IntLock();
1332 space = (uint32_t)pstQueue->readWriteableCnt[OS_QUEUE_WRITE];
1333 LOS_IntRestore(uwIntSave);
1334 }
1335 return space;
1336 }
1337
osMessageQueueDelete(osMessageQueueId_t mq_id)1338 osStatus_t osMessageQueueDelete(osMessageQueueId_t mq_id)
1339 {
1340 LosQueueCB *pstQueue = (LosQueueCB *)mq_id;
1341 UINT32 uwRet;
1342
1343 if (pstQueue == NULL) {
1344 return osErrorParameter;
1345 }
1346
1347 if (OS_INT_ACTIVE) {
1348 return osErrorISR;
1349 }
1350
1351 uwRet = LOS_QueueDelete((UINT32)pstQueue->queueID);
1352 if (uwRet == LOS_OK) {
1353 return osOK;
1354 } else if (uwRet == LOS_ERRNO_QUEUE_NOT_FOUND || uwRet == LOS_ERRNO_QUEUE_NOT_CREATE) {
1355 return osErrorParameter;
1356 } else {
1357 return osErrorResource;
1358 }
1359 }
osThreadExit(void)1360 void osThreadExit(void)
1361 {
1362 return;
1363 }
1364 #endif
1365
1366 #endif // (CMSIS_OS_VER == 2)
1367 #ifdef __cplusplus
1368 #if __cplusplus
1369 }
1370 #endif /* __cplusplus */
1371 #endif /* __cplusplus */
1372