本文主要是介绍STM32F429 RTThread自带的USBHID库实现数据收发,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
RTThread源码使用rt-thread-4.0.2中stm32f429阿波罗
1.env配置USBHID
env的使用这里不作详细介绍,可以查看RTThread官方文档
输入menuconfig,回车
收发数据长度为63,配置里设置最大为63,不能更改为64,可更改配置文件rtconfig.h
scons --target=mdk5生成工程
2.修改USB相关文件
添加E:\rt-thread-4.0.2\bsp\stm32\libraries\HAL_Drivers中的drv_usbd.c
rtconfig.h
#define BSP_USING_USBD
打开硬件相关文件
board.c
修改USB时钟,USB需要48M
添加硬件管脚
stm32f4xx_hal_msp.c
void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle)
{GPIO_InitTypeDef GPIO_InitStruct = {0};if(pcdHandle->Instance==USB_OTG_FS){/* USER CODE BEGIN USB_OTG_FS_MspInit 0 *//* USER CODE END USB_OTG_FS_MspInit 0 */__HAL_RCC_GPIOA_CLK_ENABLE();/**USB_OTG_FS GPIO ConfigurationPA11 ------> USB_OTG_FS_DMPA12 ------> USB_OTG_FS_DP*/GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);/* Peripheral clock enable */__HAL_RCC_USB_OTG_FS_CLK_ENABLE();/* Peripheral interrupt init */HAL_NVIC_SetPriority(OTG_FS_IRQn, 0, 0);HAL_NVIC_EnableIRQ(OTG_FS_IRQn);/* USER CODE BEGIN USB_OTG_FS_MspInit 1 *//* USER CODE END USB_OTG_FS_MspInit 1 */}
}void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle)
{if(pcdHandle->Instance==USB_OTG_FS){/* USER CODE BEGIN USB_OTG_FS_MspDeInit 0 *//* USER CODE END USB_OTG_FS_MspDeInit 0 *//* Peripheral clock disable */__HAL_RCC_USB_OTG_FS_CLK_DISABLE();/**USB_OTG_FS GPIO ConfigurationPA11 ------> USB_OTG_FS_DMPA12 ------> USB_OTG_FS_DP*/HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);/* Peripheral interrupt Deinit*/HAL_NVIC_DisableIRQ(OTG_FS_IRQn);/* USER CODE BEGIN USB_OTG_FS_MspDeInit 1 *//* USER CODE END USB_OTG_FS_MspDeInit 1 */}
}
3.接收数据分析
hid收发数据结构体包括id和63个字节数据,因为要去掉报文里的ID否则发送接收有一个会失败,所以也去掉结构体的id
usb_common.h
hid.c
这里的REPORT_ID(1)必须去掉,不知道为什么,否则接收不到数据
这部分是接收回调和接收数据显示部分,数据显示部分去掉了,然后加了一个邮件发送,后面接收数据线程中会接收邮件
4.发送数据分析
发送数据函数去掉ID部分
5.收发数据线程
串口list_device可以看到hidd设备
代码主要功能为创建一个发送数据线程和一个接收数据线程,接收到USB数据后修改第一字节,然后再发送USB
#include <rtthread.h>
#include "rtdevice.h"#define USB_DEV_NAME "hidd" /* 设备名称 */static rt_device_t usb_dev; /* USB 设备句柄 */
struct rt_mailbox usb_rx_mb;
static char usb_rx_mbpool[128];
struct hid_report *usb_rx;
rt_uint8_t usb_rx_flag = 0; ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t usb_rx_stack[1024];
static struct rt_thread usb_rx_thread;
void usb_rx_thread_entry(void* parameter)
{while(1){rt_mb_recv(&usb_rx_mb,(rt_ubase_t*)&usb_rx, RT_WAITING_FOREVER);//接收邮件if( (usb_rx->report[00] == 0x00)&&(usb_rx->report[01] == 0x01) ){usb_rx_flag = 1;//接收数据标志 rt_kprintf("\nUSBHID Recived:\r\n");//打印接收到的数据for (rt_uint8_t i = 0; i < usb_rx->size; i++){rt_kprintf("%02x ", usb_rx->report[i]);if ((i + 1) % 8 == 0){rt_kprintf("\n");}else if ((i + 1) % 4 == 0){rt_kprintf(" ");}} }rt_thread_mdelay(100);}
}ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t usb_tx_stack[ 1024 ];
static struct rt_thread usb_tx_thread;
static void usb_tx_thread_entry(void* parameter)
{rt_uint8_t usb_sendbuf[64] = {0x00};while(1){ if(usb_rx_flag == 1)//如果接收到数据{rt_memcpy(usb_sendbuf,usb_rx->report,usb_rx->size);//将接收数据赋值给发送缓冲区usb_sendbuf[0] = 0x88;//修改发送缓冲区第一个字节rt_device_write(usb_dev,0, usb_sendbuf, 63);//usb发送usb_rx_flag = 0;//接收数据标志清零}rt_thread_mdelay(100);}
}int usbhid_sample(int argc, char *argv[]) {rt_err_t res;/* 相关的邮箱初始化 */rt_mb_init(&usb_rx_mb, "usb_rx_mb", /*usb接收邮箱初始化 */&usb_rx_mbpool[0],sizeof(usb_rx_mbpool)/4,RT_IPC_FLAG_FIFO); /* 查找 usb 设备 */usb_dev = rt_device_find(USB_DEV_NAME);if (!usb_dev) {rt_kprintf("find %s failed!\n", USB_DEV_NAME);return RT_ERROR;}/* 打开设备 */res = rt_device_open(usb_dev, RT_DEVICE_OFLAG_RDWR);res = rt_thread_init(&usb_rx_thread, "usb_rx",usb_rx_thread_entry,RT_NULL,(rt_uint8_t*)&usb_rx_stack[0],sizeof(usb_rx_stack),7,10);if (res == RT_EOK)rt_thread_startup(&usb_rx_thread);/* init usb_tx_thread */res = rt_thread_init(&usb_tx_thread,"usb_tx",usb_tx_thread_entry,RT_NULL,(rt_uint8_t*)&usb_tx_stack[0],sizeof(usb_tx_stack),8,5);if (res == RT_EOK){rt_thread_startup(&usb_tx_thread);}return res;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(usbhid_sample, usbhid device sample);
5.验证
串口发送命令启动程序
打开usb调试助手,点击发送数据,可以看到串口有打印信息同时USB助手也有接收数据
这篇关于STM32F429 RTThread自带的USBHID库实现数据收发的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!