Files
luban-lite-t3e-pro/packages/third-party/cherryusb/docs/source/quick_start/rt-thread/rtthread.rst
刘可亮 9f7ba67007 v1.0.3
2024-01-27 08:47:24 +08:00

109 lines
4.1 KiB
ReStructuredText
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
基于 RT-Thread 软件包开发指南
===============================
本节主要介绍使用 RT-Thread 提供的软件包管理器来配置工程,以 env 作为演示。本节操作不同芯片都一样,后续不再重复讲解。打开 env 以后使用 menuconfig 进入包管理器,并在如图所示路径中选择 CherryUSB。
.. figure:: img/env0.png
从机配置
--------------------------
* 选择 Enable usb device mode 并敲回车进入。
.. figure:: img/env1.png
.. figure:: img/env2.png
* 首先第一个配置是配置 USB 的速度,分为 **FS、HS**,表示使用全速还是高速功能。
.. figure:: img/env3.png
* 其次第二个配置则是选择 USB device ip不清楚自己芯片是哪个 ip 的可以参考 **port** 目录下对应的 readme。
.. figure:: img/env4.png
* 选择好 USB device ip 以后,还需要选择是哪款芯片,第三个配置则是用来选择芯片,选择以后会帮忙配置相对应的 ip 的一些信息,比如 `USB_BASE``USBD_Handler` 以及特殊的一些配置等等,如果没找到自己的芯片,可以手动在 `usb_dc_xxx.c` 中修改。
.. figure:: img/env5.png
* 接下来是 class 的选择,用哪个 class 勾选哪个就可以了,使能 class 以后,双击进入可以选择一个 demo 的模板参与编译,当然也可以不选,自己写。
.. figure:: img/env6.png
* 最后退出保存即可。
* 退出以后不急着编译,需要在代码中实现 `usb_dc_low_level_init` 函数。
* 复制一份 `usb_config.h` 到自己的目录中,并实现以下内容, 禁止包含 `"rtthread.h"`
.. figure:: img/config_file.png
* 使用 `scons --target=mdk5` 或者 `scons` 进行编译
主机配置
--------------------------
* 选择 Enable usb host mode 并敲回车进入。
.. figure:: img/env7.png
* 选择 USB host ip不清楚自己芯片是哪个 ip 的可以参考 **port** 目录下对应的 readme。选择好 USB host ip 以后,还需要选择是哪款芯片,第二个箭头则是用来选择芯片,选择以后会帮忙配置相对应的 ip 的一些信息,比如 `USB_BASE``USBH_Handler` 以及特殊的一些配置等等,如果没找到自己的芯片,可以手动在 `usb_hc_xxx.c` 中修改。
.. figure:: img/env8.png
* 根据需要勾选 class 驱动
* 最后退出保存即可。
* 复制一份 `usb_config.h` 到自己的目录中,并实现以下内容, 禁止包含 `"rtthread.h"`
.. figure:: img/config_file.png
* 在代码中实现 `usb_hc_low_level_init` 函数USB 中断中调用 `USBH_IRQHandler`,
* 应用中调用 `usbh_alloc_bus``usbh_initialize`,
* 以上内容我们推荐放在 **board.c** 中,如下代码:
.. code-block:: C
struct usbh_bus *usb_otg_hs_bus;
void OTG_HS_IRQHandler(void)
{
extern void USBH_IRQHandler(struct usbh_bus *bus);
USBH_IRQHandler(usb_otg_hs_bus);
}
int usbh_init(void)
{
usb_otg_hs_bus = usbh_alloc_bus(0, USB_OTG_HS_PERIPH_BASE);
usbh_initialize(usb_otg_hs_bus);
return 0;
}
INIT_APP_EXPORT(usbh_init);
* 使用 `scons --target=mdk5` 或者 `scons` 进行编译,需要使用 AC6 编译器
* 如果使用的是 GCC ,需要在链接脚本(ld)中添加如下代码:
.. code-block:: C
/* section information for usbh class */
. = ALIGN(4);
__usbh_class_info_start__ = .;
KEEP(*(.usbh_class_info))
__usbh_class_info_end__ = .;
借助 STM32CubeMX 生成 USB 初始化
----------------------------------
使用 STM32CubeMX 主要是用来生成 usb 时钟、引脚、中断的配置。我们需要点击如图所示文件,并配置好 USB 的时钟、中断,点击 `Generate Code`
.. figure:: img/stm32cubemx0.png
.. figure:: img/stm32cubemx1.png
.. figure:: img/stm32cubemx2.png
.. figure:: img/stm32cubemx_clk.png
-`main.c` 中的 `SystemClock_Config` 替换掉 `board.c` 中的配置
.. figure:: img/stm32_init2.png
-`stm32xxxx_hal_msp.c` 中的 `HAL_PCD_MspInit` 或者是 `HAL_HCD_MspInit` 中的内容复制到 `usb_dc_low_level_init``usb_hc_low_level_init` 函数中,举例如下:
.. figure:: img/stm32_init.png