usb_osal_idf.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usb_osal.h"
  7. #include "usb_errno.h"
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/semphr.h"
  10. #include "freertos/timers.h"
  11. #include "freertos/event_groups.h"
  12. #include "esp_timer.h"
  13. static portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
  14. 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)
  15. {
  16. TaskHandle_t htask = NULL;
  17. stack_size /= sizeof(StackType_t);
  18. xTaskCreatePinnedToCore(entry, name, stack_size, args, configMAX_PRIORITIES - 1 - prio, &htask, CONFIG_ESP_TIMER_TASK_AFFINITY);
  19. return (usb_osal_thread_t)htask;
  20. }
  21. void usb_osal_thread_delete(usb_osal_thread_t thread)
  22. {
  23. vTaskDelete(thread);
  24. }
  25. void usb_osal_thread_schedule_other(void)
  26. {
  27. TaskHandle_t xCurrentTask = xTaskGetCurrentTaskHandle();
  28. const UBaseType_t old_priority = uxTaskPriorityGet(xCurrentTask);
  29. vTaskPrioritySet(xCurrentTask, tskIDLE_PRIORITY);
  30. taskYIELD();
  31. vTaskPrioritySet(xCurrentTask, old_priority);
  32. }
  33. usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count)
  34. {
  35. return (usb_osal_sem_t)xSemaphoreCreateCounting(1, initial_count);
  36. }
  37. void usb_osal_sem_delete(usb_osal_sem_t sem)
  38. {
  39. vSemaphoreDelete((SemaphoreHandle_t)sem);
  40. }
  41. int usb_osal_sem_take(usb_osal_sem_t sem, uint32_t timeout)
  42. {
  43. if (timeout == USB_OSAL_WAITING_FOREVER) {
  44. return (xSemaphoreTake((SemaphoreHandle_t)sem, portMAX_DELAY) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  45. } else {
  46. return (xSemaphoreTake((SemaphoreHandle_t)sem, pdMS_TO_TICKS(timeout)) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  47. }
  48. }
  49. int usb_osal_sem_give(usb_osal_sem_t sem)
  50. {
  51. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  52. int ret;
  53. if (xPortInIsrContext()) {
  54. ret = xSemaphoreGiveFromISR((SemaphoreHandle_t)sem, &xHigherPriorityTaskWoken);
  55. if (ret == pdPASS) {
  56. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  57. }
  58. } else {
  59. ret = xSemaphoreGive((SemaphoreHandle_t)sem);
  60. }
  61. return (ret == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  62. }
  63. void usb_osal_sem_reset(usb_osal_sem_t sem)
  64. {
  65. xQueueReset((QueueHandle_t)sem);
  66. }
  67. usb_osal_mutex_t usb_osal_mutex_create(void)
  68. {
  69. return (usb_osal_mutex_t)xSemaphoreCreateMutex();
  70. }
  71. void usb_osal_mutex_delete(usb_osal_mutex_t mutex)
  72. {
  73. vSemaphoreDelete((SemaphoreHandle_t)mutex);
  74. }
  75. int usb_osal_mutex_take(usb_osal_mutex_t mutex)
  76. {
  77. return (xSemaphoreTake((SemaphoreHandle_t)mutex, portMAX_DELAY) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  78. }
  79. int usb_osal_mutex_give(usb_osal_mutex_t mutex)
  80. {
  81. return (xSemaphoreGive((SemaphoreHandle_t)mutex) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  82. }
  83. usb_osal_mq_t usb_osal_mq_create(uint32_t max_msgs)
  84. {
  85. return (usb_osal_mq_t)xQueueCreate(max_msgs, sizeof(uintptr_t));
  86. }
  87. void usb_osal_mq_delete(usb_osal_mq_t mq)
  88. {
  89. vQueueDelete((QueueHandle_t)mq);
  90. }
  91. int usb_osal_mq_send(usb_osal_mq_t mq, uintptr_t addr)
  92. {
  93. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  94. int ret;
  95. if (xPortInIsrContext()) {
  96. ret = xQueueSendFromISR((usb_osal_mq_t)mq, &addr, &xHigherPriorityTaskWoken);
  97. if (ret == pdPASS) {
  98. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  99. }
  100. } else {
  101. ret = xQueueSend((usb_osal_mq_t)mq, &addr, 0xffffffff);
  102. }
  103. return (ret == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  104. }
  105. int usb_osal_mq_recv(usb_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
  106. {
  107. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  108. int ret;
  109. if (xPortInIsrContext()) {
  110. ret = xQueueReceiveFromISR((usb_osal_mq_t)mq, addr, &xHigherPriorityTaskWoken);
  111. if (ret == pdPASS) {
  112. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  113. }
  114. return (ret == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  115. } else {
  116. if (timeout == USB_OSAL_WAITING_FOREVER) {
  117. return (xQueueReceive((usb_osal_mq_t)mq, addr, portMAX_DELAY) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  118. } else {
  119. return (xQueueReceive((usb_osal_mq_t)mq, addr, pdMS_TO_TICKS(timeout)) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  120. }
  121. }
  122. }
  123. static void usb_timeout(void *arg)
  124. {
  125. struct usb_osal_timer *timer = (struct usb_osal_timer *)arg;
  126. timer->handler(timer->argument);
  127. }
  128. 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)
  129. {
  130. struct usb_osal_timer *timer;
  131. timer = pvPortMalloc(sizeof(struct usb_osal_timer));
  132. if (timer == NULL) {
  133. return NULL;
  134. }
  135. esp_timer_create_args_t timer_args = {
  136. .callback = usb_timeout,
  137. // argument specified here will be passed to timer callback function
  138. .arg = (void *)timer,
  139. .dispatch_method = ESP_TIMER_TASK,
  140. .name = name,
  141. .skip_unhandled_events = true
  142. };
  143. timer->handler = handler;
  144. timer->argument = argument;
  145. timer->is_period = is_period;
  146. timer->timeout_ms = timeout_ms;
  147. if (esp_timer_create(&timer_args, (esp_timer_handle_t *)&timer->timer) != ESP_OK) {
  148. vPortFree(timer);
  149. return NULL;
  150. }
  151. return timer;
  152. }
  153. void usb_osal_timer_delete(struct usb_osal_timer *timer)
  154. {
  155. esp_timer_stop((esp_timer_handle_t)timer->timer);
  156. esp_timer_delete((esp_timer_handle_t)timer->timer);
  157. vPortFree(timer);
  158. }
  159. void usb_osal_timer_start(struct usb_osal_timer *timer)
  160. {
  161. esp_timer_stop((esp_timer_handle_t)timer->timer);
  162. if (timer->is_period) {
  163. esp_timer_start_periodic((esp_timer_handle_t)timer->timer, ((uint64_t)timer->timeout_ms) * 1000);
  164. } else {
  165. esp_timer_start_once((esp_timer_handle_t)timer->timer, ((uint64_t)timer->timeout_ms) * 1000);
  166. }
  167. }
  168. void usb_osal_timer_stop(struct usb_osal_timer *timer)
  169. {
  170. esp_timer_stop((esp_timer_handle_t)timer->timer);
  171. }
  172. size_t usb_osal_enter_critical_section(void)
  173. {
  174. portENTER_CRITICAL_SAFE(&spinlock);
  175. return 0;
  176. }
  177. void usb_osal_leave_critical_section(size_t flag)
  178. {
  179. portEXIT_CRITICAL_SAFE(&spinlock);
  180. }
  181. void usb_osal_msleep(uint32_t delay)
  182. {
  183. vTaskDelay(pdMS_TO_TICKS(delay));
  184. }
  185. void *usb_osal_malloc(size_t size)
  186. {
  187. return malloc(size);
  188. }
  189. void usb_osal_free(void *ptr)
  190. {
  191. free(ptr);
  192. }