Skip to content

Commit 66c6774

Browse files
[bsp/stm32l496-st-nucleo] add usb device cdc configration
1 parent ce1734f commit 66c6774

File tree

4 files changed

+113
-5
lines changed

4 files changed

+113
-5
lines changed

bsp/stm32/stm32l496-st-nucleo/README_zh.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ msh >
9696

9797
本章节更多详细的介绍请参考 [STM32 系列 BSP 外设驱动使用教程](../docs/STM32系列BSP外设驱动使用教程.md)
9898

99+
100+
101+
5. USBD CDC使用,参考文章[STM32L496 USB CDC适配](https://club.rt-thread.org/ask/article/2959.html)
102+
99103
## 注意事项
100104

101105
- 开机时如果不能打印 RT-Thread 版本信息,请重新选择 PC 端串口调试软件的串口号或将 BSP 中串口的 GPIO 速率调低

bsp/stm32/stm32l496-st-nucleo/applications/SConscript

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ src = Split('''
66
main.c
77
''')
88

9-
if GetDepend(['BSP_USING_SPI_LCD']):
10-
src += ['lcd_sample.c']
11-
12-
if GetDepend(['PKG_USING_NRF24L01']):
13-
src += ['nrf24l01_init.c']
9+
if GetDepend(['BSP_USING_USBD']):
10+
if GetDepend(['RT_USB_DEVICE_CDC']):
11+
src += ['uart_sample.c']
1412

1513
CPPPATH = [str(Dir('#')), cwd]
1614

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2018-08-15 misonyo first implementation.
9+
*/
10+
/*
11+
* 程序清单:这是一个 串口 设备使用例程
12+
* 例程导出了 uart_sample 命令到控制终端
13+
* 命令调用格式:uart_sample uart2
14+
* 命令解释:命令第二个参数是要使用的串口设备名称,为空则使用默认的串口设备
15+
* 程序功能:通过串口输出字符串"hello RT-Thread!",然后错位输出输入的字符
16+
*/
17+
18+
#include <rtthread.h>
19+
20+
#define SAMPLE_UART_NAME "vcom" /* 串口设备名称 */
21+
22+
/* 用于接收消息的信号量 */
23+
static struct rt_semaphore rx_sem;
24+
static rt_device_t serial;
25+
26+
/* 接收数据回调函数 */
27+
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
28+
{
29+
/* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
30+
rt_sem_release(&rx_sem);
31+
32+
return RT_EOK;
33+
}
34+
35+
static void serial_thread_entry(void *parameter)
36+
{
37+
char ch;
38+
39+
while (1)
40+
{
41+
/* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */
42+
while (rt_device_read(serial, -1, &ch, 1) != 1)
43+
{
44+
/* 阻塞等待接收信号量,等到信号量后再次读取数据 */
45+
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
46+
}
47+
/* 读取到的数据通过串口错位输出 */
48+
ch = ch + 1;
49+
rt_device_write(serial, 0, &ch, 1);
50+
}
51+
}
52+
53+
static int uart_sample(int argc, char *argv[])
54+
{
55+
rt_err_t ret = RT_EOK;
56+
char uart_name[RT_NAME_MAX];
57+
char str[] = "hello RT-Thread!\r\n";
58+
59+
if (argc == 2)
60+
{
61+
rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
62+
}
63+
else
64+
{
65+
rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
66+
}
67+
68+
/* 查找串口设备 */
69+
serial = rt_device_find(uart_name);
70+
if (!serial)
71+
{
72+
rt_kprintf("find %s failed!\n", uart_name);
73+
return RT_ERROR;
74+
}
75+
76+
/* 初始化信号量 */
77+
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
78+
/* 以中断接收及轮询发送方式打开串口设备 */
79+
rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
80+
//rt_device_open(serial, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
81+
/* 设置接收回调函数 */
82+
rt_device_set_rx_indicate(serial, uart_input);
83+
/* 发送字符串 */
84+
rt_device_write(serial, 0, str, (sizeof(str) - 1));
85+
86+
/* 创建 serial 线程 */
87+
rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
88+
/* 创建成功则启动线程 */
89+
if (thread != RT_NULL)
90+
{
91+
rt_thread_startup(thread);
92+
}
93+
else
94+
{
95+
ret = RT_ERROR;
96+
}
97+
98+
return ret;
99+
}
100+
/* 导出到 msh 命令列表中 */
101+
MSH_CMD_EXPORT(uart_sample, uart device sample);

bsp/stm32/stm32l496-st-nucleo/board/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ menu "On-chip Peripheral Drivers"
6060
config BSP_USING_ON_CHIP_FLASH
6161
bool "Enable on-chip FLASH"
6262
default n
63+
64+
config BSP_USING_USBD
65+
bool "Enable OTGFS as USB device"
66+
select RT_USING_USB_DEVICE
67+
default n
6368

6469
source "../libraries/HAL_Drivers/Kconfig"
6570

0 commit comments

Comments
 (0)