Initial commit: TXW82x FPV v2.7.0.7-42229 SDK + project sources

This commit is contained in:
2026-07-06 11:30:13 +08:00
commit e76462eeb7
3451 changed files with 1415300 additions and 0 deletions

84
sdk/osal/csky/condv.c Normal file
View File

@@ -0,0 +1,84 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/string.h"
#include "osal/semaphore.h"
#include "osal/mutex.h"
#include "osal/condv.h"
#ifdef CSKY_OS
#include "csi_kernel.h"
#include <k_api.h>
#define CONDV_MAGIC (0x3a8d7c1d)
int32 os_condv_init(os_condv_t *cond)
{
if(cond->magic == CONDV_MAGIC){
os_printf(KERN_WARNING"condv repeat initialization ????\r\n");
}
cond->sema = csi_kernel_sem_new(65535, 0);
if (cond->sema) {
cond->magic = CONDV_MAGIC;
atomic_set(&cond->waitings, 0);
}
return (cond->sema ? RET_OK : RET_ERR);
}
int32 os_condv_broadcast(os_condv_t *cond)
{
uint32 i;
uint32 wait = atomic_read(&cond->waitings);
ASSERT(cond && cond->magic == CONDV_MAGIC);
while (wait > 0) {
if (wait == atomic_cmpxchg(&cond->waitings, wait, 0)) {
for (i = 0; i < wait; i++) {
csi_kernel_sem_post(cond->sema);
}
}
wait = atomic_read(&cond->waitings);
}
return RET_OK;
}
int32 os_condv_signal(os_condv_t *cond)
{
uint32 wait = atomic_read(&cond->waitings);
ASSERT(cond && cond->magic == CONDV_MAGIC);
while (wait > 0) {
if (wait == atomic_cmpxchg(&cond->waitings, wait, wait - 1)) {
csi_kernel_sem_post(cond->sema);
break;
}
wait = atomic_read(&cond->waitings);
}
return RET_OK;
}
int32 os_condv_del(os_condv_t *cond)
{
ASSERT(cond && cond->magic == CONDV_MAGIC);
csi_kernel_sem_del(cond->sema);
cond->sema = NULL;
cond->magic = 0;
return RET_OK;
}
int32 os_condv_wait(os_condv_t *cond, os_mutex_t *mutex, uint32 tmo_ms)
{
int32 ret;
ASSERT(cond && cond->magic == CONDV_MAGIC);
atomic_inc(&cond->waitings);
os_mutex_unlock(mutex);
ret = csi_kernel_sem_wait(cond->sema, tmo_ms);
//os_printf(KERN_NOTICE"condv 0x%x waitings:%d, tmo_ms:%d, ret:%d\r\n", cond, atomic_read(&cond->waitings), tmo_ms, ret);
os_mutex_lock(mutex, osWaitForever);
atomic_dec(&cond->waitings);
return ret ? -ETIMEDOUT : RET_OK;
}
#endif

72
sdk/osal/csky/event.c Normal file
View File

@@ -0,0 +1,72 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/string.h"
#include "osal/event.h"
#ifdef CSKY_OS
#include "csi_kernel.h"
#define EVENT_MAGIC (0xa67b3cd4)
int32 os_event_init(os_event_t *evt)
{
if(evt->magic == EVENT_MAGIC){
os_printf(KERN_WARNING"event repeat initialization ????\r\n");
}
evt->hdl = csi_kernel_event_new();
ASSERT(evt->hdl);
if (evt->hdl) {
evt->magic = EVENT_MAGIC;
}
return (evt->hdl ? RET_OK : RET_ERR);
}
int32 os_event_del(os_event_t *evt)
{
ASSERT(evt && evt->magic == EVENT_MAGIC);
int32 ret = csi_kernel_event_del(evt->hdl);
evt->hdl = NULL;
evt->magic = 0;
return ret;
}
int32 os_event_set(os_event_t *evt, uint32 flags, uint32 *rflags)
{
uint32 _rflags;
ASSERT(evt && evt->magic == EVENT_MAGIC);
if(rflags == NULL) rflags = &_rflags;
return csi_kernel_event_set(evt->hdl, flags, (uint32_t *)rflags);
}
int32 os_event_clear(os_event_t *evt, uint32 flags, uint32 *rflags)
{
uint32 _rflags;
ASSERT(evt && evt->magic == EVENT_MAGIC);
if(rflags == NULL) rflags = &_rflags;
return csi_kernel_event_clear(evt->hdl, flags, (uint32_t *)rflags);
}
int32 os_event_get(os_event_t *evt, uint32 *rflags)
{
ASSERT(evt && evt->magic == EVENT_MAGIC);
return csi_kernel_event_get(evt->hdl, (uint32_t *)rflags);
}
int32 os_event_wait(os_event_t *evt, uint32 flags, uint32 *rflags, uint32 mode, int32 timeout)
{
uint32 _rflags;
k_event_opt_t options = KEVENT_OPT_SET_ALL;
uint8_t clr_on_exit = (mode & OS_EVENT_WMODE_CLEAR) ? 1 : 0;
ASSERT(evt && evt->magic == EVENT_MAGIC);
if (mode & OS_EVENT_WMODE_OR) {
options = KEVENT_OPT_SET_ANY;
}
if(rflags == NULL) rflags = &_rflags;
return csi_kernel_event_wait(evt->hdl, flags, options, clr_on_exit, (uint32_t *)rflags, timeout);
}
#endif

65
sdk/osal/csky/msgqueue.c Normal file
View File

@@ -0,0 +1,65 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/string.h"
#include "osal/msgqueue.h"
#ifdef CSKY_OS
#include "csi_kernel.h"
#define MSGQ_MAGIC (0x4a8b1c9d)
int32 os_msgq_init(os_msgqueue_t *msgq, int32 size)
{
if(msgq->magic == MSGQ_MAGIC){
os_printf(KERN_WARNING"msgq repeat initialization ????\r\n");
}
msgq->hdl = csi_kernel_msgq_new(size, sizeof(uint32));
if(msgq->hdl) msgq->magic = MSGQ_MAGIC;
return (msgq->hdl ? RET_OK : RET_ERR);
}
uint32 os_msgq_get(os_msgqueue_t *msgq, int32 tmo_ms)
{
uint32 val = 0;
ASSERT(msgq->hdl);
csi_kernel_msgq_get(msgq->hdl, &val, csi_kernel_ms2tick(tmo_ms));
return val;
}
uint32 os_msgq_get2(struct os_msgqueue *msgq, int32 tmo_ms, int32 *err)
{
uint32 val = 0;
k_status_t ret = csi_kernel_msgq_get(msgq->hdl, &val, csi_kernel_ms2tick(tmo_ms));
if(err) *err = ret;
return val;
}
int32 os_msgq_put(os_msgqueue_t *msgq, uint32 data, int32 tmo_ms)
{
ASSERT(msgq->hdl);
return csi_kernel_msgq_put(msgq->hdl, &data, 0, csi_kernel_ms2tick(tmo_ms));
}
int32 os_msgq_put_head(os_msgqueue_t *msgq, uint32 data, int32 tmo_ms)
{
ASSERT(msgq->hdl);
return csi_kernel_msgq_put(msgq->hdl, &data, 0, csi_kernel_ms2tick(tmo_ms));
}
int32 os_msgq_del(os_msgqueue_t *msgq)
{
ASSERT(msgq->hdl);
csi_kernel_msgq_del(msgq->hdl);
msgq->hdl = NULL;
msgq->magic = 0;
return RET_OK;
}
int32 os_msgq_cnt(os_msgqueue_t *msgq)
{
return csi_kernel_msgq_get_count(msgq->hdl);
}
#endif

52
sdk/osal/csky/mutex.c Normal file
View File

@@ -0,0 +1,52 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/string.h"
#include "osal/mutex.h"
#ifdef CSKY_OS
#include "csi_kernel.h"
#define MUTEX_MAGIC (0xa8b4c2d5)
int32 os_mutex_init(os_mutex_t *mutex)
{
if(mutex->magic == MUTEX_MAGIC){
os_printf(KERN_WARNING"mutex repeat initialization ????\r\n");
}
mutex->hdl = csi_kernel_mutex_new();
if(mutex->hdl) mutex->magic = MUTEX_MAGIC;
return (mutex->hdl ? RET_OK : RET_ERR);
}
int32 os_mutex_lock(os_mutex_t *mutex, int32 tmo)
{
ASSERT(mutex && mutex->hdl);
return csi_kernel_mutex_lock(mutex->hdl, csi_kernel_ms2tick(tmo), (uint32_t)RETURN_ADDR());
}
int32 os_mutex_unlock(os_mutex_t *mutex)
{
ASSERT(mutex && mutex->hdl);
return csi_kernel_mutex_unlock(mutex->hdl);
}
int32 os_mutex_del(os_mutex_t *mutex)
{
int32 ret = 0;
ASSERT(mutex && mutex->hdl);
ret = csi_kernel_mutex_del(mutex->hdl);
mutex->hdl = NULL;
mutex->magic = 0;
return ret;
}
void *os_mutex_owner(os_mutex_t *mutex)
{
ASSERT(mutex && mutex->hdl);
return csi_kernel_mutex_get_owner(mutex->hdl);
}
#endif

64
sdk/osal/csky/semaphore.c Normal file
View File

