summaryrefslogtreecommitdiff
path: root/osal/usb_osal_liteos_m.c
blob: 543af77798519bbfa61d7b9d4600342ba242d843 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (c) 2025, HPMicro
 * SPDX-License-Identifier: Apache-2.0
 */
#include "usb_osal.h"
#include "usb_errno.h"
#include "usb_config.h"
#include "usb_log.h"

#include "los_interrupt.h"
#include "los_task.h"
#include "los_queue.h"
#include "los_sem.h"
#include "los_mux.h"
#include "los_memory.h"
#include "los_swtmr.h"

usb_osal_thread_t usb_osal_thread_create(const char *name, uint32_t stack_size, uint32_t prio, usb_thread_entry_t entry, void *args)
{
    UINT32 task_id;
    UINT32 ret;

    TSK_INIT_PARAM_S task_init_param = {
        .usTaskPrio = prio,
        .pfnTaskEntry = (TSK_ENTRY_FUNC)entry,
        .uwStackSize = stack_size,
        .uwArg = (UINT32)args,
        .pcName = name
    };

    ret = LOS_TaskCreate(&task_id, &task_init_param);
    if (ret != LOS_OK) {
        return NULL;
    }

    return (usb_osal_thread_t)task_id;
}

void usb_osal_thread_delete(usb_osal_thread_t thread)
{
    UINT32 task_id = (UINT32)thread;

    if (thread == NULL) {
        task_id = LOS_CurTaskIDGet();
    }

    LOS_TaskDelete(task_id);
}

void usb_osal_thread_schedule_other(void)
{
    UINT32 task_id = LOS_CurTaskIDGet();
    const UINT16 old_priority = LOS_TaskPriGet(task_id);

    LOS_TaskPriSet(task_id, OS_TASK_PRIORITY_LOWEST);

    LOS_TaskYield();

    LOS_TaskPriSet(task_id, old_priority);
}

usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count)
{
    UINT32 sem_handle;
    UINT32 ret;

    ret = LOS_SemCreate(initial_count, &sem_handle);
    if (ret != LOS_OK) {
        return NULL;
    }
    return (usb_osal_sem_t)sem_handle;
}

usb_osal_sem_t usb_osal_sem_create_counting(uint32_t max_count)
{
    return usb_osal_sem_create(0);
}

void usb_osal_sem_delete(usb_osal_sem_t sem)
{
    UINT32 sem_handle = (UINT32)sem;

    LOS_SemDelete(sem_handle);
}

int usb_osal_sem_take(usb_osal_sem_t sem, uint32_t timeout)
{
    UINT32 sem_handle = (UINT32)sem;
    UINT32 ret;

    ret = LOS_SemPend(sem_handle,
                      timeout == USB_OSAL_WAITING_FOREVER ?
                          LOS_WAIT_FOREVER :
                          timeout);

    return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
}

int usb_osal_sem_give(usb_osal_sem_t sem)
{
    UINT32 sem_handle = (UINT32)sem;
    UINT32 ret;

    ret = LOS_SemPost(sem_handle);

    return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
}

void usb_osal_sem_reset(usb_osal_sem_t sem)
{
}

usb_osal_mutex_t usb_osal_mutex_create(void)
{
    UINT32 mux_handle;
    UINT32 ret;

    ret = LOS_MuxCreate(&mux_handle);
    if (ret != LOS_OK) {
        return NULL;
    }
    return (usb_osal_mutex_t)mux_handle;
}

void usb_osal_mutex_delete(usb_osal_mutex_t mutex)
{
    UINT32 mux_handle = (UINT32)mutex;

    LOS_MuxDelete(mux_handle);
}

int usb_osal_mutex_take(usb_osal_mutex_t mutex)
{
    UINT32 mux_handle = (UINT32)mutex;
    UINT32 ret;

    ret = LOS_MuxPend(mux_handle, LOS_WAIT_FOREVER);

    return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
}

int usb_osal_mutex_give(usb_osal_mutex_t mutex)
{
    UINT32 mux_handle = (UINT32)mutex;
    UINT32 ret;

    ret = LOS_MuxPost(mux_handle);

    return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
}

