[bsp/stm32l496-st-nucleo] add usb device cdc configration

This commit is contained in:
chenyingchun0312 2021-08-10 10:54:58 +08:00
parent ce1734fb31
commit 66c6774dd9
4 changed files with 113 additions and 5 deletions

View File

@ -96,6 +96,10 @@ msh >
本章节更多详细的介绍请参考 [STM32 系列 BSP 外设驱动使用教程](../docs/STM32系列BSP外设驱动使用教程.md)。
5. USBD CDC使用参考文章[STM32L496 USB CDC适配](https://club.rt-thread.org/ask/article/2959.html)
## 注意事项
- 开机时如果不能打印 RT-Thread 版本信息,请重新选择 PC 端串口调试软件的串口号或将 BSP 中串口的 GPIO 速率调低

View File

@ -6,11 +6,9 @@ src = Split('''
main.c
''')
if GetDepend(['BSP_USING_SPI_LCD']):
src += ['lcd_sample.c']
if GetDepend(['PKG_USING_NRF24L01']):
src += ['nrf24l01_init.c']
if GetDepend(['BSP_USING_USBD']):
if GetDepend(['RT_USB_DEVICE_CDC']):
src += ['uart_sample.c']
CPPPATH = [str(Dir('#')), cwd]

View File

@ -0,0 +1,101 @@
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-08-15 misonyo first implementation.
*/
/*
* 使
* uart_sample
* uart_sample uart2
* 使使
* "hello RT-Thread!"
*/
#include <rtthread.h>
#define SAMPLE_UART_NAME "vcom" /* 串口设备名称 */
/* 用于接收消息的信号量 */
static struct rt_semaphore rx_sem;
static rt_device_t serial;
/* 接收数据回调函数 */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
/* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
rt_sem_release(&rx_sem);
return RT_EOK;
}
static void serial_thread_entry(void *parameter)
{
char ch;
while (1)
{
/* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */
while (rt_device_read(serial, -1, &ch, 1) != 1)
{
/* 阻塞等待接收信号量,等到信号量后再次读取数据 */
rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
}
/* 读取到的数据通过串口错位输出 */
ch = ch + 1;
rt_device_write(serial, 0, &ch, 1);
}
}
static int uart_sample(int argc, char *argv[])
{
rt_err_t ret = RT_EOK;
char uart_name[RT_NAME_MAX];
char str[] = "hello RT-Thread!\r\n";
if (argc == 2)
{
rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
}
/* 查找串口设备 */
serial = rt_device_find(uart_name);
if (!serial)
{
rt_kprintf("find %s failed!\n", uart_name);
return RT_ERROR;
}
/* 初始化信号量 */
rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
/* 以中断接收及轮询发送方式打开串口设备 */
rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
//rt_device_open(serial, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
/* 设置接收回调函数 */
rt_device_set_rx_indicate(serial, uart_input);
/* 发送字符串 */
rt_device_write(serial, 0, str, (sizeof(str) - 1));
/* 创建 serial 线程 */
rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
ret = RT_ERROR;
}
return ret;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(uart_sample, uart device sample);

View File

@ -60,6 +60,11 @@ menu "On-chip Peripheral Drivers"
config BSP_USING_ON_CHIP_FLASH
bool "Enable on-chip FLASH"
default n
config BSP_USING_USBD
bool "Enable OTGFS as USB device"
select RT_USING_USB_DEVICE
default n
source "../libraries/HAL_Drivers/Kconfig"