tinyusb_port.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #ifdef PKG_TINYUSB_DEVICE_ENABLE
  27. tud_task();
  28. #endif
  29. #ifdef PKG_TINYUSB_HOST_ENABLE
  30. tuh_task();
  31. #endif
  32. }
  33. }
  34. static int init_tinyusb(void)
  35. {
  36. rt_thread_t tid;
  37. tusb_board_init();
  38. tusb_init();
  39. #ifdef RT_USING_HEAP
  40. tid = rt_thread_create("tusb", tusb_thread_entry, RT_NULL,
  41. PKG_TINYUSB_STACK_SIZE,
  42. PKG_TINYUSB_THREAD_PRIORITY, 10);
  43. if (tid == RT_NULL)
  44. #else
  45. rt_err_t result;
  46. tid = &tusb_thread;
  47. result = rt_thread_init(tid, "tusb", tusb_thread_entry, RT_NULL,
  48. tusb_stack, sizeof(tusb_stack), 4, 10);
  49. if (result != RT_EOK)
  50. #endif /* RT_USING_HEAP */
  51. {
  52. LOG_E("Fail to create TinyUSB thread");
  53. return -1;
  54. }
  55. rt_thread_startup(tid);
  56. return 0;
  57. }
  58. INIT_COMPONENT_EXPORT(init_tinyusb);