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
|
/*
* Copyright (c) 2022, sakumisu
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "usb_osal.h"
#include "usb_errno.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/timers.h"
#include "freertos/event_groups.h"
#include "esp_timer.h"
static portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
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)
{
TaskHandle_t htask = NULL;
stack_size /= sizeof(StackType_t);
xTaskCreatePinnedToCore(entry, name, stack_size, args, configMAX_PRIORITIES - 1 - prio, &htask, CONFIG_ESP_TIMER_TASK_AFFINITY);
return (usb_osal_thread_t)htask;
}
void usb_osal_thread_delete(usb_osal_thread_t thread)
{
vTaskDelete(thread);
}
void usb_osal_thread_schedule_other(void)
{
TaskHandle_t xCurrentTask = xTaskGetCurrentTaskHandle();
const UBaseType_t old_priority = uxTaskPriorityGet(xCurrentTask);
vTaskPrioritySet(xCurrentTask, tskIDLE_PRIORITY);
taskYIELD();
vTaskPrioritySet(xCurrentTask, old_priority);
}
usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count)
{
return (usb_osal_sem_t)xSemaphoreCreateCounting(1, initial_count);
}
usb_osal_sem_t usb_osal_sem_create_counting(uint32_t max_count)
{
return (usb_osal_sem_t)xSemaphoreCreateCounting(0, max_count);
}
void usb_osal_sem_delete(usb_osal_sem_t sem)
{
vSemaphoreDelete((SemaphoreHandle_t)sem);
}
int usb_osal_sem_take(usb_osal_sem_t sem, uint32_t timeout)
{
if (timeout == USB_OSAL_WAITING_FOREVER) {
return (xSemaphoreTake((SemaphoreHandle_t)sem, portMAX_DELAY) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
} else {
return (xSemaphoreTake((SemaphoreHandle_t)sem, pdMS_TO_TICKS(timeout)) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
}
}
int usb_osal_sem_give(usb_osal_sem_t sem)
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
int ret;
if (xPortInIsrContext()) {
ret = xSemaphoreGiveFromISR((SemaphoreHandle_t)sem, &xHigherPriorityTaskWoken);
if (ret == pdPASS) {
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
} else {
ret = xSemaphoreGive((SemaphoreHandle_t)sem);
}
return (ret == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
}
void usb_osal_sem_reset(usb_osal_sem_t sem)
{
xQueueReset((QueueHandle_t)sem);
}
usb_osal_mutex_t usb_osal_mutex_create(void)
{
return (usb_osal_mutex_t)xSemaphoreCreateMutex();
}
void usb_osal_mutex_delete(usb_osal_mutex_t mutex)
{
vSemaphoreDelete((SemaphoreHandle_t)mutex);
}
int usb_osal_mutex_take(usb_osal_mutex_t mutex)
{
return (xSemaphoreTake((SemaphoreHandle_t)mutex, portMAX_DELAY) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
}
int usb_osal_mutex_give(usb_osal_mutex_t mutex)
{
return (xSemaphoreGive((SemaphoreHandle_t)mutex) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
}
usb_osal_mq_t usb_osal_mq_create(uint32_t max_msgs)
{
return (usb_osal_mq_t)xQueueCreate(max_msgs, sizeof(uintptr_t));
}
void usb_osal_mq_delete(usb_osal_mq_t mq)
{
vQueueDelete((QueueHandle_t)mq);
}
int usb_osal_mq_send(usb_osal_mq_t mq, uintptr_t addr)
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
int ret;
if (xPortInIsrContext()) {
ret = xQueueSendFromISR((usb_osal_mq_t)mq, &addr, &xHigherPriorityTaskWoken);
if (ret == pdPASS) {
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
} else {
ret = xQueueSend((usb_osal_mq_t)mq, &addr, 0xffffffff);
}
return (ret == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
}
int usb_osal_mq_recv(usb_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
int ret;
if (xPortInIsrContext()) {
ret = xQueueReceiveFromISR((usb_osal_mq_t)mq, addr, &xHigherPriorityTaskWoken);
if (ret == pdPASS) {
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
return (ret == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
} else {
if (timeout == USB_OSAL_WAITING_FOREVER) {
return (xQueueReceive((usb_osal_mq_t)mq, addr, portMAX_DELAY) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
} else {
return (xQueueReceive((usb_osal_mq_t)mq, addr, pdMS_TO_TICKS(timeout)) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
}
}
}
static void usb_timeout(void *arg)
{
struct usb_osal_timer *timer = (struct usb_osal_timer *)arg;
timer->handler(timer->argument);
}
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)
{
struct usb_osal_timer *timer;
timer = pvPortMalloc(sizeof(struct usb_osal_timer));
if (timer == NULL) {
return NULL;
}
esp_timer_create_args_t timer_args = {
.callback = usb_timeout,
// argument specified here will be passed to timer callback function
.arg = (void *)timer,
.dispatch_method = ESP_TIMER_TASK,
.name = name,
.skip_unhandled_events = true
};
timer->handler = handler;
timer->argument = argument;
timer->is_period = is_period;
timer->timeout_ms = timeout_ms;
if (esp_timer_create(&timer_args, (esp_timer_handle_t *)&timer->timer) != ESP_OK) {
vPortFree(timer);
return NULL;
}
return timer;
}
void usb_osal_timer_delete(struct usb_osal_timer *timer)
{
esp_timer_stop((esp_timer_handle_t)timer->timer);
esp_timer_delete((esp_timer_handle_t)timer->timer);
vPortFree(timer);
}
void usb_osal_timer_start(struct usb_osal_timer *timer)
{
esp_timer_stop((esp_timer_handle_t)timer->timer);
if (timer->is_period) {
esp_timer_start_periodic((esp_timer_handle_t)timer->timer, ((uint64_t)timer->timeout_ms) * 1000);
} else {
esp_timer_start_once((esp_timer_handle_t)timer->timer, ((uint64_t)timer->timeout_ms) * 1000);
}
}
void usb_osal_timer_stop(struct usb_osal_timer *timer)
{
esp_timer_stop((esp_timer_handle_t)timer->timer);
}
size_t usb_osal_enter_critical_section(void)
{
portENTER_CRITICAL_SAFE(&spinlock);
return 0;
}
void usb_osal_leave_critical_section(size_t flag)
{
portEXIT_CRITICAL_SAFE(&spinlock);
}
void usb_osal_msleep(uint32_t delay)
{
vTaskDelay(pdMS_TO_TICKS(delay));
}
void *usb_osal_malloc(size_t size)
{
return malloc(size);
}
void usb_osal_free(void *ptr)
{
free(ptr);
}
|