STM32 example USB HID bidirectional data transfer (source download)

Source: Internet
Author: User
Tags abs mongodb postgresql redis zip firewall
1. Enumerate the STM32 USB as a HID device.

2. STM32 uses 3 endpoints, endpoint 0 is used for enumeration, endpoints 1 and 2 are used for data transmission and reception.

3. The endpoint length is 64, that is, up to 64 bytes of data can be transmitted at a time.

4. STM32 obtains the data sent by the host computer and returns the data as it is via USB, and prints out the data at the same time.

5. The host computer program realizes the read and write control of the HID device by calling the windows API.


Part of the program code
STM32 report descriptor:
1. constuint8_t CustomHID_ReportDescriptor[CUSTOMHID_SIZ_REPORT_DESC] =

2.  { 

3. 0x05, 0x8c, /* USAGE_PAGE (ST Page) */

4. 0x09, 0x01, /* USAGE (Demo Kit) */

5. 0xa1, 0x01, /* COLLECTION (Application) */

6.

7. // The Input report

8. 0x09,0x03, // USAGE ID-Vendor defined

9. 0x15,0x00, // LOGICAL_MINIMUM (0)

10. 0x26,0x00, 0xFF, // LOGICAL_MAXIMUM (255)

11. 0x75,0x08, // REPORT_SIZE (8bit)

12. 0x95,0x40, // REPORT_COUNT (64Byte)

13. 0x81,0x02, // INPUT (Data,Var,Abs)

14.

15. // The Output report

16. 0x09,0x04, // USAGE ID-Vendor defined

17. 0x15,0x00, // LOGICAL_MINIMUM (0)

18. 0x26,0x00,0xFF, // LOGICAL_MAXIMUM (255)

19. 0x75,0x08, // REPORT_SIZE (8bit)

20. 0x95,0x40, // REPORT_COUNT (64Byte)

21. 0x91,0x02, // OUTPUT (Data,Var,Abs)

22.

23. 0xc0 /* END_COLLECTION */

24. }; /*CustomHID_ReportDescriptor */


PC test program

1.  /**

2. *@brief Read data after sending data

3. *@param None

4. *@retval None

5. */

6. voidHIDSampleFunc(void)

7. {

8. HANDLE hDev;

9. BYTE recvDataBuf[1024],reportBuf[1024];;

10. DWORD bytes;

11. hDev = OpenMyHIDDevice(0); // Open the device without using overlap (asynchronous) mode;

12. if (hDev == INVALID_HANDLE_VALUE){

13. printf("INVALID_HANDLE_VALUE\n");

14. return;

15.}

16. reportBuf[0] = 0; // The report ID of the output report is 0

17. for(int i=0;i<REPORT_COUNT;i++){

18. reportBuf[i+1]=i+1;//Store the data in the data buffer

19.}

20. printf("Start writing data to the device...\n");

21. // Write data to the device, note that the third parameter value must be REPORT_COUNT+1, otherwise it will return 1784 error

22. if (!WriteFile(hDev, reportBuf, REPORT_COUNT+1,&bytes, NULL)){

23. printf("writedata error! %d\n",GetLastError());

24. return;

25. }else{

26. printf("Successfully write %d data to the device...\n",bytes);

27.}

28. printf("Start reading data from the device...\n");

29. // Read data from the device, note that the third parameter value must be greater than or equal to REPORT_COUNT+1, otherwise it will return 1784 error

30. if(!ReadFile(hDev, recvDataBuf, REPORT_COUNT+1,&bytes, NULL)){ // Read the data sent by the device to the host

31. printf("readdata error! %d\n",GetLastError());

32. return;

33. }else{

34. printf("Successfully read %d data to the device...\n",bytes);

35.}

36. printf("The data returned by the device is:\n");

37. //Display the data read back

38. for(int i=0;i<REPORT_COUNT;i++){

39. printf("0x%02X",recvDataBuf[i+1]);

40.}

41. printf("\n\r");

42.}



Source download
STM32 program download: stm32_usb_hid.zip (2.62 MB, download times: 6366)
STM32F105/107 version source code download: usb_hid.rar (2.16 MB, download times: 5175)
HID host computer program download (VS2010): STM32_HID_PC_Driver.zip (259.02 KB, download times: 2590)
HID host computer program download (VS2008): PC_HID.zip (133.6 KB, download times: 1924)

