1 /*
2 * Copyright (C) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 #include <hi_types_base.h>
19 #include <los_task.h>
20 #include <los_task_pri.h>
21 #include "hi_config.h"
22 #include "hi_task.h"
23
24 extern hi_u32 ms2systick(HI_IN hi_u32 ms, HI_IN hi_bool include0);
25
hi_task_create(hi_u32 * taskid,const hi_task_attr * attr,hi_void * (* task_route)(hi_void *),hi_void * arg)26 hi_u32 hi_task_create(hi_u32 *taskid, const hi_task_attr *attr, hi_void *(*task_route)(hi_void *), hi_void *arg)
27 {
28 TSK_INIT_PARAM_S my_task = { 0, };
29 hi_u32 ret;
30
31 /* 内核接口对输入参数有判断,此处省略 */
32 if (taskid == HI_NULL || task_route == HI_NULL) {
33 return HI_ERR_TASK_INVALID_PARAM;
34 }
35
36 if (attr != NULL) {
37 my_task.pcName = attr->task_name;
38 my_task.uwStackSize = attr->stack_size;
39 my_task.usTaskPrio = attr->task_prio;
40 } else {
41 my_task.pcName = HI_DEFAULT_TSKNAME;
42 my_task.uwStackSize = HI_DEFAULT_STACKSIZE;
43 my_task.usTaskPrio = HI_DEFAULT_TSKPRIO;
44 }
45 /* user task priority is limit bteween 1 and 30. */
46 if (my_task.usTaskPrio == OS_TASK_PRIORITY_HIGHEST || my_task.usTaskPrio == OS_TASK_PRIORITY_LOWEST) {
47 my_task.usTaskPrio = 20; /* normal, usTaskPrio should be [20-30]. */
48 }
49
50 if (my_task.uwStackSize == 0) {
51 my_task.uwStackSize = HI_DEFAULT_STACKSIZE;
52 }
53
54 my_task.uwResved = LOS_TASK_STATUS_DETACHED;
55 my_task.pfnTaskEntry = (TSK_ENTRY_FUNC)task_route;
56 my_task.auwArgs[0] = (hi_u32)(uintptr_t)arg;
57 #ifdef LOSCFG_KERNEL_SMP
58 my_task.usCpuAffiMask = CPUID_TO_AFFI_MASK(0);
59 #endif
60 ret = LOS_TaskCreate(taskid, &my_task);
61 if (ret != LOS_OK) {
62 return HI_ERR_TASK_CREATE_FAIL;
63 }
64
65 return HI_ERR_SUCCESS;
66 }
67
hi_task_delete(hi_u32 taskid)68 hi_u32 hi_task_delete(hi_u32 taskid)
69 {
70 hi_u32 ret;
71
72 ret = LOS_TaskDelete(taskid);
73 if (ret != LOS_OK) {
74 return HI_ERR_TASK_DELETE_FAIL;
75 }
76
77 return HI_ERR_SUCCESS;
78 }
79
hi_task_suspend(hi_u32 taskid)80 hi_u32 hi_task_suspend(hi_u32 taskid)
81 {
82 hi_u32 ret;
83
84 ret = LOS_TaskSuspend(taskid);
85 if (ret != LOS_OK) {
86 return HI_ERR_TASK_SUPPEND_FAIL;
87 }
88
89 return HI_ERR_SUCCESS;
90 }
91
hi_task_resume(hi_u32 taskid)92 hi_u32 hi_task_resume(hi_u32 taskid)
93 {
94 hi_u32 ret;
95
96 ret = LOS_TaskResume(taskid);
97 if (ret != LOS_OK) {
98 return HI_ERR_TASK_RESUME_FAIL;
99 }
100
101 return HI_ERR_SUCCESS;
102 }
103
hi_task_get_priority(hi_u32 taskid,hi_u32 * priority)104 hi_u32 hi_task_get_priority(hi_u32 taskid, hi_u32 *priority)
105 {
106 hi_u16 pri;
107
108 if (priority == HI_NULL) {
109 return HI_ERR_TASK_INVALID_PARAM;
110 }
111
112 pri = LOS_TaskPriGet(taskid);
113 if (pri == (hi_u16)OS_INVALID) {
114 return HI_ERR_TASK_GET_PRI_FAIL;
115 }
116
117 *priority = pri;
118 return HI_ERR_SUCCESS;
119 }
120
hi_task_set_priority(hi_u32 taskid,hi_u32 priority)121 hi_u32 hi_task_set_priority(hi_u32 taskid, hi_u32 priority)
122 {
123 hi_u32 ret;
124
125 ret = LOS_TaskPriSet(taskid, (hi_u16)priority);
126 if (ret != LOS_OK) {
127 return HI_ERR_TASK_SET_PRI_FAIL;
128 }
129
130 return HI_ERR_SUCCESS;
131 }
132
hi_task_get_current_id(hi_void)133 hi_u32 hi_task_get_current_id(hi_void)
134 {
135 hi_u32 tmp_tid;
136
137 tmp_tid = LOS_CurTaskIDGet();
138 if (tmp_tid == LOS_ERRNO_TSK_ID_INVALID) {
139 return HI_INVALID_TASK_ID;
140 }
141
142 return tmp_tid;
143 }
144
hi_task_lock(hi_void)145 hi_void hi_task_lock(hi_void)
146 {
147 LOS_TaskLock();
148 }
149
hi_task_unlock(hi_void)150 hi_void hi_task_unlock(hi_void)
151 {
152 LOS_TaskUnlock();
153 }
154
hi_sleep(hi_u32 ms)155 hi_u32 hi_sleep(hi_u32 ms)
156 {
157 hi_u32 ret;
158
159 hi_u32 tick = ms2systick(ms, HI_FALSE);
160
161 ret = LOS_TaskDelay(tick);
162 if (ret != LOS_OK) {
163 return HI_ERR_TASK_DELAY_FAIL;
164 }
165
166 return HI_ERR_SUCCESS;
167 }
168
hi_task_join(hi_u32 taskid,hi_u32 wait_interval)169 ROM_TEXT_PATCH_SECTION hi_void hi_task_join(hi_u32 taskid, hi_u32 wait_interval)
170 {
171 if (taskid == hi_task_get_current_id()) {
172 return;
173 }
174
175 if (wait_interval < HI_MILLISECOND_PER_TICK) {
176 wait_interval = HI_MILLISECOND_PER_TICK;
177 }
178
179 hi_task_info info = { 0 };
180 while (!(info.status & OS_TASK_STATUS_UNUSED)) {
181 if (hi_task_get_info(taskid, &info) != HI_ERR_SUCCESS) {
182 break;
183 }
184
185 if (hi_sleep(wait_interval) != HI_ERR_SUCCESS) {
186 break;
187 }
188 }
189 }
190