@@ -0,0 +1,64 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/string.h"
#include "osal/semaphore.h"
#ifdef CSKY_OS
#include "csi_kernel.h"
#include <k_api.h>
#define SEMA_MAGIC (0x3a8d7c1d)
int32 os_sema_init(os_semaphore_t *sem, int32 val)
{
if (sem->magic == SEMA_MAGIC) {
os_printf(KERN_WARNING"sem repeat initialization ????\r\n");
}
sem->hdl = csi_kernel_sem_new(65535, val);
if (sem->hdl) { sem->magic = SEMA_MAGIC; }
return (sem->hdl ? RET_OK : RET_ERR);
}
int32 os_sema_down(os_semaphore_t *sem, int32 tmo_ms)
{
int32 ret = 0;
ASSERT(sem && sem->hdl);
ret = csi_kernel_sem_wait(sem->hdl, csi_kernel_ms2tick(tmo_ms));
return (ret == RHINO_SUCCESS) ? 1 : 0;
}
int32 os_sema_up(os_semaphore_t *sem)
{
ASSERT(sem && sem->hdl);
return csi_kernel_sem_post(sem->hdl);
}
int32 os_sema_del(os_semaphore_t *sem)
{
ASSERT(sem && sem->hdl);
int32 ret = csi_kernel_sem_del(sem->hdl);
sem->hdl = NULL;
sem->magic = 0;
return ret;
}
int32 os_sema_count(os_semaphore_t *sem)
{
return (sem && sem->hdl) ? csi_kernel_sem_get_count(sem->hdl) : 0;
}
void os_sema_eat(os_semaphore_t *sem)
{
int32 ret = 0;
while (os_sema_count(sem) > 0) {
ret = os_sema_down(sem, 0);
if (!ret) {
break;
}
}
}
#endif

22
sdk/osal/csky/sleep.c Normal file
View File

@@ -0,0 +1,22 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/sleep.h"
#ifdef CSKY_OS
#include "csi_kernel.h"
#include <k_api.h>
#include <sys/time.h>
void os_sleep(int32 sec)
{
csi_kernel_delay(csi_kernel_ms2tick(sec * 1000));
}
void os_sleep_ms(int32 msec)
{
csi_kernel_delay_ms(msec);
}
#endif

270
sdk/osal/csky/task.c Normal file
View File

@@ -0,0 +1,270 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/string.h"
#include "osal/task.h"
#ifdef CSKY_OS
#include <csi_kernel.h>
#include <k_api.h>
extern uint32_t csi_kernel_task_runtime(struct os_task_info *tsk_rumtime, int count);
extern void csi_kernel_task_dump(k_task_handle_t task_handle, void *stack);
extern void csi_kernel_task_print(void);
extern int csi_kernel_task_count(void);
static void os_task_entry(void *args)
{
os_task_t *task = (os_task_t *)args;
task->func((void *)task->args);
}
int32 os_task_init(const uint8 *name, os_task_t *task, os_task_func_t func, uint32 data)
{
//ASSERT(task);
os_memset(task, 0, sizeof(os_task_t));
task->args = data;
task->func = func;
task->name = (const char *)name;
return RET_OK;
}
int32 os_task_priority(os_task_t *task)
{
//ASSERT(task);
return task->priority;
}
int32 os_task_priority2(void *hdl)
{
return csi_kernel_task_get_prio(hdl);
}
int32 os_task_stacksize(os_task_t *task)
{
//ASSERT(task);
return task->stack_size;
}
int32 os_task_stacksize2(void *hdl)
{
return csi_kernel_task_get_stack_size(hdl);
}
int32 _os_task_set_priority(os_task_t *task, uint32 prio)
{
int32 pri = KPRIO_NORMAL;
uint8 priority = prio & 0xff;
//ASSERT(task);
//ASSERT(!task->hdl);
if (priority < OS_TASK_PRIORITY_LOW) {
pri = KPRIO_LOW0;
} else if (priority < OS_TASK_PRIORITY_BELOW_NORMAL) {
pri = KPRIO_LOW0 + (priority - OS_TASK_PRIORITY_LOW);
if (pri > KPRIO_LOW7) { pri = KPRIO_LOW7; }
} else if (priority >= OS_TASK_PRIORITY_BELOW_NORMAL && priority < OS_TASK_PRIORITY_NORMAL) {
pri = KPRIO_NORMAL_BELOW0 + (priority - OS_TASK_PRIORITY_BELOW_NORMAL);
if (pri > KPRIO_NORMAL_BELOW7) { pri = KPRIO_NORMAL_BELOW7; }
} else if (priority >= OS_TASK_PRIORITY_NORMAL && priority < OS_TASK_PRIORITY_ABOVE_NORMAL) {
pri = KPRIO_NORMAL + (priority - OS_TASK_PRIORITY_NORMAL);
if (pri > KPRIO_NORMAL7) { pri = KPRIO_NORMAL7; }
} else if (priority >= OS_TASK_PRIORITY_ABOVE_NORMAL && priority < OS_TASK_PRIORITY_HIGH) {
pri = KPRIO_NORMAL_ABOVE0 + (priority - OS_TASK_PRIORITY_ABOVE_NORMAL);
if (pri > KPRIO_NORMAL_ABOVE7) { pri = KPRIO_NORMAL_ABOVE7; }
} else if (priority >= OS_TASK_PRIORITY_HIGH && priority < OS_TASK_PRIORITY_REALTIME) {
pri = KPRIO_HIGH0 + (priority - OS_TASK_PRIORITY_HIGH);
if (pri > KPRIO_HIGH7) { pri = KPRIO_HIGH7; }
} else if (priority >= OS_TASK_PRIORITY_REALTIME && priority < OS_TASK_PRIORITY_ISR) {
pri = KPRIO_REALTIME0 + (priority - OS_TASK_PRIORITY_REALTIME);
if (pri > KPRIO_REALTIME7) { pri = KPRIO_REALTIME7; }
} else {
pri = KPRIO_ISR;
}
if (task) {
task->priority = pri;
task->lprun = (prio & OS_TASK_FLAGS_LPRUN) ? 1 : 0;
if (task->hdl) {
csi_kernel_task_set_prio(task->hdl, task->priority);
csi_kernel_task_set_lprun(task->hdl, task->lprun);
}
}
return pri;
}
int32 os_task_set_priority(os_task_t *task, uint32 pri)
{
uint8 priority = pri & 0xff;
if (priority >= OS_TASK_PRIORITY_HIGH) {
priority = OS_TASK_PRIORITY_HIGH - 1;
pri &= 0xffffff00;
pri |= priority;
os_printf("INVALID PRIORITY\r\n");
}
return _os_task_set_priority(task, pri);
}
int32 os_task_set_stack(os_task_t *task, void *stack, int32 stack_size)
{
//ASSERT(task);
//ASSERT(!task->hdl);
task->stack = stack;
task->stack_size = stack_size;
return RET_OK;
}
int32 os_task_run(os_task_t *task)
{
//ASSERT(task);
int32 ret = csi_kernel_task_new(os_task_entry, task->name,
(void *)task, task->priority, 0, task->stack,
task->stack_size, &task->hdl);
if(ret == RET_OK){
csi_kernel_task_set_lprun(task->hdl, task->lprun);
}
//ASSERT(!ret);
return ret;
}
int32 os_task_stop(os_task_t *task)
{
//ASSERT(0);
csi_kernel_task_terminate(task->hdl);
return RET_OK;
}
int32 os_task_del(os_task_t *task)
{
int32 ret = 0;
//ASSERT(task);
ASSERT(task->hdl);
void *hdl = task->hdl;
task->hdl = NULL;
ret = csi_kernel_task_del(hdl);
return ret;
}
int32 os_task_runtime(struct os_task_info *tsk_times, int32 count)
{
return csi_kernel_task_runtime(tsk_times, count);
}
void os_task_print(void)
{
csi_kernel_task_print();
}
int32 os_task_count(void)
{
return csi_kernel_task_count();
}
void *os_task_current(void)
{
return csi_kernel_task_get_cur();
}
int32 os_task_suspend(os_task_t *task)
{
return csi_kernel_task_suspend(task->hdl);
}
int32 os_task_resume(os_task_t *task)
{
return csi_kernel_task_resume(task->hdl);
}
void os_task_dump(void *hdl, void *stack)
{
csi_kernel_task_dump(hdl, stack);
}
int32 os_task_yield(void)
{
return csi_kernel_task_yield();
}
int32 os_sched_disable(void)
{
krhino_sched_disable();
return 0;
}
int32 os_sched_enbale(void)
{
krhino_sched_enable();
return 0;
}
os_task_t *os_task_hdl2tsk(void *hdl)
{
ktask_t *task = (ktask_t *)hdl;
return (task && task->arg) ? ((os_task_t *)task->arg) : NULL;
}
void *os_task_data(void *hdl)
{
ktask_t *task = (ktask_t *)hdl;
return task->arg;
}
void *os_task_create(const char *name, os_task_func_t func, void *args, uint32 prio, uint32 time, void *stack, uint32 stack_size)
{
k_task_handle_t hdl = NULL;
uint32 priority = os_task_set_priority(NULL, prio);
int32 ret = csi_kernel_task_new(func, name, args, priority, time, stack, stack_size, &hdl);
if(ret == RET_OK){
if(prio & OS_TASK_FLAGS_LPRUN){
csi_kernel_task_set_lprun(hdl, 1);
}
}
ASSERT(!ret);
return hdl;
}
int32 os_task_destroy(void *hdl)
{
return csi_kernel_task_del(hdl);
}
int32 os_task_suspend2(void *hdl)
{
return csi_kernel_task_suspend((k_task_handle_t)hdl);
}
int32 os_task_resume2(void *hdl)
{
return csi_kernel_task_resume((k_task_handle_t)hdl);
}
int32 os_blklist_init(os_blklist_t *blkobj)
{
void *csi_kernel_blklist_new();
blkobj->hdl = csi_kernel_blklist_new();
return blkobj->hdl ? RET_OK : RET_ERR;
}
void os_blklist_del(os_blklist_t *blkobj)
{
void csi_kernel_blklist_del(void *hdl);
csi_kernel_blklist_del(blkobj->hdl);
}
void os_blklist_suspend(os_blklist_t *blkobj, void *task_hdl)
{
void csi_kernel_blklist_suspend(void *hdl, k_task_handle_t task_hdl);
csi_kernel_blklist_suspend(blkobj->hdl, task_hdl);
}
void os_blklist_resume(os_blklist_t *blkobj)
{
void csi_kernel_blklist_wakeup(void *hdl);
csi_kernel_blklist_wakeup(blkobj->hdl);
}
void os_lpower_mode(uint8 enable)
{
csi_kernel_lpower_mode(enable);
}
#endif

