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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
/*
* Copyright (c) 2024, sakumisu
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "usb_osal.h"
#include "usb_errno.h"
#include "usb_config.h"
#include "usb_log.h"
#include "tx_api.h"
extern TX_BYTE_POOL usb_byte_pool; // define usb_byte_pool and call usb_osal_init first
usb_osal_mq_t usb_osal_mq;
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)
{
TX_THREAD *thread_ptr = TX_NULL;
tx_byte_allocate(&usb_byte_pool, (VOID **)&thread_ptr, USB_ALIGN_UP(sizeof(TX_THREAD), 4) + stack_size, TX_NO_WAIT);
if (thread_ptr == TX_NULL) {
USB_LOG_ERR("Create thread %s failed\r\n", name);
while (1) {
}
}
tx_thread_create(thread_ptr, (CHAR *)name, (VOID(*)(ULONG))entry, (uintptr_t)args,
((CHAR *)thread_ptr + USB_ALIGN_UP(sizeof(TX_THREAD), 4)), stack_size,
prio, prio, TX_NO_TIME_SLICE, TX_AUTO_START);
return (usb_osal_thread_t)thread_ptr;
}
void usb_osal_thread_delete(usb_osal_thread_t thread)
{
if (thread == NULL) {
thread = tx_thread_identify();
usb_osal_mq_send(usb_osal_mq, (uintptr_t)thread);
tx_thread_terminate(thread);
return;
}
tx_thread_terminate(thread);
tx_thread_delete(thread);
tx_byte_release(thread);
}
void usb_osal_thread_schedule_other(void)
{
TX_THREAD *current_thread = tx_thread_identify();
const UINT old_priority = current_thread->tx_thread_priority;
tx_thread_priority_change(current_thread, TX_MAX_PRIORITIES - 1, &old_priority);
tx_thread_relinquish();
tx_thread_priority_change(current_thread, old_priority, &old_priority);
}
usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count)
{
TX_SEMAPHORE *sem_ptr = TX_NULL;
tx_byte_allocate(&usb_byte_pool, (VOID **)&sem_ptr, sizeof(TX_SEMAPHORE), TX_NO_WAIT);
if (sem_ptr == TX_NULL) {
USB_LOG_ERR("Create semaphore failed\r\n");
while (1) {
}
}
tx_semaphore_create(sem_ptr, "usbh_sem", initial_count);
return (usb_osal_sem_t)sem_ptr;
}
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)
{
tx_semaphore_delete((TX_SEMAPHORE *)sem);
tx_byte_release(sem);
}
int usb_osal_sem_take(usb_osal_sem_t sem, uint32_t timeout)
{
int ret = 0;
ret = tx_semaphore_get((TX_SEMAPHORE *)sem, timeout);
if (ret == TX_SUCCESS) {
ret = 0;
} else if ((ret == TX_WAIT_ABORTED) || (ret == TX_NO_INSTANCE)) {
ret = -USB_ERR_TIMEOUT;
} else {
ret = -USB_ERR_INVAL;
}
return (int)ret;
}
int usb_osal_sem_give(usb_osal_sem_t sem)
{
return (tx_semaphore_put((TX_SEMAPHORE *)sem) == TX_SUCCESS) ? 0 : -USB_ERR_INVAL;
}
void usb_osal_sem_reset(usb_osal_sem_t sem)
{
tx_semaphore_get((TX_SEMAPHORE *)sem, 0);
}
usb_osal_mutex_t usb_osal_mutex_create(void)
{
TX_MUTEX *mutex_ptr = TX_NULL;
tx_byte_allocate(&usb_byte_pool, (VOID **)&mutex_ptr, sizeof(TX_MUTEX), TX_NO_WAIT);
if (mutex_ptr == TX_NULL) {
return NULL;
}
tx_mutex_create(mutex_ptr, "usbh_mutex", TX_INHERIT);
return (usb_osal_mutex_t)mutex_ptr;
}
void usb_osal_mutex_delete(usb_osal_mutex_t mutex)
{
tx_mutex_delete((TX_MUTEX *)mutex);
tx_byte_release(mutex);
}
int usb_osal_mutex_take(usb_osal_mutex_t mutex)
{
int ret = 0;
ret = tx_mutex_get((TX_MUTEX *)mutex, TX_WAIT_FOREVER);
if (ret == TX_SUCCESS) {
ret = 0;
} else if ((ret == TX_WAIT_ABORTED) || (ret == TX_NO_INSTANCE)) {
ret = -USB_ERR_TIMEOUT;
} else {
ret = -USB_ERR_INVAL;
}
return (int)ret;
}
int usb_osal_mutex_give(usb_osal_mutex_t mutex)
{
return (tx_mutex_put((TX_MUTEX *)mutex) == TX_SUCCESS) ? 0 : -USB_ERR_INVAL;
}
usb_osal_mq_t usb_osal_mq_create(uint32_t max_msgs)
{
TX_QUEUE *queue_ptr = TX_NULL;
tx_byte_allocate(&usb_byte_pool, (VOID **)&queue_ptr, USB_ALIGN_UP(sizeof(TX_QUEUE), 4) + sizeof(uintptr_t) * max_msgs, TX_NO_WAIT);
if (queue_ptr == TX_NULL) {
return NULL;
}
tx_queue_create(queue_ptr, "usbh_mq", sizeof(uintptr_t) / 4, (CHAR *)queue_ptr + USB_ALIGN_UP(sizeof(TX_QUEUE), 4), sizeof(uintptr_t) * max_msgs);
return (usb_osal_mq_t)queue_ptr;
}
void usb_osal_mq_delete(usb_osal_mq_t mq)
{
tx_queue_delete((TX_QUEUE *)mq);
tx_byte_release(mq);
}
int usb_osal_mq_send(usb_osal_mq_t mq, uintptr_t addr)
{
return (tx_queue_send((TX_QUEUE *)mq, &addr, TX_NO_WAIT) == TX_SUCCESS) ? 0 : -USB_ERR_INVAL;
}
int usb_osal_mq_recv(usb_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
{
int ret = 0;
ret = tx_queue_receive((TX_QUEUE *)mq, addr, timeout);
if (ret == TX_SUCCESS) {
ret = 0;
} else if (ret == TX_QUEUE_EMPTY) {
ret = -USB_ERR_TIMEOUT;
} else {
ret = -USB_ERR_INVAL;
}
return (int)ret;
}
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)
{
TX_TIMER *timer_ptr = TX_NULL;
struct usb_osal_timer *timer;
tx_byte_allocate(&usb_byte_pool, (VOID **)&timer, sizeof(struct usb_osal_timer), TX_NO_WAIT);
if (timer == TX_NULL) {
return NULL;
}
memset(timer, 0, sizeof(struct usb_osal_timer));
tx_byte_allocate(&usb_byte_pool, (VOID **)&timer_ptr, sizeof(TX_TIMER), TX_NO_WAIT);
if (timer_ptr == TX_NULL) {
tx_byte_release(timer);
return NULL;
}
timer->timer = timer_ptr;
timer->timeout_ms = timeout_ms;
timer->is_period = is_period;
if (tx_timer_create(timer_ptr, (CHAR *)name, (void (*)(ULONG))handler, (uintptr_t)argument, 1, is_period ? 1 : 0,
TX_NO_ACTIVATE) != TX_SUCCESS) {
tx_byte_release(timer_ptr);
tx_byte_release(timer);
return NULL;
}
return timer;
}
void usb_osal_timer_delete(struct usb_osal_timer *timer)
{
tx_timer_deactivate((TX_TIMER *)timer->timer);
tx_timer_delete((TX_TIMER *)timer->timer);
tx_byte_release(timer->timer);
tx_byte_release(timer);
}
void usb_osal_timer_start(struct usb_osal_timer *timer)
{
if (tx_timer_change((TX_TIMER *)timer->timer, timer->timeout_ms, timer->is_period ? timer->timeout_ms : 0) == TX_SUCCESS) {
/* Call the tx_timer_activate to activates the specified application
timer. The expiration routines of timers that expire at the same
time are executed in the order they were activated. */
if (tx_timer_activate((TX_TIMER *)timer->timer) == TX_SUCCESS) {
/* Return osOK for success */
} else {
/* Return osErrorResource in case of error */
}
} else {
}
}
void usb_osal_timer_stop(struct usb_osal_timer *timer)
{
tx_timer_deactivate((TX_TIMER *)timer->timer);
}
size_t usb_osal_enter_critical_section(void)
{
TX_INTERRUPT_SAVE_AREA
TX_DISABLE
return interrupt_save;
}
void usb_osal_leave_critical_section(size_t flag)
{
TX_INTERRUPT_SAVE_AREA
interrupt_save = flag;
TX_RESTORE
}
void usb_osal_msleep(uint32_t delay)
{
#if TX_TIMER_TICKS_PER_SECOND != 1000
#error "TX_TIMER_TICKS_PER_SECOND must be 1000"
#endif
tx_thread_sleep(delay);
}
void *usb_osal_malloc(size_t size)
{
CHAR *pointer = TX_NULL;
tx_byte_allocate(&usb_byte_pool, (VOID **)&pointer, size, TX_WAIT_FOREVER);
return pointer;
}
void usb_osal_free(void *ptr)
{
tx_byte_release(ptr);
}
static void usb_osal_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
{
int ret;
usb_osal_thread_t thread;
while (1) {
ret = usb_osal_mq_recv(usb_osal_mq, (uintptr_t *)&thread, TX_WAIT_FOREVER);
if (ret < 0) {
continue;
}
tx_thread_delete(thread);
tx_byte_release(thread);
}
}
void usb_osal_init(uint8_t *mem, uint32_t mem_size)
{
usb_osal_thread_t thread;
tx_byte_pool_create(&usb_byte_pool, "usb byte pool", mem, mem_size);
thread = usb_osal_thread_create("usb_osal", 2048, 10, usb_osal_thread, NULL);
if (thread == NULL) {
USB_LOG_ERR("Create usb_osal_thread failed\r\n");
while (1) {
}
}
usb_osal_mq = usb_osal_mq_create(32);
if (usb_osal_mq == NULL) {
USB_LOG_ERR("Create usb_osal_mq failed\r\n");
while (1) {
}
}
}
|