tinyusb_port.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-10-20 tfx2001 first version
  9. */
  10. #include <rtthread.h>
  11. #define DBG_TAG "TinyUSB"
  12. #define DBG_LVL DBG_INFO
  13. #include <rtdbg.h>
  14. #include <tusb.h>
  15. extern int tusb_board_init(void);
  16. #ifndef RT_USING_HEAP
  17. /* if there is not enable heap, we should use static thread and stack. */
  18. static rt_uint8_t tusb_stack[PKG_TINYUSB_STACK_SIZE];
  19. static struct rt_thread tusb_thread;
  20. #endif /* RT_USING_HEAP */
  21. static void tusb_thread_entry(void *parameter)
  22. {
  23. (void) parameter;
  24. while (1)
  25. {
  26. tud_task();
  27. }
  28. }
  29. static int init_tinyusb(void)
  30. {
  31. rt_thread_t tid;
  32. tusb_board_init();
  33. tusb_init();
  34. #ifdef RT_USING_HEAP
  35. tid = rt_thread_create("tusb", tusb_thread_entry, RT_NULL,
  36. PKG_TINYUSB_STACK_SIZE,
  37. PKG_TINYUSB_THREAD_PRIORITY, 10);
  38. if (tid == RT_NULL)
  39. {
  40. LOG_E("Fail to create TinyUSB thread");
  41. return -1;
  42. }
  43. #else
  44. rt_err_t result;
  45. tid = &tusb_thread;
  46. result = rt_thread_init(tid, "tusb", tusb_thread_entry, RT_NULL,
  47. tusb_stack, sizeof(tusb_stack), 4, 10);
  48. if (tid != RT_EOK)
  49. {
  50. LOG_E("Fail to create TinyUSB thread");
  51. return -1;
  52. }
  53. #endif
  54. rt_thread_startup(tid);
  55. return 0;
  56. }
  57. INIT_COMPONENT_EXPORT(init_tinyusb);