usb_osal_mq_t usb_osal_mq_create(uint32_t max_msgs)
{
    UINT32 queue_id;
    UINT32 ret;

    ret = LOS_QueueCreate(NULL, max_msgs, &queue_id, 0, sizeof(uintptr_t));
    if (ret != LOS_OK) {
        return NULL;
    }
    return (usb_osal_mq_t)queue_id;
}

void usb_osal_mq_delete(usb_osal_mq_t mq)
{
    UINT32 queue_id = (UINT32)mq;

    LOS_QueueDelete(queue_id);
}

int usb_osal_mq_send(usb_osal_mq_t mq, uintptr_t addr)
{
    UINT32 queue_id = (UINT32)mq;
    UINT32 ret;
    UINT32 timeout;

    if (OS_INT_ACTIVE)
        timeout = LOS_NO_WAIT;
    else
        timeout = LOS_WAIT_FOREVER;

    ret = LOS_QueueWrite(queue_id, addr, sizeof(uintptr_t), timeout);

    return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
}

int usb_osal_mq_recv(usb_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
{
    UINT32 queue_id = (UINT32)mq;
    UINT32 ret;
    UINT32 _timeout;

    if (OS_INT_ACTIVE)
        _timeout = LOS_NO_WAIT;
    else
        _timeout = timeout == USB_OSAL_WAITING_FOREVER ? LOS_WAIT_FOREVER : timeout;

    ret = LOS_QueueRead(queue_id, addr, sizeof(uintptr_t), _timeout);
    return ret == LOS_OK ? 0 : -USB_ERR_TIMEOUT;
}

struct usb_osal_timer *usb_osal_timer_create(const char *name, uint32_t timeout_ms, usb_timer_handler_t handler, void *argument, bool is_period)
{
    UINT32 timer_id;
    UINT32 ret;
    struct usb_osal_timer *timer_handle;

    timer_handle = usb_osal_malloc(sizeof(struct usb_osal_timer));
    if (timer_handle == NULL) {
        return NULL;
    }
    memset(timer_handle, 0x00, sizeof(struct usb_osal_timer));

    ret = LOS_SwtmrCreate(timeout_ms,
                          is_period ? LOS_SWTMR_MODE_PERIOD : LOS_SWTMR_MODE_NO_SELFDELETE,
                          (SWTMR_PROC_FUNC)handler, &timer_id, (UINT32)argument
#if (LOSCFG_BASE_CORE_SWTMR_ALIGN == 1)
                          ,
                          OS_SWTMR_ROUSES_IGNORE, OS_SWTMR_ALIGN_INSENSITIVE
#endif
    );

    if (ret != LOS_OK) {
        usb_osal_free(timer_handle);
        return NULL;
    }

    timer_handle->handler = handler;
    timer_handle->argument = argument;
    timer_handle->is_period = is_period;
    timer_handle->timeout_ms = timeout_ms;
    timer_handle->timer = (void *)timer_id;

    return timer_handle;
}

void usb_osal_timer_delete(struct usb_osal_timer *timer)
{
    UINT32 timer_id = (UINT32)timer->timer;

    LOS_SwtmrDelete(timer_id);
    usb_osal_free(timer);
}

void usb_osal_timer_start(struct usb_osal_timer *timer)
{
    UINT32 timer_id = (UINT32)timer->timer;

    LOS_SwtmrStart(timer_id);
}

void usb_osal_timer_stop(struct usb_osal_timer *timer)
{
    UINT32 timer_id = (UINT32)timer->timer;

    LOS_SwtmrStop(timer_id);
}

size_t usb_osal_enter_critical_section(void)
{
    return LOS_IntLock();
}

void usb_osal_leave_critical_section(size_t flag)
{
    LOS_IntRestore(flag);
}

void usb_osal_msleep(uint32_t delay)
{
    LOS_Msleep(delay);
}

void *usb_osal_malloc(size_t size)
{
    return LOS_MemAlloc((VOID *)OS_SYS_MEM_ADDR, size);
}

void usb_osal_free(void *ptr)
{
    if (ptr != NULL) {
        LOS_MemFree((VOID *)OS_SYS_MEM_ADDR, ptr);
    }
}