Skip to content

Commit eaf23c1

Browse files
committed
Update USBX.
1 parent a5d3d22 commit eaf23c1

File tree

2 files changed

+105
-32
lines changed
  • docs/code_gen/stm32
  • i18n/en/docusaurus-plugin-content-docs/current/code_gen/stm32

2 files changed

+105
-32
lines changed

docs/code_gen/stm32/uart.md

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,68 @@ sidebar_position: 10
66

77
# 串口与终端
88

9-
LibXR支持两种串口:硬件串口和USB CDC。都可以用于串口调试和终端
9+
LibXR 支持两种串口类型:**硬件串口****USB CDC**。它们均可用于串口调试与终端交互
1010

11-
硬件串口需要开启串口中断和DMA,USB CDC也需要开启相关中断,并启用官方USB库。
11+
- **硬件串口** 需启用中断与 DMA;
12+
- **USB CDC** 需启用中断,并根据所用系统(如 FreeRTOS 或 ThreadX)启用对应 USB 支持。
1213

1314
## 串口代码示例
1415

1516
```cpp
1617
// 硬件串口
1718
STM32UART usart1(&huart1, usart1_rx_buf, usart1_tx_buf, 5, 5);
1819

19-
// USB CDC
20+
// USB CDC(FreeRTOS 或裸机下)
2021
STM32VirtualUART uart_cdc(hUsbDeviceFS, UserTxBufferFS, UserRxBufferFS, 5, 5);
22+
23+
// USB CDC(ThreadX + USBX)
24+
STM32VirtualUART uart_cdc(&hpcd_USB_FS, 2048, 2, 2048, 2, 5, 12, 256);
2125
```
2226
27+
> 注意:在 **ThreadX 系统** 中,使用 USBX 替代 ST 官方 USB 库,构造函数参数也不同,使用 USB PCD 句柄(如 `&hpcd_USB_FS`)初始化。
28+
2329
## 终端代码示例
2430
2531
```cpp
26-
// 重定向标准输入输出
32+
// 如果使用硬件串口
2733
STDIO::read_ = &usart1.read_port_;
2834
STDIO::write_ = &usart1.write_port_;
2935
36+
// 如果使用 USB CDC
37+
STDIO::read_ = &uart_cdc.read_port_;
38+
STDIO::write_ = &uart_cdc.write_port_;
39+
3040
// 创建虚拟文件系统
3141
RamFS ramfs("XRobot");
3242
3343
// 创建终端
3444
Terminal<32, 32, 5, 5> terminal(ramfs);
3545
36-
// 启动终端任务
46+
// 方式一:作为任务运行(默认)
3747
auto terminal_task = Timer::CreateTask(terminal.TaskFun, &terminal, 10);
3848
Timer::Add(terminal_task);
3949
Timer::Start(terminal_task);
50+
51+
// 方式二:作为线程运行(独立线程)
52+
LibXR::Thread terminal_thread;
53+
terminal_thread.Create(&terminal, terminal.ThreadFun, "terminal", 512,
54+
LibXR::Thread::Priority::MEDIUM);
4055
```
4156

42-
## 配置文件
57+
## 配置文件说明
4358

44-
USB的buffer_size需要在STM32CubeMX的官方USB库中修改
59+
可通过配置文件控制串口与终端行为:
4560

4661
```yaml
47-
# 指定终端使用的串口,''表示不生成终端相关代码
62+
# 选择默认终端绑定的串口(usb 或 usart1 等),留空表示不启用终端
4863
terminal_source: usb
4964

65+
# 终端相关配置(可选)
66+
terminal:
67+
RunAsThread: true # 是否作为线程运行(ThreadX 推荐)
68+
ThreadStackDepth: 512 # 线程栈深度
69+
ThreadPriority: 3 # 线程优先级(对应 LibXR::Thread::Priority)
70+
5071
# 硬件串口配置
5172
USART:
5273
usart1:
@@ -55,13 +76,28 @@ USART:
5576
tx_queue_size: 5
5677
rx_queue_size: 5
5778

58-
# USB CDC配置
79+
# USB CDC 配置(FreeRTOS 下有效)
5980
USB:
6081
tx_queue_size: 12
6182
rx_queue_size: 12
6283
```
6384
64-
可直接修改该文件。如需应用更新配置,请执行以下任一命令以重新生成代码:
65-
`xr_cubemx_cfg -d .`
66-
67-
`xr_gen_code_stm32 -i ./.config.yaml -o ./User/app_main.cpp`
85+
> USB 的 `UserTxBufferFS` 与 `UserRxBufferFS` 缓冲区仅在使用 **官方 USB 库**(FreeRTOS)时有效。ThreadX + USBX 模式不需要配置这些缓冲。
86+
87+
---
88+
89+
## 生成代码命令
90+
91+
修改 `.config.yaml` 后,可使用以下任一命令重新生成代码:
92+
93+
```bash
94+
# 重新生成整个工程
95+
xr_cubemx_cfg -d .
96+
```
97+
98+
或:
99+
100+
```bash
101+
# 只重新生成app_main.cpp
102+
xr_gen_code_stm32 -i ./.config.yaml -o ./User/app_main.cpp
103+
```
Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,74 @@
11
---
22
id: stm32-code-gen-uart
3-
title: UART & Terminal
3+
title: UART and Terminal
44
sidebar_position: 10
55
---
66