323
sdk/osal/csky/time.c Normal file
View File

@@ -0,0 +1,323 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/sleep.h"
#include "osal/irq.h"
#ifdef CSKY_OS
#include "csi_kernel.h"
#include <k_api.h>
#include <sys/time.h>
#define OS_MS_PERIOD_TICK (1000/OS_SYSTICK_HZ)
extern uint32_t g_cpuloading;
extern uint64_t g_sys_tick_count;
static uint64 sys_time_base_ns = 0;
static uint64 sys_time_real_ns = 0;
uint64 os_jiffies(void)
{
return (g_sys_tick_count);
}
uint32 os_cpuloading(void)
{
return g_cpuloading;
}
uint32 os_seconds(void)
{
return krhino_curr_nanosec()/1000000000;
}
uint64 os_mseconds(void)
{
return krhino_curr_nanosec()/1000000;
}
uint64 os_useconds(void)
{
return krhino_curr_nanosec()/1000;
}
void os_systime(struct timespec *tm)
{
uint64 diff_ns = krhino_curr_nanosec() - sys_time_base_ns;
tm->tv_sec = (sys_time_real_ns + diff_ns) / NANOSECONDS_PER_SECOND;
tm->tv_nsec = (sys_time_real_ns + diff_ns) % NANOSECONDS_PER_SECOND;
}
#ifdef __NEWLIB__
int gettimeofday(struct timeval *ptimeval, void *ptimezone)
#else
int gettimeofday(struct timeval *ptimeval, struct timezone *ptimezone)
#endif
{
uint64 diff_ns = krhino_curr_nanosec() - sys_time_base_ns;
ptimeval->tv_sec = (sys_time_real_ns + diff_ns) / NANOSECONDS_PER_SECOND;
ptimeval->tv_usec = ((sys_time_real_ns + diff_ns) % NANOSECONDS_PER_SECOND) / 1000;
//加时区
//ptimeval->tv_sec += (8*3600); //东八区
return 0;
}
int settimeofday(const struct timeval *tv, const struct timezone *tz)
{
uint32 f = disable_irq();
sys_time_base_ns = krhino_curr_nanosec();
sys_time_real_ns = (uint64)tv->tv_sec * NANOSECONDS_PER_SECOND + (uint64)tv->tv_usec * 1000UL;
enable_irq(f);
return 0;
}
time_t time(time_t *t)
{
time_t v;
uint64 diff_ns = krhino_curr_nanosec() - sys_time_base_ns;
v = (sys_time_real_ns + diff_ns) / NANOSECONDS_PER_SECOND;
if (t) *t = v;
return v;
}
int clock_gettime(uint32 clk_id, struct timespec *tp)
{
if(tp){
uint64 ns = krhino_curr_nanosec();
tp->tv_sec = ns / NANOSECONDS_PER_SECOND;
tp->tv_nsec = ns % NANOSECONDS_PER_SECOND;
}
return 0;
}
int32 timespec_validate(const struct timespec *time)
{
int32 ret = 0;
if (time != NULL) {
/* Verify 0 <= tv_nsec < 1000000000. */
if ((time->tv_nsec >= 0) && (time->tv_nsec < NANOSECONDS_PER_SECOND)) {
ret = 1;
}
}
return ret;
}
int32 timespec_cmp(const struct timespec *x, const struct timespec *y)
{
int32 ret = 0;
/* Check parameters */
if ((x == NULL) && (y == NULL)) {
ret = 0;
} else if (y == NULL) {
ret = 1;
} else if (x == NULL) {
ret = -1;
} else if (x->tv_sec > y->tv_sec) {
ret = 1;
} else if (x->tv_sec < y->tv_sec) {
ret = -1;
} else {
/* seconds are equal compare nano seconds */
if (x->tv_nsec > y->tv_nsec) {
ret = 1;
} else if (x->tv_nsec < y->tv_nsec) {
ret = -1;
} else {
ret = 0;
}
}
return ret;
}
int32 timespec_to_ticks(const struct timespec *time, uint64 *result)
{
int32 ret = 0;
uint64 llTotalTicks = 0;
long lNanoseconds = 0;
/* Check parameters. */
if ((time == NULL) || (result == NULL)) {
ret = -EINVAL;
} else if ((ret == 0) && (timespec_validate(time) == FALSE)) {
ret = -EINVAL;
} else {
/* Convert timespec.tv_sec to ticks. */
llTotalTicks = (uint64) OS_SYSTICK_HZ * (time->tv_sec);
/* Convert timespec.tv_nsec to ticks. This value does not have to be checked
* for overflow because a valid timespec has 0 <= tv_nsec < 1000000000 and
* NANOSECONDS_PER_TICK > 1. */
lNanoseconds = time->tv_nsec / (long) NANOSECONDS_PER_TICK + /* Whole nanoseconds. */
(long)(time->tv_nsec % (long) NANOSECONDS_PER_TICK != 0); /* Add 1 to round up if needed. */
/* Add the nanoseconds to the total ticks. */
llTotalTicks += (uint64) lNanoseconds;
/* Write result. */
*result = (uint64) llTotalTicks;
}
return ret;
}
void nanosec_to_timespec(int64 llSource, struct timespec *time)
{
long lCarrySec = 0;
/* Convert to timespec. */
time->tv_sec = (time_t)(llSource / NANOSECONDS_PER_SECOND);
time->tv_nsec = (long)(llSource % NANOSECONDS_PER_SECOND);
/* Subtract from tv_sec if tv_nsec < 0. */
if (time->tv_nsec < 0L) {
/* Compute the number of seconds to carry. */
lCarrySec = (time->tv_nsec / (long) NANOSECONDS_PER_SECOND) + 1L;
time->tv_sec -= (time_t)(lCarrySec);
time->tv_nsec += lCarrySec * (long) NANOSECONDS_PER_SECOND;
}
}
int32 timespec_add(const struct timespec *x, const struct timespec *y, struct timespec *result)
{
int64 llPartialSec = 0;
int32 ret = 0;
/* Check parameters. */
if ((result == NULL) || (x == NULL) || (y == NULL)) {
return -1;
}
/* Perform addition. */
result->tv_nsec = x->tv_nsec + y->tv_nsec;
/* check for overflow in case nsec value was invalid */
if (result->tv_nsec < 0) {
ret = 1;
} else {
llPartialSec = (result->tv_nsec) / NANOSECONDS_PER_SECOND;
result->tv_nsec = (result->tv_nsec) % NANOSECONDS_PER_SECOND;
result->tv_sec = x->tv_sec + y->tv_sec + llPartialSec;
/* check for overflow */
if (result->tv_sec < 0) {
ret = 1;
}
}
return ret;
}
int32 timespec_add_nanosec(const struct timespec *x, int64 llNanoseconds, struct timespec *result)
{
int64 llTotalNSec = 0;
int32 ret = 0;
/* Check parameters. */
if ((result == NULL) || (x == NULL)) {
return -1;
}
/* add nano seconds */
llTotalNSec = x->tv_nsec + llNanoseconds;
/* check for nano seconds overflow */
if (llTotalNSec < 0) {
ret = 1;
} else {
result->tv_nsec = llTotalNSec % NANOSECONDS_PER_SECOND;
result->tv_sec = x->tv_sec + (llTotalNSec / NANOSECONDS_PER_SECOND);
/* check for seconds overflow */
if (result->tv_sec < 0) {
ret = 1;
}
}
return ret;
}
int32 timespec_sub(const struct timespec *x, const struct timespec *y, struct timespec *result)
{
int32 cmp_ret = 0;
int32 ret = 0;
/* Check parameters. */
if ((result == NULL) || (x == NULL) || (y == NULL)) {
return -1;
}
cmp_ret = timespec_cmp(x, y);
/* if x < y then result would be negative, return 1 */
if (cmp_ret == -1) {
ret = 1;
} else if (cmp_ret == 0) {
/* if times are the same return zero */
result->tv_sec = 0;
result->tv_nsec = 0;
} else {
/* If x > y Perform subtraction. */
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_nsec = x->tv_nsec - y->tv_nsec;
/* check if nano seconds value needs to borrow */
if (result->tv_nsec < 0) {
/* Based on comparison, tv_sec > 0 */
result->tv_sec--;
result->tv_nsec += (long) NANOSECONDS_PER_SECOND;
}
/* if nano second is negative after borrow, it is an overflow error */
if (result->tv_nsec < 0) {
ret = -1;
}
}
return ret;
}
int32 timespec_detal_ticks(const struct timespec *abstime, const struct timespec *curtime, uint64 *result)
{
int32 ret = 0;
struct timespec diff = { 0 };
if(abstime == NULL || curtime == NULL || result == NULL){
return -EINVAL;
}
ret = timespec_sub(abstime, curtime, &diff);
if (ret == 1) {
/* abstime was in the past. */
ret = -ETIMEDOUT;
} else if (ret == -1) {
/* error */
ret = -EINVAL;
}
/* Convert the time difference to ticks. */
if (ret == 0) {
ret = timespec_to_ticks(&diff, result);
}
return ret;
}
uint64 os_jiffies_to_msecs(uint64 jiff)
{
return ((jiff)*OS_MS_PERIOD_TICK);
}
uint64 os_msecs_to_jiffies(uint64 msec)
{
return ((msec)/OS_MS_PERIOD_TICK);
}
#endif