USB development related data download
 USB2.0 Technical Specification (Chinese).pdf (2.59 MB, download times: 2991)
 Detailed explanation of communication between Windows host and custom USB_HID device.pdf (259.12 KB, download times: 1934)
 STM32F10xxx USB-FS-Device firmwarelibrary.pdf (1.27 MB, download times: 2412)
 STM32F10xxx USB Development Kit.pdf (978.66 KB, download times: 2588)
 STM32_USB-FS-Device_Lib_V3.0.1.zip (822.59 KB, download times: 2068)
 STM32_usb firmware library.pdf (978.66 KB, download times: 2457)
 STM32-based USB program development notes.pdf (2.42 MB, download times: 3155)
 In-depth analysis of STM32_USB-FS-Device_Lib library.pdf (697.35 KB, download times: 1958)

 The descriptor is in the file usb_desc.c. The first thing to change is the device descriptor. The structure of the device descriptor is standard, and the length is also fixed. The USB device descriptor in the example is as follows:
/* USB Standard Device Descriptor */
const u8 Joystick_DeviceDescriptor[JOYSTICK_SIZ_DEVICE_DESC]=
{
 0x12, /*bLength */
 USB_DEVICE_DESCRIPTOR_TYPE, /*bDescriptorType*/
 0x00, /*bcdUSB */
 0x02,
 0x00, /*bDeviceClass*/
 0x00, /*bDeviceSubClass*/
 0x00, /*bDeviceProtocol*/
 0x40, /*bMaxPacketSize40*/
 0x83, /*idVendor (0x0483)*/
 0x04,
 0x10, /*idProduct = 0x5710*/
 0x57,
 0x00, /*bcdDevice rel. 2.00*/
 0x02,
 1, /*Index of string descriptor describing
                           manufacturer */
 2, /*Index of string descriptor describing
                         product*/
 3, /*Index of string descriptordescribing the
                   device serial number */
 0x01 /*bNumConfigurations*/
}; /* Joystick_DeviceDescriptor */

We only need to modify the idVendor (VID) and idProduct (PID) here. They are used by the computer to identify devices to load drivers, so they must not conflict with existing devices. Both VID and PID are two bytes, with low byte in front and high byte in back. For example, the VID here is 0x0483, and it is 0x83, 0x04 written in it. We change the VID to 0x1234 and the PID to 0x4321, which is: 0x34, 0x12, 0x21, 0x43.

    Then modify the configuration descriptor set. The set of configuration descriptors includes configuration descriptors, interface descriptors, class-specific descriptors (here, HID descriptors), and endpoint descriptors. If you need to add endpoints, just add them at the end. Remember to modify the value of JOYSTICK_SIZ_CONFIG_DESC to the length of the configuration descriptor set. The first part is the configuration descriptor. Usually here No need to modify, unless you want to change the configuration to have multiple interfaces (USB composite devices), then you should modify bNumInterfaces, and change to as many as you need. There is only one interface, so the value is 1. The second part is the interface descriptor, which determines the functions implemented by the interface, such as HID devices, or mass storage devices. Where bInterfaceNumber is the number of the interface, starting from 0. There is only one interface, so its value is 0. If there are more interfaces, they are numbered in sequence. Note that a new interface can only be started after the complete end of an interface (including the class special descriptor and endpoint descriptor under the interface). bNumEndpoints is the number of endpoints used by the interface (not including endpoint 0). The original program implements the mouse function, so there is only one input endpoint. We add an output endpoint here to control the LED (there are indicators for capital letter lock, keypad number key lock, etc. on the keyboard), so change bNumEndpoints to 2.
bInterfaceClass is the class used by the interface. It is designated as a HID device here. Both the USB keyboard and mouse are HID devices. There is no need to modify it here. If you want to implement other devices, please modify it according to the class specified in the USB protocol. bInterfaceSubClass is the subclass used by the interface. Two subclasses are defined under the HID device class, which can be used when the system is booted, and those that cannot be used when the system is booted. Here, it is 1, which means it can be used when the system is booted. bInterfaceProtocol is the protocol of the interface. It used to be a mouse, here is changed to 1, a keyboard. The third part is the HID descriptor, which is only available for HID devices. If you want to modify it to other devices, use the special descriptors of other devices instead or not. There is no need to modify it here. The fourth part is the endpoint descriptor of input endpoint 1. In the original code, the maximum packet length of the endpoint (wMaxPacketSize) is 4 bytes, and we change it to 8 bytes. In addition, we add another output endpoint 1, copy the last input endpoint 1 descriptor, and then modify the address (bEndpointAddress) to 0x01 

Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.