7-
# UART & Terminal
7+
# UART and Terminal
88

9-
LibXR supports two types of UART interfaces: hardware UART and USB CDC. Both can be used for serial debugging and terminal interaction.
9+
LibXR supports two types of UARTs: **Hardware UART** and **USB CDC**. Both can be used for serial debugging and terminal interaction.
1010

11-
For hardware UART, enable UART interrupts and DMA. For USB CDC, enable the corresponding interrupts and use the official STM32 USB library.
11+
- **Hardware UART** requires interrupt and DMA enabled;
12+
- **USB CDC** requires interrupt enabled, and the appropriate USB support depending on the OS (e.g., FreeRTOS or ThreadX).
1213

13-
## UART Code Example
14+
## UART Code Examples
1415

1516
```cpp
1617
// Hardware UART
1718
STM32UART usart1(&huart1, usart1_rx_buf, usart1_tx_buf, 5, 5);
1819

19-
// USB CDC
20+
// USB CDC (FreeRTOS or bare-metal)
2021
STM32VirtualUART uart_cdc(hUsbDeviceFS, UserTxBufferFS, UserRxBufferFS, 5, 5);
22+
23+
// USB CDC (ThreadX + USBX)
24+
STM32VirtualUART uart_cdc(&hpcd_USB_FS, 2048, 2, 2048, 2, 5, 12, 256);
2125
```
2226
23-
## Terminal Code Example
27+
> Note: On **ThreadX**, USBX replaces the official ST USB library. The constructor parameters are different and require a USB PCD handle (e.g., `&hpcd_USB_FS`).
28+
29+
## Terminal Code Examples
2430
2531
```cpp
26-
// Redirect standard input/output
32+
// If using hardware UART
2733
STDIO::read_ = &usart1.read_port_;
2834
STDIO::write_ = &usart1.write_port_;
2935
30-
// Create virtual file system
36+
// If using USB CDC
37+
STDIO::read_ = &uart_cdc.read_port_;
38+
STDIO::write_ = &uart_cdc.write_port_;
39+
40+
// Create a virtual file system
3141
RamFS ramfs("XRobot");
3242
33-
// Create terminal
43+
// Create the terminal
3444
Terminal<32, 32, 5, 5> terminal(ramfs);
3545
36-
// Launch terminal task
46+
// Method 1: Run as a task (default)
3747
auto terminal_task = Timer::CreateTask(terminal.TaskFun, &terminal, 10);
3848
Timer::Add(terminal_task);
3949
Timer::Start(terminal_task);
50+
51+
// Method 2: Run as a thread (independent thread)
52+
LibXR::Thread terminal_thread;
53+
terminal_thread.Create(&terminal, terminal.ThreadFun, "terminal", 512,
54+
LibXR::Thread::Priority::MEDIUM);
4055
```
4156

42-
## Configuration File
57+
## Configuration File Explanation
4358

44-
Note: The `buffer_size` for USB must be configured inside STM32CubeMX using the official USB library.
59+
You can control UART and terminal behavior through the configuration file:
4560

4661
```yaml
47-
# Specify the UART source used for terminal; empty string disables terminal code generation
62+
# Select the default UART source for terminal binding (usb or usart1, etc.)
63+
# Leave empty to disable terminal
4864
terminal_source: usb
4965

66+
# Terminal-related settings (optional)
67+
terminal:
68+
RunAsThread: true # Run as a thread (recommended for ThreadX)
69+
ThreadStackDepth: 512 # Thread stack depth
70+
ThreadPriority: 3 # Thread priority (matches LibXR::Thread::Priority)
71+
5072
# Hardware UART configuration
5173
USART:
5274
usart1:
@@ -55,13 +77,28 @@ USART:
5577
tx_queue_size: 5
5678
rx_queue_size: 5
5779

58-
# USB CDC configuration
80+
# USB CDC configuration (effective under FreeRTOS)
5981
USB:
6082
tx_queue_size: 12
6183
rx_queue_size: 12
6284
```
6385
64-
You can modify this file directly. To apply updated settings, run either of the following commands:
65-
`xr_cubemx_cfg -d .`
66-
or
67-
`xr_gen_code_stm32 -i ./.config.yaml -o ./User/app_main.cpp`
86+
> The `UserTxBufferFS` and `UserRxBufferFS` buffers are only used when the **official ST USB library** (under FreeRTOS) is used. These are not required in ThreadX + USBX mode.
87+
88+
---
89+
90+
## Code Generation Command
91+
92+
After modifying `.config.yaml`, use one of the following commands to regenerate the code:
93+
94+
```bash
95+
# Regenerate the entire project
96+
xr_cubemx_cfg -d .
97+
```
98+
99+
Or:
100+
101+
```bash
102+
# Regenerate only app_main.cpp
103+
xr_gen_code_stm32 -i ./.config.yaml -o ./User/app_main.cpp
104+
```

0 commit comments

Comments
 (0)