84
sdk/osal/csky/timer.c Normal file
View File

@@ -0,0 +1,84 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/timer.h"
#include "osal/string.h"
#ifdef CSKY_OS
#include "csi_kernel.h"
#define TIMER_MAGIC (0x6a7b3c4d)
static void _os_timer_cb(void *args)
{
uint32 t1, t2, t3;
os_timer_t *timer = (os_timer_t *)args;
timer->trigger_cnt++;
t1 = 0;
timer->cb(timer->data);
t2 = 0;
t3 = t2 - t1;
timer->total_time += t3;
if (t3 > timer->max_time) {
timer->max_time = t3;
}
}
int os_timer_init(os_timer_t *timer, os_timer_func_t func,
enum OS_TIMER_MODE mode, void *arg)
{
ASSERT(timer && func);
if(timer->magic == TIMER_MAGIC){
os_printf(KERN_WARNING"timer repeat initialization ????\r\n");
}
timer->cb = func;
timer->data = arg;
timer->total_time = 0;
timer->max_time = 0;
timer->trigger_cnt = 0;
timer->hdl = csi_kernel_timer_new(_os_timer_cb, mode, timer);
if(timer->hdl) timer->magic = TIMER_MAGIC;
return timer->hdl ? 0 : -1;
}
int os_timer_start(os_timer_t *timer, unsigned long expires)
{
//ASSERT(timer->hdl);
if (timer->hdl) {
return csi_kernel_timer_start(timer->hdl, csi_kernel_ms2tick(expires));
}
return 0;
}
int os_timer_stop(os_timer_t *timer)
{
int ret = 0;
//ASSERT(timer->hdl);
if (timer->hdl) {
ret = csi_kernel_timer_stop(timer->hdl);
}
return 0;
}
int os_timer_del(os_timer_t *timer)
{
int ret = 0;
//ASSERT(timer->hdl);
if (timer->hdl) {
ret = csi_kernel_timer_del(timer->hdl);
timer->hdl = 0;
timer->magic = 0;
}
return 0;
}
int os_timer_stat(os_timer_t *timer)
{
if (timer->hdl) {
return csi_kernel_timer_get_stat(timer->hdl);
}
return 0;
}
#endif

105
sdk/osal/ohos/condv.c Normal file
View File

@@ -0,0 +1,105 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/event.h"
#ifdef OHOS
#include "los_event.h"
#include "los_membox.h"
#include "los_memory.h"
#include "los_interrupt.h"
#include "los_mux.h"
#include "los_queue.h"
#include "los_sem.h"
#include "los_swtmr.h"
#include "los_task.h"
#include "los_timer.h"
#include "los_debug.h"
#if (LOSCFG_MUTEX_CREATE_TRACE == 1)
#include "los_arch.h"
#endif
#include "osal/string.h"
#include "osal/semaphore.h"
#include "osal/mutex.h"
#include "osal/condv.h"
#define CONDV_MAGIC (0x3a8d7c1d)
int32 os_condv_init(os_condv_t *cond)
{
UINT32 hdl = 0;
ASSERT(cond);
if(cond->magic == CONDV_MAGIC){
os_printf(KERN_WARNING"condv repeat initialization ????\r\n");
}
if (LOS_OK == LOS_SemCreate(0, &hdl)) {
cond->sema = (void *)(hdl + 1);
cond->magic = CONDV_MAGIC;
atomic_set(&cond->waitings, 0);
}
return (cond->sema ? RET_OK : RET_ERR);
}
int32 os_condv_broadcast(os_condv_t *cond)
{
uint32 i;
UINT32 hdl = (UINT32)cond->sema;
uint32 wait = atomic_read(&cond->waitings);
ASSERT(cond && cond->magic == CONDV_MAGIC);
while (wait > 0) {
if (wait == atomic_cmpxchg(&cond->waitings, wait, 0)) {
for (i = 0; i < wait; i++) {
LOS_SemPost(hdl - 1);
}
}
wait = atomic_read(&cond->waitings);
}
return RET_OK;
}
int32 os_condv_signal(os_condv_t *cond)
{
ASSERT(cond && cond->magic == CONDV_MAGIC);
UINT32 hdl = (UINT32)cond->sema;
uint32 wait = atomic_read(&cond->waitings);
while (wait > 0) {
if (wait == atomic_cmpxchg(&cond->waitings, wait, wait - 1)) {
LOS_SemPost(hdl - 1);
break;
}
wait = atomic_read(&cond->waitings);
}
return RET_OK;
}
int32 os_condv_del(os_condv_t *cond)
{
ASSERT(cond && cond->magic == CONDV_MAGIC);
UINT32 hdl = (UINT32)cond->sema;
LOS_SemDelete(hdl - 1);
cond->sema = NULL;
cond->magic = 0;
return RET_OK;
}
int32 os_condv_wait(os_condv_t *cond, os_mutex_t *mutex, uint32 tmo_ms)
{
ASSERT(cond && cond->magic == CONDV_MAGIC);
int32 ret;
UINT32 hdl = (UINT32)cond->sema;
atomic_inc(&cond->waitings);
os_mutex_unlock(mutex);
ret = LOS_SemPend(hdl - 1, LOS_MS2Tick(tmo_ms));
os_mutex_lock(mutex, osWaitForever);
atomic_dec(&cond->waitings);
return ret ? -ETIMEDOUT : RET_OK;
}
#endif

162
sdk/osal/ohos/event.c Normal file
View File

@@ -0,0 +1,162 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/event.h"
#include "osal/string.h"
#ifdef OHOS
#include "los_event.h"
#include "los_membox.h"
#include "los_memory.h"
#include "los_interrupt.h"
#include "los_mux.h"
#include "los_queue.h"
#include "los_sem.h"
#include "los_swtmr.h"
#include "los_task.h"
#include "los_timer.h"
#include "los_debug.h"
#if (LOSCFG_MUTEX_CREATE_TRACE == 1)
#include "los_arch.h"
#endif
#define EVENT_MAGIC (0xa67b3cd4)
int32 os_event_init(os_event_t *evt)
{
UINT32 ret;
PEVENT_CB_S pstEventCB;
if(evt->magic == EVENT_MAGIC){
os_printf(KERN_WARNING"event repeat initialization ????\r\n");
}
pstEventCB = (PEVENT_CB_S)os_malloc(sizeof(EVENT_CB_S));
if (pstEventCB == NULL) {
return RET_ERR;
}
ret = LOS_EventInit(pstEventCB);
if (ret == LOS_OK) {
evt->hdl = pstEventCB;
evt->magic = EVENT_MAGIC;
return RET_OK;
} else {
os_free(pstEventCB);
return RET_ERR;
}
}
int32 os_event_del(os_event_t *evt)
{
ASSERT(evt && evt->magic == EVENT_MAGIC);
int32 ret = LOS_EventDestroy((PEVENT_CB_S)evt->hdl);
os_free(evt->hdl);
evt->hdl = NULL;
evt->magic = 0;
return ret;
}
int32 os_event_set(os_event_t *evt, uint32 flags, uint32 *rflags)
{
PEVENT_CB_S pstEventCB = (PEVENT_CB_S)evt->hdl;
UINT32 ret;
ASSERT(evt && evt->magic == EVENT_MAGIC);
if (pstEventCB == NULL) {
return -EINVAL;
}
ret = LOS_EventWrite(pstEventCB, (UINT32)flags);
if (ret == LOS_OK) {
*rflags = pstEventCB->uwEventID;
return ret;
} else {
return RET_ERR;
}
}
int32 os_event_clear(os_event_t *evt, uint32 flags, uint32 *rflags)
{
PEVENT_CB_S pstEventCB = (PEVENT_CB_S)evt->hdl;
UINT32 intSave;
UINT32 ret;
if (pstEventCB == NULL) {
return RET_ERR;
}
intSave = LOS_IntLock();
*rflags = pstEventCB->uwEventID;
ret = LOS_EventClear(pstEventCB, ~flags);
LOS_IntRestore(intSave);
return ret;
}
int32 os_event_get(os_event_t *evt, uint32 *rflags)
{
PEVENT_CB_S pstEventCB = (PEVENT_CB_S)evt->hdl;
UINT32 intSave;
if (pstEventCB == NULL) {
return RET_ERR;
}
intSave = LOS_IntLock();
*rflags = pstEventCB->uwEventID;
LOS_IntRestore(intSave);
return 0;
}
int32 os_event_wait(os_event_t *evt, uint32 flags, uint32 *rflags, uint32 mode, int32 timeout)
{
PEVENT_CB_S pstEventCB = (PEVENT_CB_S)evt->hdl;
UINT32 imode = 0;
UINT32 ret;
if (pstEventCB == NULL) {
return RET_ERR;
}
if (OS_INT_ACTIVE && (timeout != 0)) {
return RET_ERR;
}
if (mode & OS_EVENT_WMODE_OR) {
imode |= LOS_WAITMODE_OR;
}
if (mode & OS_EVENT_WMODE_AND) {
imode |= LOS_WAITMODE_AND;
}
if (mode & OS_EVENT_WMODE_CLEAR) {
imode &= ~LOS_WAITMODE_CLR;
} else {
imode |= LOS_WAITMODE_CLR;
}
ret = LOS_EventRead(pstEventCB, (UINT32)flags, imode, (UINT32)timeout);
switch (ret) {
case LOS_ERRNO_EVENT_PTR_NULL:
case LOS_ERRNO_EVENT_EVENTMASK_INVALID:
case LOS_ERRNO_EVENT_FLAGS_INVALID:
case LOS_ERRNO_EVENT_SETBIT_INVALID:
return RET_ERR;
case LOS_ERRNO_EVENT_READ_IN_INTERRUPT:
case LOS_ERRNO_EVENT_READ_IN_LOCK:
return RET_ERR;
case LOS_ERRNO_EVENT_READ_TIMEOUT:
return -ETIMEDOUT;
default:
*rflags = (uint32_t)ret;
return RET_OK;
}
}
#endif

