rt-thread.md.txt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # RT-Thread based Software Package Development Guide
  2. [中文版](rt-thread_zh.md)
  3. To use CherryUSB package, you need to select it in the RT-Thread package manager. The specific path is as follows:
  4. ```
  5. -> RT-Thread online packages
  6. -> system packages
  7. --- CherryUSB: tiny and portable USB stack for embedded system with USB IP
  8. CherryUSB Options ---->
  9. USB Speed (FS) --->
  10. [*] Enable usb device mode
  11. [*] Enable usb cdc acm device
  12. [ ] Enable usb hid device
  13. [ ] Enable usb dfu device
  14. [ ] Enable usb msc device
  15. [ ] Enable usb hub device
  16. [ ] Enable usb audio device
  17. [ ] Enable usb video device
  18. Version (latest) --->
  19. ```
  20. ## Based ON STM32 Platform
  21. Please note that stm32 series have two usb ip. For usb ip, like stm32f0、stm32f1、stm32f3, for usb otg ip(as we know it is from **synopsys**),like stm32f4、stm32f7 and so on.
  22. ### Use USB Device
  23. - Firstly,you should have a bsp project,and then go to `board\CubeMX_Config` directory, open file that suffix name with `.ioc` in **STM32CubeMX**.
  24. - Enable **USB** or **USB_OTG_FS** or **USB_OTG_HS** in **Connectivity** List,enable USB IRQ in **NVIC Setting**.
  25. ![STM32CubeMX USB setting](img/stm32cubemx.png)
  26. - Enable USB Clock for 48Mhz in **Clock configuration**.
  27. ![STM32CubeMX USB clock](img/stm32cubemx_clk.png)
  28. - Generate code.
  29. - Copy **SystemClock_Config** into **board.c**.
  30. - ~~Copy **MX_USB_OTG_FS_PCD_Init** or **MX_USB_OTG_HS_PCD_Init** into **main.c** if you use **usb_dc_hal.c**.Also, USB Irq from **it.c** needs the same.~~
  31. - Implement **usb_dc_low_level_init** and copy codes in from ``HAL_PCD_MspInit``.
  32. ```
  33. void usb_dc_low_level_init(void)
  34. {
  35. /* Peripheral clock enable */
  36. __HAL_RCC_USB_CLK_ENABLE();
  37. /* USB interrupt Init */
  38. HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 0, 0);
  39. HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
  40. }
  41. ```
  42. - Implement **printf** or modify with **rt_kprintf** in **usb_utils.h**, usb stack needs.
  43. - Now we can call some functions provided by **usb_stack**.Your should register descriptors、interfaces and endpoint callback firstly, and then call `usb_dc_init`. Example is as follows:
  44. ```
  45. int main(void)
  46. {
  47. extern void cdc_init(void);
  48. cdc_init();
  49. usb_dc_init();
  50. while (1)
  51. {
  52. uint8_t data_buffer[10] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x31, 0x32, 0x33, 0x34, 0x35 };
  53. usbd_ep_write(0x81, data_buffer, 10, NULL);
  54. rt_thread_mdelay(500);
  55. }
  56. }
  57. ```
  58. - How to register class you can go to [stm32 class examples](https://github.com/sakumisu/usb_stack/tree/master/demo/stm32/stm32f103c8t6/example) for a reference.
  59. ### CDC Demo Demonstration
  60. ![CDC Demo](img/rtt_cdc_demo.png)
  61. ### Video manual
  62. If you have problem from steps above, you can see this video:[Use USB Stack in RT-Thread package manager](https://www.bilibili.com/video/BV1Ef4y1t73d?p=26)。