1 /*
2 * Copyright (c) 2020, HiHope Community.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef WM_OS_LITEOS_H
31 #define WM_OS_LITEOS_H
32
33 #include <assert.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include "securec.h"
37 #include "wm_config.h"
38 #include "los_config.h"
39 #include "los_task.h"
40 #include "los_mux.h"
41 #include "los_queue.h"
42 #include "los_swtmr.h"
43 #include "los_sem.h"
44 #include "los_swtmr.h"
45 #include "wm_type_def.h"
46 #include "wm_osal.h"
47 #include "wm_mem.h"
48 #include "portliteos.h"
49
50 #define TLS_USER_PRORITY_MIN 25
51 const unsigned int HZ = LOSCFG_BASE_CORE_TICK_PER_SECOND;
52
53 #include "securec.h"
54
55 typedef struct {
56 u32 handle;
57 } tls_os_handle_t;
58
tls_os_priority_translate(u16 nPriority)59 u16 tls_os_priority_translate(u16 nPriority)
60 {
61 u16 priority;
62 if (nPriority < OS_TASK_PRIORITY_LOWEST) {
63 return nPriority;
64 } else {
65 priority = TLS_USER_PRORITY_MIN +
66 (nPriority - OS_TASK_PRIORITY_LOWEST)
67 / (OS_TASK_PRIORITY_LOWEST - TLS_USER_PRORITY_MIN);
68 if (priority >= OS_TASK_PRIORITY_LOWEST) {
69 priority = OS_TASK_PRIORITY_LOWEST - 1;
70 }
71 }
72
73 return priority;
74 }
75 /*
76 *********************************************************************************************************
77 * CREATE A TASK (Extended Version)
78 *
79 * Description: This function is used to have uC/OS-II manage the execution of a task. Tasks can either
80 * be created prior to the start of multitasking or by a running task. A task cannot be
81 * created by an ISR.
82 *
83 * Arguments : task is a pointer to the task'
84 *
85 * name is the task's name
86 *
87 * entry is the task's entry function
88 *
89 * param is a pointer to an optional data area which can be used to pass parameters to
90 * the task when the task first executes. Where the task is concerned it thinks
91 * it was invoked and passed the argument 'param' as follows:
92 *
93 * void Task (void *param)
94 * {
95 * for (;;) {
96 * Task code;
97 * }
98 * }
99 *
100 * stk_start is a pointer to the task's bottom of stack.
101 *
102 * stk_size is the size of the stack in number of elements. If OS_STK is set to u8,
103 * 'stk_size' corresponds to the number of bytes available. If OS_STK is set to
104 * INT16U, 'stk_size' contains the number of 16-bit entries available. Finally, if
105 * OS_STK is set to INT32U, 'stk_size' contains the number of 32-bit entries
106 * available on the stack.
107 *
108 * prio is the task's priority. A unique priority MUST be assigned to each task and the
109 * lower the number, the higher the priority.
110 *
111 * flag contains additional information about the behavior of the task.
112 *
113 * Returns : TLS_OS_SUCCESS if the function was successful.
114 * TLS_OS_ERROR
115 *********************************************************************************************************
116 */
tls_os_task_create(tls_os_task_t * task,const char * name,void (* entry)(void * param),void * param,u8 * stk_start,u32 stk_size,u32 prio,u32 flag)117 tls_os_status_t tls_os_task_create(tls_os_task_t *task,
118 const char* name,
119 void (*entry)(void* param),
120 void* param,
121 u8 *stk_start,
122 u32 stk_size,
123 u32 prio,
124 u32 flag)
125 {
126 u32 error;
127 char *tmp_name;
128 tls_os_status_t os_status;
129 TSK_INIT_PARAM_S stInitParam = {0};
130 u32 TskID;
131 tls_os_handle_t *pHandle = NULL;
132
133 if (stk_start != NULL) {
134 printf(" --- task name %s\n", name);
135 assert(stk_start == NULL);
136 }
137 pHandle = tls_mem_alloc(sizeof(tls_os_handle_t));
138 if (!pHandle) {
139 if (task) {
140 *task = NULL;
141 }
142 printf("%s: malloc error\n", __FUNCTION__);
143 return TLS_OS_ERROR;
144 }
145 stInitParam.pfnTaskEntry = entry;
146 stInitParam.usTaskPrio = tls_os_priority_translate(prio);
147 stInitParam.uwArg = param;
148 stInitParam.uwStackSize = stk_size;
149
150 if (name == NULL) {
151 tmp_name = tls_mem_alloc(20); // 20:alloc size
152 if (tmp_name == NULL) {
153 printf("mallo c error\n");
154 }
155 sprintf_s(tmp_name, sizeof(*tmp_name), "task%d", prio);
156 stInitParam.pcName = tmp_name;
157 } else {
158 stInitParam.pcName = name;
159 }
160 error = LOS_TaskCreate(&TskID, &stInitParam);
161 if (error == LOS_OK) {
162 if (task) {
163 pHandle->handle = TskID;
164 *task = (tls_os_task_t)pHandle;
165 }
166 os_status = TLS_OS_SUCCESS;
167 printf("%s: TskID %d, task name %s\n", __FUNCTION__, TskID, stInitParam.pcName);
168 } else {
169 printf("configMAX_PRIORITIES - prio:%d name:%s size:0x%x entry:0x%x error:%d\n",
170 prio, stInitParam.pcName, stk_size, (u32)entry, error);
171 if (task) {
172 *task = NULL;
173 }
174 tls_mem_free(pHandle);
175 os_status = TLS_OS_ERROR;
176 }
177
178 return os_status;
179 }
180
181 /*
182 *********************************************************************************************************
183 * DELETE A TASK
184 *
185 * Description: This function allows you to delete a task. The calling task can delete itself by
186 * its own priority number. The deleted task is returned to the dormant state and can be
187 * re-activated by creating the deleted task again.
188 *
189 * Arguments : prio: the task priority
190 * freefun: function to free resource
191 *
192 * Returns : TLS_OS_SUCCESS if the call is successful
193 * TLS_OS_ERROR
194 *********************************************************************************************************
195 */
tls_os_task_del(u8 prio,void (* freefun)(void))196 tls_os_status_t tls_os_task_del(u8 prio, void (*freefun)(void))
197 {
198 printf("modify compile tls_os_task_del is null fun . \n");
199 assert(0);
200 return TLS_OS_ERROR;
201 }
202
203 /**
204 * @brief This function allows you to delete a task. The calling
205 task can delete itself by its handle.
206 The deleted task is returned to the dormant state
207 and can be re-activated by creating the deleted task
208 again.
209 *
210 * @param[in] handle task handle to delete
211 * @param[in] (*freefun)(void) function to free resource,default using null
212 *
213 * @retval TLS_OS_SUCCESS the call is successful
214 * @retval TLS_OS_ERROR failed
215 *
216 * @note Generally, you do not need to call this function in your application.
217 */
tls_os_task_del_by_task_handle(tls_os_task_t task,void (* freefun)(void))218 tls_os_status_t tls_os_task_del_by_task_handle(tls_os_task_t task, void (*freefun)(void))
219 {
220 tls_os_status_t os_status;
221 tls_os_handle_t *pHandle = (tls_os_handle_t *)task;
222 int ret = LOS_TaskDelete(pHandle->handle);
223 if (ret == LOS_OK) {
224 os_status = TLS_OS_SUCCESS;
225 tls_mem_free(pHandle);
226 } else {
227 os_status = TLS_OS_ERROR;
228 }
229 printf("%s: handle %d, ret %d\n", __FUNCTION__, (u32)task, ret);
230 return os_status;
231 }
232
233 /*
234 *********************************************************************************************************
235 * CREATE A MUTUAL EXCLUSION SEMAPHORE
236 *
237 * Description: This function creates a mutual exclusion semaphore.
238 *
239 * Arguments : prio is the priority to use when accessing the mutual exclusion semaphore. In
240 * other words, when the semaphore is acquired and a higher priority task
241 * attempts to obtain the semaphore then the priority of the task owning the
242 * semaphore is raised to this priority. It is assumed that you will specify
243 * a priority that is LOWER in value than ANY of the tasks competing for the
244 * mutex.
245 *
246 * mutex is a pointer to the event control clock (OS_EVENT) associated with the
247 * created mutex.
248 *
249 *
250 * Returns :TLS_OS_SUCCESS if the call was successful.
251 * TLS_OS_ERROR
252 *
253 * Note(s) : 1) The LEAST significant 8 bits of '.OSEventCnt' are used to hold the priority number
254 * of the task owning the mutex or 0xFF if no task owns the mutex.
255 *
256 * 2) The MOST significant 8 bits of '.OSEventCnt' are used to hold the priority number
257 * to use to reduce priority inversion.
258 *********************************************************************************************************
259 */
tls_os_mutex_create(u8 prio,tls_os_mutex_t ** mutex)260 tls_os_status_t tls_os_mutex_create(u8 prio, tls_os_mutex_t **mutex)
261 {
262 u32 error;
263 UINT32 mutexID = 0 ;
264 tls_os_status_t os_status;
265 tls_os_handle_t *pHandle = tls_mem_alloc(sizeof(tls_os_handle_t));
266 if (!pHandle) {
267 printf("%s: malloc error\n", __FUNCTION__);
268 return TLS_OS_ERROR;
269 }
270
271 error = LOS_MuxCreate(&mutexID);
272 if (error == LOS_OK) {
273 pHandle->handle = mutexID;
274 *mutex = (tls_os_mutex_t *)pHandle;
275 os_status = TLS_OS_SUCCESS;
276 } else {
277 *mutex = NULL;
278 os_status = TLS_OS_ERROR;
279 tls_mem_free(pHandle);
280 }
281 return os_status;
282 }
283
284 /*
285 *********************************************************************************************************
286 * DELETE A MUTEX
287 *
288 * Description: This function deletes a mutual exclusion semaphore and readies all tasks pending on the it.
289 *
290 * Arguments : mutex is a pointer to the event control block associated with the desired mutex.
291 *
292 * Returns : TLS_OS_SUCCESS The call was successful and the mutex was deleted
293 * TLS_OS_ERROR error
294 *
295 * Note(s) : 1) This function must be used with care. Tasks that would normally expect the presence of
296 * the mutex MUST check the return code of OSMutexPend().
297 *
298 * 2) This call can potentially disable interrupts for a long time. The interrupt disable
299 * time is directly proportional to the number of tasks waiting on the mutex.
300 *
301 * 3) Because ALL tasks pending on the mutex will be readied, you MUST be careful because the
302 * resource(s) will no longer be guarded by the mutex.
303 *
304 * 4) IMPORTANT: In the 'OS_DEL_ALWAYS' case, we assume that the owner of the Mutex (if there
305 * is one) is ready-to-run and is thus NOT pending on another kernel object or
306 * has delayed itself. In other words, if a task owns the mutex being deleted,
307 * that task will be made ready-to-run at its original priority.
308 *********************************************************************************************************
309 */
tls_os_mutex_delete(tls_os_mutex_t * mutex)310 tls_os_status_t tls_os_mutex_delete(tls_os_mutex_t *mutex)
311 {
312 u32 error;
313 tls_os_status_t os_status;
314 tls_os_handle_t *pHandle = (tls_os_handle_t *)mutex;
315
316 error = LOS_MuxDelete(pHandle->handle);
317 if (error == LOS_OK) {
318 os_status = TLS_OS_SUCCESS;
319 tls_mem_free(pHandle);
320 } else {
321 os_status = TLS_OS_ERROR;
322 }
323 return os_status;
324 }
325
326 /*
327 *********************************************************************************************************
328 * PEND ON MUTUAL EXCLUSION SEMAPHORE
329 *
330 * Description: This function waits for a mutual exclusion semaphore.
331 *
332 * Arguments : mutex is a pointer to the event control block associated with the desired
333 * mutex.
334 *
335 * wait_time is an optional timeout period (in clock ticks). If non-zero, your task will
336 * wait for the resource up to the amount of time specified by this argument.
337 * If you specify 0, however, your task will wait forever at the specified
338 * mutex or, until the resource becomes available.
339 *
340 *
341 *
342 * Returns : TLS_OS_SUCCESS The call was successful and your task owns the mutex
343 * TLS_OS_ERROR
344 *
345 * Note(s) : 1) The task that owns the Mutex MUST NOT pend on any other event while it owns the mutex.
346 *
347 * 2) You MUST NOT change the priority of the task that owns the mutex
348 *********************************************************************************************************
349 */
tls_os_mutex_acquire(tls_os_mutex_t * mutex,u32 wait_time)350 tls_os_status_t tls_os_mutex_acquire(tls_os_mutex_t *mutex, u32 wait_time)
351 {
352 u8 error;
353 tls_os_status_t os_status;
354 unsigned int time;
355 tls_os_handle_t *pHandle = (tls_os_handle_t *)mutex;
356
357 if (wait_time == 0)
358 time = LOS_WAIT_FOREVER;
359 else
360 time = wait_time;
361 error = LOS_MuxPend(pHandle->handle, time);
362 if (error == LOS_OK)
363 os_status = TLS_OS_SUCCESS;
364 else
365 os_status = TLS_OS_ERROR;
366
367 return os_status;
368 }
369
370 /*
371 *********************************************************************************************************
372 * POST TO A MUTUAL EXCLUSION SEMAPHORE
373 *
374 * Description: This function signals a mutual exclusion semaphore
375 *
376 * Arguments : mutex is a pointer to the event control block associated with the desired
377 * mutex.
378 *
379 * Returns : TLS_OS_SUCCESS The call was successful and the mutex was signaled.
380 * TLS_OS_ERROR
381 *********************************************************************************************************
382 */
tls_os_mutex_release(tls_os_mutex_t * mutex)383 tls_os_status_t tls_os_mutex_release(tls_os_mutex_t *mutex)
384 {
385 u32 error;
386 tls_os_status_t os_status;
387 tls_os_handle_t *pHandle = (tls_os_handle_t *)mutex;
388
389 error = LOS_MuxPost(pHandle->handle);
390 if (error == LOS_OK)
391 os_status = TLS_OS_SUCCESS;
392 else
393 os_status = TLS_OS_ERROR;
394
395 return os_status;
396 }
397
398 /*
399 *********************************************************************************************************
400 * CREATE A SEMAPHORE
401 *
402 * Description: This function creates a semaphore.
403 *
404 * Arguments :sem is a pointer to the event control block (OS_EVENT) associated with the
405 * created semaphore
406 * cnt is the initial value for the semaphore. If the value is 0, no resource is
407 * available (or no event has occurred). You initialize the semaphore to a
408 * non-zero value to specify how many resources are available (e.g. if you have
409 * 10 resources, you would initialize the semaphore to 10).
410 *
411 * Returns : TLS_OS_SUCCESS The call was successful
412 * TLS_OS_ERROR
413 *********************************************************************************************************
414 */
tls_os_sem_create(tls_os_sem_t ** sem,u32 cnt)415 tls_os_status_t tls_os_sem_create(tls_os_sem_t **sem, u32 cnt)
416 {
417 u32 error;
418
419 tls_os_status_t os_status;
420 tls_os_handle_t *pHandle = tls_mem_alloc(sizeof(tls_os_handle_t));
421 if (!pHandle) {
422 printf("%s: malloc error\n", __FUNCTION__);
423 return TLS_OS_ERROR;
424 }
425
426 error = LOS_SemCreate(cnt, &pHandle->handle);
427 if (error == LOS_OK) {
428 *sem = (tls_os_sem_t *)pHandle;
429 os_status = TLS_OS_SUCCESS;
430 } else {
431 *sem = NULL;
432 os_status = TLS_OS_ERROR;
433 tls_mem_free(pHandle);
434 }
435
436 return os_status;
437 }
438
439 /*
440 *********************************************************************************************************
441 * DELETE A SEMAPHORE
442 *
443 * Description: This function deletes a semaphore and readies all tasks pending on the semaphore.
444 *
445 * Arguments : sem is a pointer to the event control block associated with the desired
446 * semaphore.
447 *
448 * Returns : TLS_OS_SUCCESS The call was successful and the semaphore was deleted
449 * TLS_OS_ERROR
450 *
451 *********************************************************************************************************
452 */
tls_os_sem_delete(tls_os_sem_t * sem)453 tls_os_status_t tls_os_sem_delete(tls_os_sem_t *sem)
454 {
455 u32 error;
456 tls_os_status_t os_status;
457 tls_os_handle_t *pHandle = (tls_os_handle_t *)sem;
458
459 error = LOS_SemDelete(pHandle->handle);
460 if (error == LOS_OK) {
461 os_status = TLS_OS_SUCCESS;
462 tls_mem_free(pHandle);
463 } else
464 os_status = TLS_OS_ERROR;
465
466 return os_status;
467 }
468
469 /*
470 *********************************************************************************************************
471 * PEND ON SEMAPHORE
472 *
473 * Description: This function waits for a semaphore.
474 *
475 * Arguments : sem is a pointer to the event control block associated with the desired
476 * semaphore.
477 *
478 * wait_time is an optional timeout period (in clock ticks). If non-zero, your task will
479 * wait for the resource up to the amount of time specified by this argument.
480 * If you specify 0, however, your task will wait forever at the specified
481 * semaphore or, until the resource becomes available (or the event occurs).
482 *
483 * Returns : TLS_OS_SUCCESS
484 * TLS_OS_ERROR
485 *********************************************************************************************************
486 */
tls_os_sem_acquire(tls_os_sem_t * sem,u32 wait_time)487 tls_os_status_t tls_os_sem_acquire(tls_os_sem_t *sem, u32 wait_time)
488 {
489 u8 error;
490 tls_os_status_t os_status;
491 unsigned int time;
492 tls_os_handle_t *pHandle = (tls_os_handle_t *)sem;
493
494 if (wait_time == 0)
495 time = LOS_WAIT_FOREVER;
496 else
497 time = wait_time;
498 error = LOS_SemPend(pHandle->handle, time);
499 if (error == LOS_OK)
500 os_status = TLS_OS_SUCCESS;
501 else
502 os_status = TLS_OS_ERROR;
503
504 return os_status;
505 }
506
507 /*
508 *********************************************************************************************************
509 * POST TO A SEMAPHORE
510 *
511 * Description: This function signals a semaphore
512 *
513 * Arguments : sem is a pointer to the event control block associated with the desired
514 * semaphore.
515 *
516 * Returns : TLS_OS_SUCCESS
517 * TLS_OS_ERROR
518 *********************************************************************************************************
519 */
tls_os_sem_release(tls_os_sem_t * sem)520 tls_os_status_t tls_os_sem_release(tls_os_sem_t *sem)
521 {
522 u8 error;
523 tls_os_status_t os_status;
524 tls_os_handle_t *pHandle = (tls_os_handle_t *)sem;
525
526 error = LOS_SemPost(pHandle->handle);
527 if (error == LOS_OK)
528 os_status = TLS_OS_SUCCESS;
529 else
530 os_status = TLS_OS_ERROR;
531
532 return os_status;
533 }
534
LOS_SemCount(UINT32 semHandle)535 UINT32 LOS_SemCount(UINT32 semHandle)
536 {
537 UINT32 intRet = 0;
538 UINT32 intSave;
539 LosSemCB *semPosted = GET_SEM(semHandle);
540 LosTaskCB *resumedTask = NULL;
541
542 if (semHandle >= LOSCFG_BASE_IPC_SEM_LIMIT) {
543 return intRet;
544 }
545
546 intSave = LOS_IntLock();
547
548 if (semPosted->semStat == OS_SEM_UNUSED) {
549 (void)LOS_IntRestore(intSave);
550 return intRet;
551 }
552
553 if (semPosted->maxSemCount == semPosted->semCount) {
554 (void)LOS_IntRestore(intSave);
555 return intRet;
556 }
557 intRet = semPosted->semCount;
558 (void)LOS_IntRestore(intSave);
559 return intRet;
560 }
561
562 /*
563 *********************************************************************************************************
564 * GET THE SEMPHORE COUNT
565 *
566 * Description: This function get the count of a semaphore
567 *
568 * Arguments : sem is a pointer to the event control block associated with the desired
569 * semaphore.
570 *
571 * Returns : the count number
572 *********************************************************************************************************
573 */
tls_os_sem_get_count(tls_os_sem_t * sem)574 u16 tls_os_sem_get_count(tls_os_sem_t *sem)
575 {
576 tls_os_handle_t *pHandle = (tls_os_handle_t *)sem;
577
578 return (u16)LOS_SemCount(pHandle->handle);
579 }
580
581 /*
582 *********************************************************************************************************
583 * CREATE A MESSAGE QUEUE
584 *
585 * Description: This function creates a message queue if free event control blocks are available.
586 *
587 * Arguments : queue is a pointer to the event control clock (OS_EVENT) associated with the
588 * created queue
589 *
590 * queue_start is a pointer to the base address of the message queue storage area. The
591 * storage area MUST be declared as an array of pointers to 'void' as follows
592 *
593 * void *MessageStorage[size]
594 *
595 * queue_size is the number of elements in the storage area
596 *
597 * msg_size
598 *
599 * Returns : TLS_OS_SUCCESS
600 * TLS_OS_ERROR
601 *********************************************************************************************************
602 */
tls_os_queue_create(tls_os_queue_t ** queue,u32 queue_size)603 tls_os_status_t tls_os_queue_create(tls_os_queue_t **queue, u32 queue_size)
604 {
605 tls_os_status_t os_status;
606 u32 ret;
607 UINT32 uwQueueID;
608 tls_os_handle_t *pHandle = tls_mem_alloc(sizeof(tls_os_handle_t));
609 if (!pHandle) {
610 printf("%s: malloc error\n", __FUNCTION__);
611 return TLS_OS_ERROR;
612 }
613
614 ret = LOS_QueueCreate(NULL, queue_size, &uwQueueID, 0, 4); // 4:queue size
615 if (ret == LOS_OK) {
616 pHandle->handle = uwQueueID;
617 *queue = (tls_os_queue_t *)pHandle;
618 os_status = TLS_OS_SUCCESS;
619 } else {
620 *queue = NULL;
621 os_status = TLS_OS_ERROR;
622 tls_mem_free(pHandle);
623 }
624 printf("%s: %d\n", __FUNCTION__, ret);
625 return os_status;
626 }
627
628 /*
629 *********************************************************************************************************
630 * DELETE A MESSAGE QUEUE
631 *
632 * Description: This function deletes a message queue and readies all tasks pending on the queue.
633 *
634 * Arguments : queue is a pointer to the event control block associated with the desired
635 * queue.
636 *
637 *
638 * Returns : TLS_OS_SUCCESS
639 * TLS_OS_ERROR
640 *********************************************************************************************************
641 */
tls_os_queue_delete(tls_os_queue_t * queue)642 tls_os_status_t tls_os_queue_delete(tls_os_queue_t *queue)
643 {
644 tls_os_status_t os_status;
645 u32 ret;
646 tls_os_handle_t *pHandle = (tls_os_handle_t *)queue;
647
648 ret = LOS_QueueDelete(pHandle->handle);
649 if (ret == LOS_OK) {
650 os_status = TLS_OS_SUCCESS;
651 tls_mem_free(pHandle);
652 } else {
653 os_status = TLS_OS_ERROR;
654 }
655 printf("%s: %d\n", __FUNCTION__, ret);
656
657 return os_status;
658 }
659
660 /*
661 *********************************************************************************************************
662 * POST MESSAGE TO A QUEUE
663 *
664 * Description: This function sends a message to a queue
665 *
666 * Arguments : queue is a pointer to the event control block associated with the desired queue
667 *
668 * msg is a pointer to the message to send.
669 *
670 * msg_size
671 * Returns : TLS_OS_SUCCESS
672 * TLS_OS_ERROR
673 *********************************************************************************************************
674 */
tls_os_queue_send(tls_os_queue_t * queue,void * msg,u32 msg_size)675 tls_os_status_t tls_os_queue_send(tls_os_queue_t *queue, void *msg, u32 msg_size)
676 {
677 u32 ret;
678 tls_os_status_t os_status;
679 tls_os_handle_t *pHandle = (tls_os_handle_t *)queue;
680
681 if (msg_size == 0) {
682 msg_size = sizeof(void *);
683 }
684
685 ret = LOS_QueueWriteCopy(pHandle->handle, &msg, msg_size, 0);
686 if (ret == LOS_OK)
687 os_status = TLS_OS_SUCCESS;
688 else {
689 os_status = TLS_OS_ERROR;
690 printf("tls_os_queue_send ret %d\n", ret);
691 }
692 return os_status;
693 }
694
695 /*
696 *********************************************************************************************************
697 * PEND ON A QUEUE FOR A MESSAGE
698 *
699 * Description: This function waits for a message to be sent to a queue
700 *
701 * Arguments : queue is a pointer to the event control block associated with the desired queue
702 *
703 * msg is a pointer to the message received
704 *
705 * msg_size
706 *
707 * wait_time is an optional timeout period (in clock ticks). If non-zero, your task will
708 * wait for a message to arrive at the queue up to the amount of time
709 * specified by this argument. If you specify 0, however, your task will wait
710 * forever at the specified queue or, until a message arrives.
711 *
712 * Returns : TLS_OS_SUCCESS
713 * TLS_OS_ERROR
714 *********************************************************************************************************
715 */
tls_os_queue_receive(tls_os_queue_t * queue,void ** msg,u32 msg_size,u32 wait_time)716 tls_os_status_t tls_os_queue_receive(tls_os_queue_t *queue, void **msg, u32 msg_size, u32 wait_time)
717 {
718 u32 ret;
719 tls_os_status_t os_status;
720 tls_os_handle_t *pHandle = (tls_os_handle_t *)queue;
721
722 if (wait_time == 0)
723 wait_time = LOS_WAIT_FOREVER;
724
725 if (msg_size == 0) {
726 msg_size = sizeof(void *);
727 }
728 ret = LOS_QueueReadCopy(pHandle->handle, msg, msg_size, wait_time);
729 if (ret == LOS_OK) {
730 os_status = TLS_OS_SUCCESS;
731 } else {
732 os_status = TLS_OS_ERROR;
733 if (ret != LOS_ERRNO_QUEUE_TIMEOUT)
734 printf("%s: %d\n", __FUNCTION__, ret);
735 }
736
737 return os_status;
738 }
739
740 /*
741 *********************************************************************************************************
742 * FLUSH QUEUE
743 *
744 * Description : This function is used to flush the contents of the message queue.
745 *
746 * Arguments : none
747 *
748 * Returns : TLS_OS_SUCCESS
749 * TLS_OS_ERROR
750 * At present, no use for freeRTOS
751 *********************************************************************************************************
752 */
tls_os_queue_flush(tls_os_queue_t * queue)753 tls_os_status_t tls_os_queue_flush(tls_os_queue_t *queue)
754 {
755 return TLS_OS_SUCCESS;
756 }
757
758 /*
759 *********************************************************************************************************
760 * GET CURRENT SYSTEM TIME
761 *
762 * Description: This function is used by your application to obtain the current value of the 32-bit
763 * counter which keeps track of the number of clock ticks.
764 *
765 * Arguments : none
766 *
767 * Returns : The current value of OSTime
768 *********************************************************************************************************
769 */
tls_os_get_time(void)770 u32 tls_os_get_time(void)
771 {
772 UINT64 cycle = LOS_SysCycleGet();
773 UINT64 nowNsec = (cycle / OS_SYS_CLOCK) * OS_SYS_NS_PER_SECOND +
774 (cycle % OS_SYS_CLOCK) * OS_SYS_NS_PER_SECOND / OS_SYS_CLOCK;
775
776 UINT32 tv_sec = nowNsec * HZ / OS_SYS_NS_PER_SECOND;
777 return tv_sec;
778 }
779
780 /**********************************************************************************************************
781 * Description: Disable interrupts by preserving the state of interrupts.
782 *
783 * Arguments : none
784 *
785 * Returns : cpu_sr
786 ***********************************************************************************************************/
tls_os_set_critical(void)787 u32 tls_os_set_critical(void)
788 {
789 return LOS_IntLock();
790 }
791
792 /**********************************************************************************************************
793 * Description: Enable interrupts by preserving the state of interrupts.
794 *
795 * Arguments : cpu_sr
796 *
797 * Returns : none
798 ***********************************************************************************************************/
tls_os_release_critical(u32 cpu_sr)799 void tls_os_release_critical(u32 cpu_sr)
800 {
801 (void)LOS_IntRestore(cpu_sr);
802 return;
803 }
804
805 typedef struct {
806 void *arg;
807 TLS_OS_TIMER_CALLBACK callback_fn;
808 u32 swtmrId;
809 }tls_swtmr_cb_t;
810
tls_swtmr_common_callback(u32 para)811 static void tls_swtmr_common_callback(u32 para)
812 {
813 tls_swtmr_cb_t *tmr_cb = (tls_swtmr_cb_t *)para;
814 if (tmr_cb == NULL || tmr_cb->callback_fn == NULL) {
815 return;
816 }
817
818 tmr_cb->callback_fn(tmr_cb, tmr_cb->arg);
819 }
820
821 /*
822 ************************************************************************************************************************
823 * CREATE A TIMER
824 *
825 * Description: This function is called by your application code to create a timer.
826 *
827 * Arguments : timer A pointer to an OS_TMR data structure.This is the 'handle' that your application
828 * will use to reference the timer created.
829 *
830 * callback Is a pointer to a callback function that will be called when the timer expires. The
831 * callback function must be declared as follows:
832 *
833 * void MyCallback (OS_TMR *ptmr, void *p_arg);
834 *
835 * callback_arg Is an argument (a pointer) that is passed to the callback function when it is called.
836 *
837 * period The 'period' being repeated for the timer.
838 * If you specified 'OS_TMR_OPT_PERIODIC' as an option, when the timer expires, it will
839 * automatically restart with the same period.
840 *
841 * repeat if repeat
842 *
843 * pname Is a pointer to an ASCII string that is used to name the timer. Names are useful for
844 * debugging.
845 *
846 * Returns : TLS_OS_SUCCESS
847 * TLS_OS_ERROR
848 ************************************************************************************************************************
849 */
tls_os_timer_create(tls_os_timer_t ** timer,TLS_OS_TIMER_CALLBACK callback,void * callback_arg,u32 period,bool repeat,u8 * name)850 tls_os_status_t tls_os_timer_create(tls_os_timer_t **timer,
851 TLS_OS_TIMER_CALLBACK callback,
852 void *callback_arg,
853 u32 period,
854 bool repeat,
855 u8 *name)
856 {
857 u32 ret;
858 u32 uwTimerID;
859 tls_os_status_t os_status;
860 UINT8 ucMode;
861
862 if (period == 0)
863 period = 1;
864
865 if (repeat)
866 ucMode = LOS_SWTMR_MODE_PERIOD;
867 else
868 ucMode = LOS_SWTMR_MODE_NO_SELFDELETE;
869
870 tls_swtmr_cb_t *swtmr_cb = tls_mem_alloc(sizeof(tls_swtmr_cb_t));
871 if (!swtmr_cb) {
872 printf("%s: malloc error\n", __FUNCTION__);
873 return TLS_OS_ERROR;
874 }
875 swtmr_cb->callback_fn = callback;
876 swtmr_cb->arg = callback_arg;
877
878 #if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
879 ret = LOS_SwtmrCreate(period, ucMode, tls_swtmr_common_callback, &uwTimerID,
880 swtmr_cb, OS_SWTMR_ROUSES_IGNORE, OS_SWTMR_ALIGN_SENSITIVE);
881 #else
882 ret = LOS_SwtmrCreate(period, ucMode, tls_swtmr_common_callback, &uwTimerID, swtmr_cb);
883 #endif
884 if (ret == LOS_OK) {
885 *timer = (tls_os_timer_t *)swtmr_cb;
886 swtmr_cb->swtmrId = uwTimerID;
887 os_status = TLS_OS_SUCCESS;
888 } else {
889 tls_mem_free(swtmr_cb);
890 os_status = TLS_OS_ERROR;
891 printf("%s: %d\n", __FUNCTION__, ret);
892 }
893 return os_status;
894 }
895
896 /*
897 ************************************************************************************************************************
898 * START A TIMER
899 *
900 * Description: This function is called by your application code to start a timer.
901 *
902 * Arguments : timer Is a pointer to an OS_TMR
903 *
904 ************************************************************************************************************************
905 */
tls_os_timer_start(tls_os_timer_t * timer)906 tls_os_status_t tls_os_timer_start(tls_os_timer_t *timer)
907 {
908 u32 ret;
909 u32 uwTimerID;
910 tls_swtmr_cb_t *swtmr_cb = (tls_swtmr_cb_t *)timer;
911 if (swtmr_cb == NULL) {
912 return;
913 }
914 uwTimerID = swtmr_cb->swtmrId;
915 ret = LOS_SwtmrStart(uwTimerID);
916 if (ret == LOS_OK) {
917 return TLS_OS_SUCCESS;
918 } else {
919 return TLS_OS_ERROR;
920 }
921 }
922
923 /*
924 ************************************************************************************************************************
925 * CHANGE A TIMER WAIT TIME
926 *
927 * Description: This function is called by your application code to change a timer wait time.
928 *
929 * Arguments : timer Is a pointer to an OS_TMR
930 *
931 * ticks is the wait time
932 ************************************************************************************************************************
933 */
934 extern LITE_OS_SEC_BSS SWTMR_CTRL_S *g_swtmrCBArray;
935
936 #define SET_PERIOD(usSwTmrID, uvIntSave, pstSwtmr, ticks) { \
937 do { \
938 uvIntSave = LOS_IntLock(); \
939 (pstSwtmr) = g_swtmrCBArray + (usSwTmrID) % LOSCFG_BASE_CORE_SWTMR_LIMIT; \
940 if ((pstSwtmr)->usTimerID % LOSCFG_BASE_CORE_SWTMR_LIMIT != (usSwTmrID) % LOSCFG_BASE_CORE_SWTMR_LIMIT) \
941 { \
942 (void)LOS_IntRestore((uvIntSave)); \
943 printf("0x%x-%d-%d", (u32)(pstSwtmr), (pstSwtmr)->usTimerID, (usSwTmrID) ); \
944 assert(0); \
945 } \
946 (pstSwtmr)->uwInterval = (ticks); \
947 (void)LOS_IntRestore(uvIntSave); \
948 } while (0); \
949 }
950
tls_os_timer_change(tls_os_timer_t * timer,u32 ticks)951 tls_os_status_t tls_os_timer_change(tls_os_timer_t *timer, u32 ticks)
952 {
953 // must need
954 tls_swtmr_cb_t *swtmr_cb = (tls_swtmr_cb_t *)timer;
955 if (swtmr_cb == NULL) {
956 return;
957 }
958
959 UINT16 usSwTmrID = swtmr_cb->swtmrId;
960 UINTPTR uvIntSave;
961 SWTMR_CTRL_S *pstSwtmr;
962 UINT32 err = 0;
963 tls_os_status_t os_status;
964 if (ticks == 0)
965 ticks = 1;
966 err = LOS_SwtmrStop(usSwTmrID);
967 SET_PERIOD(usSwTmrID, uvIntSave, pstSwtmr, ticks);
968 err = LOS_SwtmrStart(usSwTmrID);
969 if (err == LOS_OK)
970 os_status = TLS_OS_SUCCESS;
971 else
972 os_status = TLS_OS_ERROR;
973
974 return os_status;
975 }
976
977 // /< return 1 active while 0 not active
LOS_SwtmrIsActive(UINT32 swtmrId)978 int LOS_SwtmrIsActive(UINT32 swtmrId)
979 {
980 SWTMR_CTRL_S *swtmr = NULL;
981 UINTPTR intSave;
982 UINT16 swtmrCbId;
983 int ret = 0;
984
985 if (swtmrId >= 0xFFFFFFFF) {
986 return ret;
987 }
988 intSave = LOS_IntLock();
989 swtmrCbId = swtmrId % LOSCFG_BASE_CORE_SWTMR_LIMIT;
990 swtmr = g_swtmrCBArray + swtmrCbId;
991 if (swtmr->usTimerID != swtmrId) {
992 (void)LOS_IntRestore(intSave);
993 return ret;
994 }
995 if (swtmr->ucState == OS_SWTMR_STATUS_TICKING) {
996 ret = 1;
997 }
998 (void)LOS_IntRestore(intSave);
999 return ret;
1000 }
1001
1002 /*
1003 ************************************************************************************************************************
1004 * STOP A TIMER
1005 *
1006 * Description: This function is called by your application code to stop a timer.
1007 *
1008 * Arguments : timer Is a pointer to the timer to stop.
1009 *
1010 ************************************************************************************************************************
1011 */
tls_os_timer_stop(tls_os_timer_t * timer)1012 tls_os_status_t tls_os_timer_stop(tls_os_timer_t *timer)
1013 {
1014 u32 ret;
1015 u32 uwTimerID;
1016 tls_swtmr_cb_t *swtmr_cb = (tls_swtmr_cb_t *)timer;
1017 if (swtmr_cb == NULL) {
1018 return;
1019 }
1020 tls_os_status_t os_status;
1021 uwTimerID = swtmr_cb->swtmrId;
1022 ret = LOS_SwtmrStop(uwTimerID);
1023 if (ret == LOS_OK) {
1024 os_status = TLS_OS_SUCCESS;
1025 } else {
1026 os_status = TLS_OS_ERROR;
1027 }
1028
1029 return os_status;
1030 }
1031
1032 /*
1033 ************************************************************************************************************************
1034 * Delete A TIMER
1035 *
1036 * Description: This function is called by your application code to delete a timer.
1037 *
1038 * Arguments : timer Is a pointer to the timer to delete.
1039 *
1040 ************************************************************************************************************************
1041 */
tls_os_timer_delete(tls_os_timer_t * timer)1042 tls_os_status_t tls_os_timer_delete(tls_os_timer_t *timer)
1043 {
1044 u32 ret = 0;
1045 u32 uwTimerID;
1046 tls_swtmr_cb_t *swtmr_cb = (tls_swtmr_cb_t *)timer;
1047 if (swtmr_cb == NULL) {
1048 return TLS_OS_ERROR;
1049 }
1050 uwTimerID = swtmr_cb->swtmrId;
1051 tls_os_status_t os_status;
1052 (void)LOS_SwtmrStop(uwTimerID);
1053 ret = LOS_SwtmrDelete(uwTimerID);
1054 if (ret == LOS_OK) {
1055 os_status = TLS_OS_SUCCESS;
1056 tls_mem_free(swtmr_cb);
1057 } else {
1058 os_status = TLS_OS_ERROR;
1059 }
1060 return os_status;
1061 }
1062
1063 /*
1064 ************************************************************************************************************************
1065 * Query the timer status:active or not
1066 *
1067 * Description: This function is called by your application code to query the timer status
1068 *
1069 * Arguments : timer Is a pointer to the timer to query
1070 *
1071 ************************************************************************************************************************
1072 */
1073
tls_os_timer_active(tls_os_timer_t * timer)1074 u8 tls_os_timer_active(tls_os_timer_t *timer)
1075 {
1076 u32 uwTimerID;
1077 tls_swtmr_cb_t *swtmr_cb = (tls_swtmr_cb_t *)timer;
1078 if (swtmr_cb == NULL) {
1079 return 0;
1080 }
1081 uwTimerID = swtmr_cb->swtmrId;
1082 return (u8)LOS_SwtmrIsActive(uwTimerID);
1083 }
1084
tls_os_timer_expirytime(tls_os_timer_t * timer)1085 u32 tls_os_timer_expirytime(tls_os_timer_t *timer)
1086 {
1087 u32 ret = 0;
1088 u32 uwTimerID;
1089 tls_swtmr_cb_t *swtmr_cb = (tls_swtmr_cb_t *)timer;
1090 if (swtmr_cb == NULL) {
1091 return 0;
1092 }
1093 uwTimerID = swtmr_cb->swtmrId;
1094 if (LOS_OK == LOS_SwtmrTimeGet(uwTimerID, &ret)) {
1095 ret += tls_os_get_time();
1096 }
1097 return ret;
1098 }
1099
1100 /*
1101 *********************************************************************************************************
1102 * DELAY TASK 'n' TICKS
1103 *
1104 * Description: This function is called to delay execution of the currently running task until the
1105 * specified number of system ticks expires. This, of course, directly equates to delaying
1106 * the current task for some time to expire. No delay will result If the specified delay is
1107 * 0. If the specified delay is greater than 0 then, a context switch will result.
1108 *
1109 * Arguments : ticks is the time delay that the task will be suspended in number of clock 'ticks'.
1110 * Note that by specifying 0, the task will not be delayed.
1111 *
1112 * Returns : none
1113 *********************************************************************************************************
1114 */
tls_os_time_delay(u32 ticks)1115 void tls_os_time_delay(u32 ticks)
1116 {
1117 (void)LOS_TaskDelay(ticks);
1118 }
1119
1120 /*
1121 *********************************************************************************************************
1122 * task stat info
1123 *
1124 * Description: This function is used to display stat info
1125 *
1126 * Returns : none
1127 *********************************************************************************************************
1128 */
tls_os_disp_task_stat_info(void)1129 void tls_os_disp_task_stat_info(void)
1130 {
1131 (void)LOS_TaskInfoMonitor();
1132 }
1133
1134 /*
1135 *********************************************************************************************************
1136 * OS INIT function
1137 *
1138 * Description: This function is used to init os common resource
1139 *
1140 * Arguments : None;
1141 *
1142 * Returns : None
1143 *********************************************************************************************************
1144 */
tls_os_init(void * arg)1145 void tls_os_init(void *arg)
1146 {
1147 (void)LOS_KernelInit();
1148 }
1149
1150 /*
1151 *********************************************************************************************************
1152 * OS scheduler start function
1153 *
1154 * Description: This function is used to start task schedule
1155 *
1156 * Arguments : None;
1157 *
1158 * Returns : None
1159 *********************************************************************************************************
1160 */
tls_os_start_scheduler(void)1161 void tls_os_start_scheduler(void)
1162 {
1163 (void)LOS_Start();
1164 }
1165
1166 /*
1167 *********************************************************************************************************
1168 * Get OS TYPE
1169 *
1170 * Description: This function is used to get OS type
1171 *
1172 * Arguments : None;
1173 *
1174 * Returns : TLS_OS_TYPE
1175 * OS_UCOSII = 0,
1176 * OS_FREERTOS = 1,
1177 *********************************************************************************************************
1178 */
tls_os_get_type(void)1179 int tls_os_get_type(void)
1180 {
1181 return (int)OS_LITEOS;
1182 }
1183
1184 /*
1185 *********************************************************************************************************
1186 * OS tick handler
1187 *
1188 * Description: This function is tick handler.
1189 *
1190 * Arguments : None;
1191 *
1192 * Returns : None
1193 *********************************************************************************************************
1194 */
tls_os_time_tick(void * p)1195 void tls_os_time_tick(void *p) {
1196 }
1197
CK_IN_INTRP(void)1198 static uint32_t CK_IN_INTRP(void)
1199 {
1200 uint32_t vec = 0;
1201 asm volatile(
1202 "mfcr %0, psr \n"
1203 "lsri %0, 16\n"
1204 "sextb %0\n"
1205 :"=r"(vec):);
1206
1207 if (vec >= 32 || (vec == 10)) { // 32:Analyzing conditions, 10:value of vec
1208 return 1;
1209 } else {
1210 return 0;
1211 }
1212 }
1213
1214 /**
1215 * @brief get isr count
1216 *
1217 * @param[in] None
1218 *
1219 * @retval count
1220 *
1221 * @note None
1222 */
1223
tls_get_isr_count(void)1224 u8 tls_get_isr_count(void)
1225 {
1226 return (u8)CK_IN_INTRP();
1227 }
1228
PortSaveLocalPSR(void)1229 long PortSaveLocalPSR(void)
1230 {
1231 return SaveLocalPSR();
1232 }
1233
PortRestoreLocalPSR(long ulDummy)1234 void PortRestoreLocalPSR(long ulDummy)
1235 {
1236 RestoreLocalPSR(ulDummy);
1237 }
1238
PortEnableInterrupt(void)1239 void PortEnableInterrupt(void)
1240 {
1241 portEnableInterrupt();
1242 }
1243
PorDisableInterrupt(void)1244 void PorDisableInterrupt(void)
1245 {
1246 portDisableInterrupt();
1247 }
1248
1249 static int ulCriticalNesting = 0;
vPortEnterCritical(void)1250 void vPortEnterCritical(void)
1251 {
1252 portDisableInterrupt();
1253 ulCriticalNesting ++;
1254 }
1255
vPortExitCritical(void)1256 void vPortExitCritical(void)
1257 {
1258 if (ulCriticalNesting == 0) {
1259 while (1) {};
1260 }
1261
1262 ulCriticalNesting --;
1263 if (ulCriticalNesting == 0) {
1264 portEnableInterrupt();
1265 }
1266 }
1267
1268 extern BOOL g_taskScheduled;
tls_os_task_schedule_state()1269 u8 tls_os_task_schedule_state()
1270 {
1271 return (u8)g_taskScheduled;
1272 }
1273
tls_os_task_id()1274 tls_os_task_t tls_os_task_id()
1275 {
1276 return (tls_os_task_t)LOS_CurTaskIDGet();
1277 }
1278
HalHwiHandleReInit(UINT32 hwiFormAddr)1279 void HalHwiHandleReInit(UINT32 hwiFormAddr)
1280 {
1281 HWI_PROC_FUNC *p_hwiForm = (HWI_PROC_FUNC *)hwiFormAddr;
1282
1283 p_hwiForm[0] = (HWI_PROC_FUNC)Reset_Handler;
1284 p_hwiForm[PendSV_IRQn] = (HWI_PROC_FUNC)tspend_handler;
1285 }
1286
1287 #endif /* end of WM_OS_LITEOS_H */