44
sdk/osal/ohos/malloc.c Normal file
View File

@@ -0,0 +1,44 @@
#include "basic_include.h"
#include "los_config.h"
#include "los_compiler.h"
#ifdef OHOS
UINT8 *m_aucSysMem0 = NULL;
VOID *LOS_MemAlloc(VOID *pool, UINT32 size)
{
void *caller = RETURN_ADDR();
#if defined(PSRAM_HEAP) && defined(PSRAM_TASK_STACK)
return sysheap_alloc(&psram_heap, size, caller, 0);
#else
return sysheap_alloc(&sram_heap, size, caller, 0);
#endif
}
UINT32 LOS_MemFree(VOID *pool, VOID *ptr)
{
#if defined(PSRAM_HEAP) && defined(PSRAM_TASK_STACK)
os_free_psram(ptr);
#else
os_free(ptr);
#endif
return RET_OK;
}
VOID *LOS_MemAllocAlign(VOID *pool, UINT32 size, UINT32 boundary)
{
void *ptr = LOS_MemAlloc(NULL, size + boundary);
if(ptr){
ptr = (void *)ALIGN((uint32)ptr, boundary);
}
return ptr;
}
UINT32 LOS_MemIntegrityCheck(const VOID *pool)
{
return 0;
}
#endif

97
sdk/osal/ohos/msgqueue.c Normal file
View File

@@ -0,0 +1,97 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/event.h"
#ifdef OHOS
#include "los_event.h"
#include "los_membox.h"
#include "los_memory.h"
#include "los_interrupt.h"
#include "los_mux.h"
#include "los_queue.h"
#include "los_sem.h"
#include "los_swtmr.h"
#include "los_task.h"
#include "los_timer.h"
#include "los_debug.h"
#if (LOSCFG_MUTEX_CREATE_TRACE == 1)
#include "los_arch.h"
#endif
#include "osal/string.h"
#include "osal/msgqueue.h"
#define MSGQ_MAGIC (0x4a8b1c9d)
int32 os_msgq_init(os_msgqueue_t *msgq, int32 size)
{
UINT32 hdl = 0;
if(msgq->magic == MSGQ_MAGIC){
os_printf(KERN_WARNING"msgq repeat initialization ????\r\n");
}
if (LOS_OK == LOS_QueueCreate("msgq", size, &hdl, 0, sizeof(uint32))) {
msgq->hdl = (void *)(hdl + 1);
msgq->magic = MSGQ_MAGIC;
}
return (msgq->hdl ? RET_OK : RET_ERR);
}
uint32 os_msgq_get(os_msgqueue_t *msgq, int32 tmo_ms)
{
UINT32 val = 0;
UINT32 hdl = (UINT32)msgq->hdl;
ASSERT(msgq->hdl);
LOS_QueueRead(hdl - 1, &val, sizeof(uint32), LOS_MS2Tick(tmo_ms));
return val;
}
uint32 os_msgq_get2(struct os_msgqueue *msgq, int32 tmo_ms, int32 *err)
{
UINT32 ret;
UINT32 val = 0;
UINT32 hdl = (UINT32)msgq->hdl;
ASSERT(msgq->hdl);
ret = LOS_QueueRead(hdl - 1, &val, sizeof(uint32), LOS_MS2Tick(tmo_ms));
if(err) *err = ret;
return val;
}
int32 os_msgq_put(os_msgqueue_t *msgq, uint32 data, int32 tmo_ms)
{
UINT32 hdl = (UINT32)msgq->hdl;
ASSERT(msgq->hdl);
return LOS_QueueWrite(hdl - 1, (void *)data, sizeof(uint32), LOS_MS2Tick(tmo_ms));
}
int32 os_msgq_put_head(os_msgqueue_t *msgq, uint32 data, int32 tmo_ms)
{
UINT32 hdl = (UINT32)msgq->hdl;
ASSERT(msgq->hdl);
return LOS_QueueWriteHead(hdl - 1, (void *)data, sizeof(uint32), LOS_MS2Tick(tmo_ms));
}
int32 os_msgq_del(os_msgqueue_t *msgq)
{
ASSERT(msgq->hdl);
UINT32 hdl = (UINT32)msgq->hdl;
ASSERT(msgq->hdl);
LOS_QueueDelete(hdl - 1);
msgq->hdl = NULL;
msgq->magic = 0;
return RET_OK;
}
int32 os_msgq_cnt(os_msgqueue_t *msgq)
{
QUEUE_INFO_S queueInfo;
UINT32 hdl = (UINT32)msgq->hdl;
ASSERT(msgq->hdl);
memset(&queueInfo, 0, sizeof(queueInfo));
LOS_QueueInfoGet(hdl - 1, &queueInfo);
return queueInfo.readableCnt;
}
#endif

67
sdk/osal/ohos/mutex.c Normal file
View File

@@ -0,0 +1,67 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/event.h"
#ifdef OHOS
#include "los_event.h"
#include "los_membox.h"
#include "los_memory.h"
#include "los_interrupt.h"
#include "los_mux.h"
#include "los_queue.h"
#include "los_sem.h"
#include "los_swtmr.h"
#include "los_task.h"
#include "los_timer.h"
#include "los_debug.h"
#if (LOSCFG_MUTEX_CREATE_TRACE == 1)
#include "los_arch.h"
#endif
#include "osal/string.h"
#include "osal/mutex.h"
#define MUTEX_MAGIC (0xa8b4c2d5)
int32 os_mutex_init(os_mutex_t *mutex)
{
UINT32 hdl = 0;
if(mutex->magic == MUTEX_MAGIC){
os_printf(KERN_WARNING"mutex repeat initialization ????\r\n");
}
if (LOS_OK == LOS_MuxCreate(&hdl)) {
mutex->hdl = (void *)(hdl+1);
mutex->magic = MUTEX_MAGIC;
}
return (mutex->hdl ? RET_OK : RET_ERR);
}
int32 os_mutex_lock(os_mutex_t *mutex, int32 tmo)
{
ASSERT(mutex && mutex->hdl);
UINT32 hdl = (UINT32)mutex->hdl;
return LOS_MuxPend(hdl - 1, LOS_MS2Tick(tmo), (uint32_t)RETURN_ADDR());
}
int32 os_mutex_unlock(os_mutex_t *mutex)
{
ASSERT(mutex && mutex->hdl);
UINT32 hdl = (UINT32)mutex->hdl;
return LOS_MuxPost(hdl - 1);
}
int32 os_mutex_del(os_mutex_t *mutex)
{
ASSERT(mutex && mutex->hdl);
int32 ret = 0;
UINT32 hdl = (UINT32)mutex->hdl;
ret = LOS_MuxDelete(hdl - 1);
mutex->hdl = NULL;
mutex->magic = 0;
return ret;
}
#endif

92
sdk/osal/ohos/semaphore.c Normal file
View File

@@ -0,0 +1,92 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/event.h"
#ifdef OHOS
#include "los_event.h"
#include "los_membox.h"
#include "los_memory.h"
#include "los_interrupt.h"
#include "los_mux.h"
#include "los_queue.h"
#include "los_sem.h"
#include "los_swtmr.h"
#include "los_task.h"
#include "los_timer.h"
#include "los_debug.h"
#if (LOSCFG_MUTEX_CREATE_TRACE == 1)
#include "los_arch.h"
#endif
#include "osal/string.h"
#include "osal/semaphore.h"
#define SEMA_MAGIC (0x3a8d7c1d)
int32 os_sema_init(os_semaphore_t *sem, int32 val)
{
UINT32 hdl = 0;
ASSERT(sem);
if(sem->magic == SEMA_MAGIC){
os_printf(KERN_WARNING"sem repeat initialization ????\r\n");
}
if (LOS_OK == LOS_SemCreate(val, &hdl)) {
sem->hdl = (void *)(hdl + 1);
sem->magic = SEMA_MAGIC;
} else {
os_printf(KERN_ERR"os_sema_init fail\r\n");
}
return (sem->hdl ? RET_OK : RET_ERR);
}
int32 os_sema_down(os_semaphore_t *sem, int32 tmo_ms)
{
ASSERT(sem && sem->hdl);
INT32 ret = 0;
UINT32 hdl = (UINT32)sem->hdl;
ret = LOS_SemPend((hdl - 1), LOS_MS2Tick(tmo_ms));
return (ret == LOS_OK) ? 1 : 0;
}
int32 os_sema_up(os_semaphore_t *sem)
{
ASSERT(sem && sem->hdl);
UINT32 hdl = (UINT32)sem->hdl;
return LOS_SemPost(hdl - 1);
}
int32 os_sema_del(os_semaphore_t *sem)
{
ASSERT(sem && sem->hdl);
UINT32 hdl = (UINT32)sem->hdl;
UINT32 ret = LOS_SemDelete(hdl - 1);
sem->hdl = NULL;
sem->magic = 0;
return ret;
}
int32 os_sema_count(os_semaphore_t *sem)
{
INT32 val = 0;
if (sem && sem->hdl) {
UINT32 hdl = (UINT32)sem->hdl;
LOS_SemGetValue(hdl - 1, &val);
}
return val > 0 ? val : 0;
}
void os_sema_eat(os_semaphore_t *sem)
{
int32 ret = 0;
while (os_sema_count(sem) > 0) {
ret = os_sema_down(sem, 0);
if (!ret) {
break;
}
}
}
#endif

