usb_osal_freertos.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @file usb_osal_freertos.c
  3. * @brief
  4. *
  5. * Copyright (c) 2022 sakumisu
  6. *
  7. * Licensed to the Apache Software Foundation (ASF) under one or more
  8. * contributor license agreements. See the NOTICE file distributed with
  9. * this work for additional information regarding copyright ownership. The
  10. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  11. * "License"); you may not use this file except in compliance with the
  12. * License. You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  19. * License for the specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. */
  23. #include "usb_osal.h"
  24. #include "usb_errno.h"
  25. #include <FreeRTOS.h>
  26. #include "semphr.h"
  27. #include "timers.h"
  28. #include "event_groups.h"
  29. 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)
  30. {
  31. TaskHandle_t htask = NULL;
  32. stack_size /= sizeof(StackType_t);
  33. xTaskCreate(entry, name, stack_size, args, prio, &htask);
  34. return (usb_osal_thread_t)htask;
  35. }
  36. void usb_osal_thread_suspend(usb_osal_thread_t thread)
  37. {
  38. vTaskSuspend(thread);
  39. }
  40. void usb_osal_thread_resume(usb_osal_thread_t thread)
  41. {
  42. vTaskResume(thread);
  43. }
  44. usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count)
  45. {
  46. return (usb_osal_sem_t)xSemaphoreCreateCounting(1, initial_count);
  47. }
  48. void usb_osal_sem_delete(usb_osal_sem_t sem)
  49. {
  50. vSemaphoreDelete((SemaphoreHandle_t)sem);
  51. }
  52. int usb_osal_sem_take(usb_osal_sem_t sem, uint32_t timeout)
  53. {
  54. return (xSemaphoreTake((SemaphoreHandle_t)sem, pdMS_TO_TICKS(timeout)) == pdPASS) ? 0 : -ETIMEDOUT;
  55. }
  56. int usb_osal_sem_give(usb_osal_sem_t sem)
  57. {
  58. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  59. uint32_t intstatus = 0;
  60. int ret;
  61. #ifdef __riscv
  62. /* Obtain the level of the currently executing interrupt. */
  63. __asm volatile("csrr %0, mintstatus" : "=r"(intstatus)::"memory");
  64. #else
  65. /* Obtain the number of the currently executing interrupt. */
  66. __asm volatile ( "mrs %0, ipsr" : "=r" ( intstatus )::"memory" );
  67. #endif
  68. if (intstatus == 0) {
  69. ret = xSemaphoreGive((SemaphoreHandle_t)sem);
  70. } else {
  71. ret = xSemaphoreGiveFromISR((SemaphoreHandle_t)sem, &xHigherPriorityTaskWoken);
  72. if (ret == pdPASS) {
  73. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  74. }
  75. }
  76. return (ret == pdPASS) ? 0 : -EINVAL;
  77. }
  78. usb_osal_mutex_t usb_osal_mutex_create(void)
  79. {
  80. return (usb_osal_mutex_t)xSemaphoreCreateMutex();
  81. }
  82. void usb_osal_mutex_delete(usb_osal_mutex_t mutex)
  83. {
  84. vSemaphoreDelete((SemaphoreHandle_t)mutex);
  85. }
  86. int usb_osal_mutex_take(usb_osal_mutex_t mutex)
  87. {
  88. return (xSemaphoreTake((SemaphoreHandle_t)mutex, portMAX_DELAY) == pdPASS) ? 0 : -ETIMEDOUT;
  89. }
  90. int usb_osal_mutex_give(usb_osal_mutex_t mutex)
  91. {
  92. return (xSemaphoreGive((SemaphoreHandle_t)mutex) == pdPASS) ? 0 : -EINVAL;
  93. }
  94. usb_osal_event_t usb_osal_event_create(void)
  95. {
  96. return (usb_osal_event_t)xEventGroupCreate();
  97. }
  98. void usb_osal_event_delete(usb_osal_event_t event)
  99. {
  100. vEventGroupDelete((EventGroupHandle_t)event);
  101. }
  102. int usb_osal_event_recv(usb_osal_event_t event, uint32_t set, uint32_t *recved)
  103. {
  104. *recved = xEventGroupWaitBits((EventGroupHandle_t)event, set, pdTRUE, pdFALSE, portMAX_DELAY);
  105. return 0;
  106. }
  107. int usb_osal_event_send(usb_osal_event_t event, uint32_t set)
  108. {
  109. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  110. int ret;
  111. ret = xEventGroupSetBitsFromISR((EventGroupHandle_t)event, set, &xHigherPriorityTaskWoken);
  112. if (ret == pdPASS) {
  113. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  114. }
  115. return (ret == pdPASS) ? 0 : -EINVAL;
  116. }
  117. size_t usb_osal_enter_critical_section(void)
  118. {
  119. taskDISABLE_INTERRUPTS();
  120. return 1;
  121. }
  122. void usb_osal_leave_critical_section(size_t flag)
  123. {
  124. taskENABLE_INTERRUPTS();
  125. }
  126. void usb_osal_msleep(uint32_t delay)
  127. {
  128. vTaskDelay(pdMS_TO_TICKS(delay));
  129. }