usb_osal_idf.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. usb_osal_sem_t usb_osal_sem_create_counting(uint32_t max_count)
  38. {
  39. return (usb_osal_sem_t)xSemaphoreCreateCounting(0, max_count);
  40. }
  41. void usb_osal_sem_delete(usb_osal_sem_t sem)
  42. {
  43. vSemaphoreDelete((SemaphoreHandle_t)sem);
  44. }
  45. int usb_osal_sem_take(usb_osal_sem_t sem, uint32_t timeout)
  46. {
  47. if (timeout == USB_OSAL_WAITING_FOREVER) {
  48. return (xSemaphoreTake((SemaphoreHandle_t)sem, portMAX_DELAY) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  49. } else {
  50. return (xSemaphoreTake((SemaphoreHandle_t)sem, pdMS_TO_TICKS(timeout)) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  51. }
  52. }
  53. int usb_osal_sem_give(usb_osal_sem_t sem)
  54. {
  55. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  56. int ret;
  57. if (xPortInIsrContext()) {
  58. ret = xSemaphoreGiveFromISR((SemaphoreHandle_t)sem, &xHigherPriorityTaskWoken);
  59. if (ret == pdPASS) {
  60. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  61. }
  62. } else {
  63. ret = xSemaphoreGive((SemaphoreHandle_t)sem);
  64. }
  65. return (ret == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  66. }
  67. void usb_osal_sem_reset(usb_osal_sem_t sem)
  68. {
  69. xQueueReset((QueueHandle_t)sem);
  70. }
  71. usb_osal_mutex_t usb_osal_mutex_create(void)
  72. {
  73. return (usb_osal_mutex_t)xSemaphoreCreateMutex();
  74. }
  75. void usb_osal_mutex_delete(usb_osal_mutex_t mutex)
  76. {
  77. vSemaphoreDelete((SemaphoreHandle_t)mutex);
  78. }
  79. int usb_osal_mutex_take(usb_osal_mutex_t mutex)
  80. {
  81. return (xSemaphoreTake((SemaphoreHandle_t)mutex, portMAX_DELAY) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  82. }
  83. int usb_osal_mutex_give(usb_osal_mutex_t mutex)
  84. {
  85. return (xSemaphoreGive((SemaphoreHandle_t)mutex) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  86. }
  87. usb_osal_mq_t usb_osal_mq_create(uint32_t max_msgs)
  88. {
  89. return (usb_osal_mq_t)xQueueCreate(max_msgs, sizeof(uintptr_t));
  90. }
  91. void usb_osal_mq_delete(usb_osal_mq_t mq)
  92. {
  93. vQueueDelete((QueueHandle_t)mq);
  94. }
  95. int usb_osal_mq_send(usb_osal_mq_t mq, uintptr_t addr)
  96. {
  97. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  98. int ret;
  99. if (xPortInIsrContext()) {
  100. ret = xQueueSendFromISR((usb_osal_mq_t)mq, &addr, &xHigherPriorityTaskWoken);
  101. if (ret == pdPASS) {
  102. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  103. }
  104. } else {
  105. ret = xQueueSend((usb_osal_mq_t)mq, &addr, 0xffffffff);
  106. }
  107. return (ret == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  108. }
  109. int usb_osal_mq_recv(usb_osal_mq_t mq, uintptr_t *addr, uint32_t timeout)
  110. {
  111. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  112. int ret;
  113. if (xPortInIsrContext()) {
  114. ret = xQueueReceiveFromISR((usb_osal_mq_t)mq, addr, &xHigherPriorityTaskWoken);
  115. if (ret == pdPASS) {
  116. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  117. }
  118. return (ret == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  119. } else {
  120. if (timeout == USB_OSAL_WAITING_FOREVER) {
  121. return (xQueueReceive((usb_osal_mq_t)mq, addr, portMAX_DELAY) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  122. } else {
  123. return (xQueueReceive((usb_osal_mq_t)mq, addr, pdMS_TO_TICKS(timeout)) == pdPASS) ? 0 : -USB_ERR_TIMEOUT;
  124. }
  125. }
  126. }
  127. static void usb_timeout(void *arg)
  128. {
  129. struct usb_osal_timer *timer = (struct usb_osal_timer *)arg;
  130. timer->handler(timer->argument);
  131. }
  132. 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)
  133. {
  134. struct usb_osal_timer *timer;
  135. timer = pvPortMalloc(sizeof(struct usb_osal_timer));
  136. if (timer == NULL) {
  137. return NULL;
  138. }
  139. esp_timer_create_args_t timer_args = {
  140. .callback = usb_timeout,
  141. // argument specified here will be passed to timer callback function
  142. .arg = (void *)timer,
  143. .dispatch_method = ESP_TIMER_TASK,
  144. .name = name,
  145. .skip_unhandled_events = true
  146. };
  147. timer->handler = handler;
  148. timer->argument = argument;
  149. timer->is_period = is_period;
  150. timer->timeout_ms = timeout_ms;
  151. if (esp_timer_create(&timer_args, (esp_timer_handle_t *)&timer->timer) != ESP_OK) {
  152. vPortFree(timer);
  153. return NULL;
  154. }
  155. return timer;
  156. }
  157. void usb_osal_timer_delete(struct usb_osal_timer *timer)
  158. {
  159. esp_timer_stop((esp_timer_handle_t)timer->timer);
  160. esp_timer_delete((esp_timer_handle_t)timer->timer);
  161. vPortFree(timer);
  162. }
  163. void usb_osal_timer_start(struct usb_osal_timer *timer)
  164. {
  165. esp_timer_stop((esp_timer_handle_t)timer->timer);
  166. if (timer->is_period) {
  167. esp_timer_start_periodic((esp_timer_handle_t)timer->timer, ((uint64_t)timer->timeout_ms) * 1000);
  168. } else {
  169. esp_timer_start_once((esp_timer_handle_t)timer->timer, ((uint64_t)timer->timeout_ms) * 1000);
  170. }
  171. }
  172. void usb_osal_timer_stop(struct usb_osal_timer *timer)
  173. {
  174. esp_timer_stop((esp_timer_handle_t)timer->timer);
  175. }
  176. size_t usb_osal_enter_critical_section(void)
  177. {
  178. portENTER_CRITICAL_SAFE(&spinlock);
  179. return 0;
  180. }
  181. void usb_osal_leave_critical_section(size_t flag)
  182. {
  183. portEXIT_CRITICAL_SAFE(&spinlock);
  184. }
  185. void usb_osal_msleep(uint32_t delay)
  186. {
  187. vTaskDelay(pdMS_TO_TICKS(delay));
  188. }
  189. void *usb_osal_malloc(size_t size)
  190. {
  191. return malloc(size);
  192. }
  193. void usb_osal_free(void *ptr)
  194. {
  195. free(ptr);
  196. }