34
sdk/osal/ohos/sleep.c Normal file
View File

@@ -0,0 +1,34 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/event.h"
#ifdef OHOS
#include "los_event.h"
#include "los_membox.h"
#include "los_memory.h"
#include "los_interrupt.h"
#include "los_mux.h"
#include "los_queue.h"
#include "los_sem.h"
#include "los_swtmr.h"
#include "los_task.h"
#include "los_timer.h"
#include "los_debug.h"
#if (LOSCFG_MUTEX_CREATE_TRACE == 1)
#include "los_arch.h"
#endif
#include "osal/sleep.h"
void os_sleep(int32 sec)
{
LOS_TaskDelay(LOS_MS2Tick(sec*1000));
}
void os_sleep_ms(int32 msec)
{
LOS_TaskDelay(LOS_MS2Tick(msec));
}
#endif

454
sdk/osal/ohos/task.c Normal file
View File

@@ -0,0 +1,454 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/event.h"
#ifdef OHOS
#include "los_event.h"
#include "los_membox.h"
#include "los_memory.h"
#include "los_interrupt.h"
#include "los_mux.h"
#include "los_queue.h"
#include "los_sem.h"
#include "los_swtmr.h"
#include "los_task.h"
#include "los_timer.h"
#include "los_debug.h"
#if (LOSCFG_MUTEX_CREATE_TRACE == 1)
#include "los_arch.h"
#endif
#include "osal/string.h"
#include "osal/task.h"
#include "los_cpup.h"
static void os_task_entry(void *args)
{
os_task_t *task = (os_task_t *)args;
task->func((void *)task->args);
}
int32 os_task_init(const uint8 *name, os_task_t *task, os_task_func_t func, uint32 data)
{
//ASSERT(task);
os_memset(task, 0, sizeof(os_task_t));
task->args = data;
task->func = func;
task->name = (const char *)name;
return RET_OK;
}
int32 os_task_priority(os_task_t *task)
{
//ASSERT(task);
return task->priority;
}
int32 os_task_priority2(void *hdl)
{
UINT32 tsk_hdl = (UINT32)hdl;
if(tsk_hdl > 0){
return LOS_TaskPriGet(tsk_hdl - 1);
}else{
return 0;
}
}
int32 os_task_stacksize(os_task_t *task)
{
//ASSERT(task);
return task->stack_size;
}
int32 os_task_stacksize2(void *hdl)
{
UINT32 tsk_hdl = (UINT32)hdl;
if(tsk_hdl > 0){
LosTaskCB *taskCB = OS_TCB_FROM_TID(tsk_hdl - 1);
return taskCB->stackSize;
}else{
return 0;
}
}
int32 _os_task_set_priority(os_task_t *task, uint32 prio)
{
int32 pri = 12;
uint8 priority = prio&0xff;
//ASSERT(task);
//ASSERT(!task->hdl);
if (priority < OS_TASK_PRIORITY_LOW) {
pri = 31;
} else if (priority < OS_TASK_PRIORITY_BELOW_NORMAL) {
pri = 24 + (priority - OS_TASK_PRIORITY_LOW);
if (pri > 30) { pri = 30; }
} else if (priority >= OS_TASK_PRIORITY_BELOW_NORMAL && priority < OS_TASK_PRIORITY_NORMAL) {
pri = 18 + (priority - OS_TASK_PRIORITY_BELOW_NORMAL);
if (pri > 24) { pri = 24; }
} else if (priority >= OS_TASK_PRIORITY_NORMAL && priority < OS_TASK_PRIORITY_ABOVE_NORMAL) {
pri = 12 + (priority - OS_TASK_PRIORITY_NORMAL);
if (pri > 18) { pri = 18; }
} else if (priority >= OS_TASK_PRIORITY_ABOVE_NORMAL && priority < OS_TASK_PRIORITY_HIGH) {
pri = 6 + (priority - OS_TASK_PRIORITY_ABOVE_NORMAL);
if (pri > 12) { pri = 12; }
} else if (priority >= OS_TASK_PRIORITY_HIGH && priority < OS_TASK_PRIORITY_REALTIME) {
pri = 2 + (priority - OS_TASK_PRIORITY_HIGH);
if (pri > 6) { pri = 6; }
} else if (priority >= OS_TASK_PRIORITY_REALTIME && priority < OS_TASK_PRIORITY_ISR) {
pri = 1;
} else {
pri = 0;
}
if (task) {
task->priority = pri;
task->lprun = (prio & OS_TASK_FLAGS_LPRUN) ? 1 : 0;
if (task->hdl) {
LOS_TaskPriSet((UINT32)task->hdl - 1, task->priority);
LOS_Task_LpowerRun((UINT32)task->hdl - 1, task->lprun);
}
}
return pri;
}
int32 os_task_set_priority(os_task_t *task, uint32 pri)
{
uint8 priority = pri & 0xff;
if (priority >= OS_TASK_PRIORITY_HIGH) {
priority = OS_TASK_PRIORITY_HIGH - 1;
pri &= 0xffffff00;
pri |= priority;
os_printf("INVALID PRIORITY\r\n");
}
return _os_task_set_priority(task, pri);
}
int32 os_task_set_stack(os_task_t *task, void *stack, int32 stack_size)
{
//ASSERT(task);
//ASSERT(!task->hdl);
task->stack = stack;
task->stack_size = stack_size;
return RET_OK;
}
int32 os_task_run(os_task_t *task)
{
UINT32 hdl = 0;
TSK_INIT_PARAM_S stTskInitParam;
os_memset(&stTskInitParam, 0, sizeof(stTskInitParam));
stTskInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)os_task_entry;
stTskInitParam.uwArg = (UINT32)task;
stTskInitParam.uwStackSize = task->stack_size;
stTskInitParam.pcName = (char *)task->name;
stTskInitParam.uwResved = LOS_TASK_ATTR_JOINABLE;
stTskInitParam.usTaskPrio = task->priority;
if (LOS_OK == LOS_TaskCreate(&hdl, &stTskInitParam)) {
LOS_Task_LpowerRun(hdl, task->lprun);
task->hdl = (void *)(hdl + 1);
return RET_OK;
}
return RET_ERR;
}
int32 os_task_stop(os_task_t *task)
{
//ASSERT(0);
return RET_OK;
}
int32 os_task_del(os_task_t *task)
{
UINT32 hdl = (UINT32)task->hdl;
task->hdl = NULL;
if(hdl > 0){
return LOS_TaskDelete(hdl - 1);
}else{
return -EINVAL;
}
}
int32 os_task_runtime(struct os_task_info *tsk_times, int32 count)
{
#if (LOSCFG_BASE_CORE_CPUP == 1)
int32 i = 0;
int32 j = 0;
LosTaskCB *taskCB;
CPUP_INFO_S info[LOSCFG_BASE_CORE_TSK_LIMIT + 1];
os_memset(info, 0, sizeof(info));
LOS_AllTaskCpuUsage(info, CPUP_IN_10S);
for (i = 0; i < count && i < LOSCFG_BASE_CORE_TSK_LIMIT + 1; i++) {
if (info[i].usStatus && i != g_idleTaskID) {
taskCB = OS_TCB_FROM_TID(i);
tsk_times[j].id = i;
tsk_times[j].name = (const char *)taskCB->taskName;
tsk_times[j].prio = taskCB->priority;
tsk_times[j].arg = i+1;
tsk_times[j].stack = taskCB->stackSize / 4;
tsk_times[j].time = info[i].uwUsage / LOS_CPUP_PRECISION_MULT;
tsk_times[j].status = (const char *)OsConvertTskStatus(g_taskCBArray[i].taskStatus);
j++;
}
}
return j;
#else
return 0;
#endif
}
void os_task_print(void)
{
#if (LOSCFG_BASE_CORE_CPUP == 1)
int32 i = 0;
LosTaskCB *taskCB;
CPUP_INFO_S info[LOSCFG_BASE_CORE_TSK_LIMIT + 1];
os_memset(info, 0, sizeof(info));
LOS_AllTaskCpuUsage(info, CPUP_IN_10S);
for (i = 0; i < LOSCFG_BASE_CORE_TSK_LIMIT + 1; i++) {
if (info[i].usStatus) {
taskCB = OS_TCB_FROM_TID(i);
printf("\0011Task %s %s ticks:%d stack:%d prio:%d, arg:0x%x\r\n", taskCB->taskName,
(const char *)OsConvertTskStatus(info[i].usStatus),
info[i].uwUsage / LOS_CPUP_PRECISION_MULT,
taskCB->stackSize / 4, taskCB->priority, taskCB->arg);
}
}
#endif
}
int32 os_task_count(void)
{
#if (LOSCFG_BASE_CORE_CPUP == 1)
int32 i = 0;
int32 j = 0;
CPUP_INFO_S info[LOSCFG_BASE_CORE_TSK_LIMIT + 1];
os_memset(info, 0, sizeof(info));
LOS_AllTaskCpuUsage(info, CPUP_IN_10S);
for (i = 0; i < LOSCFG_BASE_CORE_TSK_LIMIT + 1; i++) {
if (info[i].usStatus) {
j++;
}
}
return j;
#else
return 0;
#endif
}
void *os_task_current(void)
{
return (void *)(g_losTask.runTask->taskID + 1);
}
int32 os_task_suspend(os_task_t *task)
{
UINT32 hdl = (UINT32)task->hdl;
if(hdl > 0){
return LOS_TaskSuspend(hdl - 1);
}else{
return -EINVAL;
}
}
int32 os_task_resume(os_task_t *task)
{
UINT32 hdl = (UINT32)task->hdl;
if(hdl > 0){
return LOS_TaskResume(hdl - 1);
}else{
return -EINVAL;
}
}
UINT8 os_task_state_convert(UINT16 taskStatus)
{
if (taskStatus & OS_TASK_STATUS_RUNNING) {
return 1;
} else if (taskStatus & OS_TASK_STATUS_READY) {
return 1; //READY
} else if (taskStatus & OS_TASK_STATUS_EXIT) {
return 7; //delete
} else if (taskStatus & OS_TASK_STATUS_SUSPEND) {
return 3; //suspend
} else if (taskStatus & OS_TASK_STATUS_DELAY) {
return 5; //sleep
} else if (taskStatus & OS_TASK_STATUS_PEND) {
if (taskStatus & OS_TASK_STATUS_PEND_TIME) {
return 6; //
}
return 2; //pend
}
return 0;
}
void os_task_dump(void *hdl, void *stack)
{
UINT32 i = 0;
UINT32 *p;
UINT32 *addr = 0;
LosTaskCB *task;
UINT32 taskID = (UINT32)hdl;
UINT32 *sp = (uint32_t *)__get_SP();
if(hdl == NULL){
return;
}
taskID = taskID - 1;
task = OS_TCB_FROM_TID(taskID);
p = (stack ? stack : task->stackPointer);
__lable:
printf("\0011Task:%s\r\n", task->taskName);
printf("\0011 task_state: %d\r\n", os_task_state_convert(task->taskStatus));
printf("\0011 stack_size: %d\r\n", task->stackSize);
printf("\0011 task_stack: 0x%08x,0x%08x,0x%08x\r\n", (uint32_t)task->topOfStack, (uint32_t)p, (uint32_t)(task->topOfStack + task->stackSize));
if(task == g_losTask.runTask){
printf("\0011 task_lr : %p\r\n", __builtin_return_address(0));
printf("\0011 task_pc : %p\r\n", &&__lable);
p = sp;
}
printf("\0011 stack dump:\r\n ");
for(addr=p; (UINT32)addr<task->topOfStack+task->stackSize; addr++){
printf("\00110x%08x,", *addr);
if(++i == 4){
printf("\0011\r\n ");
i = 0;
}
}
printf("\0011\r\n");
}
int32 os_task_yield(void)
{
return LOS_TaskYield();
}
int32 os_sched_disable(void)
{
LOS_TaskLock();
return 0;
}
int32 os_sched_enbale(void)
{
LOS_TaskUnlock();
return 0;
}
void *os_task_data(void *hdl)
{
LosTaskCB *task;
UINT32 taskID = (UINT32)hdl;
if(taskID > 0){
task = OS_TCB_FROM_TID(taskID-1);
return (void *)task->arg;
}else{
return NULL;
}
}
void *os_task_create(const char *name, os_task_func_t func, void *args, uint32 prio, uint32 time, void *stack, uint32 stack_size)
{
UINT32 hdl = 0;
TSK_INIT_PARAM_S stTskInitParam;
os_memset(&stTskInitParam, 0, sizeof(stTskInitParam));
stTskInitParam.pfnTaskEntry = (TSK_ENTRY_FUNC)func;
stTskInitParam.uwArg = (UINT32)args;
stTskInitParam.uwStackSize = stack_size;
stTskInitParam.pcName = (char *)name;
stTskInitParam.uwResved = LOS_TASK_ATTR_JOINABLE;
stTskInitParam.usTaskPrio = os_task_set_priority(NULL, prio);
if (LOS_OK == LOS_TaskCreate(&hdl, &stTskInitParam)) {
if(prio & OS_TASK_FLAGS_LPRUN){
LOS_Task_LpowerRun(hdl, 1);
}
hdl++;
}
return (void *)hdl;
}
int32 os_task_destroy(void *hdl)
{
UINT32 tsk_hdl = (UINT32)hdl;
if(tsk_hdl > 0){
return LOS_TaskDelete(tsk_hdl - 1);
}else{
return -EINVAL;
}
}
void os_lpower_mode(uint8 enable)
{
UINT32 intSave;
UINT32 loopNum;
LosTaskCB *taskCB = (LosTaskCB *)NULL;
intSave = LOS_IntLock();
for (loopNum = 0; loopNum < g_taskMaxNum; loopNum++) {
taskCB = (((LosTaskCB *)g_taskCBArray) + loopNum);
if (taskCB->taskStatus & OS_TASK_STATUS_UNUSED) {
continue;
}
if(loopNum != g_idleTaskID && !taskCB->lpRun){
if(enable){
LOS_TaskSuspend(loopNum);
}else{
LOS_TaskResume(loopNum);
}
}
}
LOS_IntRestore(intSave);
}
int32 os_task_suspend2(void *task_hdl)
{
UINT32 hdl = (UINT32)task_hdl;
if(hdl > 0){
return LOS_TaskSuspend(hdl - 1);
}else{
return -EINVAL;
}
}
int32 os_task_resume2(void *task_hdl)
{
UINT32 hdl = (UINT32)task_hdl;
if(hdl > 0){
return LOS_TaskResume(hdl - 1);
}else{
return -EINVAL;
}
}
int32 os_blklist_init(os_blklist_t *blkobj)
{
blkobj->hdl = LOS_Blklist_New();
return blkobj->hdl ? RET_OK : RET_ERR;
}
void os_blklist_del(os_blklist_t *blkobj)
{
if(blkobj->hdl){
LOS_Blklist_Delete(blkobj->hdl);
}
}
void os_blklist_suspend(os_blklist_t *blkobj, void *hdl)
{
UINT32 task_hdl = (UINT32)hdl;
if(blkobj->hdl && task_hdl > 0){
LOS_Blklist_Suspend(blkobj->hdl, task_hdl-1);
}
}
void os_blklist_resume(os_blklist_t *blkobj)
{
if(blkobj->hdl){
LOS_Blklist_Resume(blkobj->hdl);
}
}
#endif

