tinyusb_port.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2021 RT-Thread Development Team (rt-thread.io)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. #ifdef __RTTHREAD__
  26. #include <rtthread.h>
  27. #define DBG_TAG "TinyUSB"
  28. #define DBG_LVL DBG_INFO
  29. #include <rtdbg.h>
  30. #include <tusb.h>
  31. extern int tusb_board_init(void);
  32. #ifndef RT_USING_HEAP
  33. /* if there is not enable heap, we should use static thread and stack. */
  34. static rt_uint8_t tusb_stack[PKG_TINYUSB_STACK_SIZE];
  35. static struct rt_thread tusb_thread;
  36. #endif /* RT_USING_HEAP */
  37. static void tusb_thread_entry(void *parameter) {
  38. (void) parameter;
  39. while (1) {
  40. tud_task();
  41. }
  42. }
  43. static int init_tinyusb(void) {
  44. rt_thread_t tid;
  45. tusb_board_init();
  46. tusb_init();
  47. #ifdef RT_USING_HEAP
  48. tid = rt_thread_create("tusb", tusb_thread_entry, RT_NULL,
  49. PKG_TINYUSB_STACK_SIZE, 4, 10);
  50. if (tid == RT_NULL) {
  51. LOG_E("Fail to create TinyUSB thread");
  52. return -1;
  53. }
  54. #else
  55. rt_err_t result;
  56. tid = &tusb_thread;
  57. result = rt_thread_init(tid, "tusb", tusb_thread_entry, RT_NULL,
  58. tusb_stack, sizeof(tusb_stack), 4, 10);
  59. if (tid != RT_EOK) {
  60. LOG_E("Fail to create TinyUSB thread");
  61. return -1;
  62. }
  63. #endif
  64. rt_thread_startup(tid);
  65. return 0;
  66. }
  67. INIT_COMPONENT_EXPORT(init_tinyusb);
  68. #endif