demo.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. Quick Verification Based on Existing Demos
  2. =============================================
  3. Before learning USB or CherryUSB code, we need to quickly verify based on existing demos. Why? To enhance interest in USB and build confidence for the next steps. If demos can't run, or if you explore writing code by yourself, or read USB basic concepts first, you may find that you can't understand anything in the end - there are so many concepts that you simply can't remember them, thus losing interest in USB. Therefore, running demos first is very important. Below I will list the currently supported demo repositories.
  4. Based on Bouffalolab Series Chips (Official SDK Support)
  5. --------------------------------------------------------------
  6. .. list-table::
  7. :widths: 10 10 10
  8. :header-rows: 1
  9. * - Repo url
  10. - USB IP
  11. - Version
  12. * - https://github.com/CherryUSB/cherryusb_bouffalolab
  13. - FOTG210
  14. - less than latest
  15. Based on HPMicro Series Chips (Official SDK Support)
  16. -----------------------------------------------------
  17. .. list-table::
  18. :widths: 10 10 10
  19. :header-rows: 1
  20. * - Repo url
  21. - USB IP
  22. - Version
  23. * - https://github.com/CherryUSB/cherryusb_hpmicro
  24. - CHIPIDEA
  25. - less than latest
  26. Based on esp32s2/s3/p4 Series Chips (Official SDK Support)
  27. -------------------------------------------------------------------------------
  28. .. list-table::
  29. :widths: 10 10 10
  30. :header-rows: 1
  31. * - Repo url
  32. - USB IP
  33. - Version
  34. * - https://github.com/CherryUSB/cherryusb_esp32
  35. - DWC2
  36. - less than latest
  37. Default demo uses component library installation form, can be searched at https://components.espressif.com/ for cherryusb
  38. ESP-Registry can refer to the official documentation, recommended to use vscode + esp-idf development environment.
  39. - ctrl + shift + p select ESP-IDF welcome interface, then select Component manager
  40. .. figure:: img/esp1.png
  41. - Find cherryusb and install
  42. .. figure:: img/esp2.png
  43. - Open menuconfig, and open cherryusb configuration, select host or device mode according to actual situation
  44. .. figure:: img/esp3.png
  45. .. figure:: img/esp4.png
  46. Based on Phytium Series Chips (Official SDK Support)
  47. -------------------------------------------------------
  48. .. list-table::
  49. :widths: 10 10 10
  50. :header-rows: 1
  51. * - Repo url
  52. - USB IP
  53. - Version
  54. * - https://gitee.com/phytium_embedded/phytium-free-rtos-sdk
  55. - PUSB2/XHCI
  56. - equal to v1.4.0
  57. Based on Essemi Series Chips (Official SDK Support)
  58. -------------------------------------------------------------
  59. .. list-table::
  60. :widths: 10 10 10
  61. :header-rows: 1
  62. * - Repo url
  63. - USB IP
  64. - Version
  65. * - https://github.com/CherryUSB/cherryusb_es32
  66. - MUSB
  67. - less than latest
  68. Based on Artinchip Series Chips (Official SDK Support)
  69. -------------------------------------------------------------------
  70. .. list-table::
  71. :widths: 10 10 10
  72. :header-rows: 1
  73. * - Repo url
  74. - USB IP
  75. - Version
  76. * - https://gitee.com/artinchip/luban-lite
  77. - AIC/EHCI/OHCI
  78. - less than latest
  79. Based on Kendryte canmv-k230 Chip (Official SDK Support)
  80. ---------------------------------------------------------
  81. .. list-table::
  82. :widths: 10 10 10
  83. :header-rows: 1
  84. * - Repo url
  85. - USB IP
  86. - Version
  87. * - https://github.com/CherryUSB/k230_sdk
  88. - DWC2
  89. - less than latest
  90. Based on NXP MCX Series Chips
  91. -------------------------------------
  92. .. list-table::
  93. :widths: 10 10 10
  94. :header-rows: 1
  95. * - Repo url
  96. - USB IP
  97. - Version
  98. * - https://github.com/CherryUSB/cherryusb_mcx https://github.com/RT-Thread/rt-thread/tree/master/bsp/nxp/mcx
  99. - CHIPIDEA/kinetis
  100. - less than latest
  101. Based on SiFli SF32 Series Chips (Official SDK Support)
  102. --------------------------------------------------------
  103. .. list-table::
  104. :widths: 10 10 10
  105. :header-rows: 1
  106. * - Repo url
  107. - USB IP
  108. - Version
  109. * - https://github.com/OpenSiFli/SiFli-SDK
  110. - MUSB
  111. - less than latest
  112. Based on RP2040/RP2035 Chips (Official SDK Support Coming Soon)
  113. --------------------------------------------------------------------------
  114. .. list-table::
  115. :widths: 10 10 10
  116. :header-rows: 1
  117. * - Repo url
  118. - USB IP
  119. - Version
  120. * - https://github.com/CherryUSB/pico-examples https://github.com/CherryUSB/pico-sdk
  121. - RP2040
  122. - less than latest
  123. Based on Actionstech Series Chips (Official SDK Support)
  124. -----------------------------------------------------------
  125. Not opensource, please contact to Actionstech official
  126. Based on ST Series Chips
  127. ---------------------------
  128. .. list-table::
  129. :widths: 10 10 10
  130. :header-rows: 1
  131. * - Repo url
  132. - USB IP
  133. - Version
  134. * - https://github.com/CherryUSB/cherryusb_stm32
  135. - DWC2/FSDEV
  136. - less than latest
  137. Default demo projects provided:
  138. - F103 uses fsdev ip
  139. - F429 host and device use USB1, pins pb14/pb15, default device does not enable DMA mode
  140. - H7 device uses USB0, pins pa11/pa12, DMA mode not enabled. Host uses USB1, pins pb14/pb15, and needs nocache processing
  141. Demo provides **stm32xxx.ioc** file, double click to open, click **Generate Code**.
  142. .. caution:: After generation, please use git reset function to restore the overwritten `main.c` and `stm32xxx_it.c` files, prohibit being overwritten by cubemx.
  143. Covers F1/F4/H7, other chips are basically similar and won't be repeated. Specific differences are:
  144. - usb ip difference: F1 uses fsdev, F4/H7 use dwc2
  145. - dwc2 ip difference: USB0 (pins PA11/PA12) and USB1 (pins PB14/PB15), where USB1 defaults to full-speed, can connect external PHY to form high-speed host, and has DMA functionality
  146. - F4 has no cache, H7 has cache
  147. If it's STM32F7/STM32H7 with cache functionality, you need to locate the ram used by usb to the no cache ram area. Example as follows
  148. .. code-block:: C
  149. cpu_mpu_config(0, MPU_Normal_NonCache, 0x24070000, MPU_REGION_SIZE_64KB);
  150. Corresponding sct script modification in Keil:
  151. .. code-block:: C
  152. LR_IROM1 0x08000000 0x00200000 { ; load region size_region
  153. ER_IROM1 0x08000000 0x00200000 { ; load address = execution address
  154. *.o (RESET, +First)
  155. *(InRoot$$Sections)
  156. .ANY (+RO)
  157. .ANY (+XO)
  158. }
  159. RW_IRAM2 0x24000000 0x00070000 { ; RW data
  160. .ANY (+RW +ZI)
  161. }
  162. USB_NOCACHERAM 0x24070000 0x00010000 { ; RW data
  163. *(.noncacheable)
  164. }
  165. }
  166. USB Device Porting Points
  167. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  168. - Use **stm32cubemx** to create project, configure basic RCC, UART (used as log)
  169. .. figure:: img/stm32_1.png
  170. .. figure:: img/stm32_2.png
  171. - If using fsdev ip, check **USB**. If using dwc2 ip, check **USB_OTG_FS** or **USB_OTG_HS**. Enable USB interrupt. Other configurations are useless to us, no ST USB library will be used in the code.
  172. .. figure:: img/stm32_3_1.png
  173. .. figure:: img/stm32_3_2.png
  174. - Configure usb clock to 48M
  175. .. figure:: img/stm32_4_1.png
  176. .. figure:: img/stm32_4_2.png
  177. - Select project, here we choose keil, set stack and heap properly. If using msc, it's recommended to set them larger, then click **Generate Code**.
  178. .. figure:: img/stm32_5.png
  179. - Add CherryUSB required source code (**usbd_core.c**, **dwc2/usb_dc_dwc2.c** or **fsdev/usb_dc_fsdev.c**), as well as the class drivers you want to use. You can add corresponding class templates for convenient testing.
  180. .. figure:: img/stm32_6.png
  181. - Add necessary header files
  182. .. figure:: img/stm32_7.png
  183. - Copy **cherryusb_config_template.h**, place it in the `Core/Inc` directory, and name it `usb_config.h`
  184. .. figure:: img/stm32_8.png
  185. - If using fsdev ip, (starting from V1.5.0, need to add **fsdev/usb_glue_st.c**) implement the following macro definition in `usb_config.h`. The specific value varies for different chips:
  186. .. code-block:: C
  187. #define CONFIG_USBDEV_FSDEV_PMA_ACCESS 2
  188. - Compiler recommends using **AC6**. Check **Microlib**, and implement **printf** for convenient log viewing later.
  189. .. figure:: img/stm32_10.png
  190. .. figure:: img/stm32_11.png
  191. .. note :: Starting from V1.5.0, the following two steps are no longer needed because they are already implemented in **fsdev/usb_glue_st.c** and **dwc2/usb_glue_st.c** files
  192. - Copy the content of **HAL_PCD_MspInit** function in **xxx_msp.c** to **usb_dc_low_level_init** function, disable ST generated USB initialization
  193. .. figure:: img/stm32_12.png
  194. .. figure:: img/stm32_14.png
  195. - Call `USBD_IRQHandler` in interrupt function and pass in `busid`
  196. .. figure:: img/stm32_13.png
  197. - If the chip has cache, please refer to :ref:`usb_cache` chapter for cache modification
  198. - Call template content initialization and fill in `busid` and USB IP's `reg base`. `busid` starts from 0 and cannot exceed `CONFIG_USBDEV_MAX_BUS`
  199. .. figure:: img/stm32_15.png
  200. USB Host Porting Points
  201. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  202. The first 6 steps are the same as device side. Note that host driver only supports HS port with DMA (pins PB14/PB15), so FS port (pins PA11/PA12) is not supported (what's the point of a host without DMA).
  203. - Add CherryUSB required source code (**usbh_core.c**, **usbh_hub.c**, **usb_hc_dwc2.c**, **usb_glue_st.c** and adapter layer files in **osal** directory), as well as the class drivers you want to use, and you can add corresponding **usb host.c** for convenient testing.
  204. .. figure:: img/stm32_16.png
  205. - Compiler recommends using **AC6**. Check **Microlib**, and implement **printf** for convenient log viewing later.
  206. .. figure:: img/stm32_10.png
  207. .. figure:: img/stm32_11.png
  208. - Copy **cherryusb_config_template.h**, place it in the `Core/Inc` directory, and name it `usb_config.h`
  209. .. note :: Starting from V1.5.0, the following two steps are no longer needed because they are already implemented in **fsdev/usb_glue_st.c** and **dwc2/usb_glue_st.c** files
  210. - Copy the content of `HAL_HCD_MspInit` function in **xxx_msp.c** to `usb_hc_low_level_init` function, disable ST generated USB initialization
  211. - Call `USBH_IRQHandler` in interrupt function and pass in `busid`
  212. .. figure:: img/stm32_19.png
  213. - For linker script modifications, refer to :ref:`usbh_link_script` chapter
  214. - If the chip has cache, please refer to :ref:`usb_cache` chapter for cache modification
  215. - Call `usbh_initialize` and fill in `busid` and USB IP's `reg base` and `event_handler` (can be omitted as NULL). `busid` starts from 0 and cannot exceed `CONFIG_USBHOST_MAX_BUS`
  216. - Start thread
  217. .. figure:: img/stm32_18.png