327
sdk/osal/ohos/time.c Normal file
View File

@@ -0,0 +1,327 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/event.h"
#include "osal/irq.h"
#ifdef OHOS
#include "los_event.h"
#include "los_membox.h"
#include "los_memory.h"
#include "los_interrupt.h"
#include "los_mux.h"
#include "los_queue.h"
#include "los_sem.h"
#include "los_swtmr.h"
#include "los_task.h"
#include "los_timer.h"
#include "los_debug.h"
#if (LOSCFG_MUTEX_CREATE_TRACE == 1)
#include "los_arch.h"
#endif
#include "osal/sleep.h"
#include <sys/time.h>
static uint64 sys_time_base_ns = 0;
static uint64 sys_time_real_ns = 0;
uint64 os_jiffies(void)
{
return LOS_TickCountGet();
}
uint32 os_seconds(void)
{
return LOS_CurrNanosec()/1000000000;
}
uint64 os_mseconds(void)
{
return LOS_CurrNanosec()/1000000;
}
uint64 os_useconds(void)
{
return LOS_CurrNanosec()/1000;
}
void os_systime(struct timespec *tm)
{
uint64 diff_ns = LOS_CurrNanosec() - sys_time_base_ns;
tm->tv_sec = (sys_time_real_ns + diff_ns) / NANOSECONDS_PER_SECOND;
tm->tv_nsec = (sys_time_real_ns + diff_ns) % NANOSECONDS_PER_SECOND;
}
#ifdef __NEWLIB__
int gettimeofday(struct timeval *ptimeval, void *ptimezone)
#else
int gettimeofday(struct timeval *ptimeval, struct timezone *ptimezone)
#endif
{
uint64 diff_ns = LOS_CurrNanosec() - sys_time_base_ns;
ptimeval->tv_sec = (sys_time_real_ns + diff_ns) / NANOSECONDS_PER_SECOND;
ptimeval->tv_usec = ((sys_time_real_ns + diff_ns) % NANOSECONDS_PER_SECOND) / 1000;
//加时区
//ptimeval->tv_sec += (8*3600); //东八区
return 0;
}
int settimeofday(const struct timeval *tv, const struct timezone *tz)
{
uint32 f = disable_irq();
sys_time_base_ns = LOS_CurrNanosec();
sys_time_real_ns = (uint64)tv->tv_sec * NANOSECONDS_PER_SECOND + (uint64)tv->tv_usec * 1000UL;
enable_irq(f);
return 0;
}
time_t time(time_t *t)
{
time_t v;
uint64 diff_ns = LOS_CurrNanosec() - sys_time_base_ns;
v = (sys_time_real_ns + diff_ns) / NANOSECONDS_PER_SECOND;
if (t) *t = v;
return v;
}
int clock_gettime(uint32 clk_id, struct timespec *tp)
{
if(tp){
uint64 ns = LOS_CurrNanosec();
tp->tv_sec = ns / NANOSECONDS_PER_SECOND;
tp->tv_nsec = ns % NANOSECONDS_PER_SECOND;
}
return 0;
}
int32 timespec_validate(const struct timespec *time)
{
int32 ret = 0;
if (time != NULL) {
/* Verify 0 <= tv_nsec < 1000000000. */
if ((time->tv_nsec >= 0) && (time->tv_nsec < NANOSECONDS_PER_SECOND)) {
ret = 1;
}
}
return ret;
}
int32 timespec_cmp(const struct timespec *x, const struct timespec *y)
{
int32 ret = 0;
/* Check parameters */
if ((x == NULL) && (y == NULL)) {
ret = 0;
} else if (y == NULL) {
ret = 1;
} else if (x == NULL) {
ret = -1;
} else if (x->tv_sec > y->tv_sec) {
ret = 1;
} else if (x->tv_sec < y->tv_sec) {
ret = -1;
} else {
/* seconds are equal compare nano seconds */
if (x->tv_nsec > y->tv_nsec) {
ret = 1;
} else if (x->tv_nsec < y->tv_nsec) {
ret = -1;
} else {
ret = 0;
}
}
return ret;
}
int32 timespec_to_ticks(const struct timespec *time, uint64 *result)
{
int32 ret = 0;
uint64 llTotalTicks = 0;
long lNanoseconds = 0;
/* Check parameters. */
if ((time == NULL) || (result == NULL)) {
ret = -EINVAL;
} else if ((ret == 0) && (timespec_validate(time) == FALSE)) {
ret = -EINVAL;
} else {
/* Convert timespec.tv_sec to ticks. */
llTotalTicks = (uint64) OS_SYSTICK_HZ * (time->tv_sec);
/* Convert timespec.tv_nsec to ticks. This value does not have to be checked
* for overflow because a valid timespec has 0 <= tv_nsec < 1000000000 and
* NANOSECONDS_PER_TICK > 1. */
lNanoseconds = time->tv_nsec / (long) NANOSECONDS_PER_TICK + /* Whole nanoseconds. */
(long)(time->tv_nsec % (long) NANOSECONDS_PER_TICK != 0); /* Add 1 to round up if needed. */
/* Add the nanoseconds to the total ticks. */
llTotalTicks += (uint64) lNanoseconds;
/* Write result. */
*result = (uint64) llTotalTicks;
}
return ret;
}
void nanosec_to_timespec(int64 llSource, struct timespec *time)
{
long lCarrySec = 0;
/* Convert to timespec. */
time->tv_sec = (time_t)(llSource / NANOSECONDS_PER_SECOND);
time->tv_nsec = (long)(llSource % NANOSECONDS_PER_SECOND);
/* Subtract from tv_sec if tv_nsec < 0. */
if (time->tv_nsec < 0L) {
/* Compute the number of seconds to carry. */
lCarrySec = (time->tv_nsec / (long) NANOSECONDS_PER_SECOND) + 1L;
time->tv_sec -= (time_t)(lCarrySec);
time->tv_nsec += lCarrySec * (long) NANOSECONDS_PER_SECOND;
}
}
int32 timespec_add(const struct timespec *x, const struct timespec *y, struct timespec *result)
{
int64 llPartialSec = 0;
int32 ret = 0;
/* Check parameters. */
if ((result == NULL) || (x == NULL) || (y == NULL)) {
return -1;
}
/* Perform addition. */
result->tv_nsec = x->tv_nsec + y->tv_nsec;
/* check for overflow in case nsec value was invalid */
if (result->tv_nsec < 0) {
ret = 1;
} else {
llPartialSec = (result->tv_nsec) / NANOSECONDS_PER_SECOND;
result->tv_nsec = (result->tv_nsec) % NANOSECONDS_PER_SECOND;
result->tv_sec = x->tv_sec + y->tv_sec + llPartialSec;
/* check for overflow */
if (result->tv_sec < 0) {
ret = 1;
}
}
return ret;
}
int32 timespec_add_nanosec(const struct timespec *x, int64 llNanoseconds, struct timespec *result)
{
int64 llTotalNSec = 0;
int32 ret = 0;
/* Check parameters. */
if ((result == NULL) || (x == NULL)) {
return -1;
}
/* add nano seconds */
llTotalNSec = x->tv_nsec + llNanoseconds;
/* check for nano seconds overflow */
if (llTotalNSec < 0) {
ret = 1;
} else {
result->tv_nsec = llTotalNSec % NANOSECONDS_PER_SECOND;
result->tv_sec = x->tv_sec + (llTotalNSec / NANOSECONDS_PER_SECOND);
/* check for seconds overflow */
if (result->tv_sec < 0) {
ret = 1;
}
}
return ret;
}
int32 timespec_sub(const struct timespec *x, const struct timespec *y, struct timespec *result)
{
int32 cmp_ret = 0;
int32 ret = 0;
/* Check parameters. */
if ((result == NULL) || (x == NULL) || (y == NULL)) {
return -1;
}
cmp_ret = timespec_cmp(x, y);
/* if x < y then result would be negative, return 1 */
if (cmp_ret == -1) {
ret = 1;
} else if (cmp_ret == 0) {
/* if times are the same return zero */
result->tv_sec = 0;
result->tv_nsec = 0;
} else {
/* If x > y Perform subtraction. */
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_nsec = x->tv_nsec - y->tv_nsec;
/* check if nano seconds value needs to borrow */
if (result->tv_nsec < 0) {
/* Based on comparison, tv_sec > 0 */
result->tv_sec--;
result->tv_nsec += (long) NANOSECONDS_PER_SECOND;
}
/* if nano second is negative after borrow, it is an overflow error */
if (result->tv_nsec < 0) {
ret = -1;
}
}
return ret;
}
int32 timespec_detal_ticks(const struct timespec *abstime, const struct timespec *curtime, uint64 *result)
{
int32 ret = 0;
struct timespec diff = { 0 };
if (abstime == NULL || curtime == NULL || result == NULL) {
return -EINVAL;
}
ret = timespec_sub(abstime, curtime, &diff);
if (ret == 1) {
/* abstime was in the past. */
ret = -ETIMEDOUT;
} else if (ret == -1) {
/* error */
ret = -EINVAL;
}
/* Convert the time difference to ticks. */
if (ret == 0) {
ret = timespec_to_ticks(&diff, result);
}
return ret;
}
uint64 os_jiffies_to_msecs(uint64 jiff)
{
return LOS_Tick2MS(jiff);
}
uint64 os_msecs_to_jiffies(uint64 msec)
{
return LOS_MS2Tick(msec);
}
#endif

