STM32 USB HID 自定义设备 bulk 传输
ST(意法半导体公司)为STM32系列处理器编写了外设USB的库,并提供了很好的参考例程,本文就是参考ST提供的例程,在STM32F4 discovery板子上实现usb bulk传输。Host端是在linux平台上利用libusb库函数写的读写USB应用。
本次实现在STM32 USB例程中的Device HID 鼠标例程基础上添加bulk传输端点修改而来。
usb_conf.h 文件中添加 bulk传输端点
/*
* endpoint 0x80 and 0x00 are used for enumerating device.
* endpoint 0x81 and 0x80 are used for control xfer.
*/
#define HID_IN_EP 0x81
#define HID_OUT_EP 0x01
#define HID_IN_PACKET 4
#define HID_OUT_PACKET 4
// add bulk xfer endpoint
#define HID_IN_BULK_EP 0x82 // endpoint in
#define HID_OUT_BULK_EP 0x02 // endpoint out
// define endpoint max packet size
#define HID_IN_BULK_PACKET 64
#define HID_OUT_BULK_PACKET 64
usb_desc.c 中修改设备描述符
把bDeviceClass值改为0xFF,表示用户自定义设备;修改VID 和 PID,例如以下程序中的值。
__ALIGN_BEGIN uint8_t USBD_DeviceDesc[USB_SIZ_DEVICE_DESC] __ALIGN_END =
{
// the size of device descriptor
0x12, /*bLength */
// descriptor type. device descripor type is 0x01
USB_DEVICE_DESCRIPTOR_TYPE, /*bDescriptorType*/
// we use usb2.0
0x00, /*bcdUSB */
0x02,
// user defined device
0xff, /*bDeviceClass*/
0x00, /*bDeviceSubClass*/
0x00, /*bDeviceProtocol*/
// endpoint 0 max packet size
USB_OTG_MAX_EP0_SIZE, /*bMaxPacketSize*/
// vendor ID
LOBYTE(USBD_VID),