150
sdk/osal/ohos/timer.c Normal file
View File

@@ -0,0 +1,150 @@
#include "typesdef.h"
#include "errno.h"
#include "list.h"
#include "osal/string.h"
#include "osal/event.h"
#ifdef OHOS
#include "los_event.h"
#include "los_membox.h"
#include "los_memory.h"
#include "los_interrupt.h"
#include "los_mux.h"
#include "los_queue.h"
#include "los_sem.h"
#include "los_swtmr.h"
#include "los_task.h"
#include "los_timer.h"
#include "los_debug.h"
#if (LOSCFG_MUTEX_CREATE_TRACE == 1)
#include "los_arch.h"
#endif
#include "osal/timer.h"
#define TIMER_MAGIC (0x6a7b3c4d)
static void _os_timer_cb(void *args)
{
uint64 t1, t2, t3;
os_timer_t *timer = (os_timer_t *)args;
timer->trigger_cnt++;
t1 = os_jiffies();
timer->cb(timer->data);
t2 = os_jiffies();
t3 = t2 - t1;
timer->total_time += t3;
if (t3 > timer->max_time) {
timer->max_time = t3;
}
if (timer->mode == OS_TIMER_MODE_ONCE) {
timer->hdl = NULL;
}
}
static int32 _os_timer_create(os_timer_t *timer, int32 interval)
{
int32 ret;
UINT32 hdl = 0;
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
ret = LOS_SwtmrCreate(interval, timer->mode, (SWTMR_PROC_FUNC)_os_timer_cb, &hdl, (UINT32)timer, 1, 0);
#else
ret = LOS_SwtmrCreate(interval, timer->mode, (SWTMR_PROC_FUNC)_os_timer_cb, &hdl, (UINT32)timer);
#endif
if (ret == LOS_OK) {
timer->hdl = (void *)(hdl + 1);
timer->magic = TIMER_MAGIC;
}
return ret;
}
int os_timer_init(os_timer_t *timer, os_timer_func_t func, enum OS_TIMER_MODE mode, void *arg)
{
int32 ret = RET_OK;
if(timer->magic == TIMER_MAGIC){
os_printf(KERN_WARNING"timer repeat initialization ????\r\n");
}
memset(timer, 0, sizeof(os_timer_t));
timer->cb = func;
timer->data = arg;
timer->mode = mode;
if (mode == OS_TIMER_MODE_PERIODIC) {
ret = _os_timer_create(timer, 1);
}
return ret;
}
int os_timer_start(os_timer_t *timer, unsigned long expires)
{
int ret = RET_ERR;
UINT32 intSave;
UINT32 hdl;
SWTMR_CTRL_S *swtmr = NULL;
intSave = LOS_IntLock();
switch (timer->mode) {
case OS_TIMER_MODE_ONCE:
if (timer->hdl) os_timer_del(timer);
ret = _os_timer_create(timer, LOS_MS2Tick(expires));
break;
case OS_TIMER_MODE_PERIODIC:
if (timer->hdl) {
hdl = (UINT32)timer->hdl;
swtmr = OS_SWT_FROM_SID(hdl - 1);
swtmr->uwInterval = LOS_MS2Tick(expires);
if (swtmr->uwInterval == 0) {
swtmr->uwInterval = 1;
}
ret = RET_OK;
}
break;
default:
break;
}
if (ret == LOS_OK) {
hdl = (UINT32)timer->hdl;
ret = LOS_SwtmrStart(hdl - 1);
}
LOS_IntRestore(intSave);
return ret;
}
int os_timer_stop(os_timer_t *timer)
{
int ret = 0;
UINT32 hdl = (UINT32)timer->hdl;
//ASSERT(timer->hdl);
if (timer->hdl) {
ret = LOS_SwtmrStop(hdl - 1);
}
return 0;
}
int os_timer_del(os_timer_t *timer)
{
int ret = 0;
UINT32 hdl = (UINT32)timer->hdl;
//ASSERT(timer->hdl);
if (timer->hdl) {
ret = LOS_SwtmrDelete(hdl - 1);
timer->hdl = 0;
timer->magic = 0;
}
return 0;
}
int os_timer_stat(os_timer_t *timer)
{
uint32 ticks = 0;
UINT32 hdl = (UINT32)timer->hdl;
if (timer->hdl) {
return LOS_SwtmrTimeGet(hdl - 1, &ticks);
}
return ticks ? 1 : 0;
}
#endif